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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
package javax.media.opengl.util;
import javax.media.opengl.*;
import com.sun.opengl.impl.GLReflection;
import java.nio.*;
import java.util.Iterator;
import java.util.ArrayList;
public class ImmModeSink {
public static final boolean DEBUG_BEGIN_END = false;
public static final boolean DEBUG_DRAW = false;
// public static final int GL_QUADS = 0x0007; // Needs data manipulation
public static final int GL_QUAD_STRIP = 0x0008;
public static final int GL_POLYGON = 0x0009;
/**
* Uses a GL2ES1, or ES2 fixed function emulation immediate mode sink
*/
public static ImmModeSink createFixed(int glBufferUsage, int initialSize,
int vComps, int vDataType,
int cComps, int cDataType,
int nComps, int nDataType,
int tComps, int tDataType) {
return new ImmModeSink(glBufferUsage, initialSize,
vComps, vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType, false);
}
/**
* Uses a GL2ES2 GLSL shader immediate mode sink.
* To issue the draw() command,
* a ShaderState must be current, using ShaderState.glUseProgram().
*
* @see #draw(GL, boolean)
* @see javax.media.opengl.glsl.ShaderState#glUseProgram(GL2ES2, boolean)
* @see javax.media.opengl.glsl.ShaderState#getCurrent()
*/
public static ImmModeSink createGLSL(int glBufferUsage, int initialSize,
int vComps, int vDataType,
int cComps, int cDataType,
int nComps, int nDataType,
int tComps, int tDataType) {
return new ImmModeSink(glBufferUsage, initialSize,
vComps, vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType, true);
}
public static boolean usesVBO() { return vboUsage; }
public static void setVBOUsage(boolean v) { vboUsage = v; }
public void destroy(GL gl) {
destroyList(gl);
vboSet.destroy(gl);
}
public void reset() {
reset(null);
}
public void reset(GL gl) {
destroyList(gl);
vboSet.reset(gl);
}
public String toString() {
StringBuffer sb = new StringBuffer("ImmModeSink[");
sb.append(",\n\tVBO list: "+vboSetList.size()+" [");
for(Iterator i=vboSetList.iterator(); i.hasNext() ; ) {
sb.append("\n\t");
sb.append( (VBOSet)i.next() );
}
if(vboSetList.size()>0) {
sb.append("\n\t],\nVBO current: NOP]");
} else {
sb.append("\n\t],\nVBO current: \n");
sb.append(vboSet);
sb.append("\n]");
}
return sb.toString();
}
public void draw(GL gl, boolean disableBufferAfterDraw) {
if(DEBUG_DRAW) {
Exception e = new Exception("ImmModeSink.draw(disableBufferAfterDraw: "+disableBufferAfterDraw+"):\n\t"+this);
e.printStackTrace();
}
int n=0;
for(Iterator i=vboSetList.iterator(); i.hasNext() ; n++) {
((VBOSet)i.next()).draw(gl, null, disableBufferAfterDraw, n);
}
}
public void draw(GL gl, Buffer indices, boolean disableBufferAfterDraw) {
if(DEBUG_DRAW) {
Exception e = new Exception("ImmModeSink.draw(disableBufferAfterDraw: "+disableBufferAfterDraw+"):\n\t"+this);
e.printStackTrace();
}
int n=0;
for(Iterator i=vboSetList.iterator(); i.hasNext() ; n++) {
((VBOSet)i.next()).draw(gl, indices, disableBufferAfterDraw, n);
}
}
public void glBegin(int mode) {
if(DEBUG_BEGIN_END) {
Exception e = new Exception("ImmModeSink.glBegin("+vboSet.mode+"):\n\t"+this);
e.printStackTrace();
}
vboSet.modeOrig = mode;
switch(mode) {
// Needs data manipulation ..
//case GL_QUADS:
// mode=GL.GL_LINES;
// break;
case GL_QUAD_STRIP:
mode=GL.GL_TRIANGLE_STRIP;
break;
case GL_POLYGON:
mode=GL.GL_LINES;
break;
}
vboSet.mode = mode;
vboSet.checkSeal(false);
}
public final void glEnd(GL gl) {
glEnd(gl, null, true);
}
public void glEnd(GL gl, boolean immediateDraw) {
glEnd(gl, null, immediateDraw);
}
public final void glEnd(GL gl, Buffer indices) {
glEnd(gl, indices, true);
}
private void glEnd(GL gl, Buffer indices, boolean immediateDraw) {
if(DEBUG_BEGIN_END) {
Exception e = new Exception("ImmModeSink START glEnd(immediate: "+immediateDraw+"):\n\t"+this);
e.printStackTrace();
}
if(immediateDraw) {
vboSet.seal(gl, false);
vboSet.draw(gl, indices, true, -1);
reset(gl);
} else {
vboSet.seal(gl, true);
vboSetList.add(vboSet);
vboSet = vboSet.regenerate();
}
}
public void glVertexv(Buffer v) {
vboSet.glVertexv(v);
}
public final void glVertex2f(float x, float y) {
vboSet.glVertex2f(x,y);
}
public final void glVertex3f(float x, float y, float z) {
vboSet.glVertex3f(x,y,z);
}
public void glNormalv(Buffer v) {
vboSet.glNormalv(v);
}
public final void glNormal3f(float x, float y, float z) {
vboSet.glNormal3f(x,y,z);
}
public void glColorv(Buffer v) {
vboSet.glColorv(v);
}
public final void glColor3f(float x, float y, float z) {
vboSet.glColor3f(x,y,z);
}
public final void glColor4f(float x, float y, float z, float a) {
vboSet.glColor4f(x,y,z, a);
}
public void glTexCoordv(Buffer v) {
vboSet.glTexCoordv(v);
}
public final void glTexCoord2f(float x, float y) {
vboSet.glTexCoord2f(x,y);
}
public final void glTexCoord3f(float x, float y, float z) {
vboSet.glTexCoord3f(x,y,z);
}
public final void glVertex2s(short x, short y) {
vboSet.glVertex2s(x,y);
}
public final void glVertex3s(short x, short y, short z) {
vboSet.glVertex3s(x,y,z);
}
public final void glNormal3s(short x, short y, short z) {
vboSet.glNormal3s(x,y,z);
}
public final void glColor3s(short x, short y, short z) {
vboSet.glColor3s(x,y,z);
}
public final void glColor4s(short x, short y, short z, short a) {
vboSet.glColor4s(x,y,z,a);
}
public final void glTexCoord2s(short x, short y) {
vboSet.glTexCoord2s(x,y);
}
public final void glTexCoord3s(short x, short y, short z) {
vboSet.glTexCoord3s(x,y,z);
}
public final void glVertex2b(byte x, byte y) {
vboSet.glVertex2b(x,y);
}
public final void glVertex3b(byte x, byte y, byte z) {
vboSet.glVertex3b(x,y,z);
}
public final void glNormal3b(byte x, byte y, byte z) {
vboSet.glNormal3b(x,y,z);
}
public final void glColor3b(byte x, byte y, byte z) {
vboSet.glColor3b(x,y,z);
}
public final void glColor4b(byte x, byte y, byte z, byte a) {
vboSet.glColor4b(x,y,z,a);
}
public final void glTexCoord2b(byte x, byte y) {
vboSet.glTexCoord2b(x,y);
}
public final void glTexCoord3b(byte x, byte y, byte z) {
vboSet.glTexCoord3b(x,y,z);
}
protected ImmModeSink(int glBufferUsage, int initialSize,
int vComps, int vDataType,
int cComps, int cDataType,
int nComps, int nDataType,
int tComps, int tDataType, boolean useGLSL) {
if(useGLSL && !GLProfile.isGL2ES2()) {
throw new GLException("ImmModeSink GLSL usage not supported for profile: "+GLProfile.getProfile());
}
vboSet = new VBOSet(glBufferUsage, initialSize,
vComps, vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType, useGLSL);
this.vboSetList = new ArrayList();
}
private void destroyList(GL gl) {
for(Iterator i=vboSetList.iterator(); i.hasNext() ; ) {
((VBOSet)i.next()).destroy(gl);
}
vboSetList.clear();
}
private VBOSet vboSet;
private ArrayList vboSetList;
private static boolean vboUsage = true;
protected static class VBOSet {
protected VBOSet (int glBufferUsage, int initialSize,
int vComps, int vDataType,
int cComps, int cDataType,
int nComps, int nDataType,
int tComps, int tDataType, boolean useGLSL) {
this.glBufferUsage=glBufferUsage;
this.initialSize=initialSize;
this.vDataType=vDataType;
this.vComps=vComps;
this.cDataType=cDataType;
this.cComps=cComps;
this.nDataType=nDataType;
this.nComps=nComps;
this.tDataType=tDataType;
this.tComps=tComps;
this.useGLSL=useGLSL;
if(!useGLSL) {
this.vertexVBO = GLArrayDataServer.createFixed(GL.GL_VERTEX_ARRAY, null, vComps, vDataType, false, initialSize, glBufferUsage);
this.colorVBO = GLArrayDataServer.createFixed(GL.GL_COLOR_ARRAY, null, cComps, cDataType, false, initialSize, glBufferUsage);
this.normalVBO = GLArrayDataServer.createFixed(GL.GL_NORMAL_ARRAY, null, nComps, nDataType, false, initialSize, glBufferUsage);
this.texcoordVBO = GLArrayDataServer.createFixed(GL.GL_TEXTURE_COORD_ARRAY, null, tComps, tDataType, false, initialSize, glBufferUsage);
} else {
this.vertexVBO = GLArrayDataServer.createGLSL(GLContext.mgl_Vertex, vComps, vDataType, false, initialSize, glBufferUsage);
this.colorVBO = GLArrayDataServer.createGLSL(GLContext.mgl_Color, cComps, cDataType, false, initialSize, glBufferUsage);
this.normalVBO = GLArrayDataServer.createGLSL(GLContext.mgl_Normal, nComps, nDataType, false, initialSize, glBufferUsage);
this.texcoordVBO = GLArrayDataServer.createGLSL(GLContext.mgl_MultiTexCoord, tComps, tDataType, false, initialSize, glBufferUsage);
}
if(!vboUsage) {
this.vertexVBO.setVBOUsage(vboUsage);
this.colorVBO.setVBOUsage(vboUsage);
this.normalVBO.setVBOUsage(vboUsage);
this.texcoordVBO.setVBOUsage(vboUsage);
}
this.sealed=false;
this.mode = -1;
this.modeOrig = -1;
}
protected final VBOSet regenerate() {
return new VBOSet(glBufferUsage, initialSize,
vComps, vDataType, cComps, cDataType, nComps, nDataType, tComps, tDataType, useGLSL);
}
protected void destroy(GL gl) {
vertexVBO.destroy(gl);
normalVBO.destroy(gl);
colorVBO.destroy(gl);
texcoordVBO.destroy(gl);
this.mode = -1;
this.modeOrig = -1;
this.sealed=false;
}
protected void reset(GL gl) {
vertexVBO.reset(gl);
normalVBO.reset(gl);
colorVBO.reset(gl);
texcoordVBO.reset(gl);
this.mode = -1;
this.modeOrig = -1;
this.sealed=false;
}
public String toString() {
return "VBOSet[mode "+mode+
", modeOrig "+modeOrig+
", sealed "+sealed+
",\n\t"+vertexVBO+
",\n\t"+normalVBO+
",\n\t"+colorVBO+
",\n\t"+texcoordVBO+
"]";
}
protected void checkSeal(boolean test) throws GLException {
if(mode<0) {
throw new GLException("No mode set yet, call glBegin(mode) first:\n\t"+this);
}
if(sealed!=test) {
if(test) {
throw new GLException("Not Sealed yet, call glEnd() first:\n\t"+this);
} else {
throw new GLException("Already Sealed, can't modify VBO after glEnd():\n\t"+this);
}
}
}
protected void rewind() {
checkSeal(true);
vertexVBO.rewind();
normalVBO.rewind();
colorVBO.rewind();
texcoordVBO.rewind();
}
protected void seal(GL gl, boolean disableBufferAfterSeal)
{
checkSeal(false);
sealed = true;
vertexVBO.seal(gl, true);
normalVBO.seal(gl, true);
colorVBO.seal(gl, true);
texcoordVBO.seal(gl, true);
if(disableBufferAfterSeal) {
vertexVBO.enableBuffer(gl, false);
normalVBO.enableBuffer(gl, false);
colorVBO.enableBuffer(gl, false);
texcoordVBO.enableBuffer(gl, false);
}
}
protected void draw(GL gl, Buffer indices, boolean disableBufferAfterDraw, int i)
{
if(DEBUG_DRAW) {
Exception e = new Exception("ImmModeSink.draw["+i+"](disableBufferAfterDraw: "+disableBufferAfterDraw+"):\n\t"+this);
e.printStackTrace();
}
normalVBO.enableBuffer(gl, true);
colorVBO.enableBuffer(gl, true);
texcoordVBO.enableBuffer(gl, true);
vertexVBO.enableBuffer(gl, true);
if (vertexVBO.getBuffer()!=null) {
if(null==indices) {
gl.glDrawArrays(mode, 0, vertexVBO.getElementNumber());
} else {
Class clazz = indices.getClass();
int type=-1;
if(GLReflection.instanceOf(clazz, ByteBuffer.class.getName())) {
type = GL.GL_UNSIGNED_BYTE;
} else if(GLReflection.instanceOf(clazz, ShortBuffer.class.getName())) {
type = GL.GL_UNSIGNED_SHORT;
}
if(0>type) {
throw new GLException("Given Buffer Class not supported: "+clazz+", should be ubyte or ushort:\n\t"+this);
}
gl.glDrawElements(mode, indices.remaining(), type, indices);
// GL2: gl.glDrawRangeElements(mode, 0, indices.remaining()-1, indices.remaining(), type, indices);
}
}
if(disableBufferAfterDraw) {
vertexVBO.enableBuffer(gl, false);
texcoordVBO.enableBuffer(gl, false);
colorVBO.enableBuffer(gl, false);
normalVBO.enableBuffer(gl, false);
}
}
protected void glVertexv(Buffer v) {
checkSeal(false);
vertexVBO.put(v);
}
protected void glVertex2f(float x, float y) {
checkSeal(false);
vertexVBO.putf(x);
if(vertexVBO.getComponentNumber()>1)
vertexVBO.putf(y);
vertexVBO.padding(2);
}
protected void glVertex3f(float x, float y, float z) {
checkSeal(false);
vertexVBO.putf(x);
if(vertexVBO.getComponentNumber()>1)
vertexVBO.putf(y);
if(vertexVBO.getComponentNumber()>2)
vertexVBO.putf(z);
vertexVBO.padding(3);
}
protected void glNormalv(Buffer v) {
checkSeal(false);
normalVBO.put(v);
}
protected void glNormal3f(float x, float y, float z) {
checkSeal(false);
normalVBO.putf(x);
if(normalVBO.getComponentNumber()>1)
normalVBO.putf(y);
if(normalVBO.getComponentNumber()>2)
normalVBO.putf(z);
normalVBO.padding(3);
}
protected void glColorv(Buffer v) {
checkSeal(false);
colorVBO.put(v);
}
protected void glColor3f(float x, float y, float z) {
checkSeal(false);
colorVBO.putf(x);
if(colorVBO.getComponentNumber()>1)
colorVBO.putf(y);
if(colorVBO.getComponentNumber()>2)
colorVBO.putf(z);
colorVBO.padding(3);
}
protected void glColor4f(float x, float y, float z, float a) {
checkSeal(false);
colorVBO.putf(x);
if(colorVBO.getComponentNumber()>1)
colorVBO.putf(y);
if(colorVBO.getComponentNumber()>2)
colorVBO.putf(z);
if(colorVBO.getComponentNumber()>3)
colorVBO.putf(a);
colorVBO.padding(4);
}
protected void glTexCoordv(Buffer v) {
checkSeal(false);
texcoordVBO.put(v);
}
protected void glTexCoord2f(float x, float y) {
checkSeal(false);
texcoordVBO.putf(x);
if(texcoordVBO.getComponentNumber()>1)
texcoordVBO.putf(y);
texcoordVBO.padding(2);
}
protected void glTexCoord3f(float x, float y, float z) {
checkSeal(false);
texcoordVBO.putf(x);
if(texcoordVBO.getComponentNumber()>1)
texcoordVBO.putf(y);
if(texcoordVBO.getComponentNumber()>2)
texcoordVBO.putf(z);
texcoordVBO.padding(3);
}
protected void glVertex2s(short x, short y) {
checkSeal(false);
vertexVBO.puts(x);
if(vertexVBO.getComponentNumber()>1)
vertexVBO.puts(y);
vertexVBO.padding(2);
}
protected void glVertex3s(short x, short y, short z) {
checkSeal(false);
vertexVBO.puts(x);
if(vertexVBO.getComponentNumber()>1)
vertexVBO.puts(y);
if(vertexVBO.getComponentNumber()>2)
vertexVBO.puts(z);
vertexVBO.padding(3);
}
protected void glNormal3s(short x, short y, short z) {
checkSeal(false);
normalVBO.puts(x);
if(normalVBO.getComponentNumber()>1)
normalVBO.puts(y);
if(normalVBO.getComponentNumber()>2)
normalVBO.puts(z);
normalVBO.padding(3);
}
protected void glColor3s(short x, short y, short z) {
checkSeal(false);
colorVBO.puts(x);
if(colorVBO.getComponentNumber()>1)
colorVBO.puts(y);
if(colorVBO.getComponentNumber()>2)
colorVBO.puts(z);
colorVBO.padding(3);
}
protected void glColor4s(short x, short y, short z, short a) {
checkSeal(false);
colorVBO.puts(x);
if(colorVBO.getComponentNumber()>1)
colorVBO.puts(y);
if(colorVBO.getComponentNumber()>2)
colorVBO.puts(z);
if(colorVBO.getComponentNumber()>3)
colorVBO.puts(a);
colorVBO.padding(4);
}
protected void glTexCoord2s(short x, short y) {
checkSeal(false);
texcoordVBO.puts(x);
if(texcoordVBO.getComponentNumber()>1)
texcoordVBO.puts(y);
texcoordVBO.padding(2);
}
protected void glTexCoord3s(short x, short y, short z) {
checkSeal(false);
texcoordVBO.puts(x);
if(texcoordVBO.getComponentNumber()>1)
texcoordVBO.puts(y);
if(texcoordVBO.getComponentNumber()>2)
texcoordVBO.puts(z);
texcoordVBO.padding(3);
}
protected void glVertex2b(byte x, byte y) {
checkSeal(false);
vertexVBO.putb(x);
if(vertexVBO.getComponentNumber()>1)
vertexVBO.putb(y);
vertexVBO.padding(2);
}
protected void glVertex3b(byte x, byte y, byte z) {
checkSeal(false);
vertexVBO.putb(x);
if(vertexVBO.getComponentNumber()>1)
vertexVBO.putb(y);
if(vertexVBO.getComponentNumber()>2)
vertexVBO.putb(z);
vertexVBO.padding(3);
}
protected void glNormal3b(byte x, byte y, byte z) {
checkSeal(false);
normalVBO.putb(x);
if(normalVBO.getComponentNumber()>1)
normalVBO.putb(y);
if(normalVBO.getComponentNumber()>2)
normalVBO.putb(z);
normalVBO.padding(3);
}
protected void glColor3b(byte x, byte y, byte z) {
checkSeal(false);
colorVBO.putb(x);
if(colorVBO.getComponentNumber()>1)
colorVBO.putb(y);
if(colorVBO.getComponentNumber()>2)
colorVBO.putb(z);
colorVBO.padding(3);
}
protected void glColor4b(byte x, byte y, byte z, byte a) {
checkSeal(false);
colorVBO.putb(x);
if(colorVBO.getComponentNumber()>1)
colorVBO.putb(y);
if(colorVBO.getComponentNumber()>2)
colorVBO.putb(z);
if(colorVBO.getComponentNumber()>3)
colorVBO.putb(a);
colorVBO.padding(4);
}
protected void glTexCoord2b(byte x, byte y) {
checkSeal(false);
texcoordVBO.putb(x);
if(texcoordVBO.getComponentNumber()>1)
texcoordVBO.putb(y);
texcoordVBO.padding(2);
}
protected void glTexCoord3b(byte x, byte y, byte z) {
checkSeal(false);
texcoordVBO.putb(x);
if(texcoordVBO.getComponentNumber()>1)
texcoordVBO.putb(y);
if(texcoordVBO.getComponentNumber()>2)
texcoordVBO.putb(z);
texcoordVBO.padding(3);
}
GLArrayDataServer vertexVBO;
GLArrayDataServer normalVBO;
GLArrayDataServer colorVBO;
GLArrayDataServer texcoordVBO;
int mode, modeOrig;
int glBufferUsage, initialSize;
int vComps, cComps, nComps, tComps;
int vDataType, cDataType, nDataType, tDataType;
boolean sealed, useGLSL;
}
}
|