propane 3.1.0.pre-java → 3.2.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/wrapper/maven-wrapper.properties +1 -0
  3. data/CHANGELOG.md +1 -5
  4. data/README.md +23 -12
  5. data/Rakefile +23 -12
  6. data/lib/propane/helpers/version_error.rb +6 -0
  7. data/lib/propane/runner.rb +12 -0
  8. data/lib/propane/version.rb +1 -1
  9. data/library/slider/slider.rb +1 -1
  10. data/mvnw +234 -0
  11. data/mvnw.cmd +145 -0
  12. data/pom.xml +28 -27
  13. data/propane.gemspec +2 -2
  14. data/src/main/java/japplemenubar/JAppleMenuBar.java +41 -47
  15. data/src/main/java/monkstone/ColorUtil.java +1 -1
  16. data/src/main/java/monkstone/MathToolModule.java +12 -11
  17. data/src/main/java/monkstone/PropaneLibrary.java +9 -10
  18. data/src/main/java/monkstone/core/LibraryProxy.java +124 -113
  19. data/src/main/java/monkstone/fastmath/Deglut.java +86 -89
  20. data/src/main/java/monkstone/filechooser/Chooser.java +7 -13
  21. data/src/main/java/monkstone/noise/SimplexNoise.java +0 -1
  22. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +4 -4
  23. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  24. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +9 -9
  25. data/src/main/java/monkstone/slider/SimpleSlider.java +0 -9
  26. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +11 -13
  27. data/src/main/java/monkstone/slider/Slider.java +1 -1
  28. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  29. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  30. data/src/main/java/monkstone/slider/WheelHandler.java +8 -9
  31. data/src/main/java/monkstone/vecmath/AppRender.java +2 -2
  32. data/src/main/java/monkstone/vecmath/ShapeRender.java +2 -2
  33. data/src/main/java/monkstone/vecmath/package-info.java +2 -2
  34. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +2 -2
  35. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -1
  36. data/src/main/java/monkstone/videoevent/VideoInterface.java +11 -5
  37. data/src/main/java/monkstone/videoevent/package-info.java +2 -2
  38. data/src/main/java/processing/awt/PGraphicsJava2D.java +1742 -2243
  39. data/src/main/java/processing/awt/PShapeJava2D.java +268 -270
  40. data/src/main/java/processing/awt/PSurfaceAWT.java +821 -920
  41. data/src/main/java/processing/core/DesktopHandler.java +94 -0
  42. data/src/main/java/processing/core/PApplet.java +14170 -14082
  43. data/src/main/java/processing/core/PConstants.java +447 -473
  44. data/src/main/java/processing/core/PFont.java +867 -873
  45. data/src/main/java/processing/core/PGraphics.java +7193 -7428
  46. data/src/main/java/processing/core/PImage.java +3051 -3117
  47. data/src/main/java/processing/core/PMatrix.java +159 -172
  48. data/src/main/java/processing/core/PMatrix2D.java +403 -444
  49. data/src/main/java/processing/core/PMatrix3D.java +735 -749
  50. data/src/main/java/processing/core/PShape.java +2651 -2793
  51. data/src/main/java/processing/core/PShapeOBJ.java +415 -422
  52. data/src/main/java/processing/core/PShapeSVG.java +1466 -1475
  53. data/src/main/java/processing/core/PStyle.java +37 -40
  54. data/src/main/java/processing/core/PSurface.java +98 -103
  55. data/src/main/java/processing/core/PSurfaceNone.java +208 -236
  56. data/src/main/java/processing/core/PVector.java +961 -990
  57. data/src/main/java/processing/data/DoubleDict.java +709 -753
  58. data/src/main/java/processing/data/DoubleList.java +695 -748
  59. data/src/main/java/processing/data/FloatDict.java +702 -746
  60. data/src/main/java/processing/data/FloatList.java +697 -751
  61. data/src/main/java/processing/data/IntDict.java +673 -718
  62. data/src/main/java/processing/data/IntList.java +633 -699
  63. data/src/main/java/processing/data/JSONArray.java +873 -931
  64. data/src/main/java/processing/data/JSONObject.java +1165 -1262
  65. data/src/main/java/processing/data/JSONTokener.java +341 -351
  66. data/src/main/java/processing/data/LongDict.java +662 -707
  67. data/src/main/java/processing/data/LongList.java +634 -700
  68. data/src/main/java/processing/data/Sort.java +41 -37
  69. data/src/main/java/processing/data/StringDict.java +486 -522
  70. data/src/main/java/processing/data/StringList.java +580 -624
  71. data/src/main/java/processing/data/Table.java +3508 -3686
  72. data/src/main/java/processing/data/TableRow.java +183 -182
  73. data/src/main/java/processing/data/XML.java +883 -957
  74. data/src/main/java/processing/event/Event.java +66 -87
  75. data/src/main/java/processing/event/KeyEvent.java +41 -48
  76. data/src/main/java/processing/event/MouseEvent.java +93 -103
  77. data/src/main/java/processing/event/TouchEvent.java +6 -10
  78. data/src/main/java/processing/javafx/PGraphicsFX2D.java +5 -69
  79. data/src/main/java/processing/javafx/PSurfaceFX.java +2 -7
  80. data/src/main/java/processing/opengl/FontTexture.java +270 -290
  81. data/src/main/java/processing/opengl/FrameBuffer.java +363 -375
  82. data/src/main/java/processing/opengl/LinePath.java +500 -543
  83. data/src/main/java/processing/opengl/LineStroker.java +582 -593
  84. data/src/main/java/processing/opengl/PGL.java +2881 -2904
  85. data/src/main/java/processing/opengl/PGraphics2D.java +315 -408
  86. data/src/main/java/processing/opengl/PGraphics3D.java +72 -107
  87. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +12043 -12230
  88. data/src/main/java/processing/opengl/PJOGL.java +1681 -1745
  89. data/src/main/java/processing/opengl/PShader.java +1257 -1260
  90. data/src/main/java/processing/opengl/PShapeOpenGL.java +4599 -4662
  91. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1030 -1047
  92. data/src/main/java/processing/opengl/Texture.java +1397 -1462
  93. data/src/main/java/processing/opengl/VertexBuffer.java +55 -57
  94. data/src/main/resources/icon/icon-1024.png +0 -0
  95. data/src/main/resources/icon/icon-128.png +0 -0
  96. data/src/main/resources/icon/icon-16.png +0 -0
  97. data/src/main/resources/icon/icon-256.png +0 -0
  98. data/src/main/resources/icon/icon-32.png +0 -0
  99. data/src/main/resources/icon/icon-48.png +0 -0
  100. data/src/main/resources/icon/icon-512.png +0 -0
  101. data/src/main/resources/icon/icon-64.png +0 -0
  102. data/vendors/Rakefile +1 -1
  103. metadata +12 -8
  104. data/src/main/java/processing/core/ThinkDifferent.java +0 -70
@@ -1,5 +1,3 @@
1
- /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
-
3
1
  /*
4
2
  Part of the Processing project - http://processing.org
5
3
 
@@ -20,8 +18,7 @@
20
18
  Public License along with this library; if not, write to the
21
19
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22
20
  Boston, MA 02111-1307 USA
23
- */
24
-
21
+ */
25
22
  package processing.opengl;
26
23
 
27
24
  import processing.core.*;
@@ -33,1446 +30,1446 @@ import java.nio.IntBuffer;
33
30
  import java.util.HashMap;
34
31
 
35
32
  /**
36
- * This class encapsulates a GLSL shader program, including a vertex
37
- * and a fragment shader. Based on the GLSLShader class from GLGraphics, which
38
- * in turn was originally based in the code by JohnG:
33
+ * This class encapsulates a GLSL shader program, including a vertex and a
34
+ * fragment shader. Based on the GLSLShader class from GLGraphics, which in turn
35
+ * was originally based in the code by JohnG:
39
36
  * http://processing.org/discourse/beta/num_1159494801.html
40
37
  *
41
38
  * @webref rendering:shaders
42
39
  */
43
40
  public class PShader implements PConstants {
44
- static protected final int POINT = 0;
45
- static protected final int LINE = 1;
46
- static protected final int POLY = 2;
47
- static protected final int COLOR = 3;
48
- static protected final int LIGHT = 4;
49
- static protected final int TEXTURE = 5;
50
- static protected final int TEXLIGHT = 6;
51
-
52
- static protected String pointShaderAttrRegexp =
53
- "attribute *vec2 *offset";
54
- static protected String pointShaderInRegexp =
55
- "in *vec2 *offset;";
56
- static protected String lineShaderAttrRegexp =
57
- "attribute *vec4 *direction";
58
- static protected String lineShaderInRegexp =
59
- "in *vec4 *direction";
60
- static protected String pointShaderDefRegexp =
61
- "#define *PROCESSING_POINT_SHADER";
62
- static protected String lineShaderDefRegexp =
63
- "#define *PROCESSING_LINE_SHADER";
64
- static protected String colorShaderDefRegexp =
65
- "#define *PROCESSING_COLOR_SHADER";
66
- static protected String lightShaderDefRegexp =
67
- "#define *PROCESSING_LIGHT_SHADER";
68
- static protected String texShaderDefRegexp =
69
- "#define *PROCESSING_TEXTURE_SHADER";
70
- static protected String texlightShaderDefRegexp =
71
- "#define *PROCESSING_TEXLIGHT_SHADER";
72
- static protected String polyShaderDefRegexp =
73
- "#define *PROCESSING_POLYGON_SHADER";
74
- static protected String triShaderAttrRegexp =
75
- "#define *PROCESSING_TRIANGLES_SHADER";
76
- static protected String quadShaderAttrRegexp =
77
- "#define *PROCESSING_QUADS_SHADER";
78
-
79
- protected PApplet parent;
80
- // The main renderer associated to the parent PApplet.
81
- //protected PGraphicsOpenGL pgMain;
82
- // We need a reference to the renderer since a shader might
83
- // be called by different renderers within a single application
84
- // (the one corresponding to the main surface, or other offscreen
85
- // renderers).
86
- protected PGraphicsOpenGL primaryPG;
87
- protected PGraphicsOpenGL currentPG;
88
- protected PGL pgl;
89
- protected int context; // The context that created this shader.
90
-
91
- // The shader type: POINT, LINE, POLY, etc.
92
- protected int type;
93
-
94
- public int glProgram;
95
- public int glVertex;
96
- public int glFragment;
97
- private GLResourceShader glres;
98
-
99
- protected URL vertexURL;
100
- protected URL fragmentURL;
101
-
102
- protected String vertexFilename;
103
- protected String fragmentFilename;
104
-
105
- protected String[] vertexShaderSource;
106
- protected String[] fragmentShaderSource;
107
-
108
- protected boolean bound;
109
-
110
- protected HashMap<String, UniformValue> uniformValues = null;
111
-
112
- protected HashMap<Integer, Texture> textures;
113
- protected HashMap<Integer, Integer> texUnits;
114
-
115
- // Direct buffers to pass shader data to GL
116
- protected IntBuffer intBuffer;
117
- protected FloatBuffer floatBuffer;
118
-
119
- protected boolean loadedAttributes = false;
120
- protected boolean loadedUniforms = false;
121
-
122
- // Uniforms common to all shader types
123
- protected int transformMatLoc;
124
- protected int modelviewMatLoc;
125
- protected int projectionMatLoc;
126
- protected int ppixelsLoc;
127
- protected int ppixelsUnit;
128
- protected int viewportLoc;
129
- protected int resolutionLoc;
130
-
131
- // Uniforms only for lines and points
132
- protected int perspectiveLoc;
133
- protected int scaleLoc;
134
-
135
- // Lighting uniforms
136
- protected int lightCountLoc;
137
- protected int lightPositionLoc;
138
- protected int lightNormalLoc;
139
- protected int lightAmbientLoc;
140
- protected int lightDiffuseLoc;
141
- protected int lightSpecularLoc;
142
- protected int lightFalloffLoc;
143
- protected int lightSpotLoc;
144
-
145
- // Texturing uniforms
146
- protected Texture texture;
147
- protected int texUnit;
148
- protected int textureLoc;
149
- protected int texMatrixLoc;
150
- protected int texOffsetLoc;
151
- protected float[] tcmat;
152
-
153
- // Vertex attributes
154
- protected int vertexLoc;
155
- protected int colorLoc;
156
- protected int normalLoc;
157
- protected int texCoordLoc;
158
- protected int normalMatLoc;
159
- protected int directionLoc;
160
- protected int offsetLoc;
161
- protected int ambientLoc;
162
- protected int specularLoc;
163
- protected int emissiveLoc;
164
- protected int shininessLoc;
165
-
166
- public PShader() {
167
- parent = null;
168
- pgl = null;
169
- context = -1;
170
-
171
- this.vertexURL = null;
172
- this.fragmentURL = null;
173
- this.vertexFilename = null;
174
- this.fragmentFilename = null;
175
-
176
- glProgram = 0;
177
- glVertex = 0;
178
- glFragment = 0;
179
-
180
- intBuffer = PGL.allocateIntBuffer(1);
181
- floatBuffer = PGL.allocateFloatBuffer(1);
182
-
183
- bound = false;
184
-
185
- type = -1;
186
- }
187
-
188
-
189
- public PShader(PApplet parent) {
190
- this();
191
- this.parent = parent;
192
- primaryPG = (PGraphicsOpenGL)parent.g;
193
- pgl = primaryPG.pgl;
194
- context = pgl.createEmptyContext();
195
- }
196
-
197
-
198
- /**
199
- * Creates a shader program using the specified vertex and fragment
200
- * shaders.
201
- *
202
- * @param parent the parent program
203
- * @param vertFilename name of the vertex shader
204
- * @param fragFilename name of the fragment shader
205
- */
206
- public PShader(PApplet parent, String vertFilename, String fragFilename) {
207
- this.parent = parent;
208
- primaryPG = (PGraphicsOpenGL)parent.g;
209
- pgl = primaryPG.pgl;
210
-
211
- this.vertexURL = null;
212
- this.fragmentURL = null;
213
- this.vertexFilename = vertFilename;
214
- this.fragmentFilename = fragFilename;
215
- fragmentShaderSource = pgl.loadFragmentShader(fragFilename);
216
- vertexShaderSource = pgl.loadVertexShader(vertFilename);
217
-
218
- glProgram = 0;
219
- glVertex = 0;
220
- glFragment = 0;
221
-
222
- intBuffer = PGL.allocateIntBuffer(1);
223
- floatBuffer = PGL.allocateFloatBuffer(1);
224
-
225
- int vertType = getShaderType(vertexShaderSource, -1);
226
- int fragType = getShaderType(fragmentShaderSource, -1);
227
- if (vertType == -1 && fragType == -1) {
228
- type = PShader.POLY;
229
- } else if (vertType == -1) {
230
- type = fragType;
231
- } else if (fragType == -1) {
232
- type = vertType;
233
- } else if (fragType == vertType) {
234
- type = vertType;
235
- } else {
236
- PGraphics.showWarning(PGraphicsOpenGL.INCONSISTENT_SHADER_TYPES);
237
- }
238
- }
239
-
240
-
241
- /**
242
- * @param vertURL network location of the vertex shader
243
- * @param fragURL network location of the fragment shader
244
- */
245
- public PShader(PApplet parent, URL vertURL, URL fragURL) {
246
- this.parent = parent;
247
- primaryPG = (PGraphicsOpenGL)parent.g;
248
- pgl = primaryPG.pgl;
249
-
250
- this.vertexURL = vertURL;
251
- this.fragmentURL = fragURL;
252
- this.vertexFilename = null;
253
- this.fragmentFilename = null;
254
- fragmentShaderSource = pgl.loadFragmentShader(fragURL);
255
- vertexShaderSource = pgl.loadVertexShader(vertURL);
256
-
257
- glProgram = 0;
258
- glVertex = 0;
259
- glFragment = 0;
260
-
261
- intBuffer = PGL.allocateIntBuffer(1);
262
- floatBuffer = PGL.allocateFloatBuffer(1);
263
-
264
- int vertType = getShaderType(vertexShaderSource, -1);
265
- int fragType = getShaderType(fragmentShaderSource, -1);
266
- if (vertType == -1 && fragType == -1) {
267
- type = PShader.POLY;
268
- } else if (vertType == -1) {
269
- type = fragType;
270
- } else if (fragType == -1) {
271
- type = vertType;
272
- } else if (fragType == vertType) {
273
- type = vertType;
274
- } else {
275
- PGraphics.showWarning(PGraphicsOpenGL.INCONSISTENT_SHADER_TYPES);
276
- }
277
- }
278
-
279
- public PShader(PApplet parent, String[] vertSource, String[] fragSource) {
280
- this.parent = parent;
281
- primaryPG = (PGraphicsOpenGL)parent.g;
282
- pgl = primaryPG.pgl;
283
-
284
- this.vertexURL = null;
285
- this.fragmentURL = null;
286
- this.vertexFilename = null;
287
- this.fragmentFilename = null;
288
- vertexShaderSource = vertSource;
289
- fragmentShaderSource = fragSource;
290
-
291
- glProgram = 0;
292
- glVertex = 0;
293
- glFragment = 0;
294
-
295
- intBuffer = PGL.allocateIntBuffer(1);
296
- floatBuffer = PGL.allocateFloatBuffer(1);
297
-
298
- int vertType = getShaderType(vertexShaderSource, -1);
299
- int fragType = getShaderType(fragmentShaderSource, -1);
300
- if (vertType == -1 && fragType == -1) {
301
- type = PShader.POLY;
302
- } else if (vertType == -1) {
303
- type = fragType;
304
- } else if (fragType == -1) {
305
- type = vertType;
306
- } else if (fragType == vertType) {
307
- type = vertType;
308
- } else {
309
- PGraphics.showWarning(PGraphicsOpenGL.INCONSISTENT_SHADER_TYPES);
310
- }
311
- }
312
-
313
-
314
- public void setVertexShader(String vertFilename) {
315
- this.vertexFilename = vertFilename;
316
- vertexShaderSource = pgl.loadVertexShader(vertFilename);
317
- }
318
-
319
-
320
- public void setVertexShader(URL vertURL) {
321
- this.vertexURL = vertURL;
322
- vertexShaderSource = pgl.loadVertexShader(vertURL);
323
- }
324
-
325
-
326
- public void setVertexShader(String[] vertSource) {
327
- vertexShaderSource = vertSource;
328
- }
329
-
330
-
331
- public void setFragmentShader(String fragFilename) {
332
- this.fragmentFilename = fragFilename;
333
- fragmentShaderSource = pgl.loadFragmentShader(fragFilename);
334
- }
335
-
336
-
337
- public void setFragmentShader(URL fragURL) {
338
- this.fragmentURL = fragURL;
339
- fragmentShaderSource = pgl.loadFragmentShader(fragURL);
340
- }
341
-
342
- public void setFragmentShader(String[] fragSource) {
343
- fragmentShaderSource = fragSource;
344
- }
345
-
346
-
347
- /**
348
- * Initializes (if needed) and binds the shader program.
349
- */
350
- public void bind() {
351
- init();
352
- if (!bound) {
353
- pgl.useProgram(glProgram);
354
- bound = true;
355
- consumeUniforms();
356
- bindTextures();
357
- }
358
-
359
- if (hasType()) bindTyped();
360
- }
361
41
 
42
+ static protected final int POINT = 0;
43
+ static protected final int LINE = 1;
44
+ static protected final int POLY = 2;
45
+ static protected final int COLOR = 3;
46
+ static protected final int LIGHT = 4;
47
+ static protected final int TEXTURE = 5;
48
+ static protected final int TEXLIGHT = 6;
49
+
50
+ static protected String pointShaderAttrRegexp
51
+ = "attribute *vec2 *offset";
52
+ static protected String pointShaderInRegexp
53
+ = "in *vec2 *offset;";
54
+ static protected String lineShaderAttrRegexp
55
+ = "attribute *vec4 *direction";
56
+ static protected String lineShaderInRegexp
57
+ = "in *vec4 *direction";
58
+ static protected String pointShaderDefRegexp
59
+ = "#define *PROCESSING_POINT_SHADER";
60
+ static protected String lineShaderDefRegexp
61
+ = "#define *PROCESSING_LINE_SHADER";
62
+ static protected String colorShaderDefRegexp
63
+ = "#define *PROCESSING_COLOR_SHADER";
64
+ static protected String lightShaderDefRegexp
65
+ = "#define *PROCESSING_LIGHT_SHADER";
66
+ static protected String texShaderDefRegexp
67
+ = "#define *PROCESSING_TEXTURE_SHADER";
68
+ static protected String texlightShaderDefRegexp
69
+ = "#define *PROCESSING_TEXLIGHT_SHADER";
70
+ static protected String polyShaderDefRegexp
71
+ = "#define *PROCESSING_POLYGON_SHADER";
72
+ static protected String triShaderAttrRegexp
73
+ = "#define *PROCESSING_TRIANGLES_SHADER";
74
+ static protected String quadShaderAttrRegexp
75
+ = "#define *PROCESSING_QUADS_SHADER";
76
+
77
+ protected PApplet parent;
78
+ // The main renderer associated to the parent PApplet.
79
+ //protected PGraphicsOpenGL pgMain;
80
+ // We need a reference to the renderer since a shader might
81
+ // be called by different renderers within a single application
82
+ // (the one corresponding to the main surface, or other offscreen
83
+ // renderers).
84
+ protected PGraphicsOpenGL primaryPG;
85
+ protected PGraphicsOpenGL currentPG;
86
+ protected PGL pgl;
87
+ protected int context; // The context that created this shader.
88
+
89
+ // The shader type: POINT, LINE, POLY, etc.
90
+ protected int type;
91
+
92
+ public int glProgram;
93
+ public int glVertex;
94
+ public int glFragment;
95
+ private GLResourceShader glres;
96
+
97
+ protected URL vertexURL;
98
+ protected URL fragmentURL;
99
+
100
+ protected String vertexFilename;
101
+ protected String fragmentFilename;
102
+
103
+ protected String[] vertexShaderSource;
104
+ protected String[] fragmentShaderSource;
105
+
106
+ protected boolean bound;
107
+
108
+ protected HashMap<String, UniformValue> uniformValues = null;
109
+
110
+ protected HashMap<Integer, Texture> textures;
111
+ protected HashMap<Integer, Integer> texUnits;
112
+
113
+ // Direct buffers to pass shader data to GL
114
+ protected IntBuffer intBuffer;
115
+ protected FloatBuffer floatBuffer;
116
+
117
+ protected boolean loadedAttributes = false;
118
+ protected boolean loadedUniforms = false;
119
+
120
+ // Uniforms common to all shader types
121
+ protected int transformMatLoc;
122
+ protected int modelviewMatLoc;
123
+ protected int projectionMatLoc;
124
+ protected int ppixelsLoc;
125
+ protected int ppixelsUnit;
126
+ protected int viewportLoc;
127
+ protected int resolutionLoc;
128
+
129
+ // Uniforms only for lines and points
130
+ protected int perspectiveLoc;
131
+ protected int scaleLoc;
132
+
133
+ // Lighting uniforms
134
+ protected int lightCountLoc;
135
+ protected int lightPositionLoc;
136
+ protected int lightNormalLoc;
137
+ protected int lightAmbientLoc;
138
+ protected int lightDiffuseLoc;
139
+ protected int lightSpecularLoc;
140
+ protected int lightFalloffLoc;
141
+ protected int lightSpotLoc;
142
+
143
+ // Texturing uniforms
144
+ protected Texture texture;
145
+ protected int texUnit;
146
+ protected int textureLoc;
147
+ protected int texMatrixLoc;
148
+ protected int texOffsetLoc;
149
+ protected float[] tcmat;
150
+
151
+ // Vertex attributes
152
+ protected int vertexLoc;
153
+ protected int colorLoc;
154
+ protected int normalLoc;
155
+ protected int texCoordLoc;
156
+ protected int normalMatLoc;
157
+ protected int directionLoc;
158
+ protected int offsetLoc;
159
+ protected int ambientLoc;
160
+ protected int specularLoc;
161
+ protected int emissiveLoc;
162
+ protected int shininessLoc;
163
+
164
+ public PShader() {
165
+ parent = null;
166
+ pgl = null;
167
+ context = -1;
168
+
169
+ this.vertexURL = null;
170
+ this.fragmentURL = null;
171
+ this.vertexFilename = null;
172
+ this.fragmentFilename = null;
173
+
174
+ glProgram = 0;
175
+ glVertex = 0;
176
+ glFragment = 0;
177
+
178
+ intBuffer = PGL.allocateIntBuffer(1);
179
+ floatBuffer = PGL.allocateFloatBuffer(1);
180
+
181
+ bound = false;
182
+
183
+ type = -1;
184
+ }
362
185
 
363
- /**
364
- * Unbinds the shader program.
365
- */
366
- public void unbind() {
367
- if (hasType()) unbindTyped();
368
-
369
- if (bound) {
370
- unbindTextures();
371
- pgl.useProgram(0);
372
- bound = false;
186
+ public PShader(PApplet parent) {
187
+ this();
188
+ this.parent = parent;
189
+ primaryPG = (PGraphicsOpenGL) parent.g;
190
+ pgl = primaryPG.pgl;
191
+ context = pgl.createEmptyContext();
373
192
  }
374
- }
375
193
 
194
+ /**
195
+ * Creates a shader program using the specified vertex and fragment shaders.
196
+ *
197
+ * @param parent the parent program
198
+ * @param vertFilename name of the vertex shader
199
+ * @param fragFilename name of the fragment shader
200
+ */
201
+ public PShader(PApplet parent, String vertFilename, String fragFilename) {
202
+ this.parent = parent;
203
+ primaryPG = (PGraphicsOpenGL) parent.g;
204
+ pgl = primaryPG.pgl;
205
+
206
+ this.vertexURL = null;
207
+ this.fragmentURL = null;
208
+ this.vertexFilename = vertFilename;
209
+ this.fragmentFilename = fragFilename;
210
+ fragmentShaderSource = pgl.loadFragmentShader(fragFilename);
211
+ vertexShaderSource = pgl.loadVertexShader(vertFilename);
212
+
213
+ glProgram = 0;
214
+ glVertex = 0;
215
+ glFragment = 0;
216
+
217
+ intBuffer = PGL.allocateIntBuffer(1);
218
+ floatBuffer = PGL.allocateFloatBuffer(1);
219
+
220
+ int vertType = getShaderType(vertexShaderSource, -1);
221
+ int fragType = getShaderType(fragmentShaderSource, -1);
222
+ if (vertType == -1 && fragType == -1) {
223
+ type = PShader.POLY;
224
+ } else if (vertType == -1) {
225
+ type = fragType;
226
+ } else if (fragType == -1) {
227
+ type = vertType;
228
+ } else if (fragType == vertType) {
229
+ type = vertType;
230
+ } else {
231
+ PGraphics.showWarning(PGraphicsOpenGL.INCONSISTENT_SHADER_TYPES);
232
+ }
233
+ }
376
234
 
377
- /**
378
- * Returns true if the shader is bound, false otherwise.
379
- */
380
- public boolean bound() {
381
- return bound;
382
- }
235
+ /**
236
+ * @param vertURL network location of the vertex shader
237
+ * @param fragURL network location of the fragment shader
238
+ */
239
+ public PShader(PApplet parent, URL vertURL, URL fragURL) {
240
+ this.parent = parent;
241
+ primaryPG = (PGraphicsOpenGL) parent.g;
242
+ pgl = primaryPG.pgl;
243
+
244
+ this.vertexURL = vertURL;
245
+ this.fragmentURL = fragURL;
246
+ this.vertexFilename = null;
247
+ this.fragmentFilename = null;
248
+ fragmentShaderSource = pgl.loadFragmentShader(fragURL);
249
+ vertexShaderSource = pgl.loadVertexShader(vertURL);
250
+
251
+ glProgram = 0;
252
+ glVertex = 0;
253
+ glFragment = 0;
254
+
255
+ intBuffer = PGL.allocateIntBuffer(1);
256
+ floatBuffer = PGL.allocateFloatBuffer(1);
257
+
258
+ int vertType = getShaderType(vertexShaderSource, -1);
259
+ int fragType = getShaderType(fragmentShaderSource, -1);
260
+ if (vertType == -1 && fragType == -1) {
261
+ type = PShader.POLY;
262
+ } else if (vertType == -1) {
263
+ type = fragType;
264
+ } else if (fragType == -1) {
265
+ type = vertType;
266
+ } else if (fragType == vertType) {
267
+ type = vertType;
268
+ } else {
269
+ PGraphics.showWarning(PGraphicsOpenGL.INCONSISTENT_SHADER_TYPES);
270
+ }
271
+ }
383
272
 
384
- /**
385
- * @webref rendering:shaders
386
- * @brief Sets a variable within the shader
387
- * @param name the name of the uniform variable to modify
388
- * @param x first component of the variable to modify
389
- */
390
- public void set(String name, int x) {
391
- setUniformImpl(name, UniformValue.INT1, new int[] { x });
392
- }
273
+ public PShader(PApplet parent, String[] vertSource, String[] fragSource) {
274
+ this.parent = parent;
275
+ primaryPG = (PGraphicsOpenGL) parent.g;
276
+ pgl = primaryPG.pgl;
277
+
278
+ this.vertexURL = null;
279
+ this.fragmentURL = null;
280
+ this.vertexFilename = null;
281
+ this.fragmentFilename = null;
282
+ vertexShaderSource = vertSource;
283
+ fragmentShaderSource = fragSource;
284
+
285
+ glProgram = 0;
286
+ glVertex = 0;
287
+ glFragment = 0;
288
+
289
+ intBuffer = PGL.allocateIntBuffer(1);
290
+ floatBuffer = PGL.allocateFloatBuffer(1);
291
+
292
+ int vertType = getShaderType(vertexShaderSource, -1);
293
+ int fragType = getShaderType(fragmentShaderSource, -1);
294
+ if (vertType == -1 && fragType == -1) {
295
+ type = PShader.POLY;
296
+ } else if (vertType == -1) {
297
+ type = fragType;
298
+ } else if (fragType == -1) {
299
+ type = vertType;
300
+ } else if (fragType == vertType) {
301
+ type = vertType;
302
+ } else {
303
+ PGraphics.showWarning(PGraphicsOpenGL.INCONSISTENT_SHADER_TYPES);
304
+ }
305
+ }
393
306
 
394
- /**
395
- * @param y second component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[2], vec2)
396
- */
397
- public void set(String name, int x, int y) {
398
- setUniformImpl(name, UniformValue.INT2, new int[] { x, y });
399
- }
307
+ public void setVertexShader(String vertFilename) {
308
+ this.vertexFilename = vertFilename;
309
+ vertexShaderSource = pgl.loadVertexShader(vertFilename);
310
+ }
400
311
 
401
- /**
402
- * @param z third component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[3], vec3)
403
- */
404
- public void set(String name, int x, int y, int z) {
405
- setUniformImpl(name, UniformValue.INT3, new int[] { x, y, z });
406
- }
312
+ public void setVertexShader(URL vertURL) {
313
+ this.vertexURL = vertURL;
314
+ vertexShaderSource = pgl.loadVertexShader(vertURL);
315
+ }
407
316
 
408
- /**
409
- * @param w fourth component of the variable to modify. The variable has to be declared with an array/vector type in the shader (i.e.: int[4], vec4)
410
- */
411
- public void set(String name, int x, int y, int z, int w) {
412
- setUniformImpl(name, UniformValue.INT4, new int[] { x, y, z, w });
413
- }
317
+ public void setVertexShader(String[] vertSource) {
318
+ vertexShaderSource = vertSource;
319
+ }
414
320
 
321
+ public void setFragmentShader(String fragFilename) {
322
+ this.fragmentFilename = fragFilename;
323
+ fragmentShaderSource = pgl.loadFragmentShader(fragFilename);
324
+ }
415
325
 
416
- public void set(String name, float x) {
417
- setUniformImpl(name, UniformValue.FLOAT1, new float[] { x });
418
- }
326
+ public void setFragmentShader(URL fragURL) {
327
+ this.fragmentURL = fragURL;
328
+ fragmentShaderSource = pgl.loadFragmentShader(fragURL);
329
+ }
419
330
 
331
+ public void setFragmentShader(String[] fragSource) {
332
+ fragmentShaderSource = fragSource;
333
+ }
420
334
 
421
- public void set(String name, float x, float y) {
422
- setUniformImpl(name, UniformValue.FLOAT2, new float[] { x, y });
423
- }
335
+ /**
336
+ * Initializes (if needed) and binds the shader program.
337
+ */
338
+ public void bind() {
339
+ init();
340
+ if (!bound) {
341
+ pgl.useProgram(glProgram);
342
+ bound = true;
343
+ consumeUniforms();
344
+ bindTextures();
345
+ }
424
346
 
347
+ if (hasType()) {
348
+ bindTyped();
349
+ }
350
+ }
425
351
 
426
- public void set(String name, float x, float y, float z) {
427
- setUniformImpl(name, UniformValue.FLOAT3, new float[] { x, y, z });
428
- }
352
+ /**
353
+ * Unbinds the shader program.
354
+ */
355
+ public void unbind() {
356
+ if (hasType()) {
357
+ unbindTyped();
358
+ }
429
359
 
360
+ if (bound) {
361
+ unbindTextures();
362
+ pgl.useProgram(0);
363
+ bound = false;
364
+ }
365
+ }
430
366
 
431
- public void set(String name, float x, float y, float z, float w) {
432
- setUniformImpl(name, UniformValue.FLOAT4, new float[] { x, y, z, w });
433
- }
367
+ /**
368
+ * Returns true if the shader is bound, false otherwise.
369
+ */
370
+ public boolean bound() {
371
+ return bound;
372
+ }
434
373
 
435
- /**
436
- * @param vec modifies all the components of an array/vector uniform variable. PVector can only be used if the type of the variable is vec3.
437
- */
438
- public void set(String name, PVector vec) {
439
- setUniformImpl(name, UniformValue.FLOAT3,
440
- new float[] { vec.x, vec.y, vec.z });
441
- }
374
+ /**
375
+ * @webref rendering:shaders
376
+ * @brief Sets a variable within the shader
377
+ * @param name the name of the uniform variable to modify
378
+ * @param x first component of the variable to modify
379
+ */
380
+ public void set(String name, int x) {
381
+ setUniformImpl(name, UniformValue.INT1, new int[]{x});
382
+ }
442
383
 
384
+ /**
385
+ * @param y second component of the variable to modify. The variable has to
386
+ * be declared with an array/vector type in the shader (i.e.: int[2], vec2)
387
+ */
388
+ public void set(String name, int x, int y) {
389
+ setUniformImpl(name, UniformValue.INT2, new int[]{x, y});
390
+ }
443
391
 
444
- public void set(String name, boolean x) {
445
- setUniformImpl(name, UniformValue.INT1, new int[] { (x)?1:0 });
446
- }
447
-
448
-
449
- public void set(String name, boolean x, boolean y) {
450
- setUniformImpl(name, UniformValue.INT2,
451
- new int[] { (x)?1:0, (y)?1:0 });
452
- }
453
-
454
-
455
- public void set(String name, boolean x, boolean y, boolean z) {
456
- setUniformImpl(name, UniformValue.INT3,
457
- new int[] { (x)?1:0, (y)?1:0, (z)?1:0 });
458
- }
459
-
460
-
461
- public void set(String name, boolean x, boolean y, boolean z, boolean w) {
462
- setUniformImpl(name, UniformValue.INT4,
463
- new int[] { (x)?1:0, (y)?1:0, (z)?1:0, (w)?1:0 });
464
- }
465
-
466
-
467
- public void set(String name, int[] vec) {
468
- set(name, vec, 1);
469
- }
470
-
471
-
472
- /**
473
- * @param ncoords number of coordinates per element, max 4
474
- */
475
- public void set(String name, int[] vec, int ncoords) {
476
- if (ncoords == 1) {
477
- setUniformImpl(name, UniformValue.INT1VEC, vec);
478
- } else if (ncoords == 2) {
479
- setUniformImpl(name, UniformValue.INT2VEC, vec);
480
- } else if (ncoords == 3) {
481
- setUniformImpl(name, UniformValue.INT3VEC, vec);
482
- } else if (ncoords == 4) {
483
- setUniformImpl(name, UniformValue.INT4VEC, vec);
484
- } else if (4 < ncoords) {
485
- PGraphics.showWarning("Only up to 4 coordinates per element are " +
486
- "supported.");
487
- } else {
488
- PGraphics.showWarning("Wrong number of coordinates: it is negative!");
489
- }
490
- }
491
-
492
-
493
- public void set(String name, float[] vec) {
494
- set(name, vec, 1);
495
- }
496
-
497
-
498
- public void set(String name, float[] vec, int ncoords) {
499
- if (ncoords == 1) {
500
- setUniformImpl(name, UniformValue.FLOAT1VEC, vec);
501
- } else if (ncoords == 2) {
502
- setUniformImpl(name, UniformValue.FLOAT2VEC, vec);
503
- } else if (ncoords == 3) {
504
- setUniformImpl(name, UniformValue.FLOAT3VEC, vec);
505
- } else if (ncoords == 4) {
506
- setUniformImpl(name, UniformValue.FLOAT4VEC, vec);
507
- } else if (4 < ncoords) {
508
- PGraphics.showWarning("Only up to 4 coordinates per element are " +
509
- "supported.");
510
- } else {
511
- PGraphics.showWarning("Wrong number of coordinates: it is negative!");
512
- }
513
- }
514
-
515
-
516
- public void set(String name, boolean[] vec) {
517
- set(name, vec, 1);
518
- }
519
-
520
-
521
- public void set(String name, boolean[] boolvec, int ncoords) {
522
- int[] vec = new int[boolvec.length];
523
- for (int i = 0; i < boolvec.length; i++) {
524
- vec[i] = (boolvec[i])?1:0;
525
- }
526
- set(name, vec, ncoords);
527
- }
528
-
529
-
530
- /**
531
- * @param mat matrix of values
532
- */
533
- public void set(String name, PMatrix2D mat) {
534
- float[] matv = { mat.m00, mat.m01,
535
- mat.m10, mat.m11 };
536
- setUniformImpl(name, UniformValue.MAT2, matv);
537
- }
392
+ /**
393
+ * @param z third component of the variable to modify. The variable has to
394
+ * be declared with an array/vector type in the shader (i.e.: int[3], vec3)
395
+ */
396
+ public void set(String name, int x, int y, int z) {
397
+ setUniformImpl(name, UniformValue.INT3, new int[]{x, y, z});
398
+ }
538
399
 
400
+ /**
401
+ * @param w fourth component of the variable to modify. The variable has to
402
+ * be declared with an array/vector type in the shader (i.e.: int[4], vec4)
403
+ */
404
+ public void set(String name, int x, int y, int z, int w) {
405
+ setUniformImpl(name, UniformValue.INT4, new int[]{x, y, z, w});
406
+ }
539
407
 
540
- public void set(String name, PMatrix3D mat) {
541
- set(name, mat, false);
542
- }
408
+ public void set(String name, float x) {
409
+ setUniformImpl(name, UniformValue.FLOAT1, new float[]{x});
410
+ }
543
411
 
544
- /**
545
- * @param use3x3 enforces the matrix is 3 x 3
546
- */
547
- public void set(String name, PMatrix3D mat, boolean use3x3) {
548
- if (use3x3) {
549
- float[] matv = { mat.m00, mat.m01, mat.m02,
550
- mat.m10, mat.m11, mat.m12,
551
- mat.m20, mat.m21, mat.m22 };
552
- setUniformImpl(name, UniformValue.MAT3, matv);
553
- } else {
554
- float[] matv = { mat.m00, mat.m01, mat.m02, mat.m03,
555
- mat.m10, mat.m11, mat.m12, mat.m13,
556
- mat.m20, mat.m21, mat.m22, mat.m23,
557
- mat.m30, mat.m31, mat.m32, mat.m33 };
558
- setUniformImpl(name, UniformValue.MAT4, matv);
412
+ public void set(String name, float x, float y) {
413
+ setUniformImpl(name, UniformValue.FLOAT2, new float[]{x, y});
559
414
  }
560
- }
561
415
 
562
- /**
563
- * @param tex sets the sampler uniform variable to read from this image texture
564
- */
565
- public void set(String name, PImage tex) {
566
- setUniformImpl(name, UniformValue.SAMPLER2D, tex);
567
- }
416
+ public void set(String name, float x, float y, float z) {
417
+ setUniformImpl(name, UniformValue.FLOAT3, new float[]{x, y, z});
418
+ }
568
419
 
420
+ public void set(String name, float x, float y, float z, float w) {
421
+ setUniformImpl(name, UniformValue.FLOAT4, new float[]{x, y, z, w});
422
+ }
569
423
 
570
- /**
571
- * Extra initialization method that can be used by subclasses, called after
572
- * compiling and attaching the vertex and fragment shaders, and before
573
- * linking the shader program.
574
- *
575
- */
576
- protected void setup() {
577
- }
424
+ /**
425
+ * @param vec modifies all the components of an array/vector uniform
426
+ * variable. PVector can only be used if the type of the variable is vec3.
427
+ */
428
+ public void set(String name, PVector vec) {
429
+ setUniformImpl(name, UniformValue.FLOAT3,
430
+ new float[]{vec.x, vec.y, vec.z});
431
+ }
578
432
 
433
+ public void set(String name, boolean x) {
434
+ setUniformImpl(name, UniformValue.INT1, new int[]{(x) ? 1 : 0});
435
+ }
579
436
 
580
- protected void draw(int idxId, int count, int offset) {
581
- pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, idxId);
582
- pgl.drawElements(PGL.TRIANGLES, count, PGL.INDEX_TYPE,
583
- offset * PGL.SIZEOF_INDEX);
584
- pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0);
585
- }
437
+ public void set(String name, boolean x, boolean y) {
438
+ setUniformImpl(name, UniformValue.INT2,
439
+ new int[]{(x) ? 1 : 0, (y) ? 1 : 0});
440
+ }
586
441
 
442
+ public void set(String name, boolean x, boolean y, boolean z) {
443
+ setUniformImpl(name, UniformValue.INT3,
444
+ new int[]{(x) ? 1 : 0, (y) ? 1 : 0, (z) ? 1 : 0});
445
+ }
587
446
 
588
- /**
589
- * Returns the ID location of the attribute parameter given its name.
590
- *
591
- * @param name String
592
- * @return int
593
- */
594
- protected int getAttributeLoc(String name) {
595
- init();
596
- return pgl.getAttribLocation(glProgram, name);
597
- }
447
+ public void set(String name, boolean x, boolean y, boolean z, boolean w) {
448
+ setUniformImpl(name, UniformValue.INT4,
449
+ new int[]{(x) ? 1 : 0, (y) ? 1 : 0, (z) ? 1 : 0, (w) ? 1 : 0});
450
+ }
598
451
 
452
+ public void set(String name, int[] vec) {
453
+ set(name, vec, 1);
454
+ }
599
455
 
600
- /**
601
- * Returns the ID location of the uniform parameter given its name.
602
- *
603
- * @param name String
604
- * @return int
605
- */
606
- protected int getUniformLoc(String name) {
607
- init();
608
- return pgl.getUniformLocation(glProgram, name);
609
- }
456
+ /**
457
+ * @param ncoords number of coordinates per element, max 4
458
+ */
459
+ public void set(String name, int[] vec, int ncoords) {
460
+ if (ncoords == 1) {
461
+ setUniformImpl(name, UniformValue.INT1VEC, vec);
462
+ } else if (ncoords == 2) {
463
+ setUniformImpl(name, UniformValue.INT2VEC, vec);
464
+ } else if (ncoords == 3) {
465
+ setUniformImpl(name, UniformValue.INT3VEC, vec);
466
+ } else if (ncoords == 4) {
467
+ setUniformImpl(name, UniformValue.INT4VEC, vec);
468
+ } else if (4 < ncoords) {
469
+ PGraphics.showWarning("Only up to 4 coordinates per element are "
470
+ + "supported.");
471
+ } else {
472
+ PGraphics.showWarning("Wrong number of coordinates: it is negative!");
473
+ }
474
+ }
610
475
 
476
+ public void set(String name, float[] vec) {
477
+ set(name, vec, 1);
478
+ }
611
479
 
612
- protected void setAttributeVBO(int loc, int vboId, int size, int type,
613
- boolean normalized, int stride, int offset) {
614
- if (-1 < loc) {
615
- pgl.bindBuffer(PGL.ARRAY_BUFFER, vboId);
616
- pgl.vertexAttribPointer(loc, size, type, normalized, stride, offset);
480
+ public void set(String name, float[] vec, int ncoords) {
481
+ if (ncoords == 1) {
482
+ setUniformImpl(name, UniformValue.FLOAT1VEC, vec);
483
+ } else if (ncoords == 2) {
484
+ setUniformImpl(name, UniformValue.FLOAT2VEC, vec);
485
+ } else if (ncoords == 3) {
486
+ setUniformImpl(name, UniformValue.FLOAT3VEC, vec);
487
+ } else if (ncoords == 4) {
488
+ setUniformImpl(name, UniformValue.FLOAT4VEC, vec);
489
+ } else if (4 < ncoords) {
490
+ PGraphics.showWarning("Only up to 4 coordinates per element are "
491
+ + "supported.");
492
+ } else {
493
+ PGraphics.showWarning("Wrong number of coordinates: it is negative!");
494
+ }
617
495
  }
618
- }
619
496
 
497
+ public void set(String name, boolean[] vec) {
498
+ set(name, vec, 1);
499
+ }
620
500
 
621
- protected void setUniformValue(int loc, int x) {
622
- if (-1 < loc) {
623
- pgl.uniform1i(loc, x);
501
+ public void set(String name, boolean[] boolvec, int ncoords) {
502
+ int[] vec = new int[boolvec.length];
503
+ for (int i = 0; i < boolvec.length; i++) {
504
+ vec[i] = (boolvec[i]) ? 1 : 0;
505
+ }
506
+ set(name, vec, ncoords);
624
507
  }
625
- }
626
508
 
509
+ /**
510
+ * @param mat matrix of values
511
+ */
512
+ public void set(String name, PMatrix2D mat) {
513
+ float[] matv = {mat.m00, mat.m01,
514
+ mat.m10, mat.m11};
515
+ setUniformImpl(name, UniformValue.MAT2, matv);
516
+ }
627
517
 
628
- protected void setUniformValue(int loc, int x, int y) {
629
- if (-1 < loc) {
630
- pgl.uniform2i(loc, x, y);
518
+ public void set(String name, PMatrix3D mat) {
519
+ set(name, mat, false);
631
520
  }
632
- }
633
521
 
522
+ /**
523
+ * @param use3x3 enforces the matrix is 3 x 3
524
+ */
525
+ public void set(String name, PMatrix3D mat, boolean use3x3) {
526
+ if (use3x3) {
527
+ float[] matv = {mat.m00, mat.m01, mat.m02,
528
+ mat.m10, mat.m11, mat.m12,
529
+ mat.m20, mat.m21, mat.m22};
530
+ setUniformImpl(name, UniformValue.MAT3, matv);
531
+ } else {
532
+ float[] matv = {mat.m00, mat.m01, mat.m02, mat.m03,
533
+ mat.m10, mat.m11, mat.m12, mat.m13,
534
+ mat.m20, mat.m21, mat.m22, mat.m23,
535
+ mat.m30, mat.m31, mat.m32, mat.m33};
536
+ setUniformImpl(name, UniformValue.MAT4, matv);
537
+ }
538
+ }
634
539
 
635
- protected void setUniformValue(int loc, int x, int y, int z) {
636
- if (-1 < loc) {
637
- pgl.uniform3i(loc, x, y, z);
540
+ /**
541
+ * @param tex sets the sampler uniform variable to read from this image
542
+ * texture
543
+ */
544
+ public void set(String name, PImage tex) {
545
+ setUniformImpl(name, UniformValue.SAMPLER2D, tex);
638
546
  }
639
- }
640
547
 
548
+ /**
549
+ * Extra initialization method that can be used by subclasses, called after
550
+ * compiling and attaching the vertex and fragment shaders, and before
551
+ * linking the shader program.
552
+ *
553
+ */
554
+ protected void setup() {
555
+ }
641
556
 
642
- protected void setUniformValue(int loc, int x, int y, int z, int w) {
643
- if (-1 < loc) {
644
- pgl.uniform4i(loc, x, y, z, w);
557
+ protected void draw(int idxId, int count, int offset) {
558
+ pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, idxId);
559
+ pgl.drawElements(PGL.TRIANGLES, count, PGL.INDEX_TYPE,
560
+ offset * PGL.SIZEOF_INDEX);
561
+ pgl.bindBuffer(PGL.ELEMENT_ARRAY_BUFFER, 0);
645
562
  }
646
- }
647
563
 
564
+ /**
565
+ * Returns the ID location of the attribute parameter given its name.
566
+ *
567
+ * @param name String
568
+ * @return int
569
+ */
570
+ protected int getAttributeLoc(String name) {
571
+ init();
572
+ return pgl.getAttribLocation(glProgram, name);
573
+ }
648
574
 
649
- protected void setUniformValue(int loc, float x) {
650
- if (-1 < loc) {
651
- pgl.uniform1f(loc, x);
575
+ /**
576
+ * Returns the ID location of the uniform parameter given its name.
577
+ *
578
+ * @param name String
579
+ * @return int
580
+ */
581
+ protected int getUniformLoc(String name) {
582
+ init();
583
+ return pgl.getUniformLocation(glProgram, name);
652
584
  }
653
- }
654
585
 
655
- protected void setUniformValue(int loc, float x, float y) {
656
- if (-1 < loc) {
657
- pgl.uniform2f(loc, x, y);
586
+ protected void setAttributeVBO(int loc, int vboId, int size, int type,
587
+ boolean normalized, int stride, int offset) {
588
+ if (-1 < loc) {
589
+ pgl.bindBuffer(PGL.ARRAY_BUFFER, vboId);
590
+ pgl.vertexAttribPointer(loc, size, type, normalized, stride, offset);
591
+ }
658
592
  }
659
- }
660
593
 
594
+ protected void setUniformValue(int loc, int x) {
595
+ if (-1 < loc) {
596
+ pgl.uniform1i(loc, x);
597
+ }
598
+ }
661
599
 
662
- protected void setUniformValue(int loc, float x, float y, float z) {
663
- if (-1 < loc) {
664
- pgl.uniform3f(loc, x, y, z);
600
+ protected void setUniformValue(int loc, int x, int y) {
601
+ if (-1 < loc) {
602
+ pgl.uniform2i(loc, x, y);
603
+ }
665
604
  }
666
- }
667
605
 
606
+ protected void setUniformValue(int loc, int x, int y, int z) {
607
+ if (-1 < loc) {
608
+ pgl.uniform3i(loc, x, y, z);
609
+ }
610
+ }
668
611
 
669
- protected void setUniformValue(int loc, float x, float y, float z, float w) {
670
- if (-1 < loc) {
671
- pgl.uniform4f(loc, x, y, z, w);
612
+ protected void setUniformValue(int loc, int x, int y, int z, int w) {
613
+ if (-1 < loc) {
614
+ pgl.uniform4i(loc, x, y, z, w);
615
+ }
672
616
  }
673
- }
674
617
 
618
+ protected void setUniformValue(int loc, float x) {
619
+ if (-1 < loc) {
620
+ pgl.uniform1f(loc, x);
621
+ }
622
+ }
675
623
 
676
- protected void setUniformVector(int loc, int[] vec, int ncoords,
677
- int length) {
678
- if (-1 < loc) {
679
- updateIntBuffer(vec);
680
- if (ncoords == 1) {
681
- pgl.uniform1iv(loc, length, intBuffer);
682
- } else if (ncoords == 2) {
683
- pgl.uniform2iv(loc, length, intBuffer);
684
- } else if (ncoords == 3) {
685
- pgl.uniform3iv(loc, length, intBuffer);
686
- } else if (ncoords == 4) {
687
- pgl.uniform3iv(loc, length, intBuffer);
688
- }
689
- }
690
- }
691
-
692
-
693
- protected void setUniformVector(int loc, float[] vec, int ncoords,
694
- int length) {
695
- if (-1 < loc) {
696
- updateFloatBuffer(vec);
697
- if (ncoords == 1) {
698
- pgl.uniform1fv(loc, length, floatBuffer);
699
- } else if (ncoords == 2) {
700
- pgl.uniform2fv(loc, length, floatBuffer);
701
- } else if (ncoords == 3) {
702
- pgl.uniform3fv(loc, length, floatBuffer);
703
- } else if (ncoords == 4) {
704
- pgl.uniform4fv(loc, length, floatBuffer);
705
- }
706
- }
707
- }
708
-
709
-
710
- protected void setUniformMatrix(int loc, float[] mat) {
711
- if (-1 < loc) {
712
- updateFloatBuffer(mat);
713
- if (mat.length == 4) {
714
- pgl.uniformMatrix2fv(loc, 1, false, floatBuffer);
715
- } else if (mat.length == 9) {
716
- pgl.uniformMatrix3fv(loc, 1, false, floatBuffer);
717
- } else if (mat.length == 16) {
718
- pgl.uniformMatrix4fv(loc, 1, false, floatBuffer);
719
- }
720
- }
721
- }
722
-
723
-
724
- protected void setUniformTex(int loc, Texture tex) {
725
- if (texUnits != null) {
726
- Integer unit = texUnits.get(loc);
727
- if (unit != null) {
728
- pgl.activeTexture(PGL.TEXTURE0 + unit);
729
- tex.bind();
730
- } else {
731
- throw new RuntimeException("Cannot find unit for texture " + tex);
732
- }
733
- }
734
- }
735
-
736
-
737
- protected void setUniformImpl(String name, int type, Object value) {
738
- if (uniformValues == null) {
739
- uniformValues = new HashMap<String, UniformValue>();
740
- }
741
- uniformValues.put(name, new UniformValue(type, value));
742
- }
743
-
744
-
745
- protected void consumeUniforms() {
746
- if (uniformValues != null && 0 < uniformValues.size()) {
747
- int unit = 0;
748
- for (String name: uniformValues.keySet()) {
749
- int loc = getUniformLoc(name);
750
- if (loc == -1) {
751
- PGraphics.showWarning("The shader doesn't have a uniform called \"" +
752
- name + "\" OR the uniform was removed during " +
753
- "compilation because it was unused.");
754
- continue;
755
- }
756
- UniformValue val = uniformValues.get(name);
757
- if (val.type == UniformValue.INT1) {
758
- int[] v = ((int[])val.value);
759
- pgl.uniform1i(loc, v[0]);
760
- } else if (val.type == UniformValue.INT2) {
761
- int[] v = ((int[])val.value);
762
- pgl.uniform2i(loc, v[0], v[1]);
763
- } else if (val.type == UniformValue.INT3) {
764
- int[] v = ((int[])val.value);
765
- pgl.uniform3i(loc, v[0], v[1], v[2]);
766
- } else if (val.type == UniformValue.INT4) {
767
- int[] v = ((int[])val.value);
768
- pgl.uniform4i(loc, v[0], v[1], v[2], v[3]);
769
- } else if (val.type == UniformValue.FLOAT1) {
770
- float[] v = ((float[])val.value);
771
- pgl.uniform1f(loc, v[0]);
772
- } else if (val.type == UniformValue.FLOAT2) {
773
- float[] v = ((float[])val.value);
774
- pgl.uniform2f(loc, v[0], v[1]);
775
- } else if (val.type == UniformValue.FLOAT3) {
776
- float[] v = ((float[])val.value);
777
- pgl.uniform3f(loc, v[0], v[1], v[2]);
778
- } else if (val.type == UniformValue.FLOAT4) {
779
- float[] v = ((float[])val.value);
780
- pgl.uniform4f(loc, v[0], v[1], v[2], v[3]);
781
- } else if (val.type == UniformValue.INT1VEC) {
782
- int[] v = ((int[])val.value);
783
- updateIntBuffer(v);
784
- pgl.uniform1iv(loc, v.length, intBuffer);
785
- } else if (val.type == UniformValue.INT2VEC) {
786
- int[] v = ((int[])val.value);
787
- updateIntBuffer(v);
788
- pgl.uniform2iv(loc, v.length / 2, intBuffer);
789
- } else if (val.type == UniformValue.INT3VEC) {
790
- int[] v = ((int[])val.value);
791
- updateIntBuffer(v);
792
- pgl.uniform3iv(loc, v.length / 3, intBuffer);
793
- } else if (val.type == UniformValue.INT4VEC) {
794
- int[] v = ((int[])val.value);
795
- updateIntBuffer(v);
796
- pgl.uniform4iv(loc, v.length / 4, intBuffer);
797
- } else if (val.type == UniformValue.FLOAT1VEC) {
798
- float[] v = ((float[])val.value);
799
- updateFloatBuffer(v);
800
- pgl.uniform1fv(loc, v.length, floatBuffer);
801
- } else if (val.type == UniformValue.FLOAT2VEC) {
802
- float[] v = ((float[])val.value);
803
- updateFloatBuffer(v);
804
- pgl.uniform2fv(loc, v.length / 2, floatBuffer);
805
- } else if (val.type == UniformValue.FLOAT3VEC) {
806
- float[] v = ((float[])val.value);
807
- updateFloatBuffer(v);
808
- pgl.uniform3fv(loc, v.length / 3, floatBuffer);
809
- } else if (val.type == UniformValue.FLOAT4VEC) {
810
- float[] v = ((float[])val.value);
811
- updateFloatBuffer(v);
812
- pgl.uniform4fv(loc, v.length / 4, floatBuffer);
813
- } else if (val.type == UniformValue.MAT2) {
814
- float[] v = ((float[])val.value);
815
- updateFloatBuffer(v);
816
- pgl.uniformMatrix2fv(loc, 1, false, floatBuffer);
817
- } else if (val.type == UniformValue.MAT3) {
818
- float[] v = ((float[])val.value);
819
- updateFloatBuffer(v);
820
- pgl.uniformMatrix3fv(loc, 1, false, floatBuffer);
821
- } else if (val.type == UniformValue.MAT4) {
822
- float[] v = ((float[])val.value);
823
- updateFloatBuffer(v);
824
- pgl.uniformMatrix4fv(loc, 1, false, floatBuffer);
825
- } else if (val.type == UniformValue.SAMPLER2D) {
826
- PImage img = (PImage)val.value;
827
- Texture tex = currentPG.getTexture(img);
828
-
829
- if (textures == null) textures = new HashMap<Integer, Texture>();
830
- textures.put(loc, tex);
831
-
832
- if (texUnits == null) texUnits = new HashMap<Integer, Integer>();
833
- if (texUnits.containsKey(loc)) {
834
- unit = texUnits.get(loc);
835
- pgl.uniform1i(loc, unit);
836
- } else {
837
- texUnits.put(loc, unit);
838
- pgl.uniform1i(loc, unit);
839
- }
840
- unit++;
841
- }
842
- }
843
- uniformValues.clear();
844
- }
845
- }
846
-
847
-
848
- protected void updateIntBuffer(int[] vec) {
849
- intBuffer = PGL.updateIntBuffer(intBuffer, vec, false);
850
- }
851
-
852
-
853
- protected void updateFloatBuffer(float[] vec) {
854
- floatBuffer = PGL.updateFloatBuffer(floatBuffer, vec, false);
855
- }
856
-
857
-
858
- protected void bindTextures() {
859
- if (textures != null && texUnits != null) {
860
- for (int loc: textures.keySet()) {
861
- Texture tex = textures.get(loc);
862
- Integer unit = texUnits.get(loc);
863
- if (unit != null) {
864
- pgl.activeTexture(PGL.TEXTURE0 + unit);
865
- tex.bind();
866
- } else {
867
- throw new RuntimeException("Cannot find unit for texture " + tex);
624
+ protected void setUniformValue(int loc, float x, float y) {
625
+ if (-1 < loc) {
626
+ pgl.uniform2f(loc, x, y);
868
627
  }
869
- }
870
628
  }
871
- }
872
629
 
630
+ protected void setUniformValue(int loc, float x, float y, float z) {
631
+ if (-1 < loc) {
632
+ pgl.uniform3f(loc, x, y, z);
633
+ }
634
+ }
873
635
 
874
- protected void unbindTextures() {
875
- if (textures != null && texUnits != null) {
876
- for (int loc: textures.keySet()) {
877
- Texture tex = textures.get(loc);
878
- Integer unit = texUnits.get(loc);
879
- if (unit != null) {
880
- pgl.activeTexture(PGL.TEXTURE0 + unit);
881
- tex.unbind();
882
- } else {
883
- throw new RuntimeException("Cannot find unit for texture " + tex);
636
+ protected void setUniformValue(int loc, float x, float y, float z, float w) {
637
+ if (-1 < loc) {
638
+ pgl.uniform4f(loc, x, y, z, w);
884
639
  }
885
- }
886
- pgl.activeTexture(PGL.TEXTURE0);
887
640
  }
888
- }
889
641
 
642
+ protected void setUniformVector(int loc, int[] vec, int ncoords,
643
+ int length) {
644
+ if (-1 < loc) {
645
+ updateIntBuffer(vec);
646
+ if (ncoords == 1) {
647
+ pgl.uniform1iv(loc, length, intBuffer);
648
+ } else if (ncoords == 2) {
649
+ pgl.uniform2iv(loc, length, intBuffer);
650
+ } else if (ncoords == 3) {
651
+ pgl.uniform3iv(loc, length, intBuffer);
652
+ } else if (ncoords == 4) {
653
+ pgl.uniform3iv(loc, length, intBuffer);
654
+ }
655
+ }
656
+ }
890
657
 
891
- public void init() {
892
- if (glProgram == 0 || contextIsOutdated()) {
893
- create();
894
- if (compile()) {
895
- pgl.attachShader(glProgram, glVertex);
896
- pgl.attachShader(glProgram, glFragment);
658
+ protected void setUniformVector(int loc, float[] vec, int ncoords,
659
+ int length) {
660
+ if (-1 < loc) {
661
+ updateFloatBuffer(vec);
662
+ if (ncoords == 1) {
663
+ pgl.uniform1fv(loc, length, floatBuffer);
664
+ } else if (ncoords == 2) {
665
+ pgl.uniform2fv(loc, length, floatBuffer);
666
+ } else if (ncoords == 3) {
667
+ pgl.uniform3fv(loc, length, floatBuffer);
668
+ } else if (ncoords == 4) {
669
+ pgl.uniform4fv(loc, length, floatBuffer);
670
+ }
671
+ }
672
+ }
897
673
 
898
- setup();
674
+ protected void setUniformMatrix(int loc, float[] mat) {
675
+ if (-1 < loc) {
676
+ updateFloatBuffer(mat);
677
+ if (mat.length == 4) {
678
+ pgl.uniformMatrix2fv(loc, 1, false, floatBuffer);
679
+ } else if (mat.length == 9) {
680
+ pgl.uniformMatrix3fv(loc, 1, false, floatBuffer);
681
+ } else if (mat.length == 16) {
682
+ pgl.uniformMatrix4fv(loc, 1, false, floatBuffer);
683
+ }
684
+ }
685
+ }
899
686
 
900
- pgl.linkProgram(glProgram);
687
+ protected void setUniformTex(int loc, Texture tex) {
688
+ if (texUnits != null) {
689
+ Integer unit = texUnits.get(loc);
690
+ if (unit != null) {
691
+ pgl.activeTexture(PGL.TEXTURE0 + unit);
692
+ tex.bind();
693
+ } else {
694
+ throw new RuntimeException("Cannot find unit for texture " + tex);
695
+ }
696
+ }
697
+ }
901
698
 
902
- validate();
903
- }
699
+ protected void setUniformImpl(String name, int type, Object value) {
700
+ if (uniformValues == null) {
701
+ uniformValues = new HashMap<String, UniformValue>();
702
+ }
703
+ uniformValues.put(name, new UniformValue(type, value));
904
704
  }
905
- }
906
705
 
706
+ protected void consumeUniforms() {
707
+ if (uniformValues != null && 0 < uniformValues.size()) {
708
+ int unit = 0;
709
+ for (String name : uniformValues.keySet()) {
710
+ int loc = getUniformLoc(name);
711
+ if (loc == -1) {
712
+ PGraphics.showWarning("The shader doesn't have a uniform called \""
713
+ + name + "\" OR the uniform was removed during "
714
+ + "compilation because it was unused.");
715
+ continue;
716
+ }
717
+ UniformValue val = uniformValues.get(name);
718
+ if (val.type == UniformValue.INT1) {
719
+ int[] v = ((int[]) val.value);
720
+ pgl.uniform1i(loc, v[0]);
721
+ } else if (val.type == UniformValue.INT2) {
722
+ int[] v = ((int[]) val.value);
723
+ pgl.uniform2i(loc, v[0], v[1]);
724
+ } else if (val.type == UniformValue.INT3) {
725
+ int[] v = ((int[]) val.value);
726
+ pgl.uniform3i(loc, v[0], v[1], v[2]);
727
+ } else if (val.type == UniformValue.INT4) {
728
+ int[] v = ((int[]) val.value);
729
+ pgl.uniform4i(loc, v[0], v[1], v[2], v[3]);
730
+ } else if (val.type == UniformValue.FLOAT1) {
731
+ float[] v = ((float[]) val.value);
732
+ pgl.uniform1f(loc, v[0]);
733
+ } else if (val.type == UniformValue.FLOAT2) {
734
+ float[] v = ((float[]) val.value);
735
+ pgl.uniform2f(loc, v[0], v[1]);
736
+ } else if (val.type == UniformValue.FLOAT3) {
737
+ float[] v = ((float[]) val.value);
738
+ pgl.uniform3f(loc, v[0], v[1], v[2]);
739
+ } else if (val.type == UniformValue.FLOAT4) {
740
+ float[] v = ((float[]) val.value);
741
+ pgl.uniform4f(loc, v[0], v[1], v[2], v[3]);
742
+ } else if (val.type == UniformValue.INT1VEC) {
743
+ int[] v = ((int[]) val.value);
744
+ updateIntBuffer(v);
745
+ pgl.uniform1iv(loc, v.length, intBuffer);
746
+ } else if (val.type == UniformValue.INT2VEC) {
747
+ int[] v = ((int[]) val.value);
748
+ updateIntBuffer(v);
749
+ pgl.uniform2iv(loc, v.length / 2, intBuffer);
750
+ } else if (val.type == UniformValue.INT3VEC) {
751
+ int[] v = ((int[]) val.value);
752
+ updateIntBuffer(v);
753
+ pgl.uniform3iv(loc, v.length / 3, intBuffer);
754
+ } else if (val.type == UniformValue.INT4VEC) {
755
+ int[] v = ((int[]) val.value);
756
+ updateIntBuffer(v);
757
+ pgl.uniform4iv(loc, v.length / 4, intBuffer);
758
+ } else if (val.type == UniformValue.FLOAT1VEC) {
759
+ float[] v = ((float[]) val.value);
760
+ updateFloatBuffer(v);
761
+ pgl.uniform1fv(loc, v.length, floatBuffer);
762
+ } else if (val.type == UniformValue.FLOAT2VEC) {
763
+ float[] v = ((float[]) val.value);
764
+ updateFloatBuffer(v);
765
+ pgl.uniform2fv(loc, v.length / 2, floatBuffer);
766
+ } else if (val.type == UniformValue.FLOAT3VEC) {
767
+ float[] v = ((float[]) val.value);
768
+ updateFloatBuffer(v);
769
+ pgl.uniform3fv(loc, v.length / 3, floatBuffer);
770
+ } else if (val.type == UniformValue.FLOAT4VEC) {
771
+ float[] v = ((float[]) val.value);
772
+ updateFloatBuffer(v);
773
+ pgl.uniform4fv(loc, v.length / 4, floatBuffer);
774
+ } else if (val.type == UniformValue.MAT2) {
775
+ float[] v = ((float[]) val.value);
776
+ updateFloatBuffer(v);
777
+ pgl.uniformMatrix2fv(loc, 1, false, floatBuffer);
778
+ } else if (val.type == UniformValue.MAT3) {
779
+ float[] v = ((float[]) val.value);
780
+ updateFloatBuffer(v);
781
+ pgl.uniformMatrix3fv(loc, 1, false, floatBuffer);
782
+ } else if (val.type == UniformValue.MAT4) {
783
+ float[] v = ((float[]) val.value);
784
+ updateFloatBuffer(v);
785
+ pgl.uniformMatrix4fv(loc, 1, false, floatBuffer);
786
+ } else if (val.type == UniformValue.SAMPLER2D) {
787
+ PImage img = (PImage) val.value;
788
+ Texture tex = currentPG.getTexture(img);
789
+
790
+ if (textures == null) {
791
+ textures = new HashMap<Integer, Texture>();
792
+ }
793
+ textures.put(loc, tex);
794
+
795
+ if (texUnits == null) {
796
+ texUnits = new HashMap<Integer, Integer>();
797
+ }
798
+ if (texUnits.containsKey(loc)) {
799
+ unit = texUnits.get(loc);
800
+ pgl.uniform1i(loc, unit);
801
+ } else {
802
+ texUnits.put(loc, unit);
803
+ pgl.uniform1i(loc, unit);
804
+ }
805
+ unit++;
806
+ }
807
+ }
808
+ uniformValues.clear();
809
+ }
810
+ }
907
811
 
908
- protected void create() {
909
- context = pgl.getCurrentContext();
910
- glres = new GLResourceShader(this);
911
- }
812
+ protected void updateIntBuffer(int[] vec) {
813
+ intBuffer = PGL.updateIntBuffer(intBuffer, vec, false);
814
+ }
912
815
 
816
+ protected void updateFloatBuffer(float[] vec) {
817
+ floatBuffer = PGL.updateFloatBuffer(floatBuffer, vec, false);
818
+ }
913
819
 
914
- protected boolean compile() {
915
- boolean vertRes = true;
916
- if (hasVertexShader()) {
917
- vertRes = compileVertexShader();
918
- } else {
919
- PGraphics.showException("Doesn't have a vertex shader");
820
+ protected void bindTextures() {
821
+ if (textures != null && texUnits != null) {
822
+ for (int loc : textures.keySet()) {
823
+ Texture tex = textures.get(loc);
824
+ Integer unit = texUnits.get(loc);
825
+ if (unit != null) {
826
+ pgl.activeTexture(PGL.TEXTURE0 + unit);
827
+ tex.bind();
828
+ } else {
829
+ throw new RuntimeException("Cannot find unit for texture " + tex);
830
+ }
831
+ }
832
+ }
920
833
  }
921
834
 
922
- boolean fragRes = true;
923
- if (hasFragmentShader()) {
924
- fragRes = compileFragmentShader();
925
- } else {
926
- PGraphics.showException("Doesn't have a fragment shader");
835
+ protected void unbindTextures() {
836
+ if (textures != null && texUnits != null) {
837
+ for (int loc : textures.keySet()) {
838
+ Texture tex = textures.get(loc);
839
+ Integer unit = texUnits.get(loc);
840
+ if (unit != null) {
841
+ pgl.activeTexture(PGL.TEXTURE0 + unit);
842
+ tex.unbind();
843
+ } else {
844
+ throw new RuntimeException("Cannot find unit for texture " + tex);
845
+ }
846
+ }
847
+ pgl.activeTexture(PGL.TEXTURE0);
848
+ }
927
849
  }
928
850
 
929
- return vertRes && fragRes;
930
- }
851
+ public void init() {
852
+ if (glProgram == 0 || contextIsOutdated()) {
853
+ create();
854
+ if (compile()) {
855
+ pgl.attachShader(glProgram, glVertex);
856
+ pgl.attachShader(glProgram, glFragment);
931
857
 
858
+ setup();
932
859
 
933
- protected void validate() {
934
- pgl.getProgramiv(glProgram, PGL.LINK_STATUS, intBuffer);
935
- boolean linked = intBuffer.get(0) == 0 ? false : true;
936
- if (!linked) {
937
- PGraphics.showException("Cannot link shader program:\n" +
938
- pgl.getProgramInfoLog(glProgram));
939
- }
860
+ pgl.linkProgram(glProgram);
940
861
 
941
- pgl.validateProgram(glProgram);
942
- pgl.getProgramiv(glProgram, PGL.VALIDATE_STATUS, intBuffer);
943
- boolean validated = intBuffer.get(0) == 0 ? false : true;
944
- if (!validated) {
945
- PGraphics.showException("Cannot validate shader program:\n" +
946
- pgl.getProgramInfoLog(glProgram));
862
+ validate();
863
+ }
864
+ }
947
865
  }
948
- }
949
-
950
866
 
951
- protected boolean contextIsOutdated() {
952
- boolean outdated = !pgl.contextIsCurrent(context);
953
- if (outdated) {
954
- dispose();
867
+ protected void create() {
868
+ context = pgl.getCurrentContext();
869
+ glres = new GLResourceShader(this);
955
870
  }
956
- return outdated;
957
- }
958
871
 
872
+ protected boolean compile() {
873
+ boolean vertRes = true;
874
+ if (hasVertexShader()) {
875
+ vertRes = compileVertexShader();
876
+ } else {
877
+ PGraphics.showException("Doesn't have a vertex shader");
878
+ }
959
879
 
880
+ boolean fragRes = true;
881
+ if (hasFragmentShader()) {
882
+ fragRes = compileFragmentShader();
883
+ } else {
884
+ PGraphics.showException("Doesn't have a fragment shader");
885
+ }
960
886
 
961
- protected boolean hasVertexShader() {
962
- return vertexShaderSource != null && 0 < vertexShaderSource.length;
963
- }
887
+ return vertRes && fragRes;
888
+ }
964
889
 
890
+ protected void validate() {
891
+ pgl.getProgramiv(glProgram, PGL.LINK_STATUS, intBuffer);
892
+ boolean linked = intBuffer.get(0) == 0 ? false : true;
893
+ if (!linked) {
894
+ PGraphics.showException("Cannot link shader program:\n"
895
+ + pgl.getProgramInfoLog(glProgram));
896
+ }
965
897
 
966
- protected boolean hasFragmentShader() {
967
- return fragmentShaderSource != null && 0 < fragmentShaderSource.length;
968
- }
898
+ pgl.validateProgram(glProgram);
899
+ pgl.getProgramiv(glProgram, PGL.VALIDATE_STATUS, intBuffer);
900
+ boolean validated = intBuffer.get(0) == 0 ? false : true;
901
+ if (!validated) {
902
+ PGraphics.showException("Cannot validate shader program:\n"
903
+ + pgl.getProgramInfoLog(glProgram));
904
+ }
905
+ }
969
906
 
907
+ protected boolean contextIsOutdated() {
908
+ boolean outdated = !pgl.contextIsCurrent(context);
909
+ if (outdated) {
910
+ dispose();
911
+ }
912
+ return outdated;
913
+ }
970
914
 
971
- /**
972
- * @param shaderSource a string containing the shader's code
973
- */
974
- protected boolean compileVertexShader() {
975
- pgl.shaderSource(glVertex, PApplet.join(vertexShaderSource, "\n"));
976
- pgl.compileShader(glVertex);
915
+ protected boolean hasVertexShader() {
916
+ return vertexShaderSource != null && 0 < vertexShaderSource.length;
917
+ }
977
918
 
978
- pgl.getShaderiv(glVertex, PGL.COMPILE_STATUS, intBuffer);
979
- boolean compiled = intBuffer.get(0) == 0 ? false : true;
980
- if (!compiled) {
981
- PGraphics.showException("Cannot compile vertex shader:\n" +
982
- pgl.getShaderInfoLog(glVertex));
983
- return false;
984
- } else {
985
- return true;
919
+ protected boolean hasFragmentShader() {
920
+ return fragmentShaderSource != null && 0 < fragmentShaderSource.length;
986
921
  }
987
- }
988
922
 
923
+ /**
924
+ * @param shaderSource a string containing the shader's code
925
+ */
926
+ protected boolean compileVertexShader() {
927
+ pgl.shaderSource(glVertex, PApplet.join(vertexShaderSource, "\n"));
928
+ pgl.compileShader(glVertex);
929
+
930
+ pgl.getShaderiv(glVertex, PGL.COMPILE_STATUS, intBuffer);
931
+ boolean compiled = intBuffer.get(0) == 0 ? false : true;
932
+ if (!compiled) {
933
+ PGraphics.showException("Cannot compile vertex shader:\n"
934
+ + pgl.getShaderInfoLog(glVertex));
935
+ return false;
936
+ } else {
937
+ return true;
938
+ }
939
+ }
989
940
 
990
- /**
991
- * @param shaderSource a string containing the shader's code
992
- */
993
- protected boolean compileFragmentShader() {
994
- pgl.shaderSource(glFragment, PApplet.join(fragmentShaderSource, "\n"));
995
- pgl.compileShader(glFragment);
941
+ /**
942
+ * @param shaderSource a string containing the shader's code
943
+ */
944
+ protected boolean compileFragmentShader() {
945
+ pgl.shaderSource(glFragment, PApplet.join(fragmentShaderSource, "\n"));
946
+ pgl.compileShader(glFragment);
947
+
948
+ pgl.getShaderiv(glFragment, PGL.COMPILE_STATUS, intBuffer);
949
+ boolean compiled = intBuffer.get(0) == 0 ? false : true;
950
+ if (!compiled) {
951
+ PGraphics.showException("Cannot compile fragment shader:\n"
952
+ + pgl.getShaderInfoLog(glFragment));
953
+ return false;
954
+ } else {
955
+ return true;
956
+ }
957
+ }
996
958
 
997
- pgl.getShaderiv(glFragment, PGL.COMPILE_STATUS, intBuffer);
998
- boolean compiled = intBuffer.get(0) == 0 ? false : true;
999
- if (!compiled) {
1000
- PGraphics.showException("Cannot compile fragment shader:\n" +
1001
- pgl.getShaderInfoLog(glFragment));
1002
- return false;
1003
- } else {
1004
- return true;
959
+ protected void dispose() {
960
+ if (glres != null) {
961
+ glres.dispose();
962
+ glVertex = 0;
963
+ glFragment = 0;
964
+ glProgram = 0;
965
+ glres = null;
966
+ }
1005
967
  }
1006
- }
1007
968
 
969
+ static protected int getShaderType(String[] source, int defaultType) {
970
+ for (int i = 0; i < source.length; i++) {
971
+ String line = source[i].trim();
972
+
973
+ if (PApplet.match(line, colorShaderDefRegexp) != null) {
974
+ return PShader.COLOR;
975
+ } else if (PApplet.match(line, lightShaderDefRegexp) != null) {
976
+ return PShader.LIGHT;
977
+ } else if (PApplet.match(line, texShaderDefRegexp) != null) {
978
+ return PShader.TEXTURE;
979
+ } else if (PApplet.match(line, texlightShaderDefRegexp) != null) {
980
+ return PShader.TEXLIGHT;
981
+ } else if (PApplet.match(line, polyShaderDefRegexp) != null) {
982
+ return PShader.POLY;
983
+ } else if (PApplet.match(line, triShaderAttrRegexp) != null) {
984
+ return PShader.POLY;
985
+ } else if (PApplet.match(line, quadShaderAttrRegexp) != null) {
986
+ return PShader.POLY;
987
+ } else if (PApplet.match(line, pointShaderDefRegexp) != null) {
988
+ return PShader.POINT;
989
+ } else if (PApplet.match(line, lineShaderDefRegexp) != null) {
990
+ return PShader.LINE;
991
+ } else if (PApplet.match(line, pointShaderAttrRegexp) != null) {
992
+ return PShader.POINT;
993
+ } else if (PApplet.match(line, pointShaderInRegexp) != null) {
994
+ return PShader.POINT;
995
+ } else if (PApplet.match(line, lineShaderAttrRegexp) != null) {
996
+ return PShader.LINE;
997
+ } else if (PApplet.match(line, lineShaderInRegexp) != null) {
998
+ return PShader.LINE;
999
+ }
1000
+ }
1001
+ return defaultType;
1002
+ }
1008
1003
 
1009
- protected void dispose() {
1010
- if (glres != null) {
1011
- glres.dispose();
1012
- glVertex = 0;
1013
- glFragment = 0;
1014
- glProgram = 0;
1015
- glres = null;
1004
+ // ***************************************************************************
1005
+ //
1006
+ // Processing specific
1007
+ protected int getType() {
1008
+ return type;
1016
1009
  }
1017
- }
1018
1010
 
1011
+ protected void setType(int type) {
1012
+ this.type = type;
1013
+ }
1019
1014
 
1020
- static protected int getShaderType(String[] source, int defaultType) {
1021
- for (int i = 0; i < source.length; i++) {
1022
- String line = source[i].trim();
1015
+ protected boolean hasType() {
1016
+ return POINT <= type && type <= TEXLIGHT;
1017
+ }
1023
1018
 
1024
- if (PApplet.match(line, colorShaderDefRegexp) != null)
1025
- return PShader.COLOR;
1026
- else if (PApplet.match(line, lightShaderDefRegexp) != null)
1027
- return PShader.LIGHT;
1028
- else if (PApplet.match(line, texShaderDefRegexp) != null)
1029
- return PShader.TEXTURE;
1030
- else if (PApplet.match(line, texlightShaderDefRegexp) != null)
1031
- return PShader.TEXLIGHT;
1032
- else if (PApplet.match(line, polyShaderDefRegexp) != null)
1033
- return PShader.POLY;
1034
- else if (PApplet.match(line, triShaderAttrRegexp) != null)
1035
- return PShader.POLY;
1036
- else if (PApplet.match(line, quadShaderAttrRegexp) != null)
1037
- return PShader.POLY;
1038
- else if (PApplet.match(line, pointShaderDefRegexp) != null)
1039
- return PShader.POINT;
1040
- else if (PApplet.match(line, lineShaderDefRegexp) != null)
1041
- return PShader.LINE;
1042
- else if (PApplet.match(line, pointShaderAttrRegexp) != null)
1043
- return PShader.POINT;
1044
- else if (PApplet.match(line, pointShaderInRegexp) != null)
1045
- return PShader.POINT;
1046
- else if (PApplet.match(line, lineShaderAttrRegexp) != null)
1047
- return PShader.LINE;
1048
- else if (PApplet.match(line, lineShaderInRegexp) != null)
1049
- return PShader.LINE;
1019
+ protected boolean isPointShader() {
1020
+ return type == POINT;
1050
1021
  }
1051
- return defaultType;
1052
- }
1053
1022
 
1023
+ protected boolean isLineShader() {
1024
+ return type == LINE;
1025
+ }
1054
1026
 
1055
- // ***************************************************************************
1056
- //
1057
- // Processing specific
1027
+ protected boolean isPolyShader() {
1028
+ return POLY <= type && type <= TEXLIGHT;
1029
+ }
1058
1030
 
1031
+ protected boolean checkPolyType(int type) {
1032
+ if (getType() == PShader.POLY) {
1033
+ return true;
1034
+ }
1059
1035
 
1060
- protected int getType() {
1061
- return type;
1062
- }
1036
+ if (getType() != type) {
1037
+ if (type == TEXLIGHT) {
1038
+ PGraphics.showWarning(PGraphicsOpenGL.NO_TEXLIGHT_SHADER_ERROR);
1039
+ } else if (type == LIGHT) {
1040
+ PGraphics.showWarning(PGraphicsOpenGL.NO_LIGHT_SHADER_ERROR);
1041
+ } else if (type == TEXTURE) {
1042
+ PGraphics.showWarning(PGraphicsOpenGL.NO_TEXTURE_SHADER_ERROR);
1043
+ } else if (type == COLOR) {
1044
+ PGraphics.showWarning(PGraphicsOpenGL.NO_COLOR_SHADER_ERROR);
1045
+ }
1046
+ return false;
1047
+ }
1063
1048
 
1049
+ return true;
1050
+ }
1064
1051
 
1065
- protected void setType(int type) {
1066
- this.type = type;
1067
- }
1052
+ protected int getLastTexUnit() {
1053
+ return texUnits == null ? -1 : texUnits.size() - 1;
1054
+ }
1068
1055
 
1056
+ protected void setRenderer(PGraphicsOpenGL pg) {
1057
+ this.currentPG = pg;
1058
+ }
1069
1059
 
1070
- protected boolean hasType() {
1071
- return POINT <= type && type <= TEXLIGHT;
1072
- }
1060
+ protected void loadAttributes() {
1061
+ if (loadedAttributes) {
1062
+ return;
1063
+ }
1073
1064
 
1065
+ vertexLoc = getAttributeLoc("vertex");
1066
+ if (vertexLoc == -1) {
1067
+ vertexLoc = getAttributeLoc("position");
1068
+ }
1074
1069
 
1075
- protected boolean isPointShader() {
1076
- return type == POINT;
1077
- }
1070
+ colorLoc = getAttributeLoc("color");
1071
+ texCoordLoc = getAttributeLoc("texCoord");
1072
+ normalLoc = getAttributeLoc("normal");
1078
1073
 
1074
+ ambientLoc = getAttributeLoc("ambient");
1075
+ specularLoc = getAttributeLoc("specular");
1076
+ emissiveLoc = getAttributeLoc("emissive");
1077
+ shininessLoc = getAttributeLoc("shininess");
1079
1078
 
1080
- protected boolean isLineShader() {
1081
- return type == LINE;
1082
- }
1079
+ directionLoc = getAttributeLoc("direction");
1083
1080
 
1081
+ offsetLoc = getAttributeLoc("offset");
1084
1082
 
1085
- protected boolean isPolyShader() {
1086
- return POLY <= type && type <= TEXLIGHT;
1087
- }
1083
+ directionLoc = getAttributeLoc("direction");
1084
+ offsetLoc = getAttributeLoc("offset");
1088
1085
 
1086
+ loadedAttributes = true;
1087
+ }
1089
1088
 
1090
- protected boolean checkPolyType(int type) {
1091
- if (getType() == PShader.POLY) return true;
1089
+ protected void loadUniforms() {
1090
+ if (loadedUniforms) {
1091
+ return;
1092
+ }
1093
+ transformMatLoc = getUniformLoc("transform");
1094
+ if (transformMatLoc == -1) {
1095
+ transformMatLoc = getUniformLoc("transformMatrix");
1096
+ }
1092
1097
 
1093
- if (getType() != type) {
1094
- if (type == TEXLIGHT) {
1095
- PGraphics.showWarning(PGraphicsOpenGL.NO_TEXLIGHT_SHADER_ERROR);
1096
- } else if (type == LIGHT) {
1097
- PGraphics.showWarning(PGraphicsOpenGL.NO_LIGHT_SHADER_ERROR);
1098
- } else if (type == TEXTURE) {
1099
- PGraphics.showWarning(PGraphicsOpenGL.NO_TEXTURE_SHADER_ERROR);
1100
- } else if (type == COLOR) {
1101
- PGraphics.showWarning(PGraphicsOpenGL.NO_COLOR_SHADER_ERROR);
1102
- }
1103
- return false;
1104
- }
1098
+ modelviewMatLoc = getUniformLoc("modelview");
1099
+ if (modelviewMatLoc == -1) {
1100
+ modelviewMatLoc = getUniformLoc("modelviewMatrix");
1101
+ }
1105
1102
 
1106
- return true;
1107
- }
1103
+ projectionMatLoc = getUniformLoc("projection");
1104
+ if (projectionMatLoc == -1) {
1105
+ projectionMatLoc = getUniformLoc("projectionMatrix");
1106
+ }
1108
1107
 
1108
+ viewportLoc = getUniformLoc("viewport");
1109
+ resolutionLoc = getUniformLoc("resolution");
1110
+ ppixelsLoc = getUniformLoc("ppixels");
1109
1111
 
1110
- protected int getLastTexUnit() {
1111
- return texUnits == null ? -1 : texUnits.size() - 1;
1112
- }
1112
+ normalMatLoc = getUniformLoc("normalMatrix");
1113
1113
 
1114
+ lightCountLoc = getUniformLoc("lightCount");
1115
+ lightPositionLoc = getUniformLoc("lightPosition");
1116
+ lightNormalLoc = getUniformLoc("lightNormal");
1117
+ lightAmbientLoc = getUniformLoc("lightAmbient");
1118
+ lightDiffuseLoc = getUniformLoc("lightDiffuse");
1119
+ lightSpecularLoc = getUniformLoc("lightSpecular");
1120
+ lightFalloffLoc = getUniformLoc("lightFalloff");
1121
+ lightSpotLoc = getUniformLoc("lightSpot");
1114
1122
 
1115
- protected void setRenderer(PGraphicsOpenGL pg) {
1116
- this.currentPG = pg;
1117
- }
1123
+ textureLoc = getUniformLoc("texture");
1124
+ if (textureLoc == -1) {
1125
+ textureLoc = getUniformLoc("texMap");
1126
+ }
1118
1127
 
1128
+ texMatrixLoc = getUniformLoc("texMatrix");
1129
+ texOffsetLoc = getUniformLoc("texOffset");
1119
1130
 
1120
- protected void loadAttributes() {
1121
- if (loadedAttributes) return;
1131
+ perspectiveLoc = getUniformLoc("perspective");
1132
+ scaleLoc = getUniformLoc("scale");
1133
+ loadedUniforms = true;
1134
+ }
1122
1135
 
1123
- vertexLoc = getAttributeLoc("vertex");
1124
- if (vertexLoc == -1) vertexLoc = getAttributeLoc("position");
1136
+ protected void setCommonUniforms() {
1137
+ if (-1 < transformMatLoc) {
1138
+ currentPG.updateGLProjmodelview();
1139
+ setUniformMatrix(transformMatLoc, currentPG.glProjmodelview);
1140
+ }
1125
1141
 
1126
- colorLoc = getAttributeLoc("color");
1127
- texCoordLoc = getAttributeLoc("texCoord");
1128
- normalLoc = getAttributeLoc("normal");
1142
+ if (-1 < modelviewMatLoc) {
1143
+ currentPG.updateGLModelview();
1144
+ setUniformMatrix(modelviewMatLoc, currentPG.glModelview);
1145
+ }
1129
1146
 
1130
- ambientLoc = getAttributeLoc("ambient");
1131
- specularLoc = getAttributeLoc("specular");
1132
- emissiveLoc = getAttributeLoc("emissive");
1133
- shininessLoc = getAttributeLoc("shininess");
1147
+ if (-1 < projectionMatLoc) {
1148
+ currentPG.updateGLProjection();
1149
+ setUniformMatrix(projectionMatLoc, currentPG.glProjection);
1150
+ }
1134
1151
 
1135
- directionLoc = getAttributeLoc("direction");
1152
+ if (-1 < viewportLoc) {
1153
+ float x = currentPG.viewport.get(0);
1154
+ float y = currentPG.viewport.get(1);
1155
+ float w = currentPG.viewport.get(2);
1156
+ float h = currentPG.viewport.get(3);
1157
+ setUniformValue(viewportLoc, x, y, w, h);
1158
+ }
1136
1159
 
1137
- offsetLoc = getAttributeLoc("offset");
1160
+ if (-1 < resolutionLoc) {
1161
+ float w = currentPG.viewport.get(2);
1162
+ float h = currentPG.viewport.get(3);
1163
+ setUniformValue(resolutionLoc, w, h);
1164
+ }
1138
1165
 
1139
- directionLoc = getAttributeLoc("direction");
1140
- offsetLoc = getAttributeLoc("offset");
1166
+ if (-1 < ppixelsLoc) {
1167
+ ppixelsUnit = getLastTexUnit() + 1;
1168
+ setUniformValue(ppixelsLoc, ppixelsUnit);
1169
+ pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
1170
+ currentPG.bindFrontTexture();
1171
+ } else {
1172
+ ppixelsUnit = -1;
1173
+ }
1174
+ }
1141
1175
 
1142
- loadedAttributes = true;
1143
- }
1176
+ protected void bindTyped() {
1177
+ if (currentPG == null) {
1178
+ setRenderer(primaryPG.getCurrentPG());
1179
+ loadAttributes();
1180
+ loadUniforms();
1181
+ }
1182
+ setCommonUniforms();
1144
1183
 
1184
+ if (-1 < vertexLoc) {
1185
+ pgl.enableVertexAttribArray(vertexLoc);
1186
+ }
1187
+ if (-1 < colorLoc) {
1188
+ pgl.enableVertexAttribArray(colorLoc);
1189
+ }
1190
+ if (-1 < texCoordLoc) {
1191
+ pgl.enableVertexAttribArray(texCoordLoc);
1192
+ }
1193
+ if (-1 < normalLoc) {
1194
+ pgl.enableVertexAttribArray(normalLoc);
1195
+ }
1145
1196
 
1146
- protected void loadUniforms() {
1147
- if (loadedUniforms) return;
1148
- transformMatLoc = getUniformLoc("transform");
1149
- if (transformMatLoc == -1)
1150
- transformMatLoc = getUniformLoc("transformMatrix");
1197
+ if (-1 < normalMatLoc) {
1198
+ currentPG.updateGLNormal();
1199
+ setUniformMatrix(normalMatLoc, currentPG.glNormal);
1200
+ }
1151
1201
 
1152
- modelviewMatLoc = getUniformLoc("modelview");
1153
- if (modelviewMatLoc == -1)
1154
- modelviewMatLoc = getUniformLoc("modelviewMatrix");
1202
+ if (-1 < ambientLoc) {
1203
+ pgl.enableVertexAttribArray(ambientLoc);
1204
+ }
1205
+ if (-1 < specularLoc) {
1206
+ pgl.enableVertexAttribArray(specularLoc);
1207
+ }
1208
+ if (-1 < emissiveLoc) {
1209
+ pgl.enableVertexAttribArray(emissiveLoc);
1210
+ }
1211
+ if (-1 < shininessLoc) {
1212
+ pgl.enableVertexAttribArray(shininessLoc);
1213
+ }
1155
1214
 
1156
- projectionMatLoc = getUniformLoc("projection");
1157
- if (projectionMatLoc == -1)
1158
- projectionMatLoc = getUniformLoc("projectionMatrix");
1215
+ int count = currentPG.lightCount;
1216
+ setUniformValue(lightCountLoc, count);
1217
+ if (0 < count) {
1218
+ setUniformVector(lightPositionLoc, currentPG.lightPosition, 4, count);
1219
+ setUniformVector(lightNormalLoc, currentPG.lightNormal, 3, count);
1220
+ setUniformVector(lightAmbientLoc, currentPG.lightAmbient, 3, count);
1221
+ setUniformVector(lightDiffuseLoc, currentPG.lightDiffuse, 3, count);
1222
+ setUniformVector(lightSpecularLoc, currentPG.lightSpecular, 3, count);
1223
+ setUniformVector(lightFalloffLoc, currentPG.lightFalloffCoefficients,
1224
+ 3, count);
1225
+ setUniformVector(lightSpotLoc, currentPG.lightSpotParameters, 2, count);
1226
+ }
1159
1227
 
1160
- viewportLoc = getUniformLoc("viewport");
1161
- resolutionLoc = getUniformLoc("resolution");
1162
- ppixelsLoc = getUniformLoc("ppixels");
1228
+ if (-1 < directionLoc) {
1229
+ pgl.enableVertexAttribArray(directionLoc);
1230
+ }
1163
1231
 
1164
- normalMatLoc = getUniformLoc("normalMatrix");
1232
+ if (-1 < offsetLoc) {
1233
+ pgl.enableVertexAttribArray(offsetLoc);
1234
+ }
1165
1235
 
1166
- lightCountLoc = getUniformLoc("lightCount");
1167
- lightPositionLoc = getUniformLoc("lightPosition");
1168
- lightNormalLoc = getUniformLoc("lightNormal");
1169
- lightAmbientLoc = getUniformLoc("lightAmbient");
1170
- lightDiffuseLoc = getUniformLoc("lightDiffuse");
1171
- lightSpecularLoc = getUniformLoc("lightSpecular");
1172
- lightFalloffLoc = getUniformLoc("lightFalloff");
1173
- lightSpotLoc = getUniformLoc("lightSpot");
1236
+ if (-1 < perspectiveLoc) {
1237
+ if (currentPG.getHint(ENABLE_STROKE_PERSPECTIVE)
1238
+ && currentPG.nonOrthoProjection()) {
1239
+ setUniformValue(perspectiveLoc, 1);
1240
+ } else {
1241
+ setUniformValue(perspectiveLoc, 0);
1242
+ }
1243
+ }
1174
1244
 
1175
- textureLoc = getUniformLoc("texture");
1176
- if (textureLoc == -1) {
1177
- textureLoc = getUniformLoc("texMap");
1245
+ if (-1 < scaleLoc) {
1246
+ if (currentPG.getHint(DISABLE_OPTIMIZED_STROKE)) {
1247
+ setUniformValue(scaleLoc, 1.0f, 1.0f, 1.0f);
1248
+ } else {
1249
+ float f = PGL.STROKE_DISPLACEMENT;
1250
+ if (currentPG.orthoProjection()) {
1251
+ setUniformValue(scaleLoc, 1, 1, f);
1252
+ } else {
1253
+ setUniformValue(scaleLoc, f, f, f);
1254
+ }
1255
+ }
1256
+ }
1178
1257
  }
1179
1258
 
1180
- texMatrixLoc = getUniformLoc("texMatrix");
1181
- texOffsetLoc = getUniformLoc("texOffset");
1259
+ protected void unbindTyped() {
1260
+ if (-1 < offsetLoc) {
1261
+ pgl.disableVertexAttribArray(offsetLoc);
1262
+ }
1182
1263
 
1183
- perspectiveLoc = getUniformLoc("perspective");
1184
- scaleLoc = getUniformLoc("scale");
1185
- loadedUniforms = true;
1186
- }
1264
+ if (-1 < directionLoc) {
1265
+ pgl.disableVertexAttribArray(directionLoc);
1266
+ }
1187
1267
 
1268
+ if (-1 < textureLoc && texture != null) {
1269
+ pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1270
+ texture.unbind();
1271
+ pgl.activeTexture(PGL.TEXTURE0);
1272
+ texture = null;
1273
+ }
1188
1274
 
1189
- protected void setCommonUniforms() {
1190
- if (-1 < transformMatLoc) {
1191
- currentPG.updateGLProjmodelview();
1192
- setUniformMatrix(transformMatLoc, currentPG.glProjmodelview);
1193
- }
1275
+ if (-1 < ambientLoc) {
1276
+ pgl.disableVertexAttribArray(ambientLoc);
1277
+ }
1278
+ if (-1 < specularLoc) {
1279
+ pgl.disableVertexAttribArray(specularLoc);
1280
+ }
1281
+ if (-1 < emissiveLoc) {
1282
+ pgl.disableVertexAttribArray(emissiveLoc);
1283
+ }
1284
+ if (-1 < shininessLoc) {
1285
+ pgl.disableVertexAttribArray(shininessLoc);
1286
+ }
1194
1287
 
1195
- if (-1 < modelviewMatLoc) {
1196
- currentPG.updateGLModelview();
1197
- setUniformMatrix(modelviewMatLoc, currentPG.glModelview);
1198
- }
1288
+ if (-1 < vertexLoc) {
1289
+ pgl.disableVertexAttribArray(vertexLoc);
1290
+ }
1291
+ if (-1 < colorLoc) {
1292
+ pgl.disableVertexAttribArray(colorLoc);
1293
+ }
1294
+ if (-1 < texCoordLoc) {
1295
+ pgl.disableVertexAttribArray(texCoordLoc);
1296
+ }
1297
+ if (-1 < normalLoc) {
1298
+ pgl.disableVertexAttribArray(normalLoc);
1299
+ }
1199
1300
 
1200
- if (-1 < projectionMatLoc) {
1201
- currentPG.updateGLProjection();
1202
- setUniformMatrix(projectionMatLoc, currentPG.glProjection);
1203
- }
1301
+ if (-1 < ppixelsLoc) {
1302
+ pgl.enableFBOLayer();
1303
+ pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
1304
+ currentPG.unbindFrontTexture();
1305
+ pgl.activeTexture(PGL.TEXTURE0);
1306
+ }
1204
1307
 
1205
- if (-1 < viewportLoc) {
1206
- float x = currentPG.viewport.get(0);
1207
- float y = currentPG.viewport.get(1);
1208
- float w = currentPG.viewport.get(2);
1209
- float h = currentPG.viewport.get(3);
1210
- setUniformValue(viewportLoc, x, y, w, h);
1308
+ pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);
1211
1309
  }
1212
1310
 
1213
- if (-1 < resolutionLoc) {
1214
- float w = currentPG.viewport.get(2);
1215
- float h = currentPG.viewport.get(3);
1216
- setUniformValue(resolutionLoc, w, h);
1217
- }
1311
+ protected void setTexture(Texture tex) {
1312
+ texture = tex;
1313
+
1314
+ float scaleu = 1;
1315
+ float scalev = 1;
1316
+ float dispu = 0;
1317
+ float dispv = 0;
1318
+
1319
+ if (tex != null) {
1320
+ if (tex.invertedX()) {
1321
+ scaleu = -1;
1322
+ dispu = 1;
1323
+ }
1324
+
1325
+ if (tex.invertedY()) {
1326
+ scalev = -1;
1327
+ dispv = 1;
1328
+ }
1329
+
1330
+ scaleu *= tex.maxTexcoordU();
1331
+ dispu *= tex.maxTexcoordU();
1332
+ scalev *= tex.maxTexcoordV();
1333
+ dispv *= tex.maxTexcoordV();
1334
+
1335
+ setUniformValue(texOffsetLoc, 1.0f / tex.width, 1.0f / tex.height);
1336
+
1337
+ if (-1 < textureLoc) {
1338
+ texUnit = -1 < ppixelsUnit ? ppixelsUnit + 1 : getLastTexUnit() + 1;
1339
+ setUniformValue(textureLoc, texUnit);
1340
+ pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1341
+ tex.bind();
1342
+ }
1343
+ }
1218
1344
 
1219
- if (-1 < ppixelsLoc) {
1220
- ppixelsUnit = getLastTexUnit() + 1;
1221
- setUniformValue(ppixelsLoc, ppixelsUnit);
1222
- pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
1223
- currentPG.bindFrontTexture();
1224
- } else {
1225
- ppixelsUnit = -1;
1345
+ if (-1 < texMatrixLoc) {
1346
+ if (tcmat == null) {
1347
+ tcmat = new float[16];
1348
+ }
1349
+ tcmat[0] = scaleu;
1350
+ tcmat[4] = 0;
1351
+ tcmat[8] = 0;
1352
+ tcmat[12] = dispu;
1353
+ tcmat[1] = 0;
1354
+ tcmat[5] = scalev;
1355
+ tcmat[9] = 0;
1356
+ tcmat[13] = dispv;
1357
+ tcmat[2] = 0;
1358
+ tcmat[6] = 0;
1359
+ tcmat[10] = 0;
1360
+ tcmat[14] = 0;
1361
+ tcmat[3] = 0;
1362
+ tcmat[7] = 0;
1363
+ tcmat[11] = 0;
1364
+ tcmat[15] = 0;
1365
+ setUniformMatrix(texMatrixLoc, tcmat);
1366
+ }
1226
1367
  }
1227
- }
1228
-
1229
1368
 
1230
- protected void bindTyped() {
1231
- if (currentPG == null) {
1232
- setRenderer(primaryPG.getCurrentPG());
1233
- loadAttributes();
1234
- loadUniforms();
1369
+ protected boolean supportsTexturing() {
1370
+ return -1 < textureLoc;
1235
1371
  }
1236
- setCommonUniforms();
1237
1372
 
1238
- if (-1 < vertexLoc) pgl.enableVertexAttribArray(vertexLoc);
1239
- if (-1 < colorLoc) pgl.enableVertexAttribArray(colorLoc);
1240
- if (-1 < texCoordLoc) pgl.enableVertexAttribArray(texCoordLoc);
1241
- if (-1 < normalLoc) pgl.enableVertexAttribArray(normalLoc);
1242
-
1243
- if (-1 < normalMatLoc) {
1244
- currentPG.updateGLNormal();
1245
- setUniformMatrix(normalMatLoc, currentPG.glNormal);
1373
+ protected boolean supportLighting() {
1374
+ return -1 < lightCountLoc || -1 < lightPositionLoc || -1 < lightNormalLoc;
1246
1375
  }
1247
1376
 
1248
- if (-1 < ambientLoc) pgl.enableVertexAttribArray(ambientLoc);
1249
- if (-1 < specularLoc) pgl.enableVertexAttribArray(specularLoc);
1250
- if (-1 < emissiveLoc) pgl.enableVertexAttribArray(emissiveLoc);
1251
- if (-1 < shininessLoc) pgl.enableVertexAttribArray(shininessLoc);
1252
-
1253
- int count = currentPG.lightCount;
1254
- setUniformValue(lightCountLoc, count);
1255
- if (0 < count) {
1256
- setUniformVector(lightPositionLoc, currentPG.lightPosition, 4, count);
1257
- setUniformVector(lightNormalLoc, currentPG.lightNormal, 3, count);
1258
- setUniformVector(lightAmbientLoc, currentPG.lightAmbient, 3, count);
1259
- setUniformVector(lightDiffuseLoc, currentPG.lightDiffuse, 3, count);
1260
- setUniformVector(lightSpecularLoc, currentPG.lightSpecular, 3, count);
1261
- setUniformVector(lightFalloffLoc, currentPG.lightFalloffCoefficients,
1262
- 3, count);
1263
- setUniformVector(lightSpotLoc, currentPG.lightSpotParameters, 2, count);
1377
+ protected boolean accessTexCoords() {
1378
+ return -1 < texCoordLoc;
1264
1379
  }
1265
1380
 
1266
- if (-1 < directionLoc) pgl.enableVertexAttribArray(directionLoc);
1267
-
1268
- if (-1 < offsetLoc) pgl.enableVertexAttribArray(offsetLoc);
1269
-
1270
- if (-1 < perspectiveLoc) {
1271
- if (currentPG.getHint(ENABLE_STROKE_PERSPECTIVE) &&
1272
- currentPG.nonOrthoProjection()) {
1273
- setUniformValue(perspectiveLoc, 1);
1274
- } else {
1275
- setUniformValue(perspectiveLoc, 0);
1276
- }
1381
+ protected boolean accessNormals() {
1382
+ return -1 < normalLoc;
1277
1383
  }
1278
1384
 
1279
- if (-1 < scaleLoc) {
1280
- if (currentPG.getHint(DISABLE_OPTIMIZED_STROKE)) {
1281
- setUniformValue(scaleLoc, 1.0f, 1.0f, 1.0f);
1282
- } else {
1283
- float f = PGL.STROKE_DISPLACEMENT;
1284
- if (currentPG.orthoProjection()) {
1285
- setUniformValue(scaleLoc, 1, 1, f);
1286
- } else {
1287
- setUniformValue(scaleLoc, f, f, f);
1288
- }
1289
- }
1385
+ protected boolean accessLightAttribs() {
1386
+ return -1 < ambientLoc || -1 < specularLoc || -1 < emissiveLoc
1387
+ || -1 < shininessLoc;
1290
1388
  }
1291
- }
1292
1389
 
1293
- protected void unbindTyped() {
1294
- if (-1 < offsetLoc) pgl.disableVertexAttribArray(offsetLoc);
1390
+ protected void setVertexAttribute(int vboId, int size, int type,
1391
+ int stride, int offset) {
1392
+ setAttributeVBO(vertexLoc, vboId, size, type, false, stride, offset);
1393
+ }
1295
1394
 
1296
- if (-1 < directionLoc) pgl.disableVertexAttribArray(directionLoc);
1395
+ protected void setColorAttribute(int vboId, int size, int type,
1396
+ int stride, int offset) {
1397
+ setAttributeVBO(colorLoc, vboId, size, type, true, stride, offset);
1398
+ }
1297
1399
 
1298
- if (-1 < textureLoc && texture != null) {
1299
- pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1300
- texture.unbind();
1301
- pgl.activeTexture(PGL.TEXTURE0);
1302
- texture = null;
1400
+ protected void setNormalAttribute(int vboId, int size, int type,
1401
+ int stride, int offset) {
1402
+ setAttributeVBO(normalLoc, vboId, size, type, false, stride, offset);
1303
1403
  }
1304
1404
 
1305
- if (-1 < ambientLoc) pgl.disableVertexAttribArray(ambientLoc);
1306
- if (-1 < specularLoc) pgl.disableVertexAttribArray(specularLoc);
1307
- if (-1 < emissiveLoc) pgl.disableVertexAttribArray(emissiveLoc);
1308
- if (-1 < shininessLoc) pgl.disableVertexAttribArray(shininessLoc);
1405
+ protected void setTexcoordAttribute(int vboId, int size, int type,
1406
+ int stride, int offset) {
1407
+ setAttributeVBO(texCoordLoc, vboId, size, type, false, stride, offset);
1408
+ }
1309
1409
 
1310
- if (-1 < vertexLoc) pgl.disableVertexAttribArray(vertexLoc);
1311
- if (-1 < colorLoc) pgl.disableVertexAttribArray(colorLoc);
1312
- if (-1 < texCoordLoc) pgl.disableVertexAttribArray(texCoordLoc);
1313
- if (-1 < normalLoc) pgl.disableVertexAttribArray(normalLoc);
1410
+ protected void setAmbientAttribute(int vboId, int size, int type,
1411
+ int stride, int offset) {
1412
+ setAttributeVBO(ambientLoc, vboId, size, type, true, stride, offset);
1413
+ }
1314
1414
 
1315
- if (-1 < ppixelsLoc) {
1316
- pgl.enableFBOLayer();
1317
- pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
1318
- currentPG.unbindFrontTexture();
1319
- pgl.activeTexture(PGL.TEXTURE0);
1415
+ protected void setSpecularAttribute(int vboId, int size, int type,
1416
+ int stride, int offset) {
1417
+ setAttributeVBO(specularLoc, vboId, size, type, true, stride, offset);
1320
1418
  }
1321
1419
 
1322
- pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);
1323
- }
1420
+ protected void setEmissiveAttribute(int vboId, int size, int type,
1421
+ int stride, int offset) {
1422
+ setAttributeVBO(emissiveLoc, vboId, size, type, true, stride, offset);
1423
+ }
1324
1424
 
1325
- protected void setTexture(Texture tex) {
1326
- texture = tex;
1425
+ protected void setShininessAttribute(int vboId, int size, int type,
1426
+ int stride, int offset) {
1427
+ setAttributeVBO(shininessLoc, vboId, size, type, false, stride, offset);
1428
+ }
1327
1429
 
1328
- float scaleu = 1;
1329
- float scalev = 1;
1330
- float dispu = 0;
1331
- float dispv = 0;
1430
+ protected void setLineAttribute(int vboId, int size, int type,
1431
+ int stride, int offset) {
1432
+ setAttributeVBO(directionLoc, vboId, size, type, false, stride, offset);
1433
+ }
1332
1434
 
1333
- if (tex != null) {
1334
- if (tex.invertedX()) {
1335
- scaleu = -1;
1336
- dispu = 1;
1337
- }
1338
-
1339
- if (tex.invertedY()) {
1340
- scalev = -1;
1341
- dispv = 1;
1342
- }
1435
+ protected void setPointAttribute(int vboId, int size, int type,
1436
+ int stride, int offset) {
1437
+ setAttributeVBO(offsetLoc, vboId, size, type, false, stride, offset);
1438
+ }
1343
1439
 
1344
- scaleu *= tex.maxTexcoordU();
1345
- dispu *= tex.maxTexcoordU();
1346
- scalev *= tex.maxTexcoordV();
1347
- dispv *= tex.maxTexcoordV();
1348
-
1349
- setUniformValue(texOffsetLoc, 1.0f / tex.width, 1.0f / tex.height);
1350
-
1351
- if (-1 < textureLoc) {
1352
- texUnit = -1 < ppixelsUnit ? ppixelsUnit + 1 : getLastTexUnit() + 1;
1353
- setUniformValue(textureLoc, texUnit);
1354
- pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1355
- tex.bind();
1356
- }
1357
- }
1358
-
1359
- if (-1 < texMatrixLoc) {
1360
- if (tcmat == null) {
1361
- tcmat = new float[16];
1362
- }
1363
- tcmat[0] = scaleu; tcmat[4] = 0; tcmat[ 8] = 0; tcmat[12] = dispu;
1364
- tcmat[1] = 0; tcmat[5] = scalev; tcmat[ 9] = 0; tcmat[13] = dispv;
1365
- tcmat[2] = 0; tcmat[6] = 0; tcmat[10] = 0; tcmat[14] = 0;
1366
- tcmat[3] = 0; tcmat[7] = 0; tcmat[11] = 0; tcmat[15] = 0;
1367
- setUniformMatrix(texMatrixLoc, tcmat);
1368
- }
1369
- }
1370
-
1371
-
1372
- protected boolean supportsTexturing() {
1373
- return -1 < textureLoc;
1374
- }
1375
-
1376
- protected boolean supportLighting() {
1377
- return -1 < lightCountLoc || -1 < lightPositionLoc || -1 < lightNormalLoc;
1378
- }
1379
-
1380
- protected boolean accessTexCoords() {
1381
- return -1 < texCoordLoc;
1382
- }
1383
-
1384
- protected boolean accessNormals() {
1385
- return -1 < normalLoc;
1386
- }
1387
-
1388
- protected boolean accessLightAttribs() {
1389
- return -1 < ambientLoc || -1 < specularLoc || -1 < emissiveLoc ||
1390
- -1 < shininessLoc;
1391
- }
1392
-
1393
- protected void setVertexAttribute(int vboId, int size, int type,
1394
- int stride, int offset) {
1395
- setAttributeVBO(vertexLoc, vboId, size, type, false, stride, offset);
1396
- }
1397
-
1398
- protected void setColorAttribute(int vboId, int size, int type,
1399
- int stride, int offset) {
1400
- setAttributeVBO(colorLoc, vboId, size, type, true, stride, offset);
1401
- }
1402
-
1403
- protected void setNormalAttribute(int vboId, int size, int type,
1404
- int stride, int offset) {
1405
- setAttributeVBO(normalLoc, vboId, size, type, false, stride, offset);
1406
- }
1407
-
1408
- protected void setTexcoordAttribute(int vboId, int size, int type,
1409
- int stride, int offset) {
1410
- setAttributeVBO(texCoordLoc, vboId, size, type, false, stride, offset);
1411
- }
1412
-
1413
- protected void setAmbientAttribute(int vboId, int size, int type,
1414
- int stride, int offset) {
1415
- setAttributeVBO(ambientLoc, vboId, size, type, true, stride, offset);
1416
- }
1417
-
1418
- protected void setSpecularAttribute(int vboId, int size, int type,
1419
- int stride, int offset) {
1420
- setAttributeVBO(specularLoc, vboId, size, type, true, stride, offset);
1421
- }
1422
-
1423
- protected void setEmissiveAttribute(int vboId, int size, int type,
1424
- int stride, int offset) {
1425
- setAttributeVBO(emissiveLoc, vboId, size, type, true, stride, offset);
1426
- }
1427
-
1428
- protected void setShininessAttribute(int vboId, int size, int type,
1429
- int stride, int offset) {
1430
- setAttributeVBO(shininessLoc, vboId, size, type, false, stride, offset);
1431
- }
1432
-
1433
- protected void setLineAttribute(int vboId, int size, int type,
1434
- int stride, int offset) {
1435
- setAttributeVBO(directionLoc, vboId, size, type, false, stride, offset);
1436
- }
1437
-
1438
- protected void setPointAttribute(int vboId, int size, int type,
1439
- int stride, int offset) {
1440
- setAttributeVBO(offsetLoc, vboId, size, type, false, stride, offset);
1441
- }
1442
-
1443
-
1444
- // ***************************************************************************
1445
- //
1446
- // Class to store a user-specified value for a uniform parameter
1447
- // in the shader
1448
- protected static class UniformValue {
1449
- static final int INT1 = 0;
1450
- static final int INT2 = 1;
1451
- static final int INT3 = 2;
1452
- static final int INT4 = 3;
1453
- static final int FLOAT1 = 4;
1454
- static final int FLOAT2 = 5;
1455
- static final int FLOAT3 = 6;
1456
- static final int FLOAT4 = 7;
1457
- static final int INT1VEC = 8;
1458
- static final int INT2VEC = 9;
1459
- static final int INT3VEC = 10;
1460
- static final int INT4VEC = 11;
1461
- static final int FLOAT1VEC = 12;
1462
- static final int FLOAT2VEC = 13;
1463
- static final int FLOAT3VEC = 14;
1464
- static final int FLOAT4VEC = 15;
1465
- static final int MAT2 = 16;
1466
- static final int MAT3 = 17;
1467
- static final int MAT4 = 18;
1468
- static final int SAMPLER2D = 19;
1469
-
1470
- int type;
1471
- Object value;
1472
-
1473
- UniformValue(int type, Object value) {
1474
- this.type = type;
1475
- this.value = value;
1476
- }
1477
- }
1440
+ // ***************************************************************************
1441
+ //
1442
+ // Class to store a user-specified value for a uniform parameter
1443
+ // in the shader
1444
+ protected static class UniformValue {
1445
+
1446
+ static final int INT1 = 0;
1447
+ static final int INT2 = 1;
1448
+ static final int INT3 = 2;
1449
+ static final int INT4 = 3;
1450
+ static final int FLOAT1 = 4;
1451
+ static final int FLOAT2 = 5;
1452
+ static final int FLOAT3 = 6;
1453
+ static final int FLOAT4 = 7;
1454
+ static final int INT1VEC = 8;
1455
+ static final int INT2VEC = 9;
1456
+ static final int INT3VEC = 10;
1457
+ static final int INT4VEC = 11;
1458
+ static final int FLOAT1VEC = 12;
1459
+ static final int FLOAT2VEC = 13;
1460
+ static final int FLOAT3VEC = 14;
1461
+ static final int FLOAT4VEC = 15;
1462
+ static final int MAT2 = 16;
1463
+ static final int MAT3 = 17;
1464
+ static final int MAT4 = 18;
1465
+ static final int SAMPLER2D = 19;
1466
+
1467
+ int type;
1468
+ Object value;
1469
+
1470
+ UniformValue(int type, Object value) {
1471
+ this.type = type;
1472
+ this.value = value;
1473
+ }
1474
+ }
1478
1475
  }