propane 3.4.0-java → 3.7.0.pre-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (155) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -2
  3. data/.mvn/wrapper/MavenWrapperDownloader.java +2 -2
  4. data/.mvn/wrapper/maven-wrapper.properties +2 -2
  5. data/.travis.yml +2 -2
  6. data/CHANGELOG.md +12 -0
  7. data/Gemfile +2 -0
  8. data/README.md +17 -8
  9. data/Rakefile +10 -11
  10. data/bin/propane +3 -1
  11. data/lib/propane.rb +6 -4
  12. data/lib/propane/app.rb +20 -10
  13. data/lib/propane/creators/sketch_class.rb +7 -1
  14. data/lib/propane/creators/sketch_factory.rb +4 -2
  15. data/lib/propane/creators/sketch_writer.rb +1 -0
  16. data/lib/propane/helper_methods.rb +23 -24
  17. data/lib/propane/helpers/numeric.rb +2 -0
  18. data/lib/propane/helpers/version_error.rb +1 -0
  19. data/lib/propane/library.rb +5 -1
  20. data/lib/propane/library_loader.rb +2 -0
  21. data/lib/propane/native_folder.rb +10 -9
  22. data/lib/propane/native_loader.rb +3 -0
  23. data/lib/propane/runner.rb +14 -6
  24. data/lib/propane/version.rb +2 -1
  25. data/library/boids/boids.rb +21 -11
  26. data/library/color_group/color_group.rb +28 -0
  27. data/library/control_panel/control_panel.rb +8 -5
  28. data/library/dxf/dxf.rb +6 -0
  29. data/library/file_chooser/chooser.rb +10 -9
  30. data/library/file_chooser/file_chooser.rb +10 -9
  31. data/library/library_proxy/library_proxy.rb +2 -0
  32. data/library/net/net.rb +7 -0
  33. data/library/simplex_noise/simplex_noise.rb +2 -0
  34. data/library/slider/slider.rb +23 -22
  35. data/library/vector_utils/vector_utils.rb +4 -0
  36. data/library/video_event/video_event.rb +4 -1
  37. data/pom.rb +37 -36
  38. data/pom.xml +7 -7
  39. data/propane.gemspec +16 -12
  40. data/src/main/java/monkstone/ColorUtil.java +13 -1
  41. data/src/main/java/monkstone/MathToolModule.java +253 -203
  42. data/src/main/java/monkstone/PropaneLibrary.java +2 -2
  43. data/src/main/java/monkstone/fastmath/Deglut.java +1 -1
  44. data/src/main/java/monkstone/filechooser/Chooser.java +2 -1
  45. data/src/main/java/monkstone/noise/SimplexNoise.java +2 -2
  46. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
  47. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  48. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +1 -1
  49. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
  50. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  51. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  52. data/src/main/java/monkstone/slider/WheelHandler.java +7 -6
  53. data/src/main/java/monkstone/vecmath/package-info.java +1 -1
  54. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +1 -1
  55. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +3 -3
  56. data/src/main/java/monkstone/videoevent/CaptureEvent.java +27 -0
  57. data/src/main/java/monkstone/videoevent/{VideoInterface.java → MovieEvent.java} +11 -27
  58. data/src/main/java/monkstone/videoevent/package-info.java +1 -1
  59. data/src/main/java/processing/awt/PGraphicsJava2D.java +781 -285
  60. data/src/main/java/processing/awt/PImageAWT.java +377 -0
  61. data/src/main/java/processing/awt/PShapeJava2D.java +56 -52
  62. data/src/main/java/processing/awt/PSurfaceAWT.java +309 -209
  63. data/src/main/java/processing/awt/ShimAWT.java +581 -0
  64. data/src/main/java/processing/core/PApplet.java +4510 -4503
  65. data/src/main/java/processing/core/PConstants.java +477 -447
  66. data/src/main/java/processing/core/PFont.java +914 -880
  67. data/src/main/java/processing/core/PGraphics.java +193 -177
  68. data/src/main/java/processing/core/PImage.java +611 -309
  69. data/src/main/java/processing/core/PMatrix.java +172 -159
  70. data/src/main/java/processing/core/PMatrix2D.java +478 -415
  71. data/src/main/java/processing/core/PMatrix3D.java +762 -735
  72. data/src/main/java/processing/core/PShape.java +2888 -2652
  73. data/src/main/java/processing/core/PShapeOBJ.java +97 -92
  74. data/src/main/java/processing/core/PShapeSVG.java +1705 -1490
  75. data/src/main/java/processing/core/PStyle.java +40 -37
  76. data/src/main/java/processing/core/PSurface.java +139 -97
  77. data/src/main/java/processing/core/PSurfaceNone.java +296 -218
  78. data/src/main/java/processing/core/PVector.java +997 -965
  79. data/src/main/java/processing/core/ThinkDifferent.java +15 -13
  80. data/src/main/java/processing/data/DoubleDict.java +756 -710
  81. data/src/main/java/processing/data/DoubleList.java +749 -696
  82. data/src/main/java/processing/data/FloatDict.java +748 -702
  83. data/src/main/java/processing/data/FloatList.java +751 -697
  84. data/src/main/java/processing/data/IntDict.java +720 -673
  85. data/src/main/java/processing/data/IntList.java +699 -633
  86. data/src/main/java/processing/data/JSONArray.java +931 -873
  87. data/src/main/java/processing/data/JSONObject.java +1262 -1165
  88. data/src/main/java/processing/data/JSONTokener.java +351 -341
  89. data/src/main/java/processing/data/LongDict.java +710 -663
  90. data/src/main/java/processing/data/LongList.java +701 -635
  91. data/src/main/java/processing/data/Sort.java +37 -41
  92. data/src/main/java/processing/data/StringDict.java +525 -486
  93. data/src/main/java/processing/data/StringList.java +626 -580
  94. data/src/main/java/processing/data/Table.java +3690 -3510
  95. data/src/main/java/processing/data/TableRow.java +182 -183
  96. data/src/main/java/processing/data/XML.java +957 -883
  97. data/src/main/java/processing/dxf/RawDXF.java +404 -0
  98. data/src/main/java/processing/event/Event.java +87 -67
  99. data/src/main/java/processing/event/KeyEvent.java +48 -41
  100. data/src/main/java/processing/event/MouseEvent.java +88 -113
  101. data/src/main/java/processing/event/TouchEvent.java +10 -6
  102. data/src/main/java/processing/javafx/PGraphicsFX2D.java +20 -345
  103. data/src/main/java/processing/javafx/PSurfaceFX.java +149 -121
  104. data/src/main/java/processing/net/Client.java +744 -0
  105. data/src/main/java/processing/net/Server.java +388 -0
  106. data/src/main/java/processing/opengl/FontTexture.java +289 -270
  107. data/src/main/java/processing/opengl/FrameBuffer.java +386 -364
  108. data/src/main/java/processing/opengl/LinePath.java +547 -500
  109. data/src/main/java/processing/opengl/LineStroker.java +588 -581
  110. data/src/main/java/processing/opengl/PGL.java +3047 -2914
  111. data/src/main/java/processing/opengl/PGraphics2D.java +408 -315
  112. data/src/main/java/processing/opengl/PGraphics3D.java +107 -72
  113. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +12378 -12075
  114. data/src/main/java/processing/opengl/PJOGL.java +1753 -1670
  115. data/src/main/java/processing/opengl/PShader.java +1266 -1257
  116. data/src/main/java/processing/opengl/PShapeOpenGL.java +4678 -4580
  117. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1114 -1027
  118. data/src/main/java/processing/opengl/Texture.java +1492 -1401
  119. data/src/main/java/processing/opengl/VertexBuffer.java +57 -55
  120. data/test/create_test.rb +21 -20
  121. data/test/deglut_spec_test.rb +4 -2
  122. data/test/helper_methods_test.rb +49 -20
  123. data/test/math_tool_test.rb +39 -32
  124. data/test/native_folder.rb +47 -0
  125. data/test/respond_to_test.rb +3 -2
  126. data/test/sketches/key_event.rb +2 -2
  127. data/test/sketches/library/my_library/my_library.rb +3 -0
  128. data/test/test_helper.rb +2 -0
  129. data/test/vecmath_spec_test.rb +35 -22
  130. data/vendors/Rakefile +33 -62
  131. metadata +56 -48
  132. data/src/main/java/processing/core/util/image/ImageLoadFacade.java +0 -161
  133. data/src/main/java/processing/core/util/image/ImageSaveFacade.java +0 -169
  134. data/src/main/java/processing/core/util/image/constants/TifConstants.java +0 -45
  135. data/src/main/java/processing/core/util/image/load/AwtImageLoadStrategy.java +0 -80
  136. data/src/main/java/processing/core/util/image/load/Base64StringImageLoadStrategy.java +0 -73
  137. data/src/main/java/processing/core/util/image/load/FallbackImageLoadStrategy.java +0 -70
  138. data/src/main/java/processing/core/util/image/load/ImageIoImageLoadStrategy.java +0 -132
  139. data/src/main/java/processing/core/util/image/load/ImageLoadStrategy.java +0 -48
  140. data/src/main/java/processing/core/util/image/load/ImageLoadUtil.java +0 -45
  141. data/src/main/java/processing/core/util/image/load/TgaImageLoadStrategy.java +0 -255
  142. data/src/main/java/processing/core/util/image/load/TiffImageLoadStrategy.java +0 -98
  143. data/src/main/java/processing/core/util/image/save/ImageSaveStrategy.java +0 -49
  144. data/src/main/java/processing/core/util/image/save/ImageSaveUtil.java +0 -48
  145. data/src/main/java/processing/core/util/image/save/ImageWriterImageSaveStrategy.java +0 -179
  146. data/src/main/java/processing/core/util/image/save/SaveImageException.java +0 -41
  147. data/src/main/java/processing/core/util/image/save/TgaImageSaveStrategy.java +0 -198
  148. data/src/main/java/processing/core/util/image/save/TiffImageSaveStrategy.java +0 -91
  149. data/src/main/java/processing/core/util/image/save/TiffNakedFilenameImageSaveStrategy.java +0 -57
  150. data/src/main/java/processing/core/util/io/InputFactory.java +0 -285
  151. data/src/main/java/processing/core/util/io/PathUtil.java +0 -109
  152. data/src/main/java/processing/opengl/shaders/LightVert-brcm.glsl +0 -154
  153. data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +0 -154
  154. data/src/main/java/processing/opengl/shaders/TexLightVert-brcm.glsl +0 -160
  155. data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +0 -160
@@ -1,3 +1,5 @@
1
+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
1
3
  /*
2
4
  Part of the Processing project - http://processing.org
3
5
 
@@ -18,7 +20,8 @@
18
20
  Public License along with this library; if not, write to the
19
21
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
22
  Boston, MA 02111-1307 USA
21
- */
23
+ */
24
+
22
25
  package processing.opengl;
23
26
 
24
27
  import processing.core.*;
@@ -30,1446 +33,1452 @@ import java.nio.IntBuffer;
30
33
  import java.util.HashMap;
31
34
 
32
35
  /**
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:
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:
36
39
  * http://processing.org/discourse/beta/num_1159494801.html
37
40
  *
38
41
  * @webref rendering:shaders
39
42
  */
40
43
  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
+ }
41
361
 
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
- }
185
362
 
186
- public PShader(PApplet parent) {
187
- this();
188
- this.parent = parent;
189
- primaryPG = (PGraphicsOpenGL) parent.g;
190
- pgl = primaryPG.pgl;
191
- context = pgl.createEmptyContext();
192
- }
363
+ /**
364
+ * Unbinds the shader program.
365
+ */
366
+ public void unbind() {
367
+ if (hasType()) unbindTyped();
193
368
 
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
- }
369
+ if (bound) {
370
+ unbindTextures();
371
+ pgl.useProgram(0);
372
+ bound = false;
233
373
  }
374
+ }
234
375
 
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
- }
272
376
 
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
- }
377
+ /**
378
+ * Returns true if the shader is bound, false otherwise.
379
+ */
380
+ public boolean bound() {
381
+ return bound;
382
+ }
306
383
 
307
- public void setVertexShader(String vertFilename) {
308
- this.vertexFilename = vertFilename;
309
- vertexShaderSource = pgl.loadVertexShader(vertFilename);
310
- }
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
+ }
311
393
 
312
- public void setVertexShader(URL vertURL) {
313
- this.vertexURL = vertURL;
314
- vertexShaderSource = pgl.loadVertexShader(vertURL);
315
- }
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
+ }
316
400
 
317
- public void setVertexShader(String[] vertSource) {
318
- vertexShaderSource = vertSource;
319
- }
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
+ }
320
407
 
321
- public void setFragmentShader(String fragFilename) {
322
- this.fragmentFilename = fragFilename;
323
- fragmentShaderSource = pgl.loadFragmentShader(fragFilename);
324
- }
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
+ }
325
414
 
326
- public void setFragmentShader(URL fragURL) {
327
- this.fragmentURL = fragURL;
328
- fragmentShaderSource = pgl.loadFragmentShader(fragURL);
329
- }
330
415
 
331
- public void setFragmentShader(String[] fragSource) {
332
- fragmentShaderSource = fragSource;
333
- }
416
+ public void set(String name, float x) {
417
+ setUniformImpl(name, UniformValue.FLOAT1, new float[] { x });
418
+ }
334
419
 
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
- }
346
420
 
347
- if (hasType()) {
348
- bindTyped();
349
- }
350
- }
421
+ public void set(String name, float x, float y) {
422
+ setUniformImpl(name, UniformValue.FLOAT2, new float[] { x, y });
423
+ }
351
424
 
352
- /**
353
- * Unbinds the shader program.
354
- */
355
- public void unbind() {
356
- if (hasType()) {
357
- unbindTyped();
358
- }
359
425
 
360
- if (bound) {
361
- unbindTextures();
362
- pgl.useProgram(0);
363
- bound = false;
364
- }
365
- }
426
+ public void set(String name, float x, float y, float z) {
427
+ setUniformImpl(name, UniformValue.FLOAT3, new float[] { x, y, z });
428
+ }
366
429
 
367
- /**
368
- * Returns true if the shader is bound, false otherwise.
369
- */
370
- public boolean bound() {
371
- return bound;
372
- }
373
430
 
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
- }
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
+ }
383
434
 
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
- }
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
+ }
391
442
 
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
- }
399
443
 
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
- }
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
+ }
407
538
 
408
- public void set(String name, float x) {
409
- setUniformImpl(name, UniformValue.FLOAT1, new float[]{x});
410
- }
411
539
 
412
- public void set(String name, float x, float y) {
413
- setUniformImpl(name, UniformValue.FLOAT2, new float[]{x, y});
414
- }
540
+ public void set(String name, PMatrix3D mat) {
541
+ set(name, mat, false);
542
+ }
415
543
 
416
- public void set(String name, float x, float y, float z) {
417
- setUniformImpl(name, UniformValue.FLOAT3, new float[]{x, y, z});
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);
418
559
  }
560
+ }
419
561
 
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
- }
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
+ }
423
568
 
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
- }
432
569
 
433
- public void set(String name, boolean x) {
434
- setUniformImpl(name, UniformValue.INT1, new int[]{(x) ? 1 : 0});
435
- }
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
+ }
436
578
 
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
- }
441
579
 
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
- }
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
+ }
446
586
 
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
- }
451
587
 
452
- public void set(String name, int[] vec) {
453
- set(name, vec, 1);
454
- }
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
+ }
455
598
 
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
- }
475
599
 
476
- public void set(String name, float[] vec) {
477
- set(name, vec, 1);
478
- }
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
+ }
479
610
 
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
- }
495
- }
496
611
 
497
- public void set(String name, boolean[] vec) {
498
- set(name, vec, 1);
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);
499
617
  }
618
+ }
500
619
 
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);
507
- }
508
620
 
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);
621
+ protected void setUniformValue(int loc, int x) {
622
+ if (-1 < loc) {
623
+ pgl.uniform1i(loc, x);
516
624
  }
625
+ }
517
626
 
518
- public void set(String name, PMatrix3D mat) {
519
- set(name, mat, false);
520
- }
521
627
 
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
- }
628
+ protected void setUniformValue(int loc, int x, int y) {
629
+ if (-1 < loc) {
630
+ pgl.uniform2i(loc, x, y);
538
631
  }
632
+ }
539
633
 
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);
546
- }
547
634
 
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() {
635
+ protected void setUniformValue(int loc, int x, int y, int z) {
636
+ if (-1 < loc) {
637
+ pgl.uniform3i(loc, x, y, z);
555
638
  }
639
+ }
556
640
 
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);
562
- }
563
641
 
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);
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);
573
645
  }
646
+ }
574
647
 
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);
584
- }
585
648
 
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
- }
649
+ protected void setUniformValue(int loc, float x) {
650
+ if (-1 < loc) {
651
+ pgl.uniform1f(loc, x);
592
652
  }
653
+ }
593
654
 
594
- protected void setUniformValue(int loc, int x) {
595
- if (-1 < loc) {
596
- pgl.uniform1i(loc, x);
597
- }
655
+ protected void setUniformValue(int loc, float x, float y) {
656
+ if (-1 < loc) {
657
+ pgl.uniform2f(loc, x, y);
598
658
  }
659
+ }
599
660
 
600
- protected void setUniformValue(int loc, int x, int y) {
601
- if (-1 < loc) {
602
- pgl.uniform2i(loc, x, y);
603
- }
604
- }
605
661
 
606
- protected void setUniformValue(int loc, int x, int y, int z) {
607
- if (-1 < loc) {
608
- pgl.uniform3i(loc, x, y, z);
609
- }
662
+ protected void setUniformValue(int loc, float x, float y, float z) {
663
+ if (-1 < loc) {
664
+ pgl.uniform3f(loc, x, y, z);
610
665
  }
666
+ }
611
667
 
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
- }
616
- }
617
668
 
618
- protected void setUniformValue(int loc, float x) {
619
- if (-1 < loc) {
620
- pgl.uniform1f(loc, x);
621
- }
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);
622
672
  }
673
+ }
623
674
 
624
- protected void setUniformValue(int loc, float x, float y) {
625
- if (-1 < loc) {
626
- pgl.uniform2f(loc, x, y);
627
- }
628
- }
629
675
 
630
- protected void setUniformValue(int loc, float x, float y, float z) {
631
- if (-1 < loc) {
632
- pgl.uniform3f(loc, x, y, z);
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<>();
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<>();
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);
633
868
  }
869
+ }
634
870
  }
871
+ }
635
872
 
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);
639
- }
640
- }
641
873
 
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
- }
874
+ protected void unbindTextures() {
875
+ if (textures != null && texUnits != null) {
876
+ textures.keySet().forEach((loc) -> {
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);
655
884
  }
885
+ });
886
+ pgl.activeTexture(PGL.TEXTURE0);
656
887
  }
888
+ }
657
889
 
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
- }
673
890
 
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
- }
891
+ public void init() {
892
+ if (glProgram == 0 || contextIsOutdated()) {
893
+ create();
894
+ if (compile()) {
895
+ pgl.attachShader(glProgram, glVertex);
896
+ pgl.attachShader(glProgram, glFragment);
686
897
 
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
- }
898
+ setup();
698
899
 
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));
704
- }
900
+ pgl.linkProgram(glProgram);
705
901
 
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
- }
902
+ validate();
903
+ }
810
904
  }
905
+ }
811
906
 
812
- protected void updateIntBuffer(int[] vec) {
813
- intBuffer = PGL.updateIntBuffer(intBuffer, vec, false);
814
- }
815
907
 
816
- protected void updateFloatBuffer(float[] vec) {
817
- floatBuffer = PGL.updateFloatBuffer(floatBuffer, vec, false);
818
- }
908
+ protected void create() {
909
+ context = pgl.getCurrentContext();
910
+ glres = new GLResourceShader(this);
911
+ }
819
912
 
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
- }
833
- }
834
913
 
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
- }
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");
849
920
  }
850
921
 
851
- public void init() {
852
- if (glProgram == 0 || contextIsOutdated()) {
853
- create();
854
- if (compile()) {
855
- pgl.attachShader(glProgram, glVertex);
856
- pgl.attachShader(glProgram, glFragment);
857
-
858
- setup();
859
-
860
- pgl.linkProgram(glProgram);
861
-
862
- validate();
863
- }
864
- }
922
+ boolean fragRes = true;
923
+ if (hasFragmentShader()) {
924
+ fragRes = compileFragmentShader();
925
+ } else {
926
+ PGraphics.showException("Doesn't have a fragment shader");
865
927
  }
866
928
 
867
- protected void create() {
868
- context = pgl.getCurrentContext();
869
- glres = new GLResourceShader(this);
870
- }
929
+ return vertRes && fragRes;
930
+ }
871
931
 
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
- }
879
932
 
880
- boolean fragRes = true;
881
- if (hasFragmentShader()) {
882
- fragRes = compileFragmentShader();
883
- } else {
884
- PGraphics.showException("Doesn't have a fragment shader");
885
- }
886
-
887
- return vertRes && fragRes;
933
+ protected void validate() {
934
+ pgl.getProgramiv(glProgram, PGL.LINK_STATUS, intBuffer);
935
+ boolean linked = intBuffer.get(0) != 0;
936
+ if (!linked) {
937
+ PGraphics.showException("Cannot link shader program:\n" +
938
+ pgl.getProgramInfoLog(glProgram));
888
939
  }
889
940
 
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
- }
897
-
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
- }
941
+ pgl.validateProgram(glProgram);
942
+ pgl.getProgramiv(glProgram, PGL.VALIDATE_STATUS, intBuffer);
943
+ boolean validated = intBuffer.get(0) != 0;
944
+ if (!validated) {
945
+ PGraphics.showException("Cannot validate shader program:\n" +
946
+ pgl.getProgramInfoLog(glProgram));
905
947
  }
948
+ }
906
949
 
907
- protected boolean contextIsOutdated() {
908
- boolean outdated = !pgl.contextIsCurrent(context);
909
- if (outdated) {
910
- dispose();
911
- }
912
- return outdated;
913
- }
914
950
 
915
- protected boolean hasVertexShader() {
916
- return vertexShaderSource != null && 0 < vertexShaderSource.length;
951
+ protected boolean contextIsOutdated() {
952
+ boolean outdated = !pgl.contextIsCurrent(context);
953
+ if (outdated) {
954
+ dispose();
917
955
  }
956
+ return outdated;
957
+ }
918
958
 
919
- protected boolean hasFragmentShader() {
920
- return fragmentShaderSource != null && 0 < fragmentShaderSource.length;
921
- }
922
959
 
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
- }
940
960
 
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
- }
961
+ protected boolean hasVertexShader() {
962
+ return vertexShaderSource != null && 0 < vertexShaderSource.length;
963
+ }
958
964
 
959
- protected void dispose() {
960
- if (glres != null) {
961
- glres.dispose();
962
- glVertex = 0;
963
- glFragment = 0;
964
- glProgram = 0;
965
- glres = null;
966
- }
967
- }
968
965
 
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
- }
966
+ protected boolean hasFragmentShader() {
967
+ return fragmentShaderSource != null && 0 < fragmentShaderSource.length;
968
+ }
1003
969
 
1004
- // ***************************************************************************
1005
- //
1006
- // Processing specific
1007
- protected int getType() {
1008
- return type;
1009
- }
1010
970
 
1011
- protected void setType(int type) {
1012
- this.type = type;
1013
- }
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);
1014
977
 
1015
- protected boolean hasType() {
1016
- return POINT <= type && type <= TEXLIGHT;
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;
1017
986
  }
987
+ }
1018
988
 
1019
- protected boolean isPointShader() {
1020
- return type == POINT;
1021
- }
1022
989
 
1023
- protected boolean isLineShader() {
1024
- return type == LINE;
1025
- }
990
+ /**
991
+ * @return
992
+ */
993
+ protected boolean compileFragmentShader() {
994
+ pgl.shaderSource(glFragment, PApplet.join(fragmentShaderSource, "\n"));
995
+ pgl.compileShader(glFragment);
1026
996
 
1027
- protected boolean isPolyShader() {
1028
- return POLY <= type && type <= TEXLIGHT;
997
+ pgl.getShaderiv(glFragment, PGL.COMPILE_STATUS, intBuffer);
998
+ boolean compiled = intBuffer.get(0) != 0;
999
+ if (!compiled) {
1000
+ PGraphics.showException("Cannot compile fragment shader:\n" +
1001
+ pgl.getShaderInfoLog(glFragment));
1002
+ return false;
1003
+ } else {
1004
+ return true;
1029
1005
  }
1006
+ }
1030
1007
 
1031
- protected boolean checkPolyType(int type) {
1032
- if (getType() == PShader.POLY) {
1033
- return true;
1034
- }
1035
1008
 
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
- }
1048
-
1049
- return true;
1009
+ protected void dispose() {
1010
+ if (glres != null) {
1011
+ glres.dispose();
1012
+ glVertex = 0;
1013
+ glFragment = 0;
1014
+ glProgram = 0;
1015
+ glres = null;
1050
1016
  }
1017
+ }
1051
1018
 
1052
- protected int getLastTexUnit() {
1053
- return texUnits == null ? -1 : texUnits.size() - 1;
1054
- }
1055
1019
 
1056
- protected void setRenderer(PGraphicsOpenGL pg) {
1057
- this.currentPG = pg;
1020
+ static protected int getShaderType(String[] source, int defaultType) {
1021
+ for (String source1 : source) {
1022
+ String line = source1.trim();
1023
+ if (PApplet.match(line, colorShaderDefRegexp) != null)
1024
+ return PShader.COLOR;
1025
+ else if (PApplet.match(line, lightShaderDefRegexp) != null)
1026
+ return PShader.LIGHT;
1027
+ else if (PApplet.match(line, texShaderDefRegexp) != null)
1028
+ return PShader.TEXTURE;
1029
+ else if (PApplet.match(line, texlightShaderDefRegexp) != null)
1030
+ return PShader.TEXLIGHT;
1031
+ else if (PApplet.match(line, polyShaderDefRegexp) != null)
1032
+ return PShader.POLY;
1033
+ else if (PApplet.match(line, triShaderAttrRegexp) != null)
1034
+ return PShader.POLY;
1035
+ else if (PApplet.match(line, quadShaderAttrRegexp) != null)
1036
+ return PShader.POLY;
1037
+ else if (PApplet.match(line, pointShaderDefRegexp) != null)
1038
+ return PShader.POINT;
1039
+ else if (PApplet.match(line, lineShaderDefRegexp) != null)
1040
+ return PShader.LINE;
1041
+ else if (PApplet.match(line, pointShaderAttrRegexp) != null)
1042
+ return PShader.POINT;
1043
+ else if (PApplet.match(line, pointShaderInRegexp) != null)
1044
+ return PShader.POINT;
1045
+ else if (PApplet.match(line, lineShaderAttrRegexp) != null)
1046
+ return PShader.LINE;
1047
+ else if (PApplet.match(line, lineShaderInRegexp) != null)
1048
+ return PShader.LINE;
1058
1049
  }
1050
+ return defaultType;
1051
+ }
1059
1052
 
1060
- protected void loadAttributes() {
1061
- if (loadedAttributes) {
1062
- return;
1063
- }
1064
1053
 
1065
- vertexLoc = getAttributeLoc("vertex");
1066
- if (vertexLoc == -1) {
1067
- vertexLoc = getAttributeLoc("position");
1068
- }
1054
+ // ***************************************************************************
1055
+ //
1056
+ // Processing specific
1069
1057
 
1070
- colorLoc = getAttributeLoc("color");
1071
- texCoordLoc = getAttributeLoc("texCoord");
1072
- normalLoc = getAttributeLoc("normal");
1073
1058
 
1074
- ambientLoc = getAttributeLoc("ambient");
1075
- specularLoc = getAttributeLoc("specular");
1076
- emissiveLoc = getAttributeLoc("emissive");
1077
- shininessLoc = getAttributeLoc("shininess");
1059
+ protected int getType() {
1060
+ return type;
1061
+ }
1078
1062
 
1079
- directionLoc = getAttributeLoc("direction");
1080
1063
 
1081
- offsetLoc = getAttributeLoc("offset");
1064
+ protected void setType(int type) {
1065
+ this.type = type;
1066
+ }
1082
1067
 
1083
- directionLoc = getAttributeLoc("direction");
1084
- offsetLoc = getAttributeLoc("offset");
1085
1068
 
1086
- loadedAttributes = true;
1087
- }
1069
+ protected boolean hasType() {
1070
+ return POINT <= type && type <= TEXLIGHT;
1071
+ }
1088
1072
 
1089
- protected void loadUniforms() {
1090
- if (loadedUniforms) {
1091
- return;
1092
- }
1093
- transformMatLoc = getUniformLoc("transform");
1094
- if (transformMatLoc == -1) {
1095
- transformMatLoc = getUniformLoc("transformMatrix");
1096
- }
1097
1073
 
1098
- modelviewMatLoc = getUniformLoc("modelview");
1099
- if (modelviewMatLoc == -1) {
1100
- modelviewMatLoc = getUniformLoc("modelviewMatrix");
1101
- }
1074
+ protected boolean isPointShader() {
1075
+ return type == POINT;
1076
+ }
1102
1077
 
1103
- projectionMatLoc = getUniformLoc("projection");
1104
- if (projectionMatLoc == -1) {
1105
- projectionMatLoc = getUniformLoc("projectionMatrix");
1106
- }
1107
1078
 
1108
- viewportLoc = getUniformLoc("viewport");
1109
- resolutionLoc = getUniformLoc("resolution");
1110
- ppixelsLoc = getUniformLoc("ppixels");
1079
+ protected boolean isLineShader() {
1080
+ return type == LINE;
1081
+ }
1111
1082
 
1112
- normalMatLoc = getUniformLoc("normalMatrix");
1113
1083
 
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");
1084
+ protected boolean isPolyShader() {
1085
+ return POLY <= type && type <= TEXLIGHT;
1086
+ }
1122
1087
 
1123
- textureLoc = getUniformLoc("texture");
1124
- if (textureLoc == -1) {
1125
- textureLoc = getUniformLoc("texMap");
1126
- }
1127
1088
 
1128
- texMatrixLoc = getUniformLoc("texMatrix");
1129
- texOffsetLoc = getUniformLoc("texOffset");
1089
+ protected boolean checkPolyType(int type) {
1090
+ if (getType() == PShader.POLY) return true;
1130
1091
 
1131
- perspectiveLoc = getUniformLoc("perspective");
1132
- scaleLoc = getUniformLoc("scale");
1133
- loadedUniforms = true;
1092
+ if (getType() != type) {
1093
+ switch (type) {
1094
+ case TEXLIGHT:
1095
+ PGraphics.showWarning(PGraphicsOpenGL.NO_TEXLIGHT_SHADER_ERROR);
1096
+ break;
1097
+ case LIGHT:
1098
+ PGraphics.showWarning(PGraphicsOpenGL.NO_LIGHT_SHADER_ERROR);
1099
+ break;
1100
+ case TEXTURE:
1101
+ PGraphics.showWarning(PGraphicsOpenGL.NO_TEXTURE_SHADER_ERROR);
1102
+ break;
1103
+ case COLOR:
1104
+ PGraphics.showWarning(PGraphicsOpenGL.NO_COLOR_SHADER_ERROR);
1105
+ break;
1106
+ default:
1107
+ break;
1108
+ }
1109
+ return false;
1134
1110
  }
1135
1111
 
1136
- protected void setCommonUniforms() {
1137
- if (-1 < transformMatLoc) {
1138
- currentPG.updateGLProjmodelview();
1139
- setUniformMatrix(transformMatLoc, currentPG.glProjmodelview);
1140
- }
1112
+ return true;
1113
+ }
1141
1114
 
1142
- if (-1 < modelviewMatLoc) {
1143
- currentPG.updateGLModelview();
1144
- setUniformMatrix(modelviewMatLoc, currentPG.glModelview);
1145
- }
1146
1115
 
1147
- if (-1 < projectionMatLoc) {
1148
- currentPG.updateGLProjection();
1149
- setUniformMatrix(projectionMatLoc, currentPG.glProjection);
1150
- }
1116
+ protected int getLastTexUnit() {
1117
+ return texUnits == null ? -1 : texUnits.size() - 1;
1118
+ }
1151
1119
 
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
- }
1159
1120
 
1160
- if (-1 < resolutionLoc) {
1161
- float w = currentPG.viewport.get(2);
1162
- float h = currentPG.viewport.get(3);
1163
- setUniformValue(resolutionLoc, w, h);
1164
- }
1121
+ protected void setRenderer(PGraphicsOpenGL pg) {
1122
+ this.currentPG = pg;
1123
+ }
1165
1124
 
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
- }
1175
1125
 
1176
- protected void bindTyped() {
1177
- if (currentPG == null) {
1178
- setRenderer(primaryPG.getCurrentPG());
1179
- loadAttributes();
1180
- loadUniforms();
1181
- }
1182
- setCommonUniforms();
1126
+ protected void loadAttributes() {
1127
+ if (loadedAttributes) return;
1183
1128
 
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
- }
1129
+ vertexLoc = getAttributeLoc("vertex");
1130
+ if (vertexLoc == -1) vertexLoc = getAttributeLoc("position");
1196
1131
 
1197
- if (-1 < normalMatLoc) {
1198
- currentPG.updateGLNormal();
1199
- setUniformMatrix(normalMatLoc, currentPG.glNormal);
1200
- }
1132
+ colorLoc = getAttributeLoc("color");
1133
+ texCoordLoc = getAttributeLoc("texCoord");
1134
+ normalLoc = getAttributeLoc("normal");
1201
1135
 
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
- }
1136
+ ambientLoc = getAttributeLoc("ambient");
1137
+ specularLoc = getAttributeLoc("specular");
1138
+ emissiveLoc = getAttributeLoc("emissive");
1139
+ shininessLoc = getAttributeLoc("shininess");
1214
1140
 
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
- }
1141
+ directionLoc = getAttributeLoc("direction");
1227
1142
 
1228
- if (-1 < directionLoc) {
1229
- pgl.enableVertexAttribArray(directionLoc);
1230
- }
1143
+ offsetLoc = getAttributeLoc("offset");
1231
1144
 
1232
- if (-1 < offsetLoc) {
1233
- pgl.enableVertexAttribArray(offsetLoc);
1234
- }
1145
+ directionLoc = getAttributeLoc("direction");
1146
+ offsetLoc = getAttributeLoc("offset");
1235
1147
 
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
- }
1148
+ loadedAttributes = true;
1149
+ }
1244
1150
 
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
- }
1257
- }
1258
1151
 
1259
- protected void unbindTyped() {
1260
- if (-1 < offsetLoc) {
1261
- pgl.disableVertexAttribArray(offsetLoc);
1262
- }
1152
+ protected void loadUniforms() {
1153
+ if (loadedUniforms) return;
1154
+ transformMatLoc = getUniformLoc("transform");
1155
+ if (transformMatLoc == -1)
1156
+ transformMatLoc = getUniformLoc("transformMatrix");
1263
1157
 
1264
- if (-1 < directionLoc) {
1265
- pgl.disableVertexAttribArray(directionLoc);
1266
- }
1158
+ modelviewMatLoc = getUniformLoc("modelview");
1159
+ if (modelviewMatLoc == -1)
1160
+ modelviewMatLoc = getUniformLoc("modelviewMatrix");
1267
1161
 
1268
- if (-1 < textureLoc && texture != null) {
1269
- pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1270
- texture.unbind();
1271
- pgl.activeTexture(PGL.TEXTURE0);
1272
- texture = null;
1273
- }
1162
+ projectionMatLoc = getUniformLoc("projection");
1163
+ if (projectionMatLoc == -1)
1164
+ projectionMatLoc = getUniformLoc("projectionMatrix");
1274
1165
 
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
- }
1166
+ viewportLoc = getUniformLoc("viewport");
1167
+ resolutionLoc = getUniformLoc("resolution");
1168
+ ppixelsLoc = getUniformLoc("ppixels");
1287
1169
 
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
- }
1170
+ normalMatLoc = getUniformLoc("normalMatrix");
1300
1171
 
1301
- if (-1 < ppixelsLoc) {
1302
- pgl.enableFBOLayer();
1303
- pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
1304
- currentPG.unbindFrontTexture();
1305
- pgl.activeTexture(PGL.TEXTURE0);
1306
- }
1172
+ lightCountLoc = getUniformLoc("lightCount");
1173
+ lightPositionLoc = getUniformLoc("lightPosition");
1174
+ lightNormalLoc = getUniformLoc("lightNormal");
1175
+ lightAmbientLoc = getUniformLoc("lightAmbient");
1176
+ lightDiffuseLoc = getUniformLoc("lightDiffuse");
1177
+ lightSpecularLoc = getUniformLoc("lightSpecular");
1178
+ lightFalloffLoc = getUniformLoc("lightFalloff");
1179
+ lightSpotLoc = getUniformLoc("lightSpot");
1307
1180
 
1308
- pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);
1181
+ textureLoc = getUniformLoc("texture");
1182
+ if (textureLoc == -1) {
1183
+ textureLoc = getUniformLoc("texMap");
1309
1184
  }
1310
1185
 
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
- }
1186
+ texMatrixLoc = getUniformLoc("texMatrix");
1187
+ texOffsetLoc = getUniformLoc("texOffset");
1344
1188
 
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
- }
1367
- }
1189
+ perspectiveLoc = getUniformLoc("perspective");
1190
+ scaleLoc = getUniformLoc("scale");
1191
+ loadedUniforms = true;
1192
+ }
1368
1193
 
1369
- protected boolean supportsTexturing() {
1370
- return -1 < textureLoc;
1371
- }
1372
1194
 
1373
- protected boolean supportLighting() {
1374
- return -1 < lightCountLoc || -1 < lightPositionLoc || -1 < lightNormalLoc;
1195
+ protected void setCommonUniforms() {
1196
+ if (-1 < transformMatLoc) {
1197
+ currentPG.updateGLProjmodelview();
1198
+ setUniformMatrix(transformMatLoc, currentPG.glProjmodelview);
1375
1199
  }
1376
1200
 
1377
- protected boolean accessTexCoords() {
1378
- return -1 < texCoordLoc;
1201
+ if (-1 < modelviewMatLoc) {
1202
+ currentPG.updateGLModelview();
1203
+ setUniformMatrix(modelviewMatLoc, currentPG.glModelview);
1379
1204
  }
1380
1205
 
1381
- protected boolean accessNormals() {
1382
- return -1 < normalLoc;
1206
+ if (-1 < projectionMatLoc) {
1207
+ currentPG.updateGLProjection();
1208
+ setUniformMatrix(projectionMatLoc, currentPG.glProjection);
1383
1209
  }
1384
1210
 
1385
- protected boolean accessLightAttribs() {
1386
- return -1 < ambientLoc || -1 < specularLoc || -1 < emissiveLoc
1387
- || -1 < shininessLoc;
1211
+ if (-1 < viewportLoc) {
1212
+ float x = currentPG.viewport.get(0);
1213
+ float y = currentPG.viewport.get(1);
1214
+ float w = currentPG.viewport.get(2);
1215
+ float h = currentPG.viewport.get(3);
1216
+ setUniformValue(viewportLoc, x, y, w, h);
1388
1217
  }
1389
1218
 
1390
- protected void setVertexAttribute(int vboId, int size, int type,
1391
- int stride, int offset) {
1392
- setAttributeVBO(vertexLoc, vboId, size, type, false, stride, offset);
1219
+ if (-1 < resolutionLoc) {
1220
+ float w = currentPG.viewport.get(2);
1221
+ float h = currentPG.viewport.get(3);
1222
+ setUniformValue(resolutionLoc, w, h);
1393
1223
  }
1394
1224
 
1395
- protected void setColorAttribute(int vboId, int size, int type,
1396
- int stride, int offset) {
1397
- setAttributeVBO(colorLoc, vboId, size, type, true, stride, offset);
1225
+ if (-1 < ppixelsLoc) {
1226
+ ppixelsUnit = getLastTexUnit() + 1;
1227
+ setUniformValue(ppixelsLoc, ppixelsUnit);
1228
+ pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
1229
+ currentPG.bindFrontTexture();
1230
+ } else {
1231
+ ppixelsUnit = -1;
1398
1232
  }
1233
+ }
1399
1234
 
1400
- protected void setNormalAttribute(int vboId, int size, int type,
1401
- int stride, int offset) {
1402
- setAttributeVBO(normalLoc, vboId, size, type, false, stride, offset);
1403
- }
1404
1235
 
1405
- protected void setTexcoordAttribute(int vboId, int size, int type,
1406
- int stride, int offset) {
1407
- setAttributeVBO(texCoordLoc, vboId, size, type, false, stride, offset);
1236
+ protected void bindTyped() {
1237
+ if (currentPG == null) {
1238
+ setRenderer(primaryPG.getCurrentPG());
1239
+ loadAttributes();
1240
+ loadUniforms();
1408
1241
  }
1242
+ setCommonUniforms();
1409
1243
 
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
- }
1244
+ if (-1 < vertexLoc) pgl.enableVertexAttribArray(vertexLoc);
1245
+ if (-1 < colorLoc) pgl.enableVertexAttribArray(colorLoc);
1246
+ if (-1 < texCoordLoc) pgl.enableVertexAttribArray(texCoordLoc);
1247
+ if (-1 < normalLoc) pgl.enableVertexAttribArray(normalLoc);
1414
1248
 
1415
- protected void setSpecularAttribute(int vboId, int size, int type,
1416
- int stride, int offset) {
1417
- setAttributeVBO(specularLoc, vboId, size, type, true, stride, offset);
1249
+ if (-1 < normalMatLoc) {
1250
+ currentPG.updateGLNormal();
1251
+ setUniformMatrix(normalMatLoc, currentPG.glNormal);
1418
1252
  }
1419
1253
 
1420
- protected void setEmissiveAttribute(int vboId, int size, int type,
1421
- int stride, int offset) {
1422
- setAttributeVBO(emissiveLoc, vboId, size, type, true, stride, offset);
1254
+ if (-1 < ambientLoc) pgl.enableVertexAttribArray(ambientLoc);
1255
+ if (-1 < specularLoc) pgl.enableVertexAttribArray(specularLoc);
1256
+ if (-1 < emissiveLoc) pgl.enableVertexAttribArray(emissiveLoc);
1257
+ if (-1 < shininessLoc) pgl.enableVertexAttribArray(shininessLoc);
1258
+
1259
+ int count = currentPG.lightCount;
1260
+ setUniformValue(lightCountLoc, count);
1261
+ if (0 < count) {
1262
+ setUniformVector(lightPositionLoc, currentPG.lightPosition, 4, count);
1263
+ setUniformVector(lightNormalLoc, currentPG.lightNormal, 3, count);
1264
+ setUniformVector(lightAmbientLoc, currentPG.lightAmbient, 3, count);
1265
+ setUniformVector(lightDiffuseLoc, currentPG.lightDiffuse, 3, count);
1266
+ setUniformVector(lightSpecularLoc, currentPG.lightSpecular, 3, count);
1267
+ setUniformVector(lightFalloffLoc, currentPG.lightFalloffCoefficients,
1268
+ 3, count);
1269
+ setUniformVector(lightSpotLoc, currentPG.lightSpotParameters, 2, count);
1423
1270
  }
1424
1271
 
1425
- protected void setShininessAttribute(int vboId, int size, int type,
1426
- int stride, int offset) {
1427
- setAttributeVBO(shininessLoc, vboId, size, type, false, stride, offset);
1272
+ if (-1 < directionLoc) pgl.enableVertexAttribArray(directionLoc);
1273
+
1274
+ if (-1 < offsetLoc) pgl.enableVertexAttribArray(offsetLoc);
1275
+
1276
+ if (-1 < perspectiveLoc) {
1277
+ if (currentPG.getHint(ENABLE_STROKE_PERSPECTIVE) &&
1278
+ currentPG.nonOrthoProjection()) {
1279
+ setUniformValue(perspectiveLoc, 1);
1280
+ } else {
1281
+ setUniformValue(perspectiveLoc, 0);
1282
+ }
1428
1283
  }
1429
1284
 
1430
- protected void setLineAttribute(int vboId, int size, int type,
1431
- int stride, int offset) {
1432
- setAttributeVBO(directionLoc, vboId, size, type, false, stride, offset);
1285
+ if (-1 < scaleLoc) {
1286
+ if (currentPG.getHint(DISABLE_OPTIMIZED_STROKE)) {
1287
+ setUniformValue(scaleLoc, 1.0f, 1.0f, 1.0f);
1288
+ } else {
1289
+ float f = PGL.STROKE_DISPLACEMENT;
1290
+ if (currentPG.orthoProjection()) {
1291
+ setUniformValue(scaleLoc, 1, 1, f);
1292
+ } else {
1293
+ setUniformValue(scaleLoc, f, f, f);
1294
+ }
1295
+ }
1433
1296
  }
1297
+ }
1298
+
1299
+ protected void unbindTyped() {
1300
+ if (-1 < offsetLoc) pgl.disableVertexAttribArray(offsetLoc);
1301
+
1302
+ if (-1 < directionLoc) pgl.disableVertexAttribArray(directionLoc);
1434
1303
 
1435
- protected void setPointAttribute(int vboId, int size, int type,
1436
- int stride, int offset) {
1437
- setAttributeVBO(offsetLoc, vboId, size, type, false, stride, offset);
1304
+ if (-1 < textureLoc && texture != null) {
1305
+ pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1306
+ texture.unbind();
1307
+ pgl.activeTexture(PGL.TEXTURE0);
1308
+ texture = null;
1438
1309
  }
1439
1310
 
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
- }
1311
+ if (-1 < ambientLoc) pgl.disableVertexAttribArray(ambientLoc);
1312
+ if (-1 < specularLoc) pgl.disableVertexAttribArray(specularLoc);
1313
+ if (-1 < emissiveLoc) pgl.disableVertexAttribArray(emissiveLoc);
1314
+ if (-1 < shininessLoc) pgl.disableVertexAttribArray(shininessLoc);
1315
+
1316
+ if (-1 < vertexLoc) pgl.disableVertexAttribArray(vertexLoc);
1317
+ if (-1 < colorLoc) pgl.disableVertexAttribArray(colorLoc);
1318
+ if (-1 < texCoordLoc) pgl.disableVertexAttribArray(texCoordLoc);
1319
+ if (-1 < normalLoc) pgl.disableVertexAttribArray(normalLoc);
1320
+
1321
+ if (-1 < ppixelsLoc) {
1322
+ pgl.enableFBOLayer();
1323
+ pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
1324
+ currentPG.unbindFrontTexture();
1325
+ pgl.activeTexture(PGL.TEXTURE0);
1474
1326
  }
1327
+
1328
+ pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);
1329
+ }
1330
+
1331
+ protected void setTexture(Texture tex) {
1332
+ texture = tex;
1333
+
1334
+ float scaleu = 1;
1335
+ float scalev = 1;
1336
+ float dispu = 0;
1337
+ float dispv = 0;
1338
+
1339
+ if (tex != null) {
1340
+ if (tex.invertedX()) {
1341
+ scaleu = -1;
1342
+ dispu = 1;
1343
+ }
1344
+
1345
+ if (tex.invertedY()) {
1346
+ scalev = -1;
1347
+ dispv = 1;
1348
+ }
1349
+
1350
+ scaleu *= tex.maxTexcoordU();
1351
+ dispu *= tex.maxTexcoordU();
1352
+ scalev *= tex.maxTexcoordV();
1353
+ dispv *= tex.maxTexcoordV();
1354
+
1355
+ setUniformValue(texOffsetLoc, 1.0f / tex.width, 1.0f / tex.height);
1356
+
1357
+ if (-1 < textureLoc) {
1358
+ texUnit = -1 < ppixelsUnit ? ppixelsUnit + 1 : getLastTexUnit() + 1;
1359
+ setUniformValue(textureLoc, texUnit);
1360
+ pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1361
+ tex.bind();
1362
+ }
1363
+ }
1364
+
1365
+ if (-1 < texMatrixLoc) {
1366
+ if (tcmat == null) {
1367
+ tcmat = new float[16];
1368
+ }
1369
+ tcmat[0] = scaleu; tcmat[4] = 0; tcmat[ 8] = 0; tcmat[12] = dispu;
1370
+ tcmat[1] = 0; tcmat[5] = scalev; tcmat[ 9] = 0; tcmat[13] = dispv;
1371
+ tcmat[2] = 0; tcmat[6] = 0; tcmat[10] = 0; tcmat[14] = 0;
1372
+ tcmat[3] = 0; tcmat[7] = 0; tcmat[11] = 0; tcmat[15] = 0;
1373
+ setUniformMatrix(texMatrixLoc, tcmat);
1374
+ }
1375
+ }
1376
+
1377
+
1378
+ protected boolean supportsTexturing() {
1379
+ return -1 < textureLoc;
1380
+ }
1381
+
1382
+ protected boolean supportLighting() {
1383
+ return -1 < lightCountLoc || -1 < lightPositionLoc || -1 < lightNormalLoc;
1384
+ }
1385
+
1386
+ protected boolean accessTexCoords() {
1387
+ return -1 < texCoordLoc;
1388
+ }
1389
+
1390
+ protected boolean accessNormals() {
1391
+ return -1 < normalLoc;
1392
+ }
1393
+
1394
+ protected boolean accessLightAttribs() {
1395
+ return -1 < ambientLoc || -1 < specularLoc || -1 < emissiveLoc ||
1396
+ -1 < shininessLoc;
1397
+ }
1398
+
1399
+ protected void setVertexAttribute(int vboId, int size, int type,
1400
+ int stride, int offset) {
1401
+ setAttributeVBO(vertexLoc, vboId, size, type, false, stride, offset);
1402
+ }
1403
+
1404
+ protected void setColorAttribute(int vboId, int size, int type,
1405
+ int stride, int offset) {
1406
+ setAttributeVBO(colorLoc, vboId, size, type, true, stride, offset);
1407
+ }
1408
+
1409
+ protected void setNormalAttribute(int vboId, int size, int type,
1410
+ int stride, int offset) {
1411
+ setAttributeVBO(normalLoc, vboId, size, type, false, stride, offset);
1412
+ }
1413
+
1414
+ protected void setTexcoordAttribute(int vboId, int size, int type,
1415
+ int stride, int offset) {
1416
+ setAttributeVBO(texCoordLoc, vboId, size, type, false, stride, offset);
1417
+ }
1418
+
1419
+ protected void setAmbientAttribute(int vboId, int size, int type,
1420
+ int stride, int offset) {
1421
+ setAttributeVBO(ambientLoc, vboId, size, type, true, stride, offset);
1422
+ }
1423
+
1424
+ protected void setSpecularAttribute(int vboId, int size, int type,
1425
+ int stride, int offset) {
1426
+ setAttributeVBO(specularLoc, vboId, size, type, true, stride, offset);
1427
+ }
1428
+
1429
+ protected void setEmissiveAttribute(int vboId, int size, int type,
1430
+ int stride, int offset) {
1431
+ setAttributeVBO(emissiveLoc, vboId, size, type, true, stride, offset);
1432
+ }
1433
+
1434
+ protected void setShininessAttribute(int vboId, int size, int type,
1435
+ int stride, int offset) {
1436
+ setAttributeVBO(shininessLoc, vboId, size, type, false, stride, offset);
1437
+ }
1438
+
1439
+ protected void setLineAttribute(int vboId, int size, int type,
1440
+ int stride, int offset) {
1441
+ setAttributeVBO(directionLoc, vboId, size, type, false, stride, offset);
1442
+ }
1443
+
1444
+ protected void setPointAttribute(int vboId, int size, int type,
1445
+ int stride, int offset) {
1446
+ setAttributeVBO(offsetLoc, vboId, size, type, false, stride, offset);
1447
+ }
1448
+
1449
+
1450
+ // ***************************************************************************
1451
+ //
1452
+ // Class to store a user-specified value for a uniform parameter
1453
+ // in the shader
1454
+ protected static class UniformValue {
1455
+ static final int INT1 = 0;
1456
+ static final int INT2 = 1;
1457
+ static final int INT3 = 2;
1458
+ static final int INT4 = 3;
1459
+ static final int FLOAT1 = 4;
1460
+ static final int FLOAT2 = 5;
1461
+ static final int FLOAT3 = 6;
1462
+ static final int FLOAT4 = 7;
1463
+ static final int INT1VEC = 8;
1464
+ static final int INT2VEC = 9;
1465
+ static final int INT3VEC = 10;
1466
+ static final int INT4VEC = 11;
1467
+ static final int FLOAT1VEC = 12;
1468
+ static final int FLOAT2VEC = 13;
1469
+ static final int FLOAT3VEC = 14;
1470
+ static final int FLOAT4VEC = 15;
1471
+ static final int MAT2 = 16;
1472
+ static final int MAT3 = 17;
1473
+ static final int MAT4 = 18;
1474
+ static final int SAMPLER2D = 19;
1475
+
1476
+ int type;
1477
+ Object value;
1478
+
1479
+ UniformValue(int type, Object value) {
1480
+ this.type = type;
1481
+ this.value = value;
1482
+ }
1483
+ }
1475
1484
  }