forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPJOGL.java
More file actions
2628 lines (2154 loc) · 75.5 KB
/
PJOGL.java
File metadata and controls
2628 lines (2154 loc) · 75.5 KB
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
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package processing.jogl;
import java.awt.Canvas;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Shape;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.PathIterator;
import java.io.IOException;
import java.net.URL;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
//import java.util.concurrent.CountDownLatch;
import javax.media.opengl.GL;
import javax.media.opengl.GL2;
import javax.media.opengl.GL2ES1;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GL2ES3;
import javax.media.opengl.GL2GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilitiesImmutable;
import javax.media.opengl.GLContext;
import javax.media.opengl.GLDrawable;
//import javax.media.opengl.GLEventListener;
//import javax.media.opengl.GLFBODrawable;
import javax.media.opengl.GLProfile;
//import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;
import javax.media.opengl.glu.GLUtessellator;
import javax.media.opengl.glu.GLUtessellatorCallbackAdapter;
import processing.core.PApplet;
import processing.core.PGraphics;
import processing.opengl.PGL;
import processing.opengl.PGraphicsOpenGL;
//import com.jogamp.newt.awt.NewtCanvasAWT;
//import com.jogamp.newt.opengl.GLWindow;
//import com.jogamp.opengl.FBObject;
public class PJOGL extends PGL {
// OpenGL profile to use (2, 3 or 4)
public static int PROFILE = 2;
// Enables/disables Retina support on OSX
public static boolean RETINA = false;
// The two windowing toolkits available to use in JOGL:
public static final int AWT = 0; // http://jogamp.org/wiki/index.php/Using_JOGL_in_AWT_SWT_and_Swing
public static final int NEWT = 1; // http://jogamp.org/jogl/doc/NEWT-Overview.html
// ........................................................
// Public members to access the underlying GL objects and context
/** Basic GL functionality, common to all profiles */
public GL gl;
/** GLU interface **/
public GLU glu;
/** The rendering context (holds rendering state info) */
public GLContext context;
/** The canvas where OpenGL rendering takes place */
public Canvas canvas;
/** Selected GL profile */
public static GLProfile profile;
// ........................................................
// Additional parameters
/** Time that the Processing's animation thread will wait for JOGL's rendering
* thread to be done with a single frame.
*/
protected static int DRAW_TIMEOUT_MILLIS = 500;
// ........................................................
// OS-specific configuration
/*
protected static int WINDOW_TOOLKIT;
protected static int EVENTS_TOOLKIT;
protected static boolean USE_JOGL_FBOLAYER;
static {
if (PApplet.platform == PConstants.WINDOWS) {
// Using AWT on Windows because NEWT displays a black background while
// initializing, and the cursor functions don't work. GLWindow has some
// functions for basic cursor handling (hide/show):
// GLWindow.setPointerVisible(false);
// but apparently nothing to set the cursor icon:
// https://jogamp.org/bugzilla/show_bug.cgi?id=409
WINDOW_TOOLKIT = AWT;
EVENTS_TOOLKIT = AWT;
USE_FBOLAYER_BY_DEFAULT = false;
USE_JOGL_FBOLAYER = false;
} else if (PApplet.platform == PConstants.MACOSX) {
// Note: The JOGL FBO layer (in 2.0.2) seems incompatible with NEWT.
WINDOW_TOOLKIT = AWT;
EVENTS_TOOLKIT = AWT;
USE_FBOLAYER_BY_DEFAULT = true;
USE_JOGL_FBOLAYER = true;
} else if (PApplet.platform == PConstants.LINUX) {
WINDOW_TOOLKIT = AWT;
EVENTS_TOOLKIT = AWT;
USE_FBOLAYER_BY_DEFAULT = false;
USE_JOGL_FBOLAYER = false;
} else if (PApplet.platform == PConstants.OTHER) {
WINDOW_TOOLKIT = NEWT; // NEWT works on the Raspberry pi?
EVENTS_TOOLKIT = NEWT;
USE_FBOLAYER_BY_DEFAULT = false;
USE_JOGL_FBOLAYER = false;
}
}
*/
// protected static boolean USE_FBOLAYER_BY_DEFAULT = false;
// protected static boolean USE_JOGL_FBOLAYER = false;
// ........................................................
// Protected JOGL-specific objects needed to access the GL profiles
/** The capabilities of the OpenGL rendering surface */
protected GLCapabilitiesImmutable capabilities;
/** The rendering surface */
protected GLDrawable drawable;
/** GLES2 functionality (shaders, etc) */
protected GL2ES2 gl2;
/** GL3 interface */
protected GL2GL3 gl3;
/** GL2 desktop functionality (blit framebuffer, map buffer range,
* multisampled renderbuffers) */
protected GL2 gl2x;
/** The AWT-OpenGL canvas */
// protected GLCanvas canvasAWT;
/** The NEWT window */
// protected GLWindow windowNEWT;
/** The NEWT-OpenGL canvas */
// protected NewtCanvasAWT canvasNEWT;
/** The listener that fires the frame rendering in Processing */
// protected PGLListener listener;
/** This countdown latch is used to maintain the synchronization between
* Processing's drawing thread and JOGL's rendering thread */
// protected CountDownLatch drawLatch = new CountDownLatch(0);
/** Flag used to do request final display() call to make sure that the
* buffers are properly swapped.
*/
// protected boolean prevCanDraw = false;
/** Stores exceptions that ocurred during drawing */
protected Exception drawException;
// ........................................................
// JOGL's FBO-layer
/** Back (== draw, current frame) buffer */
// protected FBObject backFBO;
/** Sink buffer, used in the multisampled case */
// protected FBObject sinkFBO;
/** Front (== read, previous frame) buffer */
// protected FBObject frontFBO;
// protected FBObject.TextureAttachment backTexAttach;
// protected FBObject.TextureAttachment frontTexAttach;
// protected boolean changedFrontTex = false;
// protected boolean changedBackTex = false;
// ........................................................
// Retina support
int pixel_scale = 1;
// ........................................................
// Utility arrays to copy projection/modelview matrices to GL
protected float[] projMatrix;
protected float[] mvMatrix;
// ........................................................
// Static initialization for some parameters that need to be different for
// JOGL
static {
MIN_DIRECT_BUFFER_SIZE = 2;
INDEX_TYPE = GL.GL_UNSIGNED_SHORT;
}
///////////////////////////////////////////////////////////////
// Initialization, finalization
public PJOGL(PGraphicsOpenGL pg) {
super(pg);
glu = new GLU();
}
/*
@Override
public Canvas getCanvas() {
return canvas;
}
*/
protected void setFps(float fps) {
if (!setFps || targetFps != fps) {
if (60 < fps) {
// Disables v-sync
gl.setSwapInterval(0);
} else if (30 < fps) {
gl.setSwapInterval(1);
} else {
gl.setSwapInterval(2);
}
targetFps = currentFps = fps;
setFps = true;
}
}
/*
@Override
protected void initSurface(int antialias) {
if (profile == null) {
if (PROFILE == 2) {
try {
profile = GLProfile.getGL2ES1();
} catch (GLException ex) {
profile = GLProfile.getMaxFixedFunc(true);
}
} else if (PROFILE == 3) {
try {
profile = GLProfile.getGL2GL3();
} catch (GLException ex) {
profile = GLProfile.getMaxProgrammable(true);
}
if (!profile.isGL3()) {
PGraphics.showWarning("Requested profile GL3 but is not available, got: " + profile);
}
} else if (PROFILE == 4) {
try {
profile = GLProfile.getGL4ES3();
} catch (GLException ex) {
profile = GLProfile.getMaxProgrammable(true);
}
if (!profile.isGL4()) {
PGraphics.showWarning("Requested profile GL4 but is not available, got: " + profile);
}
} else throw new RuntimeException(UNSUPPORTED_GLPROF_ERROR);
if (2 < PROFILE) {
texVertShaderSource = convertVertexSource(texVertShaderSource, 120, 150);
tex2DFragShaderSource = convertFragmentSource(tex2DFragShaderSource, 120, 150);
texRectFragShaderSource = convertFragmentSource(texRectFragShaderSource, 120, 150);
}
}
if (canvasAWT != null || canvasNEWT != null) {
// Restarting...
if (canvasAWT != null) {
canvasAWT.removeGLEventListener(listener);
pg.parent.removeListeners(canvasAWT);
pg.parent.remove(canvasAWT);
} else if (canvasNEWT != null) {
windowNEWT.removeGLEventListener(listener);
pg.parent.remove(canvasNEWT);
}
sinkFBO = backFBO = frontFBO = null;
}
// Setting up the desired capabilities;
GLCapabilities caps = new GLCapabilities(profile);
caps.setAlphaBits(REQUESTED_ALPHA_BITS);
caps.setDepthBits(REQUESTED_DEPTH_BITS);
caps.setStencilBits(REQUESTED_STENCIL_BITS);
caps.setBackgroundOpaque(true);
caps.setOnscreen(true);
if (USE_FBOLAYER_BY_DEFAULT) {
if (USE_JOGL_FBOLAYER) {
caps.setPBuffer(false);
caps.setFBO(true);
if (1 < antialias) {
caps.setSampleBuffers(true);
caps.setNumSamples(antialias);
} else {
caps.setSampleBuffers(false);
}
fboLayerRequested = false;
} else {
caps.setPBuffer(false);
caps.setFBO(false);
caps.setSampleBuffers(false);
fboLayerRequested = 1 < antialias;
}
} else {
if (1 < antialias) {
caps.setSampleBuffers(true);
caps.setNumSamples(antialias);
} else {
caps.setSampleBuffers(false);
}
fboLayerRequested = false;
}
caps.setDepthBits(REQUESTED_DEPTH_BITS);
caps.setStencilBits(REQUESTED_STENCIL_BITS);
caps.setAlphaBits(REQUESTED_ALPHA_BITS);
reqNumSamples = qualityToSamples(antialias);
if (WINDOW_TOOLKIT == AWT) {
canvasAWT = new GLCanvas(caps);
if (RETINA) {
canvasAWT.setSurfaceScale(new int[] { ScalableSurface.AUTOMAX_PIXELSCALE,
ScalableSurface.AUTOMAX_PIXELSCALE });
retf = 2;
} else {
canvasAWT.setSurfaceScale(new int[] { ScalableSurface.IDENTITY_PIXELSCALE,
ScalableSurface.IDENTITY_PIXELSCALE });
}
canvasAWT.setBounds(0, 0, pg.width, pg.height);
canvasAWT.setBackground(new Color(pg.backgroundColor, true));
canvasAWT.setFocusable(true);
pg.parent.setLayout(new BorderLayout());
pg.parent.add(canvasAWT, BorderLayout.CENTER);
canvasAWT.requestFocusInWindow();
canvas = canvasAWT;
canvasNEWT = null;
} else if (WINDOW_TOOLKIT == NEWT) {
windowNEWT = GLWindow.create(caps);
canvasNEWT = new NewtCanvasAWT(windowNEWT);
canvasNEWT.setBounds(0, 0, pg.width, pg.height);
canvasNEWT.setBackground(new Color(pg.backgroundColor, true));
canvasNEWT.setFocusable(true);
pg.parent.setLayout(new BorderLayout());
pg.parent.add(canvasNEWT, BorderLayout.CENTER);
canvasNEWT.requestFocusInWindow();
int[] reqSurfacePixelScale = new int[] { ScalableSurface.AUTOMAX_PIXELSCALE, ScalableSurface.AUTOMAX_PIXELSCALE };
windowNEWT.setSurfaceScale(reqSurfacePixelScale);
canvas = canvasNEWT;
canvasAWT = null;
}
pg.parent.defaultSize = false;
registerListeners();
fboLayerCreated = false;
fboLayerInUse = false;
firstFrame = true;
setFps = false;
}
*/
/*
@Override
protected void reinitSurface() {
sinkFBO = backFBO = frontFBO = null;
fboLayerCreated = false;
fboLayerInUse = false;
firstFrame = true;
pg.parent.defaultSize = false;
}
*/
// @Override
// protected void registerListeners() {
// if (WINDOW_TOOLKIT == AWT) {
// pg.parent.addListeners(canvasAWT);
//
// listener = new PGLListener();
// canvasAWT.addGLEventListener(listener);
// } else if (WINDOW_TOOLKIT == NEWT) {
// if (EVENTS_TOOLKIT == NEWT) {
// NEWTMouseListener mouseListener = new NEWTMouseListener();
// windowNEWT.addMouseListener(mouseListener);
// NEWTKeyListener keyListener = new NEWTKeyListener();
// windowNEWT.addKeyListener(keyListener);
// NEWTWindowListener winListener = new NEWTWindowListener();
// windowNEWT.addWindowListener(winListener);
// } else if (EVENTS_TOOLKIT == AWT) {
// pg.parent.addListeners(canvasNEWT);
// }
//
// listener = new PGLListener();
// windowNEWT.addGLEventListener(listener);
// }
//
// if (canvas != null) {
// canvas.setFocusTraversalKeysEnabled(false);
// }
// }
// @Override
// protected void deleteSurface() {
// super.deleteSurface();
//
// if (canvasAWT != null) {
// canvasAWT.removeGLEventListener(listener);
// pg.parent.removeListeners(canvasAWT);
// } else if (canvasNEWT != null) {
// windowNEWT.removeGLEventListener(listener);
// }
// }
/*
@Override
protected int getReadFramebuffer() {
if (fboLayerInUse) {
return glColorFbo.get(0);
} else if (capabilities.isFBO()) {
return context.getDefaultReadFramebuffer();
} else {
return 0;
}
}
@Override
protected int getDrawFramebuffer() {
if (fboLayerInUse) {
if (1 < numSamples) {
return glMultiFbo.get(0);
} else {
return glColorFbo.get(0);
}
} else if (capabilities.isFBO()) {
return context.getDefaultDrawFramebuffer();
} else {
return 0;
}
}
@Override
protected int getDefaultDrawBuffer() {
if (fboLayerInUse) {
return COLOR_ATTACHMENT0;
} else if (capabilities.isFBO()) {
return GL.GL_COLOR_ATTACHMENT0;
} else if (capabilities.getDoubleBuffered()) {
return GL.GL_BACK;
} else {
return GL.GL_FRONT;
}
}
@Override
protected int getDefaultReadBuffer() {
if (fboLayerInUse) {
return COLOR_ATTACHMENT0;
} else if (capabilities.isFBO()) {
return GL.GL_COLOR_ATTACHMENT0;
} else if (capabilities.getDoubleBuffered()) {
return GL.GL_BACK;
} else {
return GL.GL_FRONT;
}
}
@Override
protected boolean isFBOBacked() {
return super.isFBOBacked() || capabilities.isFBO();
}
@Override
protected int getDepthBits() {
return capabilities.getDepthBits();
}
@Override
protected int getStencilBits() {
return capabilities.getStencilBits();
}
@Override
protected Texture wrapBackTexture(Texture texture) {
if (texture == null || changedBackTex) {
if (USE_JOGL_FBOLAYER) {
texture = new Texture(pg);
texture.init(pg.width, pg.height,
backTexAttach.getName(), TEXTURE_2D, RGBA,
backTexAttach.getWidth(), backTexAttach.getHeight(),
backTexAttach.minFilter, backTexAttach.magFilter,
backTexAttach.wrapS, backTexAttach.wrapT);
texture.invertedY(true);
texture.colorBuffer(true);
pg.setCache(pg, texture);
} else {
texture = super.wrapBackTexture(null);
}
} else {
if (USE_JOGL_FBOLAYER) {
texture.glName = backTexAttach.getName();
} else {
texture = super.wrapBackTexture(texture);
}
}
return texture;
}
@Override
protected Texture wrapFrontTexture(Texture texture) {
if (texture == null || changedFrontTex) {
if (USE_JOGL_FBOLAYER) {
texture = new Texture(pg);
texture.init(pg.width, pg.height,
backTexAttach.getName(), TEXTURE_2D, RGBA,
frontTexAttach.getWidth(), frontTexAttach.getHeight(),
frontTexAttach.minFilter, frontTexAttach.magFilter,
frontTexAttach.wrapS, frontTexAttach.wrapT);
texture.invertedY(true);
texture.colorBuffer(true);
} else {
texture = super.wrapFrontTexture(null);
}
} else {
if (USE_JOGL_FBOLAYER) {
texture.glName = frontTexAttach.getName();
} else {
texture = super.wrapFrontTexture(texture);
}
}
return texture;
}
@Override
protected void bindFrontTexture() {
if (USE_JOGL_FBOLAYER) {
usingFrontTex = true;
if (!texturingIsEnabled(TEXTURE_2D)) {
enableTexturing(TEXTURE_2D);
}
bindTexture(TEXTURE_2D, frontTexAttach.getName());
} else super.bindFrontTexture();
}
@Override
protected void unbindFrontTexture() {
if (USE_JOGL_FBOLAYER) {
if (textureIsBound(TEXTURE_2D, frontTexAttach.getName())) {
// We don't want to unbind another texture
// that might be bound instead of this one.
if (!texturingIsEnabled(TEXTURE_2D)) {
enableTexturing(TEXTURE_2D);
bindTexture(TEXTURE_2D, 0);
disableTexturing(TEXTURE_2D);
} else {
bindTexture(TEXTURE_2D, 0);
}
}
} else super.unbindFrontTexture();
}
@Override
protected void syncBackTexture() {
if (USE_JOGL_FBOLAYER) {
if (usingFrontTex) needSepFrontTex = true;
if (1 < numSamples && backFBO != null) {
backFBO.syncSamplingSink(gl);
backFBO.bind(gl);
}
} else super.syncBackTexture();
}
@Override
protected void beginDraw(boolean clear0) {
if (!setFps) setFps(targetFps);
if (USE_JOGL_FBOLAYER) return;
super.beginDraw(clear0);
}
@Override
protected void endDraw(boolean clear0) {
if (isFBOBacked()) {
if (USE_JOGL_FBOLAYER) {
if (!clear0 && isFBOBacked() && !isMultisampled() &&
frontFBO != null && backFBO != null) {
// Draw the back texture into the front texture, which will be used as
// back texture in the next frame. Otherwise flickering will occur if
// the sketch uses "incremental drawing" (background() not called).
frontFBO.bind(gl);
gl.glDisable(GL.GL_BLEND);
drawTexture(TEXTURE_2D, backTexAttach.getName(),
backTexAttach.getWidth(), backTexAttach.getHeight(),
pg.width, pg.height,
0, 0, pg.width, pg.height, 0, 0, pg.width, pg.height);
backFBO.bind(gl);
}
} else {
super.endDraw(clear0);
}
}
}
*/
@Override
protected void getGL(PGL pgl) {
PJOGL pjogl = (PJOGL)pgl;
this.drawable = pjogl.drawable;
this.context = pjogl.context;
this.glContext = pjogl.glContext;
setThread(pjogl.glThread);
this.gl = pjogl.gl;
this.gl2 = pjogl.gl2;
this.gl2x = pjogl.gl2x;
this.gl3 = pjogl.gl3;
}
protected void getGL(GLAutoDrawable glDrawable) {
context = glDrawable.getContext();
glContext = context.hashCode();
setThread(Thread.currentThread());
gl = context.getGL();
gl2 = gl.getGL2ES2();
try {
gl2x = gl.getGL2();
} catch (javax.media.opengl.GLException e) {
gl2x = null;
}
try {
gl3 = gl.getGL2GL3();
} catch (javax.media.opengl.GLException e) {
gl3 = null;
}
}
/*
@Override
protected boolean canDraw() {
return true;
// return pg.initialized;
// && pg.parent.isDisplayable();
}
@Override
protected void requestFocus() { }
@Override
protected void requestDraw() {
drawException = null;
boolean canDraw = pg.parent.canDraw();
if (pg.initialized && (canDraw || prevCanDraw)) {
drawLatch = new CountDownLatch(1);
if (WINDOW_TOOLKIT == AWT) {
canvasAWT.display();
} else if (WINDOW_TOOLKIT == NEWT) {
windowNEWT.display();
}
try {
drawLatch.await(DRAW_TIMEOUT_MILLIS, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (canDraw) prevCanDraw = true;
else prevCanDraw = false;
}
// Throw wherever exception happened during drawing outside the GL thread
// to it is properly picked up by the PDE.
if (drawException != null) {
if (drawException instanceof RuntimeException) {
throw (RuntimeException)drawException;
} else {
throw new RuntimeException(drawException);
}
}
}
@Override
protected void swapBuffers() {
if (WINDOW_TOOLKIT == AWT) {
canvasAWT.swapBuffers();
} else if (WINDOW_TOOLKIT == NEWT) {
windowNEWT.swapBuffers();
}
}
*/
@Override
protected void beginGL() {
if (gl2x != null) {
if (projMatrix == null) {
projMatrix = new float[16];
}
gl2x.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
projMatrix[ 0] = pg.projection.m00;
projMatrix[ 1] = pg.projection.m10;
projMatrix[ 2] = pg.projection.m20;
projMatrix[ 3] = pg.projection.m30;
projMatrix[ 4] = pg.projection.m01;
projMatrix[ 5] = pg.projection.m11;
projMatrix[ 6] = pg.projection.m21;
projMatrix[ 7] = pg.projection.m31;
projMatrix[ 8] = pg.projection.m02;
projMatrix[ 9] = pg.projection.m12;
projMatrix[10] = pg.projection.m22;
projMatrix[11] = pg.projection.m32;
projMatrix[12] = pg.projection.m03;
projMatrix[13] = pg.projection.m13;
projMatrix[14] = pg.projection.m23;
projMatrix[15] = pg.projection.m33;
gl2x.glLoadMatrixf(projMatrix, 0);
if (mvMatrix == null) {
mvMatrix = new float[16];
}
gl2x.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
mvMatrix[ 0] = pg.modelview.m00;
mvMatrix[ 1] = pg.modelview.m10;
mvMatrix[ 2] = pg.modelview.m20;
mvMatrix[ 3] = pg.modelview.m30;
mvMatrix[ 4] = pg.modelview.m01;
mvMatrix[ 5] = pg.modelview.m11;
mvMatrix[ 6] = pg.modelview.m21;
mvMatrix[ 7] = pg.modelview.m31;
mvMatrix[ 8] = pg.modelview.m02;
mvMatrix[ 9] = pg.modelview.m12;
mvMatrix[10] = pg.modelview.m22;
mvMatrix[11] = pg.modelview.m32;
mvMatrix[12] = pg.modelview.m03;
mvMatrix[13] = pg.modelview.m13;
mvMatrix[14] = pg.modelview.m23;
mvMatrix[15] = pg.modelview.m33;
gl2x.glLoadMatrixf(mvMatrix, 0);
}
}
@Override
protected boolean hasFBOs() {
if (context.hasBasicFBOSupport()) return true;
else return super.hasFBOs();
}
@Override
protected boolean hasShaders() {
if (context.hasGLSL()) return true;
else return super.hasShaders();
}
///////////////////////////////////////////////////////////
// JOGL event listeners
/*
protected void getBuffers(GLWindow glWindow) {
if (false) {
// if (capabilities.isFBO()) {
// if (USE_JOGL_FBOLAYER && capabilities.isFBO()) {
// The onscreen drawing surface is backed by an FBO layer.
GLFBODrawable fboDrawable = null;
fboDrawable = (GLFBODrawable)glWindow.getDelegatedDrawable();
if (fboDrawable != null) {
backFBO = fboDrawable.getFBObject(GL.GL_BACK);
if (1 < numSamples) {
if (needSepFrontTex) {
// When using multisampled FBO, the back buffer is the MSAA
// surface so it cannot be read from. The sink buffer contains
// the readable 2D texture.
// In this case, we create an auxiliary "front" buffer that it is
// swapped with the sink buffer at the beginning of each frame.
// In this way, we always have a readable copy of the previous
// frame in the front texture, while the back is synchronized
// with the contents of the MSAA back buffer when requested.
if (frontFBO == null) {
// init
frontFBO = new FBObject();
frontFBO.reset(gl, pg.width, pg.height, numSamples);
frontFBO.attachTexture2D(gl, 0, true);
sinkFBO = backFBO.getSamplingSinkFBO();
changedFrontTex = changedBackTex = true;
} else {
// swap
FBObject temp = sinkFBO;
sinkFBO = frontFBO;
frontFBO = temp;
backFBO.setSamplingSink(sinkFBO);
changedFrontTex = changedBackTex = false;
}
backTexAttach = (FBObject.TextureAttachment) sinkFBO.
getColorbuffer(0);
frontTexAttach = (FBObject.TextureAttachment)frontFBO.
getColorbuffer(0);
} else {
changedFrontTex = changedBackTex = sinkFBO == null;
// Default setting (to save resources): the front and back
// textures are the same.
sinkFBO = backFBO.getSamplingSinkFBO();
backTexAttach = (FBObject.TextureAttachment) sinkFBO.
getColorbuffer(0);
frontTexAttach = backTexAttach;
}
} else {
// w/out multisampling, rendering is done on the back buffer.
frontFBO = fboDrawable.getFBObject(GL.GL_FRONT);
backTexAttach = (FBObject.TextureAttachment) backFBO.getColorbuffer(0);
frontTexAttach = (FBObject.TextureAttachment) frontFBO.getColorbuffer(0);
}
}
}
}
*/
protected void init(GLAutoDrawable glDrawable) {
firstFrame = true;
capabilities = glDrawable.getChosenGLCapabilities();
if (!hasFBOs()) {
throw new RuntimeException(MISSING_FBO_ERROR);
}
if (!hasShaders()) {
throw new RuntimeException(MISSING_GLSL_ERROR);
}
// if (USE_JOGL_FBOLAYER && capabilities.isFBO()) {
// int maxs = maxSamples();
// numSamples = PApplet.min(capabilities.getNumSamples(), maxs);
// }
}
/*
protected class PGLListener implements GLEventListener {
public PGLListener() {}
@Override
public void display(GLAutoDrawable glDrawable) {
getGL(glDrawable);
if (USE_JOGL_FBOLAYER && capabilities.isFBO()) {
// The onscreen drawing surface is backed by an FBO layer.
GLFBODrawable fboDrawable = null;
if (WINDOW_TOOLKIT == AWT) {
GLCanvas glCanvas = (GLCanvas)glDrawable;
fboDrawable = (GLFBODrawable)glCanvas.getDelegatedDrawable();
} else {
GLWindow glWindow = (GLWindow)glDrawable;
fboDrawable = (GLFBODrawable)glWindow.getDelegatedDrawable();
}
if (fboDrawable != null) {
backFBO = fboDrawable.getFBObject(GL.GL_BACK);
if (1 < numSamples) {
if (needSepFrontTex) {
// When using multisampled FBO, the back buffer is the MSAA
// surface so it cannot be read from. The sink buffer contains
// the readable 2D texture.
// In this case, we create an auxiliary "front" buffer that it is
// swapped with the sink buffer at the beginning of each frame.
// In this way, we always have a readable copy of the previous
// frame in the front texture, while the back is synchronized
// with the contents of the MSAA back buffer when requested.
if (frontFBO == null) {
// init
frontFBO = new FBObject();
frontFBO.reset(gl, pg.width, pg.height, numSamples);
frontFBO.attachTexture2D(gl, 0, true);
sinkFBO = backFBO.getSamplingSinkFBO();
changedFrontTex = changedBackTex = true;
} else {
// swap
FBObject temp = sinkFBO;
sinkFBO = frontFBO;
frontFBO = temp;
backFBO.setSamplingSink(sinkFBO);
changedFrontTex = changedBackTex = false;
}
backTexAttach = (FBObject.TextureAttachment) sinkFBO.
getColorbuffer(0);
frontTexAttach = (FBObject.TextureAttachment)frontFBO.
getColorbuffer(0);
} else {
changedFrontTex = changedBackTex = sinkFBO == null;
// Default setting (to save resources): the front and back
// textures are the same.
sinkFBO = backFBO.getSamplingSinkFBO();
backTexAttach = (FBObject.TextureAttachment) sinkFBO.
getColorbuffer(0);
frontTexAttach = backTexAttach;
}
} else {
// w/out multisampling, rendering is done on the back buffer.
frontFBO = fboDrawable.getFBObject(GL.GL_FRONT);
backTexAttach = (FBObject.TextureAttachment) backFBO.getColorbuffer(0);
frontTexAttach = (FBObject.TextureAttachment) frontFBO.getColorbuffer(0);
}
}
}
try {
pg.parent.handleDraw();
} catch (Exception ex) {
drawException = ex;
}
drawLatch.countDown();
}
@Override
public void dispose(GLAutoDrawable adrawable) {
}
@Override
public void init(GLAutoDrawable glDrawable) {
getGL(glDrawable);
capabilities = glDrawable.getChosenGLCapabilities();
if (!hasFBOs()) {
throw new RuntimeException(MISSING_FBO_ERROR);
}
if (!hasShaders()) {
throw new RuntimeException(MISSING_GLSL_ERROR);
}
if (USE_JOGL_FBOLAYER && capabilities.isFBO()) {
int maxs = maxSamples();
numSamples = PApplet.min(capabilities.getNumSamples(), maxs);
}
}
@Override
public void reshape(GLAutoDrawable glDrawable, int x, int y, int w, int h) {
//getGL(glDrawable);
}
// private void getGL(GLAutoDrawable glDrawable) {
// drawable = glDrawable;
// context = glDrawable.getContext();
// glContext = context.hashCode();
// glThread = Thread.currentThread();
//
// gl = context.getGL();
// gl2 = gl.getGL2ES2();
// try {