forked from processing/processing
/
PGraphicsOpenGL.java
12250 lines (10069 loc) · 366 KB
/
PGraphicsOpenGL.java
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
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2004-13 Ben Fry and Casey Reas
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License version 2.1 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.opengl;
import processing.core.*;
import java.net.URL;
import java.nio.*;
import java.util.*;
/**
* OpenGL renderer.
*
*/
public class PGraphicsOpenGL extends PGraphics {
/** Interface between Processing and OpenGL */
public PGL pgl;
/** The renderer currently in use. */
protected PGraphicsOpenGL currentPG;
/** Font cache for texture objects. */
protected WeakHashMap<PFont, FontTexture> fontMap;
// ........................................................
// Basic rendering parameters:
/** Whether the PGraphics object is ready to render or not. */
public boolean initialized;
/** Flush modes: continuously (geometry is flushed after each call to
* endShape) when-full (geometry is accumulated until a maximum size is
* reached. */
static protected final int FLUSH_CONTINUOUSLY = 0;
static protected final int FLUSH_WHEN_FULL = 1;
/** Type of geometry: immediate is that generated with beginShape/vertex/
* endShape, retained is the result of creating a PShapeOpenGL object with
* createShape. */
static protected final int IMMEDIATE = 0;
static protected final int RETAINED = 1;
/** Current flush mode. */
protected int flushMode = FLUSH_WHEN_FULL;
// ........................................................
// VBOs for immediate rendering:
public int glPolyVertex;
public int glPolyColor;
public int glPolyNormal;
public int glPolyTexcoord;
public int glPolyAmbient;
public int glPolySpecular;
public int glPolyEmissive;
public int glPolyShininess;
public int glPolyIndex;
protected boolean polyBuffersCreated = false;
protected int polyBuffersContext;
public int glLineVertex;
public int glLineColor;
public int glLineAttrib;
public int glLineIndex;
protected boolean lineBuffersCreated = false;
protected int lineBuffersContext;
public int glPointVertex;
public int glPointColor;
public int glPointAttrib;
public int glPointIndex;
protected boolean pointBuffersCreated = false;
protected int pointBuffersContext;
static protected final int INIT_VERTEX_BUFFER_SIZE = 256;
static protected final int INIT_INDEX_BUFFER_SIZE = 512;
// ........................................................
// GL parameters
static protected boolean glParamsRead = false;
/** Extensions used by Processing */
static public boolean npotTexSupported;
static public boolean autoMipmapGenSupported;
static public boolean fboMultisampleSupported;
static public boolean packedDepthStencilSupported;
static public boolean anisoSamplingSupported;
static public boolean blendEqSupported;
/** Some hardware limits */
static public int maxTextureSize;
static public int maxSamples;
static public float maxAnisoAmount;
static public int depthBits;
static public int stencilBits;
/** OpenGL information strings */
static public String OPENGL_VENDOR;
static public String OPENGL_RENDERER;
static public String OPENGL_VERSION;
static public String OPENGL_EXTENSIONS;
static public String GLSL_VERSION;
// ........................................................
// GL resources:
static protected HashMap<GLResource, Boolean> glTextureObjects =
new HashMap<GLResource, Boolean>();
static protected HashMap<GLResource, Boolean> glVertexBuffers =
new HashMap<GLResource, Boolean>();
static protected HashMap<GLResource, Boolean> glFrameBuffers =
new HashMap<GLResource, Boolean>();
static protected HashMap<GLResource, Boolean> glRenderBuffers =
new HashMap<GLResource, Boolean>();
static protected HashMap<GLResource, Boolean> glslPrograms =
new HashMap<GLResource, Boolean>();
static protected HashMap<GLResource, Boolean> glslVertexShaders =
new HashMap<GLResource, Boolean>();
static protected HashMap<GLResource, Boolean> glslFragmentShaders =
new HashMap<GLResource, Boolean>();
// ........................................................
// Shaders
static protected URL defColorShaderVertURL =
PGraphicsOpenGL.class.getResource("ColorVert.glsl");
static protected URL defTextureShaderVertURL =
PGraphicsOpenGL.class.getResource("TextureVert.glsl");
static protected URL defLightShaderVertURL =
PGraphicsOpenGL.class.getResource("LightVert.glsl");
static protected URL defTexlightShaderVertURL =
PGraphicsOpenGL.class.getResource("TexlightVert.glsl");
static protected URL defColorShaderFragURL =
PGraphicsOpenGL.class.getResource("ColorFrag.glsl");
static protected URL defTextureShaderFragURL =
PGraphicsOpenGL.class.getResource("TextureFrag.glsl");
static protected URL defLightShaderFragURL =
PGraphicsOpenGL.class.getResource("LightFrag.glsl");
static protected URL defTexlightShaderFragURL =
PGraphicsOpenGL.class.getResource("TexlightFrag.glsl");
static protected URL defLineShaderVertURL =
PGraphicsOpenGL.class.getResource("LineVert.glsl");
static protected URL defLineShaderFragURL =
PGraphicsOpenGL.class.getResource("LineFrag.glsl");
static protected URL defPointShaderVertURL =
PGraphicsOpenGL.class.getResource("PointVert.glsl");
static protected URL defPointShaderFragURL =
PGraphicsOpenGL.class.getResource("PointFrag.glsl");
static protected URL maskShaderFragURL =
PGraphicsOpenGL.class.getResource("MaskFrag.glsl");
protected PShader defColorShader;
protected PShader defTextureShader;
protected PShader defLightShader;
protected PShader defTexlightShader;
protected PShader defLineShader;
protected PShader defPointShader;
protected PShader maskShader;
protected PShader polyShader;
protected PShader lineShader;
protected PShader pointShader;
// ........................................................
// Tessellator, geometry
protected InGeometry inGeo;
protected TessGeometry tessGeo;
protected TexCache texCache;
static protected Tessellator tessellator;
// ........................................................
// Camera:
/** Camera field of view. */
public float cameraFOV;
/** Default position of the camera. */
public float cameraX, cameraY, cameraZ;
/** Distance of the near and far planes. */
public float cameraNear, cameraFar;
/** Aspect ratio of camera's view. */
public float cameraAspect;
/** Actual position of the camera. */
protected float cameraEyeX, cameraEyeY, cameraEyeZ;
/** Flag to indicate that we are inside beginCamera/endCamera block. */
protected boolean manipulatingCamera;
// ........................................................
// All the matrices required for camera and geometry transformations.
public PMatrix3D projection;
public PMatrix3D camera;
public PMatrix3D cameraInv;
public PMatrix3D modelview;
public PMatrix3D modelviewInv;
public PMatrix3D projmodelview;
// To pass to shaders
protected float[] glProjection;
protected float[] glModelview;
protected float[] glProjmodelview;
protected float[] glNormal;
// Useful to have around.
static protected PMatrix3D identity = new PMatrix3D();
protected boolean matricesAllocated = false;
/**
* Marks when changes to the size have occurred, so that the camera
* will be reset in beginDraw().
*/
protected boolean sized;
static protected final int MATRIX_STACK_DEPTH = 32;
protected int modelviewStackDepth;
protected int projectionStackDepth;
/** Modelview matrix stack **/
protected float[][] modelviewStack = new float[MATRIX_STACK_DEPTH][16];
/** Inverse modelview matrix stack **/
protected float[][] modelviewInvStack = new float[MATRIX_STACK_DEPTH][16];
/** Camera matrix stack **/
protected float[][] cameraStack = new float[MATRIX_STACK_DEPTH][16];
/** Inverse camera matrix stack **/
protected float[][] cameraInvStack = new float[MATRIX_STACK_DEPTH][16];
/** Projection matrix stack **/
protected float[][] projectionStack = new float[MATRIX_STACK_DEPTH][16];
// ........................................................
// Lights:
public boolean lights;
public int lightCount = 0;
/** Light types */
public int[] lightType;
/** Light positions */
public float[] lightPosition;
/** Light direction (normalized vector) */
public float[] lightNormal;
/**
* Ambient colors for lights.
*/
public float[] lightAmbient;
/**
* Diffuse colors for lights.
*/
public float[] lightDiffuse;
/**
* Specular colors for lights. Internally these are stored as numbers between
* 0 and 1.
*/
public float[] lightSpecular;
/** Light falloff */
public float[] lightFalloffCoefficients;
/** Light spot parameters: Cosine of light spot angle
* and concentration */
public float[] lightSpotParameters;
/** Current specular color for lighting */
public float[] currentLightSpecular;
/** Current light falloff */
public float currentLightFalloffConstant;
public float currentLightFalloffLinear;
public float currentLightFalloffQuadratic;
protected boolean lightsAllocated = false;
// ........................................................
// Texturing:
protected int textureWrap = CLAMP;
protected int textureSampling = Texture.TRILINEAR;
// ........................................................
// Clipping
protected boolean clip = false;
/** Clipping rectangle. */
protected int[] clipRect = {0, 0, 0, 0};
// ........................................................
// Text:
/** Font texture of currently selected font. */
FontTexture textTex;
// .......................................................
// Framebuffer stack:
static protected final int FB_STACK_DEPTH = 16;
protected int fbStackDepth;
protected FrameBuffer[] fbStack;
protected FrameBuffer drawFramebuffer;
protected FrameBuffer readFramebuffer;
protected FrameBuffer currentFramebuffer;
// .......................................................
// Offscreen rendering:
protected FrameBuffer offscreenFramebuffer;
protected FrameBuffer multisampleFramebuffer;
protected boolean offscreenMultisample;
protected boolean pixOpChangedFB;
// ........................................................
// Screen surface:
/** Texture containing the current frame */
protected Texture texture;
/** Texture containing the previous frame */
protected Texture ptexture;
/** IntBuffer wrapping the pixels array. */
protected IntBuffer pixelBuffer;
/** Array to store pixels in OpenGL format. */
protected int[] nativePixels;
/** IntBuffer wrapping the native pixels array. */
protected IntBuffer nativePixelBuffer;
/** texture used to apply a filter on the screen image. */
protected Texture filterTexture;
/** PImage that wraps filterTexture. */
protected PImage filterImage;
/** Flag to indicate that pixels array is up-to-date and
* ready to be manipulated through the set()/get() methods */
protected boolean arePixelsUpToDate;
// ........................................................
// Utility variables:
/** True if we are inside a beginDraw()/endDraw() block. */
protected boolean drawing = false;
/** Used to indicate an OpenGL surface recreation */
protected boolean restoreSurface = false;
/** Used to detect continuous use of the smooth/noSmooth functions */
protected boolean smoothDisabled = false;
protected int smoothCallCount = 0;
protected int lastSmoothCall = -10;
/** Used to avoid flushing the geometry when blendMode() is called with the
* same blend mode as the last */
protected int lastBlendMode = -1;
/** Type of pixels operation. */
static protected final int OP_NONE = 0;
static protected final int OP_READ = 1;
static protected final int OP_WRITE = 2;
protected int pixelsOp = OP_NONE;
/** Viewport dimensions. */
protected IntBuffer viewport;
/** Used to register calls to glClear. */
protected boolean clearColorBuffer;
protected boolean clearColorBuffer0;
protected boolean openContour = false;
protected boolean breakShape = false;
protected boolean defaultEdges = false;
static protected final int EDGE_MIDDLE = 0;
static protected final int EDGE_START = 1;
static protected final int EDGE_STOP = 2;
static protected final int EDGE_SINGLE = 3;
static protected final int EDGE_CLOSE = -1;
/** Used in round point and ellipse tessellation. The
* number of subdivisions per round point or ellipse is
* calculated with the following formula:
* n = min(M, max(N, (TWO_PI * size / F)))
* where size is a measure of the dimensions of the circle
* when projected on screen coordinates. F just sets the
* minimum number of subdivisions, while a smaller F
* would allow to have more detailed circles.
* N = MIN_POINT_ACCURACY
* M = MAX_POINT_ACCURACY
* F = POINT_ACCURACY_FACTOR
*/
final static protected int MIN_POINT_ACCURACY = 20;
final static protected int MAX_POINT_ACCURACY = 200;
final static protected float POINT_ACCURACY_FACTOR = 10.0f;
/** Used in quad point tessellation. */
final static protected float[][] QUAD_POINT_SIGNS =
{ {-1, +1}, {-1, -1}, {+1, -1}, {+1, +1} };
/** To get data from OpenGL. */
static protected IntBuffer intBuffer;
static protected FloatBuffer floatBuffer;
// ........................................................
// Error strings:
static final String OPENGL_THREAD_ERROR =
"Cannot run the OpenGL renderer outside the main thread, change your code" +
"\nso the drawing calls are all inside the main thread, " +
"\nor use the default renderer instead.";
static final String BLEND_DRIVER_ERROR =
"blendMode(%1$s) is not supported by this hardware (or driver)";
static final String BLEND_RENDERER_ERROR =
"blendMode(%1$s) is not supported by this renderer";
static final String ALREADY_BEGAN_CONTOUR_ERROR =
"Already called beginContour()";
static final String NO_BEGIN_CONTOUR_ERROR =
"Need to call beginContour() first";
static final String UNSUPPORTED_SMOOTH_LEVEL_ERROR =
"Smooth level %1$s is not available. Using %2$s instead";
static final String UNSUPPORTED_SMOOTH_ERROR =
"Smooth is not supported by this hardware (or driver)";
static final String TOO_MANY_SMOOTH_CALLS_ERROR =
"The smooth/noSmooth functions are being called too often.\n" +
"This results in screen flickering, so they will be disabled\n" +
"for the rest of the sketch's execution";
static final String UNSUPPORTED_SHAPE_FORMAT_ERROR =
"Unsupported shape format";
static final String MISSING_UV_TEXCOORDS_ERROR =
"No uv texture coordinates supplied with vertex() call";
static final String INVALID_FILTER_SHADER_ERROR =
"Your shader cannot be used as a filter because is of type POINT or LINES";
static final String INCONSISTENT_SHADER_TYPES =
"The vertex and fragment shaders have different types";
static final String WRONG_SHADER_TYPE_ERROR =
"shader() called with a wrong shader";
static final String SHADER_NEED_LIGHT_ATTRIBS =
"The provided shader needs light attributes (ambient, diffuse, etc.), but " +
"the current scene is unlit, so the default shader will be used instead";
static final String MISSING_FRAGMENT_SHADER =
"The fragment shader is missing, cannot create shader object";
static final String MISSING_VERTEX_SHADER =
"The vertex shader is missing, cannot create shader object";
static final String UNKNOWN_SHADER_KIND_ERROR =
"Unknown shader kind";
static final String NO_TEXLIGHT_SHADER_ERROR =
"Your shader needs to be of TEXLIGHT type " +
"to render this geometry properly, using default shader instead.";
static final String NO_LIGHT_SHADER_ERROR =
"Your shader needs to be of LIGHT type " +
"to render this geometry properly, using default shader instead.";
static final String NO_TEXTURE_SHADER_ERROR =
"Your shader needs to be of TEXTURE type " +
"to render this geometry properly, using default shader instead.";
static final String NO_COLOR_SHADER_ERROR =
"Your shader needs to be of COLOR type " +
"to render this geometry properly, using default shader instead.";
static final String TOO_LONG_STROKE_PATH_ERROR =
"Stroke path is too long, some bevel triangles won't be added";
static final String TESSELLATION_ERROR =
"Tessellation Error: %1$s";
//////////////////////////////////////////////////////////////
// INIT/ALLOCATE/FINISH
public PGraphicsOpenGL() {
pgl = createPGL(this);
if (tessellator == null) {
tessellator = new Tessellator();
}
if (intBuffer == null) {
intBuffer = PGL.allocateIntBuffer(2);
floatBuffer = PGL.allocateFloatBuffer(2);
}
viewport = PGL.allocateIntBuffer(4);
inGeo = newInGeometry(this, IMMEDIATE);
tessGeo = newTessGeometry(this, IMMEDIATE);
texCache = newTexCache(this);
initialized = false;
}
@Override
public void setPrimary(boolean primary) {
super.setPrimary(primary);
pgl.setPrimary(primary);
format = ARGB;
if (primary) {
fbStack = new FrameBuffer[FB_STACK_DEPTH];
fontMap = new WeakHashMap<PFont, FontTexture>();
}
}
//public void setPath(String path) // PGraphics
//public void setAntiAlias(int samples) // PGraphics
@Override
public void setFrameRate(float frameRate) {
pgl.setFps(frameRate);
}
@Override
public void setSize(int iwidth, int iheight) {
width = iwidth;
height = iheight;
allocate();
// init perspective projection based on new dimensions
cameraFOV = 60 * DEG_TO_RAD; // at least for now
cameraX = width / 2.0f;
cameraY = height / 2.0f;
cameraZ = cameraY / ((float) Math.tan(cameraFOV / 2.0f));
cameraNear = cameraZ / 10.0f;
cameraFar = cameraZ * 10.0f;
cameraAspect = (float) width / (float) height;
sized = true;
}
/**
* Called by resize(), this handles creating the actual GLCanvas the
* first time around, or simply resizing it on subsequent calls.
* There is no pixel array to allocate for an OpenGL canvas
* because OpenGL's pixel buffer is all handled internally.
*/
@Override
protected void allocate() {
super.allocate();
if (!matricesAllocated) {
projection = new PMatrix3D();
camera = new PMatrix3D();
cameraInv = new PMatrix3D();
modelview = new PMatrix3D();
modelviewInv = new PMatrix3D();
projmodelview = new PMatrix3D();
matricesAllocated = true;
}
if (!lightsAllocated) {
lightType = new int[PGL.MAX_LIGHTS];
lightPosition = new float[4 * PGL.MAX_LIGHTS];
lightNormal = new float[3 * PGL.MAX_LIGHTS];
lightAmbient = new float[3 * PGL.MAX_LIGHTS];
lightDiffuse = new float[3 * PGL.MAX_LIGHTS];
lightSpecular = new float[3 * PGL.MAX_LIGHTS];
lightFalloffCoefficients = new float[3 * PGL.MAX_LIGHTS];
lightSpotParameters = new float[2 * PGL.MAX_LIGHTS];
currentLightSpecular = new float[3];
lightsAllocated = true;
}
}
@Override
public void dispose() { // PGraphics
super.dispose();
if (primarySurface) {
// Swap buffers the end to make sure that no
// garbage is shown on the screen, this particularly
// affects non-interactive sketches on windows that
// render only 1 frame, so no enough rendering
// iterations have been conducted so far to properly
// initialize all the buffers.
pgl.swapBuffers();
}
finalizePolyBuffers();
finalizeLineBuffers();
finalizePointBuffers();
deleteSurfaceTextures();
if (primarySurface) {
deleteDefaultShaders();
} else {
if (offscreenFramebuffer != null) {
offscreenFramebuffer.dispose();
}
if (multisampleFramebuffer != null) {
multisampleFramebuffer.dispose();
}
}
deleteFinalizedGLResources(pgl);
if (primarySurface) {
pgl.deleteSurface();
}
}
@Override
protected void finalize() throws Throwable {
try {
finalizePolyBuffers();
finalizeLineBuffers();
finalizePointBuffers();
deleteSurfaceTextures();
if (!primarySurface) {
if (offscreenFramebuffer != null) {
offscreenFramebuffer.dispose();
offscreenFramebuffer = null;
}
if (multisampleFramebuffer != null) {
multisampleFramebuffer.dispose();
multisampleFramebuffer = null;
}
}
} finally {
super.finalize();
}
}
protected void setFlushMode(int mode) {
flushMode = mode;
}
//////////////////////////////////////////////////////////////
// IMAGE METADATA FOR THIS RENDERER
@Override
public void setCache(PImage image, Object storage) {
getPrimaryPG().cacheMap.put(image, storage);
}
@Override
public Object getCache(PImage image) {
return getPrimaryPG().cacheMap.get(image);
}
@Override
public void removeCache(PImage image) {
getPrimaryPG().cacheMap.remove(image);
}
//////////////////////////////////////////////////////////////
protected void setFontTexture(PFont font, FontTexture fontTexture) {
getPrimaryPG().fontMap.put(font, fontTexture);
}
protected FontTexture getFontTexture(PFont font) {
return getPrimaryPG().fontMap.get(font);
}
protected void removeFontTexture(PFont font) {
getPrimaryPG().fontMap.remove(font);
}
//////////////////////////////////////////////////////////////
// RESOURCE HANDLING
protected static class GLResource {
int id;
int context;
GLResource(int id, int context) {
this.id = id;
this.context = context;
}
@Override
public boolean equals(Object obj) {
GLResource other = (GLResource)obj;
return other.id == id && other.context == context;
}
@Override
public int hashCode() {
int result = 17;
result = 31 * result + id;
result = 31 * result + context;
return result;
}
}
// Texture Objects -----------------------------------------------------------
protected static int createTextureObject(int context, PGL pgl) {
deleteFinalizedTextureObjects(pgl);
pgl.genTextures(1, intBuffer);
int id = intBuffer.get(0);
GLResource res = new GLResource(id, context);
if (!glTextureObjects.containsKey(res)) {
glTextureObjects.put(res, false);
}
return id;
}
protected static void deleteTextureObject(int id, int context, PGL pgl) {
GLResource res = new GLResource(id, context);
if (glTextureObjects.containsKey(res)) {
intBuffer.put(0, id);
if (pgl.threadIsCurrent()) pgl.deleteTextures(1, intBuffer);
glTextureObjects.remove(res);
}
}
protected static void deleteAllTextureObjects(PGL pgl) {
for (GLResource res : glTextureObjects.keySet()) {
intBuffer.put(0, res.id);
if (pgl.threadIsCurrent()) pgl.deleteTextures(1, intBuffer);
}
glTextureObjects.clear();
}
// This is synchronized because it is called from the GC thread.
synchronized protected static void finalizeTextureObject(int id, int context) {
GLResource res = new GLResource(id, context);
if (glTextureObjects.containsKey(res)) {
glTextureObjects.put(res, true);
}
}
protected static void deleteFinalizedTextureObjects(PGL pgl) {
Set<GLResource> finalized = new HashSet<GLResource>();
for (GLResource res : glTextureObjects.keySet()) {
if (glTextureObjects.get(res)) {
finalized.add(res);
intBuffer.put(0, res.id);
if (pgl.threadIsCurrent()) pgl.deleteTextures(1, intBuffer);
}
}
for (GLResource res : finalized) {
glTextureObjects.remove(res);
}
}
protected static void removeTextureObject(int id, int context) {
GLResource res = new GLResource(id, context);
if (glTextureObjects.containsKey(res)) {
glTextureObjects.remove(res);
}
}
// Vertex Buffer Objects -----------------------------------------------------
protected static int createVertexBufferObject(int context, PGL pgl) {
deleteFinalizedVertexBufferObjects(pgl);
pgl.genBuffers(1, intBuffer);
int id = intBuffer.get(0);
GLResource res = new GLResource(id, context);
if (!glVertexBuffers.containsKey(res)) {
glVertexBuffers.put(res, false);
}
return id;
}
protected static void deleteVertexBufferObject(int id, int context, PGL pgl) {
GLResource res = new GLResource(id, context);
if (glVertexBuffers.containsKey(res)) {
intBuffer.put(0, id);
if (pgl.threadIsCurrent()) pgl.deleteBuffers(1, intBuffer);
glVertexBuffers.remove(res);
}
}
protected static void deleteAllVertexBufferObjects(PGL pgl) {
for (GLResource res : glVertexBuffers.keySet()) {
intBuffer.put(0, res.id);
if (pgl.threadIsCurrent()) pgl.deleteBuffers(1, intBuffer);
}
glVertexBuffers.clear();
}
// This is synchronized because it is called from the GC thread.
synchronized static protected void finalizeVertexBufferObject(int id, int context) {
GLResource res = new GLResource(id, context);
if (glVertexBuffers.containsKey(res)) {
glVertexBuffers.put(res, true);
}
}
protected static void deleteFinalizedVertexBufferObjects(PGL pgl) {
Set<GLResource> finalized = new HashSet<GLResource>();
for (GLResource res : glVertexBuffers.keySet()) {
if (glVertexBuffers.get(res)) {
finalized.add(res);
intBuffer.put(0, res.id);
if (pgl.threadIsCurrent()) pgl.deleteBuffers(1, intBuffer);
}
}
for (GLResource res : finalized) {
glVertexBuffers.remove(res);
}
}
protected static void removeVertexBufferObject(int id, int context) {
GLResource res = new GLResource(id, context);
if (glVertexBuffers.containsKey(res)) {
glVertexBuffers.remove(res);
}
}
// FrameBuffer Objects -------------------------------------------------------
protected static int createFrameBufferObject(int context, PGL pgl) {
deleteFinalizedFrameBufferObjects(pgl);
pgl.genFramebuffers(1, intBuffer);
int id = intBuffer.get(0);
GLResource res = new GLResource(id, context);
if (!glFrameBuffers.containsKey(res)) {
glFrameBuffers.put(res, false);
}
return id;
}
protected static void deleteFrameBufferObject(int id, int context, PGL pgl) {
GLResource res = new GLResource(id, context);
if (glFrameBuffers.containsKey(res)) {
intBuffer.put(0, id);
if (pgl.threadIsCurrent()) pgl.deleteFramebuffers(1, intBuffer);
glFrameBuffers.remove(res);
}
}
protected static void deleteAllFrameBufferObjects(PGL pgl) {
for (GLResource res : glFrameBuffers.keySet()) {
intBuffer.put(0, res.id);
if (pgl.threadIsCurrent()) pgl.deleteFramebuffers(1, intBuffer);
}
glFrameBuffers.clear();
}
// This is synchronized because it is called from the GC thread.
synchronized static protected void finalizeFrameBufferObject(int id, int context) {
GLResource res = new GLResource(id, context);
if (glFrameBuffers.containsKey(res)) {
glFrameBuffers.put(res, true);
}
}
protected static void deleteFinalizedFrameBufferObjects(PGL pgl) {
Set<GLResource> finalized = new HashSet<GLResource>();
for (GLResource res : glFrameBuffers.keySet()) {
if (glFrameBuffers.get(res)) {
finalized.add(res);
intBuffer.put(0, res.id);
if (pgl.threadIsCurrent()) {
pgl.deleteFramebuffers(1, intBuffer);
}
}
}
for (GLResource res : finalized) {
glFrameBuffers.remove(res);
}
}
protected static void removeFrameBufferObject(int id, int context) {
GLResource res = new GLResource(id, context);
if (glFrameBuffers.containsKey(res)) {
glFrameBuffers.remove(res);
}
}
// RenderBuffer Objects ------------------------------------------------------
protected static int createRenderBufferObject(int context, PGL pgl) {
deleteFinalizedRenderBufferObjects(pgl);
pgl.genRenderbuffers(1, intBuffer);
int id = intBuffer.get(0);
GLResource res = new GLResource(id, context);
if (!glRenderBuffers.containsKey(res)) {
glRenderBuffers.put(res, false);
}
return id;
}
protected static void deleteRenderBufferObject(int id, int context, PGL pgl) {
GLResource res = new GLResource(id, context);
if (glRenderBuffers.containsKey(res)) {
intBuffer.put(0, id);
if (pgl.threadIsCurrent()) pgl.deleteRenderbuffers(1, intBuffer);
glRenderBuffers.remove(res);
}
}
protected static void deleteAllRenderBufferObjects(PGL pgl) {
for (GLResource res : glRenderBuffers.keySet()) {
intBuffer.put(0, res.id);
if (pgl.threadIsCurrent()) pgl.deleteRenderbuffers(1, intBuffer);
}
glRenderBuffers.clear();
}
// This is synchronized because it is called from the GC thread.
synchronized static protected void finalizeRenderBufferObject(int id, int context) {
GLResource res = new GLResource(id, context);
if (glRenderBuffers.containsKey(res)) {
glRenderBuffers.put(res, true);
}
}
protected static void deleteFinalizedRenderBufferObjects(PGL pgl) {
Set<GLResource> finalized = new HashSet<GLResource>();
for (GLResource res : glRenderBuffers.keySet()) {
if (glRenderBuffers.get(res)) {
finalized.add(res);
intBuffer.put(0, res.id);