propane 3.9.0-java → 3.10.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +2 -2
  4. data/README.md +3 -3
  5. data/Rakefile +6 -6
  6. data/lib/java/japplemenubar/JAppleMenuBar.java +88 -0
  7. data/lib/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  8. data/lib/java/monkstone/ColorUtil.java +127 -0
  9. data/lib/java/monkstone/MathToolModule.java +287 -0
  10. data/lib/java/monkstone/PropaneLibrary.java +46 -0
  11. data/lib/java/monkstone/core/LibraryProxy.java +136 -0
  12. data/lib/java/monkstone/fastmath/DegLutTables.java +111 -0
  13. data/lib/java/monkstone/fastmath/Deglut.java +71 -0
  14. data/lib/java/monkstone/fastmath/package-info.java +6 -0
  15. data/lib/java/monkstone/filechooser/Chooser.java +39 -0
  16. data/{src/main → lib}/java/monkstone/noise/FastTerrain.java +0 -0
  17. data/{src/main → lib}/java/monkstone/noise/Noise.java +0 -0
  18. data/{src/main → lib}/java/monkstone/noise/NoiseGenerator.java +0 -0
  19. data/{src/main → lib}/java/monkstone/noise/NoiseMode.java +0 -0
  20. data/lib/java/monkstone/noise/OpenSimplex2F.java +881 -0
  21. data/lib/java/monkstone/noise/OpenSimplex2S.java +1106 -0
  22. data/{src/main → lib}/java/monkstone/noise/SmoothTerrain.java +0 -0
  23. data/lib/java/monkstone/slider/CustomHorizontalSlider.java +164 -0
  24. data/lib/java/monkstone/slider/CustomVerticalSlider.java +178 -0
  25. data/lib/java/monkstone/slider/SimpleHorizontalSlider.java +145 -0
  26. data/lib/java/monkstone/slider/SimpleSlider.java +166 -0
  27. data/lib/java/monkstone/slider/SimpleVerticalSlider.java +157 -0
  28. data/lib/java/monkstone/slider/Slider.java +61 -0
  29. data/lib/java/monkstone/slider/SliderBar.java +245 -0
  30. data/lib/java/monkstone/slider/SliderGroup.java +56 -0
  31. data/lib/java/monkstone/slider/WheelHandler.java +35 -0
  32. data/lib/java/monkstone/vecmath/GfxRender.java +86 -0
  33. data/lib/java/monkstone/vecmath/JRender.java +56 -0
  34. data/lib/java/monkstone/vecmath/ShapeRender.java +87 -0
  35. data/lib/java/monkstone/vecmath/package-info.java +20 -0
  36. data/lib/java/monkstone/vecmath/vec2/Vec2.java +802 -0
  37. data/lib/java/monkstone/vecmath/vec2/package-info.java +6 -0
  38. data/lib/java/monkstone/vecmath/vec3/Vec3.java +727 -0
  39. data/lib/java/monkstone/vecmath/vec3/package-info.java +6 -0
  40. data/lib/java/monkstone/videoevent/CaptureEvent.java +27 -0
  41. data/lib/java/monkstone/videoevent/MovieEvent.java +32 -0
  42. data/lib/java/monkstone/videoevent/package-info.java +20 -0
  43. data/lib/java/processing/awt/PGraphicsJava2D.java +3040 -0
  44. data/lib/java/processing/awt/PImageAWT.java +377 -0
  45. data/lib/java/processing/awt/PShapeJava2D.java +387 -0
  46. data/lib/java/processing/awt/PSurfaceAWT.java +1581 -0
  47. data/lib/java/processing/awt/ShimAWT.java +581 -0
  48. data/lib/java/processing/core/PApplet.java +15156 -0
  49. data/lib/java/processing/core/PConstants.java +523 -0
  50. data/lib/java/processing/core/PFont.java +1126 -0
  51. data/lib/java/processing/core/PGraphics.java +8600 -0
  52. data/lib/java/processing/core/PImage.java +3377 -0
  53. data/lib/java/processing/core/PMatrix.java +208 -0
  54. data/lib/java/processing/core/PMatrix2D.java +562 -0
  55. data/lib/java/processing/core/PMatrix3D.java +890 -0
  56. data/lib/java/processing/core/PShape.java +3561 -0
  57. data/lib/java/processing/core/PShapeOBJ.java +483 -0
  58. data/lib/java/processing/core/PShapeSVG.java +2016 -0
  59. data/lib/java/processing/core/PStyle.java +63 -0
  60. data/lib/java/processing/core/PSurface.java +198 -0
  61. data/lib/java/processing/core/PSurfaceNone.java +431 -0
  62. data/lib/java/processing/core/PVector.java +1066 -0
  63. data/lib/java/processing/core/ThinkDifferent.java +115 -0
  64. data/lib/java/processing/data/DoubleDict.java +850 -0
  65. data/lib/java/processing/data/DoubleList.java +928 -0
  66. data/lib/java/processing/data/FloatDict.java +847 -0
  67. data/lib/java/processing/data/FloatList.java +936 -0
  68. data/lib/java/processing/data/IntDict.java +807 -0
  69. data/lib/java/processing/data/IntList.java +936 -0
  70. data/lib/java/processing/data/JSONArray.java +1260 -0
  71. data/lib/java/processing/data/JSONObject.java +2282 -0
  72. data/lib/java/processing/data/JSONTokener.java +435 -0
  73. data/lib/java/processing/data/LongDict.java +802 -0
  74. data/lib/java/processing/data/LongList.java +937 -0
  75. data/lib/java/processing/data/Sort.java +46 -0
  76. data/lib/java/processing/data/StringDict.java +613 -0
  77. data/lib/java/processing/data/StringList.java +800 -0
  78. data/lib/java/processing/data/Table.java +4936 -0
  79. data/lib/java/processing/data/TableRow.java +198 -0
  80. data/lib/java/processing/data/XML.java +1156 -0
  81. data/lib/java/processing/dxf/RawDXF.java +404 -0
  82. data/lib/java/processing/event/Event.java +125 -0
  83. data/lib/java/processing/event/KeyEvent.java +70 -0
  84. data/lib/java/processing/event/MouseEvent.java +114 -0
  85. data/lib/java/processing/event/TouchEvent.java +57 -0
  86. data/lib/java/processing/javafx/PGraphicsFX2D.java +32 -0
  87. data/lib/java/processing/javafx/PSurfaceFX.java +173 -0
  88. data/lib/java/processing/net/Client.java +744 -0
  89. data/lib/java/processing/net/Server.java +388 -0
  90. data/lib/java/processing/opengl/FontTexture.java +378 -0
  91. data/lib/java/processing/opengl/FrameBuffer.java +513 -0
  92. data/lib/java/processing/opengl/LinePath.java +627 -0
  93. data/lib/java/processing/opengl/LineStroker.java +681 -0
  94. data/lib/java/processing/opengl/PGL.java +3483 -0
  95. data/lib/java/processing/opengl/PGraphics2D.java +615 -0
  96. data/lib/java/processing/opengl/PGraphics3D.java +281 -0
  97. data/lib/java/processing/opengl/PGraphicsOpenGL.java +13753 -0
  98. data/lib/java/processing/opengl/PJOGL.java +2008 -0
  99. data/lib/java/processing/opengl/PShader.java +1484 -0
  100. data/lib/java/processing/opengl/PShapeOpenGL.java +5269 -0
  101. data/lib/java/processing/opengl/PSurfaceJOGL.java +1385 -0
  102. data/lib/java/processing/opengl/Texture.java +1696 -0
  103. data/lib/java/processing/opengl/VertexBuffer.java +88 -0
  104. data/lib/java/processing/opengl/cursors/arrow.png +0 -0
  105. data/lib/java/processing/opengl/cursors/cross.png +0 -0
  106. data/lib/java/processing/opengl/cursors/hand.png +0 -0
  107. data/lib/java/processing/opengl/cursors/license.txt +27 -0
  108. data/lib/java/processing/opengl/cursors/move.png +0 -0
  109. data/lib/java/processing/opengl/cursors/text.png +0 -0
  110. data/lib/java/processing/opengl/cursors/wait.png +0 -0
  111. data/lib/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
  112. data/lib/java/processing/opengl/shaders/ColorVert.glsl +34 -0
  113. data/lib/java/processing/opengl/shaders/LightFrag.glsl +33 -0
  114. data/lib/java/processing/opengl/shaders/LightVert.glsl +151 -0
  115. data/lib/java/processing/opengl/shaders/LineFrag.glsl +32 -0
  116. data/lib/java/processing/opengl/shaders/LineVert.glsl +100 -0
  117. data/lib/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
  118. data/lib/java/processing/opengl/shaders/PointFrag.glsl +32 -0
  119. data/lib/java/processing/opengl/shaders/PointVert.glsl +56 -0
  120. data/lib/java/processing/opengl/shaders/TexFrag.glsl +37 -0
  121. data/lib/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
  122. data/lib/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
  123. data/lib/java/processing/opengl/shaders/TexVert.glsl +38 -0
  124. data/lib/java/processing/pdf/PGraphicsPDF.java +581 -0
  125. data/lib/java/processing/svg/PGraphicsSVG.java +378 -0
  126. data/lib/propane/app.rb +8 -13
  127. data/lib/propane/version.rb +1 -1
  128. data/mvnw +3 -3
  129. data/mvnw.cmd +2 -2
  130. data/pom.rb +7 -2
  131. data/pom.xml +14 -2
  132. data/propane.gemspec +2 -2
  133. data/src/main/java/monkstone/FastNoiseModuleJava.java +127 -0
  134. data/src/main/java/monkstone/MathToolModule.java +30 -30
  135. data/src/main/java/monkstone/PropaneLibrary.java +2 -0
  136. data/src/main/java/monkstone/SmoothNoiseModuleJava.java +127 -0
  137. data/src/main/java/monkstone/fastmath/DegLutTables.java +15 -15
  138. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  139. data/src/main/java/monkstone/noise/OpenSimplex2F.java +752 -820
  140. data/src/main/java/monkstone/noise/OpenSimplex2S.java +1138 -1106
  141. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  142. data/src/main/java/monkstone/vecmath/JRender.java +6 -6
  143. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +20 -19
  144. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +12 -12
  145. data/src/main/java/processing/awt/PGraphicsJava2D.java +11 -3
  146. data/src/main/java/processing/core/PApplet.java +89 -89
  147. data/src/main/java/processing/core/PConstants.java +155 -163
  148. data/src/main/java/processing/opengl/PJOGL.java +6 -5
  149. data/vendors/Rakefile +1 -1
  150. metadata +136 -19
@@ -0,0 +1,1484 @@
1
+ /* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2
+
3
+ /*
4
+ Part of the Processing project - http://processing.org
5
+
6
+ Copyright (c) 2012-15 The Processing Foundation
7
+ Copyright (c) 2004-12 Ben Fry and Casey Reas
8
+ Copyright (c) 2001-04 Massachusetts Institute of Technology
9
+
10
+ This library is free software; you can redistribute it and/or
11
+ modify it under the terms of the GNU Lesser General Public
12
+ License as published by the Free Software Foundation, version 2.1.
13
+
14
+ This library is distributed in the hope that it will be useful,
15
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
16
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17
+ Lesser General Public License for more details.
18
+
19
+ You should have received a copy of the GNU Lesser General
20
+ Public License along with this library; if not, write to the
21
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
22
+ Boston, MA 02111-1307 USA
23
+ */
24
+
25
+ package processing.opengl;
26
+
27
+ import processing.core.*;
28
+ import processing.opengl.PGraphicsOpenGL.GLResourceShader;
29
+
30
+ import java.net.URL;
31
+ import java.nio.FloatBuffer;
32
+ import java.nio.IntBuffer;
33
+ import java.util.HashMap;
34
+
35
+ /**
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:
39
+ * http://processing.org/discourse/beta/num_1159494801.html
40
+ *
41
+ * @webref rendering:shaders
42
+ */
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
+ }
361
+
362
+
363
+ /**
364
+ * Unbinds the shader program.
365
+ */
366
+ public void unbind() {
367
+ if (hasType()) unbindTyped();
368
+
369
+ if (bound) {
370
+ unbindTextures();
371
+ pgl.useProgram(0);
372
+ bound = false;
373
+ }
374
+ }
375
+
376
+
377
+ /**
378
+ * Returns true if the shader is bound, false otherwise.
379
+ */
380
+ public boolean bound() {
381
+ return bound;
382
+ }
383
+
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
+ }
393
+
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
+ }
400
+
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
+ }
407
+
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
+ }
414
+
415
+
416
+ public void set(String name, float x) {
417
+ setUniformImpl(name, UniformValue.FLOAT1, new float[] { x });
418
+ }
419
+
420
+
421
+ public void set(String name, float x, float y) {
422
+ setUniformImpl(name, UniformValue.FLOAT2, new float[] { x, y });
423
+ }
424
+
425
+
426
+ public void set(String name, float x, float y, float z) {
427
+ setUniformImpl(name, UniformValue.FLOAT3, new float[] { x, y, z });
428
+ }
429
+
430
+
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
+ }
434
+
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
+ }
442
+
443
+
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
+ }
538
+
539
+
540
+ public void set(String name, PMatrix3D mat) {
541
+ set(name, mat, false);
542
+ }
543
+
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);
559
+ }
560
+ }
561
+
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
+ }
568
+
569
+
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
+ }
578
+
579
+
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
+ }
586
+
587
+
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
+ }
598
+
599
+
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
+ }
610
+
611
+
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);
617
+ }
618
+ }
619
+
620
+
621
+ protected void setUniformValue(int loc, int x) {
622
+ if (-1 < loc) {
623
+ pgl.uniform1i(loc, x);
624
+ }
625
+ }
626
+
627
+
628
+ protected void setUniformValue(int loc, int x, int y) {
629
+ if (-1 < loc) {
630
+ pgl.uniform2i(loc, x, y);
631
+ }
632
+ }
633
+
634
+
635
+ protected void setUniformValue(int loc, int x, int y, int z) {
636
+ if (-1 < loc) {
637
+ pgl.uniform3i(loc, x, y, z);
638
+ }
639
+ }
640
+
641
+
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);
645
+ }
646
+ }
647
+
648
+
649
+ protected void setUniformValue(int loc, float x) {
650
+ if (-1 < loc) {
651
+ pgl.uniform1f(loc, x);
652
+ }
653
+ }
654
+
655
+ protected void setUniformValue(int loc, float x, float y) {
656
+ if (-1 < loc) {
657
+ pgl.uniform2f(loc, x, y);
658
+ }
659
+ }
660
+
661
+
662
+ protected void setUniformValue(int loc, float x, float y, float z) {
663
+ if (-1 < loc) {
664
+ pgl.uniform3f(loc, x, y, z);
665
+ }
666
+ }
667
+
668
+
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);
672
+ }
673
+ }
674
+
675
+
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);
868
+ }
869
+ }
870
+ }
871
+ }
872
+
873
+
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);
884
+ }
885
+ });
886
+ pgl.activeTexture(PGL.TEXTURE0);
887
+ }
888
+ }
889
+
890
+
891
+ public void init() {
892
+ if (glProgram == 0 || contextIsOutdated()) {
893
+ create();
894
+ if (compile()) {
895
+ pgl.attachShader(glProgram, glVertex);
896
+ pgl.attachShader(glProgram, glFragment);
897
+
898
+ setup();
899
+
900
+ pgl.linkProgram(glProgram);
901
+
902
+ validate();
903
+ }
904
+ }
905
+ }
906
+
907
+
908
+ protected void create() {
909
+ context = pgl.getCurrentContext();
910
+ glres = new GLResourceShader(this);
911
+ }
912
+
913
+
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");
920
+ }
921
+
922
+ boolean fragRes = true;
923
+ if (hasFragmentShader()) {
924
+ fragRes = compileFragmentShader();
925
+ } else {
926
+ PGraphics.showException("Doesn't have a fragment shader");
927
+ }
928
+
929
+ return vertRes && fragRes;
930
+ }
931
+
932
+
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));
939
+ }
940
+
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));
947
+ }
948
+ }
949
+
950
+
951
+ protected boolean contextIsOutdated() {
952
+ boolean outdated = !pgl.contextIsCurrent(context);
953
+ if (outdated) {
954
+ dispose();
955
+ }
956
+ return outdated;
957
+ }
958
+
959
+
960
+
961
+ protected boolean hasVertexShader() {
962
+ return vertexShaderSource != null && 0 < vertexShaderSource.length;
963
+ }
964
+
965
+
966
+ protected boolean hasFragmentShader() {
967
+ return fragmentShaderSource != null && 0 < fragmentShaderSource.length;
968
+ }
969
+
970
+
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);
977
+
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;
986
+ }
987
+ }
988
+
989
+
990
+ /**
991
+ * @return
992
+ */
993
+ protected boolean compileFragmentShader() {
994
+ pgl.shaderSource(glFragment, PApplet.join(fragmentShaderSource, "\n"));
995
+ pgl.compileShader(glFragment);
996
+
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;
1005
+ }
1006
+ }
1007
+
1008
+
1009
+ protected void dispose() {
1010
+ if (glres != null) {
1011
+ glres.dispose();
1012
+ glVertex = 0;
1013
+ glFragment = 0;
1014
+ glProgram = 0;
1015
+ glres = null;
1016
+ }
1017
+ }
1018
+
1019
+
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;
1049
+ }
1050
+ return defaultType;
1051
+ }
1052
+
1053
+
1054
+ // ***************************************************************************
1055
+ //
1056
+ // Processing specific
1057
+
1058
+
1059
+ protected int getType() {
1060
+ return type;
1061
+ }
1062
+
1063
+
1064
+ protected void setType(int type) {
1065
+ this.type = type;
1066
+ }
1067
+
1068
+
1069
+ protected boolean hasType() {
1070
+ return POINT <= type && type <= TEXLIGHT;
1071
+ }
1072
+
1073
+
1074
+ protected boolean isPointShader() {
1075
+ return type == POINT;
1076
+ }
1077
+
1078
+
1079
+ protected boolean isLineShader() {
1080
+ return type == LINE;
1081
+ }
1082
+
1083
+
1084
+ protected boolean isPolyShader() {
1085
+ return POLY <= type && type <= TEXLIGHT;
1086
+ }
1087
+
1088
+
1089
+ protected boolean checkPolyType(int type) {
1090
+ if (getType() == PShader.POLY) return true;
1091
+
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;
1110
+ }
1111
+
1112
+ return true;
1113
+ }
1114
+
1115
+
1116
+ protected int getLastTexUnit() {
1117
+ return texUnits == null ? -1 : texUnits.size() - 1;
1118
+ }
1119
+
1120
+
1121
+ protected void setRenderer(PGraphicsOpenGL pg) {
1122
+ this.currentPG = pg;
1123
+ }
1124
+
1125
+
1126
+ protected void loadAttributes() {
1127
+ if (loadedAttributes) return;
1128
+
1129
+ vertexLoc = getAttributeLoc("vertex");
1130
+ if (vertexLoc == -1) vertexLoc = getAttributeLoc("position");
1131
+
1132
+ colorLoc = getAttributeLoc("color");
1133
+ texCoordLoc = getAttributeLoc("texCoord");
1134
+ normalLoc = getAttributeLoc("normal");
1135
+
1136
+ ambientLoc = getAttributeLoc("ambient");
1137
+ specularLoc = getAttributeLoc("specular");
1138
+ emissiveLoc = getAttributeLoc("emissive");
1139
+ shininessLoc = getAttributeLoc("shininess");
1140
+
1141
+ directionLoc = getAttributeLoc("direction");
1142
+
1143
+ offsetLoc = getAttributeLoc("offset");
1144
+
1145
+ directionLoc = getAttributeLoc("direction");
1146
+ offsetLoc = getAttributeLoc("offset");
1147
+
1148
+ loadedAttributes = true;
1149
+ }
1150
+
1151
+
1152
+ protected void loadUniforms() {
1153
+ if (loadedUniforms) return;
1154
+ transformMatLoc = getUniformLoc("transform");
1155
+ if (transformMatLoc == -1)
1156
+ transformMatLoc = getUniformLoc("transformMatrix");
1157
+
1158
+ modelviewMatLoc = getUniformLoc("modelview");
1159
+ if (modelviewMatLoc == -1)
1160
+ modelviewMatLoc = getUniformLoc("modelviewMatrix");
1161
+
1162
+ projectionMatLoc = getUniformLoc("projection");
1163
+ if (projectionMatLoc == -1)
1164
+ projectionMatLoc = getUniformLoc("projectionMatrix");
1165
+
1166
+ viewportLoc = getUniformLoc("viewport");
1167
+ resolutionLoc = getUniformLoc("resolution");
1168
+ ppixelsLoc = getUniformLoc("ppixels");
1169
+
1170
+ normalMatLoc = getUniformLoc("normalMatrix");
1171
+
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");
1180
+
1181
+ textureLoc = getUniformLoc("texture");
1182
+ if (textureLoc == -1) {
1183
+ textureLoc = getUniformLoc("texMap");
1184
+ }
1185
+
1186
+ texMatrixLoc = getUniformLoc("texMatrix");
1187
+ texOffsetLoc = getUniformLoc("texOffset");
1188
+
1189
+ perspectiveLoc = getUniformLoc("perspective");
1190
+ scaleLoc = getUniformLoc("scale");
1191
+ loadedUniforms = true;
1192
+ }
1193
+
1194
+
1195
+ protected void setCommonUniforms() {
1196
+ if (-1 < transformMatLoc) {
1197
+ currentPG.updateGLProjmodelview();
1198
+ setUniformMatrix(transformMatLoc, currentPG.glProjmodelview);
1199
+ }
1200
+
1201
+ if (-1 < modelviewMatLoc) {
1202
+ currentPG.updateGLModelview();
1203
+ setUniformMatrix(modelviewMatLoc, currentPG.glModelview);
1204
+ }
1205
+
1206
+ if (-1 < projectionMatLoc) {
1207
+ currentPG.updateGLProjection();
1208
+ setUniformMatrix(projectionMatLoc, currentPG.glProjection);
1209
+ }
1210
+
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);
1217
+ }
1218
+
1219
+ if (-1 < resolutionLoc) {
1220
+ float w = currentPG.viewport.get(2);
1221
+ float h = currentPG.viewport.get(3);
1222
+ setUniformValue(resolutionLoc, w, h);
1223
+ }
1224
+
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;
1232
+ }
1233
+ }
1234
+
1235
+
1236
+ protected void bindTyped() {
1237
+ if (currentPG == null) {
1238
+ setRenderer(primaryPG.getCurrentPG());
1239
+ loadAttributes();
1240
+ loadUniforms();
1241
+ }
1242
+ setCommonUniforms();
1243
+
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);
1248
+
1249
+ if (-1 < normalMatLoc) {
1250
+ currentPG.updateGLNormal();
1251
+ setUniformMatrix(normalMatLoc, currentPG.glNormal);
1252
+ }
1253
+
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);
1270
+ }
1271
+
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
+ }
1283
+ }
1284
+
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
+ }
1296
+ }
1297
+ }
1298
+
1299
+ protected void unbindTyped() {
1300
+ if (-1 < offsetLoc) pgl.disableVertexAttribArray(offsetLoc);
1301
+
1302
+ if (-1 < directionLoc) pgl.disableVertexAttribArray(directionLoc);
1303
+
1304
+ if (-1 < textureLoc && texture != null) {
1305
+ pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1306
+ texture.unbind();
1307
+ pgl.activeTexture(PGL.TEXTURE0);
1308
+ texture = null;
1309
+ }
1310
+
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);
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
+ }
1484
+ }