Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 1712 | chris | 1 | package com.gebauz.bauzoid2.graphics.geometry; |
| 2 | |||
| 3 | import com.badlogic.gdx.Gdx; |
||
| 4 | import com.badlogic.gdx.graphics.GL20; |
||
| 5 | import com.gebauz.bauzoid2.game.Consts; |
||
| 6 | |||
| 7 | /** Simpler Geometry class for creating geometry from code. */ |
||
| 8 | public class SimpleGeometry |
||
| 9 | { |
||
| 10 | public static final int POSITION_COORD_PER_ELEMENT = 3; |
||
| 11 | public static final int NORMAL_COORD_PER_ELEMENT = 3; |
||
| 12 | public static final int COLOR_COORD_PER_ELEMENT = 4; |
||
| 13 | public static final int TEXCOORD_COORD_PER_ELEMENT = 2; |
||
| 14 | |||
| 15 | private Geometry.PrimitiveType mPrimitiveType = Geometry.PrimitiveType.TRIANGLES; |
||
| 16 | private int mPrimitiveTypeGL = GL20.GL_TRIANGLES; |
||
| 17 | |||
| 18 | private VertexStream mPositionStream = null; |
||
| 19 | private VertexStream mNormalStream = null; |
||
| 20 | private VertexStream mColorStream = null; |
||
| 21 | private VertexStream mTexCoordStream = null; |
||
| 22 | private IndexStream mIndexStream = null; |
||
| 23 | |||
| 24 | public SimpleGeometry(Geometry.PrimitiveType primitiveType) |
||
| 25 | { |
||
| 26 | mPrimitiveType = primitiveType; |
||
| 27 | mPrimitiveTypeGL = Geometry.toPrimitiveGL(primitiveType); |
||
| 28 | } |
||
| 29 | |||
| 30 | public void dispose() |
||
| 31 | { |
||
| 32 | destroyPositionStream(); |
||
| 33 | destroyNormalStream(); |
||
| 34 | destroyColorStream(); |
||
| 35 | destroyTexCoordStream(); |
||
| 36 | destroyIndexStream(); |
||
| 37 | } |
||
| 38 | |||
| 39 | public void destroyPositionStream() |
||
| 40 | { |
||
| 41 | if (mPositionStream != null) |
||
| 42 | { |
||
| 43 | mPositionStream.dispose(); |
||
| 44 | mPositionStream = null; |
||
| 45 | } |
||
| 46 | } |
||
| 47 | |||
| 48 | public void destroyNormalStream() |
||
| 49 | { |
||
| 50 | if (mNormalStream != null) |
||
| 51 | { |
||
| 52 | mNormalStream.dispose(); |
||
| 53 | mNormalStream = null; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | public void destroyColorStream() |
||
| 58 | { |
||
| 59 | if (mColorStream != null) |
||
| 60 | { |
||
| 61 | mColorStream.dispose(); |
||
| 62 | mColorStream = null; |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | public void destroyTexCoordStream() |
||
| 67 | { |
||
| 68 | if (mTexCoordStream != null) |
||
| 69 | { |
||
| 70 | mTexCoordStream.dispose(); |
||
| 71 | mTexCoordStream = null; |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | public void destroyIndexStream() |
||
| 76 | { |
||
| 77 | if (mIndexStream != null) |
||
| 78 | { |
||
| 79 | mIndexStream.dispose(); |
||
| 80 | mIndexStream = null; |
||
| 81 | } |
||
| 82 | } |
||
| 83 | |||
| 84 | public void setPositions(float[] positions) |
||
| 85 | { |
||
| 86 | setPositions(positions, false); |
||
| 87 | } |
||
| 88 | |||
| 89 | public void setPositions(float[] positions, boolean isDynamic) |
||
| 90 | { |
||
| 91 | if ((mPositionStream == null) || |
||
| 92 | (positions.length != (mPositionStream.getVertexCount() * mPositionStream.getCoordsPerElement())) || |
||
| 93 | (isDynamic != mPositionStream.isDynamic())) |
||
| 94 | { |
||
| 95 | destroyPositionStream(); |
||
| 96 | |||
| 97 | mPositionStream = new VertexStream(VertexStream.StreamType.INDIVIDUAL, "Positions", |
||
| 98 | new VertexAttribute[] { new VertexAttribute(VertexAttribute.POSITION, POSITION_COORD_PER_ELEMENT, 0) }, |
||
| 99 | isDynamic); |
||
| 100 | } |
||
| 101 | mPositionStream.setData(positions, POSITION_COORD_PER_ELEMENT, POSITION_COORD_PER_ELEMENT * Consts.SIZEOF_FLOAT); |
||
| 102 | mPositionStream.reupload(); |
||
| 103 | } |
||
| 104 | |||
| 105 | public void setNormals(float[] normals) |
||
| 106 | { |
||
| 107 | setNormals(normals, false); |
||
| 108 | } |
||
| 109 | |||
| 110 | public void setNormals(float[] normals, boolean isDynamic) |
||
| 111 | { |
||
| 112 | if ((mNormalStream == null) || |
||
| 113 | (normals.length != (mNormalStream.getVertexCount() * mNormalStream.getCoordsPerElement())) || |
||
| 114 | (isDynamic != mNormalStream.isDynamic())) |
||
| 115 | { |
||
| 116 | destroyNormalStream(); |
||
| 117 | |||
| 118 | mNormalStream = new VertexStream(VertexStream.StreamType.INDIVIDUAL, "Normals", |
||
| 119 | new VertexAttribute[] { new VertexAttribute(VertexAttribute.NORMAL, NORMAL_COORD_PER_ELEMENT, 0) }, |
||
| 120 | isDynamic); |
||
| 121 | } |
||
| 122 | mNormalStream.setData(normals, NORMAL_COORD_PER_ELEMENT, NORMAL_COORD_PER_ELEMENT * Consts.SIZEOF_FLOAT); |
||
| 123 | mNormalStream.reupload(); |
||
| 124 | } |
||
| 125 | |||
| 126 | public void setColors(float[] colors) |
||
| 127 | { |
||
| 128 | setColors(colors, false); |
||
| 129 | } |
||
| 130 | |||
| 131 | public void setColors(float[] colors, boolean isDynamic) |
||
| 132 | { |
||
| 133 | if ((mColorStream == null) || |
||
| 134 | (colors.length != (mColorStream.getVertexCount() * mColorStream.getCoordsPerElement())) || |
||
| 135 | (isDynamic != mColorStream.isDynamic())) |
||
| 136 | { |
||
| 137 | destroyColorStream(); |
||
| 138 | |||
| 139 | mColorStream = new VertexStream(VertexStream.StreamType.INDIVIDUAL, "Colors", |
||
| 140 | new VertexAttribute[] { new VertexAttribute(VertexAttribute.COLOR, COLOR_COORD_PER_ELEMENT, 0) }, |
||
| 141 | isDynamic); |
||
| 142 | } |
||
| 143 | mColorStream.setData(colors, COLOR_COORD_PER_ELEMENT, COLOR_COORD_PER_ELEMENT * Consts.SIZEOF_FLOAT); |
||
| 144 | mColorStream.reupload(); |
||
| 145 | } |
||
| 146 | |||
| 147 | public void setTexCoords(float[] texCoords) |
||
| 148 | { |
||
| 149 | setTexCoords(texCoords, false); |
||
| 150 | } |
||
| 151 | |||
| 152 | public void setTexCoords(float[] texCoords, boolean isDynamic) |
||
| 153 | { |
||
| 154 | if ((mTexCoordStream == null) || |
||
| 155 | (texCoords.length != (mTexCoordStream.getVertexCount() * mTexCoordStream.getCoordsPerElement())) || |
||
| 156 | (isDynamic != mTexCoordStream.isDynamic())) |
||
| 157 | { |
||
| 158 | destroyTexCoordStream(); |
||
| 159 | |||
| 160 | mTexCoordStream = new VertexStream(VertexStream.StreamType.INDIVIDUAL, "TexCoord", |
||
| 161 | new VertexAttribute[] { new VertexAttribute(VertexAttribute.TEXCOORD0, TEXCOORD_COORD_PER_ELEMENT, 0) }, |
||
| 162 | isDynamic); |
||
| 163 | } |
||
| 164 | mTexCoordStream.setData(texCoords, TEXCOORD_COORD_PER_ELEMENT, TEXCOORD_COORD_PER_ELEMENT * Consts.SIZEOF_FLOAT); |
||
| 165 | mTexCoordStream.reupload(); |
||
| 166 | } |
||
| 167 | |||
| 168 | public void setIndices(short[] indices) |
||
| 169 | { |
||
| 170 | if ((mIndexStream == null) || (indices.length != (mIndexStream.getIndexCount()))) |
||
| 171 | { |
||
| 172 | destroyIndexStream(); |
||
| 173 | |||
| 174 | mIndexStream = new IndexStream(); |
||
| 175 | } |
||
| 176 | mIndexStream.setData(indices); |
||
| 177 | mIndexStream.reupload(); |
||
| 178 | } |
||
| 179 | |||
| 180 | public void render(int first, int num) |
||
| 181 | { |
||
| 182 | if (mPositionStream == null) |
||
| 183 | return; |
||
| 184 | |||
| 185 | mPositionStream.activate(); |
||
| 186 | |||
| 187 | if (mNormalStream != null) |
||
| 188 | mNormalStream.activate(); |
||
| 189 | |||
| 190 | if (mColorStream != null) |
||
| 191 | mColorStream.activate(); |
||
| 192 | |||
| 193 | if (mTexCoordStream != null) |
||
| 194 | mTexCoordStream.activate(); |
||
| 195 | |||
| 196 | if (mIndexStream != null) |
||
| 197 | { |
||
| 198 | mIndexStream.activate(); |
||
| 199 | |||
| 200 | Gdx.gl20.glDrawElements(mPrimitiveTypeGL, num, GL20.GL_UNSIGNED_SHORT, first * Consts.SIZEOF_SHORT); |
||
| 201 | |||
| 202 | mIndexStream.deactivate(); |
||
| 203 | } |
||
| 204 | else |
||
| 205 | { |
||
| 206 | Gdx.gl20.glDrawArrays(mPrimitiveTypeGL, first, num); |
||
| 207 | } |
||
| 208 | |||
| 209 | mPositionStream.deactivate(); |
||
| 210 | |||
| 211 | if (mNormalStream != null) |
||
| 212 | mNormalStream.deactivate(); |
||
| 213 | |||
| 214 | if (mColorStream != null) |
||
| 215 | mColorStream.deactivate(); |
||
| 216 | |||
| 217 | if (mTexCoordStream != null) |
||
| 218 | mTexCoordStream.deactivate(); |
||
| 219 | } |
||
| 220 | |||
| 221 | public void render() |
||
| 222 | { |
||
| 223 | if (hasIndices()) |
||
| 224 | { |
||
| 225 | //Gdx.gl20.glDrawElements(mPrimitiveTypeGL, mIndexStream.getIndexCount(), GL20.GL_UNSIGNED_SHORT, 0); |
||
| 226 | render(0, mIndexStream.getIndexCount()); |
||
| 227 | } |
||
| 228 | else |
||
| 229 | { |
||
| 230 | if (mPositionStream == null) |
||
| 231 | return; |
||
| 232 | |||
| 233 | render(0, mPositionStream.getVertexCount()); |
||
| 234 | //Gdx.gl20.glDrawArrays(mPrimitiveTypeGL, 0, mPositionStream.getVertexCount()); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | public Geometry.PrimitiveType getPrimitiveType() |
||
| 239 | { |
||
| 240 | return mPrimitiveType; |
||
| 241 | } |
||
| 242 | |||
| 243 | public boolean hasIndices() |
||
| 244 | { |
||
| 245 | return (mIndexStream != null); |
||
| 246 | } |
||
| 247 | |||
| 248 | } |