Subversion Repositories AndroidProjects

Rev

Rev 224 | Details | Compare with Previous | Last modification | View Log | RSS feed

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