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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
|
package ru.olamedia.geom;
import java.nio.Buffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GLContext;
import javax.media.opengl.fixedfunc.GLPointerFunc;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.util.GLArrayDataServer;
public class ImmModeMesh {
private IndexedMeshBulder indexed = IndexedMeshBulder.instance;
private boolean useIndexed = false;
protected GLArrayDataServer interleaved;
protected FloatBuffer buf;
private int vertexCount = 0;
protected int mode = GL2.GL_QUADS;
protected boolean isGLSL = false;
protected byte vertexComponent = 0;
protected byte normalComponent = 0;
protected byte colorComponent = 0;
protected byte texCoordComponent = 0;
protected float red = 1;
protected float green = 1;
protected float blue = 1;
protected float alpha = 1;
protected float offsetX = 0;
protected float offsetY = 0;
protected float offsetZ = 0;
protected float x = 0;
protected float y = 0;
protected float z = 0;
protected float nx = 0;
protected float ny = 0;
protected float nz = 0;
protected float u = 0;
protected float v = 0;
protected boolean isFinished = false;
protected boolean isServer = false;
public void setGLSL(boolean glsl) {
this.isGLSL = glsl;
}
public void makeServer() {
if (null == interleaved) {
if (isGLSL) {
allocateGLSLBuffer();
} else {
allocateFixedBuffer();
}
if (null != buf) {
buf.flip();
interleaved.put(buf);
buf.clear();
buf = null;
}
interleaved.seal(true);
isServer = true;
}
}
public Buffer getBuffer() {
if (!isServer) {
return buf;
}
return interleaved.getBuffer();
}
public void put(ImmModeMesh mesh) {
if (isServer) {
final FloatBuffer b = (FloatBuffer) mesh.getBuffer();
b.rewind();
interleaved.put(b);
} else {
final FloatBuffer b = (FloatBuffer) mesh.getBuffer();
b.rewind();
if (null == buf) {
allocateBuffer();
}
growBufferIfNecessary(b.remaining());
buf.put(b);
}
vertexCount += mesh.getVertexCount();
}
public void destroy() {
if (isServer) {
interleaved.destroy(GLContext.getCurrentGL().getGL2ES2());
interleaved = null;
} else {
if (null != buf) {
buf.clear();
buf = null;
}
}
}
public int getComponents() {
return vertexComponent + normalComponent + colorComponent + texCoordComponent;
}
protected void allocateBuffer() {
if (!isServer) {
buf = Buffers.newDirectFloatBuffer(getComponents() * getVertexCount());
} else {
if (isGLSL) {
allocateGLSLBuffer();
} else {
allocateFixedBuffer();
}
}
}
protected final void growBuffer(int additionalElements) {
final int osize = (buf != null) ? buf.capacity() : 0;
final int nsize = osize + (additionalElements * getComponents());
FloatBuffer newFBuffer = Buffers.newDirectFloatBuffer(nsize);
if (buf != null) {
buf.flip();
newFBuffer.put((FloatBuffer) buf);
}
buf = newFBuffer;
}
protected void allocateFixedBuffer() {
final GLArrayDataServer buf = GLArrayDataServer.createFixedInterleaved(getComponents(), GL.GL_FLOAT, false,
getVertexCount(), GL.GL_STATIC_DRAW);
if (vertexComponent != 0) {
buf.addFixedSubArray(GLPointerFunc.GL_VERTEX_ARRAY, vertexComponent, GL.GL_ARRAY_BUFFER);
}
if (colorComponent != 0) {
buf.addFixedSubArray(GLPointerFunc.GL_COLOR_ARRAY, colorComponent, GL.GL_ARRAY_BUFFER);
}
if (normalComponent != 0) {
buf.addFixedSubArray(GLPointerFunc.GL_NORMAL_ARRAY, normalComponent, GL.GL_ARRAY_BUFFER);
}
if (texCoordComponent != 0) {
buf.addFixedSubArray(GLPointerFunc.GL_TEXTURE_COORD_ARRAY, texCoordComponent, GL.GL_ARRAY_BUFFER);
}
interleaved = buf;
}
protected void allocateGLSLBuffer() {
final GLArrayDataServer buf = GLArrayDataServer.createGLSLInterleaved(getComponents(), GL.GL_FLOAT, false,
getVertexCount(), GL.GL_STATIC_DRAW);
if (vertexComponent != 0) {
buf.addGLSLSubArray("mesh_vertices", vertexComponent, GL.GL_ARRAY_BUFFER);
}
if (colorComponent != 0) {
buf.addGLSLSubArray("mesh_colors", colorComponent, GL.GL_ARRAY_BUFFER);
}
if (normalComponent != 0) {
buf.addGLSLSubArray("mesh_normal", normalComponent, GL.GL_ARRAY_BUFFER);
}
if (texCoordComponent != 0) {
buf.addGLSLSubArray("mesh_texCoord", texCoordComponent, GL.GL_ARRAY_BUFFER);
}
buf.rewind();
interleaved = buf;
}
protected final boolean growBufferIfNecessary(int spare) {
if (buf == null || buf.remaining() < spare) {
growBuffer(spare);
return true;
}
return false;
}
private void putf(float f) {
if (isServer) {
if (null == interleaved) {
allocateBuffer();
}
interleaved.putf(f);
} else {
// buf.put(f);
growBufferIfNecessary(256);
Buffers.putf(buf, f);
}
}
protected void putVertex() {
if (vertexComponent > 0) {
putf(x);
putf(y);
if (vertexComponent > 2) {
putf(z);
}
}
if (colorComponent > 0) {
putf(red);
putf(green);
putf(blue);
if (colorComponent > 3) {
putf(alpha);
}
}
if (normalComponent != 0) {
putf(nx);
putf(ny);
if (normalComponent > 2) {
putf(nz);
}
}
if (texCoordComponent > 0) {
putf(u);
putf(v);
}
}
public void getIndexed() {
}
public void enableColor3() {
colorComponent = 3;
if (useIndexed) {
indexed.setColorComponent(colorComponent);
}
}
public void enableColor4() {
colorComponent = 4;
if (useIndexed) {
indexed.setColorComponent(colorComponent);
}
}
public void enableVertex2() {
vertexComponent = 2;
if (useIndexed) {
indexed.setVertexComponent(vertexComponent);
}
}
public void enableVertex3() {
vertexComponent = 3;
if (useIndexed) {
indexed.setVertexComponent(vertexComponent);
}
}
public void enableNormal3() {
normalComponent = 3;
if (useIndexed) {
indexed.setNormalComponent(normalComponent);
}
}
public void enableTexCoord2() {
texCoordComponent = 2;
if (useIndexed) {
indexed.setUVComponent(texCoordComponent);
}
}
public void enableTexCoord4() {
texCoordComponent = 4;
if (useIndexed) {
indexed.setUVComponent(texCoordComponent);
}
}
public void glBegin(int mode) {
this.mode = mode;
allocateBuffer();
if (useIndexed) {
indexed.reset();
}
}
public void beginQuads() {
glBegin(GL2.GL_QUADS);
}
public void beginTriangles() {
glBegin(GL2.GL_TRIANGLES);
}
public void glEnd() {
if (useIndexed) {
indexed.end();
final IntBuffer ind = indexed.getIndices();
System.out.println("glEnd: " + vertexCount + " / " + ind.limit());
}
if (isServer) {
if (null == interleaved) {
allocateBuffer();
}
interleaved.seal(true);
}
isFinished = true;
}
public void end() {
glEnd();
}
public void setServer(boolean isServer) {
this.isServer = isServer;
}
public int getVBOName() {
return interleaved.getVBOName();
}
public int getMode() {
return mode;
}
public boolean draw() {
if (!isFinished) {
return false;
}
makeServer();
final GL2ES2 gl = GLContext.getCurrentGL().getGL2ES2();
interleaved.enableBuffer(gl, true);
// Вывод геометрии VBO выполняется такими же функциями, как и при
// использовании буфера в оперативной памяти.
gl.glDrawArrays(mode, 0, interleaved.getElementCount());
interleaved.enableBuffer(gl, false);
//gl.glFlush();
return true;
}
public void setColor(float red, float green, float blue) {
this.red = red;
this.green = green;
this.blue = blue;
if (useIndexed) {
indexed.glColor3f(red, green, blue);
}
}
public void setColor(float red, float green, float blue, float alpha) {
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = alpha;
if (useIndexed) {
indexed.glColor4f(red, green, blue, alpha);
}
}
public void glColor3f(float red, float green, float blue) {
this.red = red;
this.green = green;
this.blue = blue;
if (useIndexed) {
indexed.glColor3f(red, green, blue);
}
}
public void glColor4f(float red, float green, float blue, float alpha) {
this.red = red;
this.green = green;
this.blue = blue;
this.alpha = alpha;
if (useIndexed) {
indexed.glColor4f(red, green, blue, alpha);
}
}
public void glVertex3f(float x, float y, float z) {
this.x = offsetX + x;
this.y = offsetY + y;
this.z = offsetZ + z;
putVertex();
if (useIndexed) {
indexed.glVertex3f(x, y, z);
}
}
public void glVertex2f(float x, float y) {
this.x = offsetX + x;
this.y = offsetY + y;
putVertex();
if (useIndexed) {
indexed.glVertex2f(x, y);
}
}
public void setUV(float u, float v) {
this.u = u;
this.v = v;
if (useIndexed) {
indexed.setUV(u, v);
}
}
public void glTexCoord2f(float u, float v) {
this.u = u;
this.v = v;
if (useIndexed) {
indexed.setUV(u, v);
}
}
public void glRectf(float x1, float y1, float x2, float y2) {
glVertex2f(x1, y1);
glVertex2f(x1, y2);
glVertex2f(x2, y2);
glVertex2f(x2, y1);
}
protected ImmModeMesh(int vertexCount) {
this.setVertexCount(vertexCount);
}
public static ImmModeMesh allocate(int elementCount) {
return new ImmModeMesh(elementCount);
}
public int getVertexCount() {
return vertexCount;
}
public void setVertexCount(int vertexCount) {
this.vertexCount = vertexCount;
}
public void glTranslate(float x, float y, float z) {
offsetX = x;
offsetY = y;
offsetZ = z;
if (useIndexed) {
indexed.glTranslate(x, y, z);
}
}
public void dispose() {
if (null != interleaved) {
interleaved.destroy(GLContext.getCurrentGL().getGL2ES2());
}
if (null != buf) {
buf.clear();
buf = null;
}
}
public void compact() {
if (null != interleaved) {
if (null != interleaved.getBuffer()) {
interleaved.getBuffer().flip();
((FloatBuffer) interleaved.getBuffer()).compact();
}
}
}
public void glNormal3f(float nx, float ny, float nz) {
this.nx = nx;
this.ny = ny;
this.nz = nz;
if (useIndexed) {
indexed.glNormal3f(nx, ny, nz);
}
}
}
|