Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
108 chris 1
package com.gebauz.pingK.common.framework;
2
 
3
import java.nio.ByteBuffer;
4
import java.nio.ByteOrder;
5
import java.nio.FloatBuffer;
6
import java.nio.ShortBuffer;
7
 
8
import javax.microedition.khronos.opengles.GL10;
9
 
10
/**
11
 * Simple Mesh class.
12
 * @author chiu
13
 *
14
 */
15
public class Mesh
16
{
17
        /**
18
         * Helper class to fill an array with vertex information
19
         *
20
         */
21
        public static class AttributeArray
22
        {
23
                private float[] mAttributeArray;
24
                private int mCurrentIndex = 0;
25
 
26
                public AttributeArray(int size)
27
                {
28
                        mAttributeArray = new float[size];
29
                        mCurrentIndex = 0;
30
                }
31
 
32
                public void fill(float x)
33
                {
34
                        mAttributeArray[mCurrentIndex] = x; mCurrentIndex++;
35
                }
36
 
37
                public void fill(float x, float y)
38
                {
39
                        mAttributeArray[mCurrentIndex] = x; mCurrentIndex++;
40
                        mAttributeArray[mCurrentIndex] = y; mCurrentIndex++;
41
                }
42
 
43
                public void fill(float x, float y, float z)
44
                {
45
                        mAttributeArray[mCurrentIndex] = x; mCurrentIndex++;
46
                        mAttributeArray[mCurrentIndex] = y; mCurrentIndex++;           
47
                        mAttributeArray[mCurrentIndex] = z; mCurrentIndex++;
48
                }
49
 
50
                public void fill(float x, float y, float z, float w)
51
                {
52
                        mAttributeArray[mCurrentIndex] = x; mCurrentIndex++;
53
                        mAttributeArray[mCurrentIndex] = y; mCurrentIndex++;           
54
                        mAttributeArray[mCurrentIndex] = z; mCurrentIndex++;
55
                        mAttributeArray[mCurrentIndex] = w; mCurrentIndex++;                   
56
                }
57
 
58
                public float[] getAttributeArray()
59
                {
60
                        return mAttributeArray;
61
                }
62
 
63
                public int getUsedCount()
64
                {
65
                        return mCurrentIndex;
66
                }
67
        }
68
 
69
        public class AttributeBuffer
70
        {
71
                public ByteBuffer byteBuffer;
72
                public FloatBuffer floatBuffer;
73
                public int length;
74
 
75
                public AttributeBuffer()
76
                {
77
                }
78
 
79
                public AttributeBuffer(float data[])
80
                {
81
                        setData(data);
82
                }
83
 
84
                public void setData(float data[])
85
                {
86
                        byteBuffer = ByteBuffer.allocateDirect(data.length * 4);
87
                        byteBuffer.order(ByteOrder.nativeOrder());
88
                        floatBuffer = byteBuffer.asFloatBuffer();
89
                        floatBuffer.put(data);
90
                        floatBuffer.position(0);
91
                        length = data.length;
92
                }
93
        }
94
 
95
        public class IndexBuffer
96
        {
97
                public ByteBuffer byteBuffer;
98
                public ShortBuffer shortBuffer;
99
                public int length;
100
 
101
                public IndexBuffer()
102
                {
103
                }
104
 
105
                public IndexBuffer(short data[])
106
                {
107
                        setData(data);
108
                }
109
 
110
                public void setData(short data[])
111
                {
112
                        byteBuffer = ByteBuffer.allocateDirect(data.length * 2);
113
                        byteBuffer.order(ByteOrder.nativeOrder());
114
                        shortBuffer = byteBuffer.asShortBuffer();
115
                        shortBuffer.put(data);
116
                        shortBuffer.position(0);
117
                        length = data.length;
118
                }
119
        }
120
 
121
        AttributeBuffer vertexBuffer = null;
122
        AttributeBuffer colorBuffer = null;
123
        AttributeBuffer normalBuffer = null;
124
        AttributeBuffer texCoordBuffer = null;
125
        IndexBuffer indexBuffer = null;
126
 
127
        public static final int COORD_ELEMENTS_COUNT = 3;
128
        public static final int COLOR_ELEMENTS_COUNT = 4;
129
        public static final int TEX_COORD_ELEMENTS_COUNT = 2;
130
        public static final int NORMAL_ELEMENTS_COUNT = 3;
131
 
132
        private int mPrimitiveType = GL10.GL_TRIANGLES;
133
 
134
        public Mesh()
135
        {
136
 
137
        }
138
 
139
        public void setVertices(float vertices[])
140
        {
141
                if (vertices == null)
142
                {
143
                        vertexBuffer = null;
144
                        return;
145
                }
146
 
147
                vertexBuffer = new AttributeBuffer(vertices);          
148
        }
149
 
150
        public void setColors(float colors[])
151
        {
152
                if (colors == null)
153
                {
154
                        colorBuffer = null;
155
                        return;
156
                }
157
 
158
                colorBuffer = new AttributeBuffer(colors);     
159
        }
160
 
161
        public void setNormals(float normals[])
162
        {
163
                if (normals == null)
164
                {
165
                        normalBuffer = null;
166
                        return;
167
                }
168
 
169
                normalBuffer = new AttributeBuffer(normals);   
170
        }
171
 
172
        public void setTexCoords(float texCoords[])
173
        {
174
                if (texCoords == null)
175
                {
176
                        texCoordBuffer = null;
177
                        return;
178
                }
179
 
180
                texCoordBuffer = new AttributeBuffer(texCoords);       
181
        }
182
 
183
        public void setIndices(short indices[])
184
        {
185
                if (indices == null)
186
                {
187
                        indexBuffer = null;
188
                        return;
189
                }
190
 
191
                indexBuffer = new IndexBuffer(indices);
192
        }
193
 
194
        public void setVertex(int index, Vector3 vertex)
195
        {
196
                vertexBuffer.floatBuffer.put(index * COORD_ELEMENTS_COUNT + 0, vertex.x);
197
                vertexBuffer.floatBuffer.put(index * COORD_ELEMENTS_COUNT + 1, vertex.y);
198
                vertexBuffer.floatBuffer.put(index * COORD_ELEMENTS_COUNT + 2, vertex.z);
199
        }
200
 
201
        public Vector3 getVertex(int index)
202
        {
203
                return new Vector3(
204
                                vertexBuffer.floatBuffer.get(index * COORD_ELEMENTS_COUNT + 0),
205
                                vertexBuffer.floatBuffer.get(index * COORD_ELEMENTS_COUNT + 1),
206
                                vertexBuffer.floatBuffer.get(index * COORD_ELEMENTS_COUNT + 2));
207
        }
208
 
209
        public int getVertexCount()
210
        {
211
                return vertexBuffer.floatBuffer.limit() / COORD_ELEMENTS_COUNT;
212
        }
213
 
214
        public void setColor(int index, Vector4 color)
215
        {
216
                colorBuffer.floatBuffer.put(index * COLOR_ELEMENTS_COUNT + 0, color.x);
217
                colorBuffer.floatBuffer.put(index * COLOR_ELEMENTS_COUNT + 1, color.y);
218
                colorBuffer.floatBuffer.put(index * COLOR_ELEMENTS_COUNT + 2, color.z);
219
                colorBuffer.floatBuffer.put(index * COLOR_ELEMENTS_COUNT + 3, color.w);
220
        }
221
 
222
        public int getColorCount()
223
        {
224
                return colorBuffer.floatBuffer.limit() / COLOR_ELEMENTS_COUNT;
225
        }
226
 
227
        public Vector4 getColor(int index)
228
        {
229
                return new Vector4(
230
                                colorBuffer.floatBuffer.get(index * COLOR_ELEMENTS_COUNT + 0),
231
                                colorBuffer.floatBuffer.get(index * COLOR_ELEMENTS_COUNT + 1),
232
                                colorBuffer.floatBuffer.get(index * COLOR_ELEMENTS_COUNT + 2),
233
                                colorBuffer.floatBuffer.get(index * COLOR_ELEMENTS_COUNT + 3));
234
        }
235
 
236
        public void setNormal(int index, Vector3 normal)
237
        {
238
                normalBuffer.floatBuffer.put(index * NORMAL_ELEMENTS_COUNT + 0, normal.x);
239
                normalBuffer.floatBuffer.put(index * NORMAL_ELEMENTS_COUNT + 1, normal.y);
240
                normalBuffer.floatBuffer.put(index * NORMAL_ELEMENTS_COUNT + 2, normal.z);
241
        }
242
 
243
        public Vector3 getNormal(int index)
244
        {
245
                return new Vector3(
246
                                normalBuffer.floatBuffer.get(index * NORMAL_ELEMENTS_COUNT + 0),
247
                                normalBuffer.floatBuffer.get(index * NORMAL_ELEMENTS_COUNT + 1),
248
                                normalBuffer.floatBuffer.get(index * NORMAL_ELEMENTS_COUNT + 2));
249
        }
250
 
251
        public int getNormalCount()
252
        {
253
                return normalBuffer.floatBuffer.limit() / NORMAL_ELEMENTS_COUNT;
254
        }
255
 
256
        public void setTexCoord(int index, Vector2 texCoord)
257
        {
258
                texCoordBuffer.floatBuffer.put(index * TEX_COORD_ELEMENTS_COUNT + 0, texCoord.x);
259
                texCoordBuffer.floatBuffer.put(index * TEX_COORD_ELEMENTS_COUNT + 1, texCoord.y);
260
        }
261
 
262
        public Vector2 getTexCoord(int index)
263
        {
264
                return new Vector2(
265
                                texCoordBuffer.floatBuffer.get(index * TEX_COORD_ELEMENTS_COUNT + 0),
266
                                texCoordBuffer.floatBuffer.get(index * TEX_COORD_ELEMENTS_COUNT + 1));
267
        }      
268
 
269
        public int getTexCoordCount()
270
        {
271
                return texCoordBuffer.floatBuffer.limit() / TEX_COORD_ELEMENTS_COUNT;
272
        }
273
 
274
 
275
        /**
276
         * Activate the vertex buffer for rendering
277
         */
278
        public void activate()
279
        {
280
 
281
                GL10 gl = GLUtil.getGL();
282
 
283
                if (vertexBuffer != null)
284
                {
285
                        gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);                  
286
                        gl.glVertexPointer(COORD_ELEMENTS_COUNT, GL10.GL_FLOAT, 0, vertexBuffer.floatBuffer);
287
                }
288
                if (colorBuffer != null)
289
                {
290
                        gl.glEnableClientState(GL10.GL_COLOR_ARRAY);                   
291
                        gl.glColorPointer(COLOR_ELEMENTS_COUNT, GL10.GL_FLOAT, 0, colorBuffer.floatBuffer);
292
                }
293
                if (normalBuffer != null)
294
                {
295
                        gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);
296
                        gl.glNormalPointer(GL10.GL_FLOAT, 0, normalBuffer.floatBuffer);
297
                }
298
                if (texCoordBuffer != null)
299
                {
300
                        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
301
                        gl.glTexCoordPointer(TEX_COORD_ELEMENTS_COUNT, GL10.GL_FLOAT, 0, texCoordBuffer.floatBuffer);
302
                }
303
        }
304
 
305
        /**
306
         * Deactivate the vertex buffer for rendering
307
         */
308
        public void deactivate()
309
        {
310
                GL10 gl = GLUtil.getGL();
311
 
312
                if (vertexBuffer != null)
313
                {
314
                        gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
315
                }
316
                if (colorBuffer != null)
317
                {
318
                        gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
319
                }
320
                if (normalBuffer != null)
321
                {
322
                        gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
323
                }
324
                if (texCoordBuffer != null)
325
                {
326
                        gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
327
                }
328
        }
329
 
330
        public void setPrimitiveType(int primitiveType)
331
        {
332
                mPrimitiveType = primitiveType;
333
        }
334
 
335
        /**
336
         * Render the vertex buffer
337
         */
338
        public void render()
339
        {              
340
                GL10 gl = GLUtil.getGL();
341
 
342
                activate();
343
 
344
                if (indexBuffer != null)
345
                {
346
                        // indexed drawing
347
                        gl.glDrawElements(mPrimitiveType, indexBuffer.length, GL10.GL_UNSIGNED_SHORT, indexBuffer.shortBuffer);
348
                }
349
                else if (vertexBuffer != null)
350
                {                      
351
                        // non-indexed drawing
352
                        gl.glDrawArrays(mPrimitiveType, 0, vertexBuffer.length);
353
                }
354
 
355
                deactivate();
356
        }
357
 
358
        /**
359
         * Render the vertex buffer with a specified amount of primitives
360
         */
361
        public void render(int numPrimitives)
362
        {
363
                GL10 gl = GLUtil.getGL();
364
 
365
                activate();
366
 
367
                if (indexBuffer != null)
368
                {
369
                        // indexed drawing
370
                        gl.glDrawElements(mPrimitiveType, numPrimitives, GL10.GL_UNSIGNED_SHORT, indexBuffer.shortBuffer);
371
                }
372
                else if (vertexBuffer != null)
373
                {                      
374
                        // non-indexed drawing
375
                        gl.glDrawArrays(mPrimitiveType, 0, numPrimitives);
376
                }
377
 
378
                deactivate();
379
        }
380
 
381
}