My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
package com.badlogic.gamedev.tools;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import javax.microedition.khronos.opengles.GL10;
import javax.microedition.khronos.opengles.GL11;

/**
* A simple Mesh class that wraps OpenGL ES
* Vertex Arrays. Just instantiate it with
* the proper parameters then fill it with
* the color, texCoord, normal and vertex
* method.
*
* @author mzechner
*
*/
public final class Mesh
{
public enum PrimitiveType
{
Points,
Lines,
Triangles,
LineStrip,
TriangleStrip,
TriangleFan
}

/** The gl instance **/
private GL10 gl;

/** vertex position buffer and array **/
private float vertices[];
private int vertexHandle = -1;
private FloatBuffer vertexBuffer;

/** color buffer and array **/
private float colors[];
private int colorHandle = -1;
private FloatBuffer colorBuffer;

/** texture coordinate buffer and array **/
private float texCoords[];
private int texHandle = -1;
private FloatBuffer texCoordBuffer;

/** normal buffer and array **/
private float normals[];
private int normalHandle = -1;
private FloatBuffer normalBuffer;

/** vertex index at which the next vertex gets inserted **/
private int index = 0;

/** number of vertices defined for the mesh **/
private int numVertices = 0;

/** is the mesh dirty? **/
private boolean dirty = true;

/** last mesh **/
private static Mesh lastMesh;

/** renderer supports vbos **/
public static boolean globalVBO = true;

/** mesh count **/
public static int meshes = 0;

public Mesh( GL10 gl, int numVertices, boolean hasColors, boolean hasTextureCoordinates, boolean hasNormals )
{
this.gl = gl;
vertices = new float[numVertices * 3];
int[] buffer = new int[1];

if(!globalVBO )
vertexBuffer = allocateBuffer( numVertices * 3 );
else
{
((GL11)gl).glGenBuffers(1, buffer, 0);
vertexHandle = buffer[0];
vertexBuffer = FloatBuffer.wrap( vertices );
}

if( hasColors )
{
colors = new float[numVertices * 4];
if(!globalVBO )
colorBuffer = allocateBuffer( numVertices * 4 );
else
{
((GL11)gl).glGenBuffers(1, buffer, 0);
colorHandle = buffer[0];
colorBuffer = FloatBuffer.wrap( colors );
}
}

if( hasTextureCoordinates )
{
texCoords = new float[numVertices * 2];
if(!globalVBO )
texCoordBuffer = allocateBuffer( numVertices * 2 );
else
{
((GL11)gl).glGenBuffers(1, buffer, 0);
texHandle = buffer[0];
texCoordBuffer = FloatBuffer.wrap( texCoords );
}
}

if( hasNormals )
{
normals = new float[numVertices * 3];
if(!globalVBO )
normalBuffer = allocateBuffer( numVertices * 3 );
else
{
((GL11)gl).glGenBuffers(1, buffer, 0);
normalHandle = buffer[0];
normalBuffer = FloatBuffer.wrap( normals );
}
}
}

/**
* Allocates a direct FloatBuffer of the given size.
* Sets order to native
* @param size The size in number of floats
* @return The FloatBuffer
*/
private FloatBuffer allocateBuffer( int size )
{
ByteBuffer buffer = ByteBuffer.allocateDirect( size * 4 );
buffer.order(ByteOrder.nativeOrder());
return buffer.asFloatBuffer();
}

/**
* updates the direct buffers in case the user
*/
private void update( )
{
if( !globalVBO )
{
vertexBuffer.put(vertices);
vertexBuffer.position(0);

if( colors != null )
{
colorBuffer.put( colors );
colorBuffer.position(0);
}

if( texCoords != null )
{
texCoordBuffer.put( texCoords );
texCoordBuffer.position(0);
}

if( normals != null )
{
normalBuffer.put( normals );
normalBuffer.position(0);
}
}
else
{
GL11 gl = (GL11)this.gl;

gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, vertexHandle );
gl.glBufferData( GL11.GL_ARRAY_BUFFER, vertices.length * 4, vertexBuffer, GL11.GL_DYNAMIC_DRAW);

if( colors != null )
{
gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, colorHandle );
gl.glBufferData( GL11.GL_ARRAY_BUFFER, colors.length * 4, colorBuffer, GL11.GL_DYNAMIC_DRAW);
}

if( normals != null )
{
gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, normalHandle );
gl.glBufferData( GL11.GL_ARRAY_BUFFER, normals.length * 4, normalBuffer, GL11.GL_DYNAMIC_DRAW);
}

if( texCoords != null )
{
gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, texHandle );
gl.glBufferData( GL11.GL_ARRAY_BUFFER, texCoords.length * 4, texCoordBuffer, GL11.GL_DYNAMIC_DRAW);
}

gl.glBindBuffer( GL11.GL_ARRAY_BUFFER, 0 );
}

numVertices = index;
index = 0;
dirty = false;
}

/**
* Returns the OpenGL constant for the given
* primitive type
* @param type the type
* @return the OpenGL constant
*/
private int getPrimitiveType( PrimitiveType type )
{
if( type == PrimitiveType.Lines )
return GL10.GL_LINES;
else
if( type == PrimitiveType.Triangles )
return GL10.GL_TRIANGLES;
else
if( type == PrimitiveType.LineStrip)
return GL10.GL_LINE_STRIP;
else
if( type == PrimitiveType.TriangleStrip)
return GL10.GL_TRIANGLE_STRIP;
else
if( type == PrimitiveType.Points )
return GL10.GL_POINTS;
else
return GL10.GL_TRIANGLE_FAN;

}

/**
* Renders the mesh as the given type, starting at offset using
* numVertices vertices.
* @param type the type
* @param offset the offset, in number of vertices
* @param numVertices the number of vertices to use
*/
public void render( PrimitiveType type, int offset, int numVertices )
{
boolean wasDirty = dirty;
if( dirty )
update();

if( this == lastMesh && !wasDirty )
{
gl.glDrawArrays( getPrimitiveType( type ), offset, numVertices );
return;
}
else
{
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY );
gl.glDisableClientState(GL10.GL_COLOR_ARRAY );
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY );
gl.glDisableClientState(GL10.GL_NORMAL_ARRAY );
}

gl.glEnableClientState( GL10.GL_VERTEX_ARRAY );
if( globalVBO )
{
((GL11)gl).glBindBuffer( GL11.GL_ARRAY_BUFFER, vertexHandle );
((GL11)gl).glVertexPointer( 3, GL10.GL_FLOAT, 0, 0 );
}
else
gl.glVertexPointer( 3, GL10.GL_FLOAT, 0, vertexBuffer );

if( colors != null )
{
gl.glEnableClientState( GL10.GL_COLOR_ARRAY );
if( globalVBO )
{
((GL11)gl).glBindBuffer( GL11.GL_ARRAY_BUFFER, colorHandle );
((GL11)gl).glColorPointer( 4, GL10.GL_FLOAT, 0, 0 );
}
else
gl.glColorPointer( 4, GL10.GL_FLOAT, 0, colorBuffer );
}

if( texCoords != null )
{
gl.glEnableClientState( GL10.GL_TEXTURE_COORD_ARRAY );
if( globalVBO )
{
((GL11)gl).glBindBuffer( GL11.GL_ARRAY_BUFFER, texHandle );
((GL11)gl).glTexCoordPointer( 2, GL10.GL_FLOAT, 0, 0 );
}
else
gl.glTexCoordPointer( 2, GL10.GL_FLOAT, 0, texCoordBuffer );
}

if( normals != null )
{
gl.glEnableClientState( GL10.GL_NORMAL_ARRAY );
if( globalVBO )
{
((GL11)gl).glBindBuffer( GL11.GL_ARRAY_BUFFER, normalHandle );
((GL11)gl).glNormalPointer( GL10.GL_FLOAT, 0, 0 );
}
else
gl.glNormalPointer( GL10.GL_FLOAT, 0, normalBuffer );
}

gl.glDrawArrays( getPrimitiveType( type ), offset, numVertices );
lastMesh = this;
}

/**
* Renders the mesh as the given type using as many vertices as have
* been defined by calling vertex().
* @param type the type
*/
public void render( PrimitiveType type )
{
render( type, 0, numVertices );
}

/**
* Defines the position of the current vertex. Before
* you call this you have to call any other method like
* color, normal and texCoord for the current vertex!
*
* @param x the x coordinate
* @param y the y coordinate
* @param z the z coordinate
*/
public void vertex( float x, float y, float z )
{
dirty = true;
int offset = index * 3;
vertices[offset] = x;
vertices[offset+1] = y;
vertices[offset+2] = z;
index++;
}

/**
* Sets the color of the current vertex
*
* @param r the red component
* @param g the green component
* @param b the blue component
* @param a the alpha component
*/
public void color( float r, float g, float b, float a )
{
dirty = true;
int offset = index * 4;
colors[offset] = r;
colors[offset+1] = g;
colors[offset+2] = b;
colors[offset+3] = a;
}

/**
* Sets the normal of the current vertex
* @param x the x component
* @param y the y component
* @param z the z component
*/
public void normal( float x, float y, float z )
{
dirty = true;
int offset = index * 3;
normals[offset] = x;
normals[offset+1] = y;
normals[offset+2] = z;
}

/**
* Sets the texture coordinates of the current vertex
* @param s the s coordinate
* @param t the t coordinate
*/
public void texCoord( float s, float t )
{
dirty = true;
int offset = index * 2;
texCoords[offset] = s;
texCoords[offset+1] = t;
}

public int getMaximumVertices()
{
return vertices.length / 3;
}

public void reset( )
{
dirty = true;
index = 0;
}

public void dispose( )
{
if( globalVBO )
{
GL11 gl = (GL11)this.gl;
if( vertexHandle != -1 )
gl.glDeleteBuffers(1, new int[] { vertexHandle }, 0);
if( colorHandle != -1 )
gl.glDeleteBuffers(1, new int[] { colorHandle }, 0);
if( normalHandle != -1 )
gl.glDeleteBuffers(1, new int[] { normalHandle }, 0);
if( texHandle != -1 )
gl.glDeleteBuffers(1, new int[] { texHandle }, 0);
}

vertices = null;
vertexBuffer = null;
colors = null;
colorBuffer = null;
normals = null;
normalBuffer = null;
texCoords = null;
texCoordBuffer = null;
meshes--;
}
}

Change log

r51 by quantumgame on Jan 13, 2010   Diff
[No log message]
Go to: 
Project members, sign in to write a code review

Older revisions

r43 by quantumgame on Jan 11, 2010   Diff
[No log message]
r16 by quantumgame on Jan 8, 2010   Diff
[No log message]
r11 by quantumgame on Jan 8, 2010   Diff
[No log message]
All revisions of this file

File info

Size: 10009 bytes, 415 lines
Powered by Google Project Hosting