picrate 0.0.2-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +47 -0
  3. data/.mvn/extensions.xml +9 -0
  4. data/.mvn/wrapper/maven-wrapper.properties +1 -0
  5. data/.travis.yml +10 -0
  6. data/CHANGELOG.md +4 -0
  7. data/LICENSE.md +165 -0
  8. data/README.md +51 -0
  9. data/Rakefile +59 -0
  10. data/bin/picrate +8 -0
  11. data/docs/.gitignore +6 -0
  12. data/docs/_config.yml +30 -0
  13. data/docs/_includes/footer.html +38 -0
  14. data/docs/_includes/head.html +16 -0
  15. data/docs/_includes/header.html +27 -0
  16. data/docs/_includes/icon-github.html +1 -0
  17. data/docs/_includes/icon-github.svg +1 -0
  18. data/docs/_includes/icon-twitter.html +1 -0
  19. data/docs/_includes/icon-twitter.svg +1 -0
  20. data/docs/_includes/navigation.html +24 -0
  21. data/docs/_layouts/default.html +20 -0
  22. data/docs/_layouts/page.html +14 -0
  23. data/docs/_layouts/post.html +15 -0
  24. data/docs/_posts/2018-05-06-getting_started.md +8 -0
  25. data/docs/_posts/2018-05-06-install_jruby.md +35 -0
  26. data/docs/_sass/_base.scss +206 -0
  27. data/docs/_sass/_layout.scss +242 -0
  28. data/docs/_sass/_syntax-highlighting.scss +71 -0
  29. data/docs/about.md +10 -0
  30. data/docs/css/main.scss +38 -0
  31. data/docs/favicon.ico +0 -0
  32. data/docs/feed.xml +30 -0
  33. data/docs/index.html +38 -0
  34. data/lib/picrate.rb +10 -0
  35. data/lib/picrate/app.rb +187 -0
  36. data/lib/picrate/creators/sketch_class.rb +57 -0
  37. data/lib/picrate/creators/sketch_factory.rb +12 -0
  38. data/lib/picrate/creators/sketch_writer.rb +21 -0
  39. data/lib/picrate/helper_methods.rb +214 -0
  40. data/lib/picrate/helpers/numeric.rb +9 -0
  41. data/lib/picrate/library.rb +69 -0
  42. data/lib/picrate/library_loader.rb +53 -0
  43. data/lib/picrate/native_folder.rb +35 -0
  44. data/lib/picrate/native_loader.rb +27 -0
  45. data/lib/picrate/runner.rb +81 -0
  46. data/lib/picrate/version.rb +4 -0
  47. data/library/boids/boids.rb +209 -0
  48. data/library/chooser/chooser.rb +19 -0
  49. data/library/control_panel/control_panel.rb +182 -0
  50. data/library/library_proxy/README.md +99 -0
  51. data/library/library_proxy/library_proxy.rb +14 -0
  52. data/library/slider/slider.rb +42 -0
  53. data/library/vector_utils/vector_utils.rb +69 -0
  54. data/library/video_event/video_event.rb +3 -0
  55. data/license.txt +508 -0
  56. data/picrate.gemspec +35 -0
  57. data/pom.rb +122 -0
  58. data/pom.xml +214 -0
  59. data/src/main/java/japplemenubar/JAppleMenuBar.java +88 -0
  60. data/src/main/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  61. data/src/main/java/monkstone/ColorUtil.java +115 -0
  62. data/src/main/java/monkstone/MathToolModule.java +236 -0
  63. data/src/main/java/monkstone/PicrateLibrary.java +47 -0
  64. data/src/main/java/monkstone/core/LibraryProxy.java +127 -0
  65. data/src/main/java/monkstone/fastmath/Deglut.java +122 -0
  66. data/src/main/java/monkstone/fastmath/package-info.java +6 -0
  67. data/src/main/java/monkstone/filechooser/Chooser.java +48 -0
  68. data/src/main/java/monkstone/noise/SimplexNoise.java +465 -0
  69. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +168 -0
  70. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +182 -0
  71. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +149 -0
  72. data/src/main/java/monkstone/slider/SimpleSlider.java +196 -0
  73. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +163 -0
  74. data/src/main/java/monkstone/slider/Slider.java +67 -0
  75. data/src/main/java/monkstone/slider/SliderBar.java +277 -0
  76. data/src/main/java/monkstone/slider/SliderGroup.java +78 -0
  77. data/src/main/java/monkstone/slider/WheelHandler.java +35 -0
  78. data/src/main/java/monkstone/vecmath/AppRender.java +87 -0
  79. data/src/main/java/monkstone/vecmath/JRender.java +56 -0
  80. data/src/main/java/monkstone/vecmath/ShapeRender.java +87 -0
  81. data/src/main/java/monkstone/vecmath/package-info.java +20 -0
  82. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +757 -0
  83. data/src/main/java/monkstone/vecmath/vec2/package-info.java +6 -0
  84. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +727 -0
  85. data/src/main/java/monkstone/vecmath/vec3/package-info.java +6 -0
  86. data/src/main/java/monkstone/videoevent/VideoInterface.java +42 -0
  87. data/src/main/java/monkstone/videoevent/package-info.java +20 -0
  88. data/src/main/java/processing/awt/PGraphicsJava2D.java +3098 -0
  89. data/src/main/java/processing/awt/PShapeJava2D.java +401 -0
  90. data/src/main/java/processing/awt/PSurfaceAWT.java +1660 -0
  91. data/src/main/java/processing/core/PApplet.java +17647 -0
  92. data/src/main/java/processing/core/PConstants.java +1033 -0
  93. data/src/main/java/processing/core/PFont.java +1250 -0
  94. data/src/main/java/processing/core/PGraphics.java +9614 -0
  95. data/src/main/java/processing/core/PImage.java +3608 -0
  96. data/src/main/java/processing/core/PMatrix.java +347 -0
  97. data/src/main/java/processing/core/PMatrix2D.java +694 -0
  98. data/src/main/java/processing/core/PMatrix3D.java +1153 -0
  99. data/src/main/java/processing/core/PShape.java +4332 -0
  100. data/src/main/java/processing/core/PShapeOBJ.java +544 -0
  101. data/src/main/java/processing/core/PShapeSVG.java +1987 -0
  102. data/src/main/java/processing/core/PStyle.java +208 -0
  103. data/src/main/java/processing/core/PSurface.java +242 -0
  104. data/src/main/java/processing/core/PSurfaceNone.java +479 -0
  105. data/src/main/java/processing/core/PVector.java +1140 -0
  106. data/src/main/java/processing/data/FloatDict.java +829 -0
  107. data/src/main/java/processing/data/FloatList.java +912 -0
  108. data/src/main/java/processing/data/IntDict.java +796 -0
  109. data/src/main/java/processing/data/IntList.java +913 -0
  110. data/src/main/java/processing/data/JSONArray.java +1260 -0
  111. data/src/main/java/processing/data/JSONObject.java +2282 -0
  112. data/src/main/java/processing/data/JSONTokener.java +435 -0
  113. data/src/main/java/processing/data/Sort.java +46 -0
  114. data/src/main/java/processing/data/StringDict.java +601 -0
  115. data/src/main/java/processing/data/StringList.java +775 -0
  116. data/src/main/java/processing/data/Table.java +4923 -0
  117. data/src/main/java/processing/data/TableRow.java +198 -0
  118. data/src/main/java/processing/data/XML.java +1149 -0
  119. data/src/main/java/processing/event/Event.java +108 -0
  120. data/src/main/java/processing/event/KeyEvent.java +70 -0
  121. data/src/main/java/processing/event/MouseEvent.java +149 -0
  122. data/src/main/java/processing/event/TouchEvent.java +57 -0
  123. data/src/main/java/processing/javafx/PGraphicsFX2D.java +354 -0
  124. data/src/main/java/processing/opengl/FontTexture.java +379 -0
  125. data/src/main/java/processing/opengl/FrameBuffer.java +503 -0
  126. data/src/main/java/processing/opengl/LinePath.java +623 -0
  127. data/src/main/java/processing/opengl/LineStroker.java +685 -0
  128. data/src/main/java/processing/opengl/PGL.java +3366 -0
  129. data/src/main/java/processing/opengl/PGraphics2D.java +615 -0
  130. data/src/main/java/processing/opengl/PGraphics3D.java +281 -0
  131. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +13634 -0
  132. data/src/main/java/processing/opengl/PJOGL.java +1966 -0
  133. data/src/main/java/processing/opengl/PShader.java +1478 -0
  134. data/src/main/java/processing/opengl/PShapeOpenGL.java +5234 -0
  135. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1315 -0
  136. data/src/main/java/processing/opengl/Texture.java +1670 -0
  137. data/src/main/java/processing/opengl/VertexBuffer.java +88 -0
  138. data/src/main/java/processing/opengl/cursors/arrow.png +0 -0
  139. data/src/main/java/processing/opengl/cursors/cross.png +0 -0
  140. data/src/main/java/processing/opengl/cursors/hand.png +0 -0
  141. data/src/main/java/processing/opengl/cursors/license.txt +27 -0
  142. data/src/main/java/processing/opengl/cursors/move.png +0 -0
  143. data/src/main/java/processing/opengl/cursors/text.png +0 -0
  144. data/src/main/java/processing/opengl/cursors/wait.png +0 -0
  145. data/src/main/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
  146. data/src/main/java/processing/opengl/shaders/ColorVert.glsl +34 -0
  147. data/src/main/java/processing/opengl/shaders/LightFrag.glsl +33 -0
  148. data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +154 -0
  149. data/src/main/java/processing/opengl/shaders/LightVert.glsl +151 -0
  150. data/src/main/java/processing/opengl/shaders/LineFrag.glsl +32 -0
  151. data/src/main/java/processing/opengl/shaders/LineVert.glsl +100 -0
  152. data/src/main/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
  153. data/src/main/java/processing/opengl/shaders/PointFrag.glsl +32 -0
  154. data/src/main/java/processing/opengl/shaders/PointVert.glsl +56 -0
  155. data/src/main/java/processing/opengl/shaders/TexFrag.glsl +37 -0
  156. data/src/main/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
  157. data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +160 -0
  158. data/src/main/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
  159. data/src/main/java/processing/opengl/shaders/TexVert.glsl +38 -0
  160. data/src/main/resources/icon/icon-1024.png +0 -0
  161. data/src/main/resources/icon/icon-128.png +0 -0
  162. data/src/main/resources/icon/icon-16.png +0 -0
  163. data/src/main/resources/icon/icon-256.png +0 -0
  164. data/src/main/resources/icon/icon-32.png +0 -0
  165. data/src/main/resources/icon/icon-48.png +0 -0
  166. data/src/main/resources/icon/icon-512.png +0 -0
  167. data/src/main/resources/icon/icon-64.png +0 -0
  168. data/src/main/resources/license.txt +508 -0
  169. data/test/create_test.rb +68 -0
  170. data/test/deglut_spec_test.rb +24 -0
  171. data/test/helper_methods_test.rb +58 -0
  172. data/test/math_tool_test.rb +75 -0
  173. data/test/respond_to_test.rb +215 -0
  174. data/test/sketches/key_event.rb +37 -0
  175. data/test/sketches/library/my_library/my_library.rb +32 -0
  176. data/test/test_helper.rb +3 -0
  177. data/test/vecmath_spec_test.rb +522 -0
  178. data/vendors/Rakefile +127 -0
  179. metadata +289 -0
@@ -0,0 +1,1478 @@
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<String, UniformValue>();
740
+ }
741
+ uniformValues.put(name, new UniformValue(type, value));
742
+ }
743
+
744
+
745
+ protected void consumeUniforms() {
746
+ if (uniformValues != null && 0 < uniformValues.size()) {
747
+ int unit = 0;
748
+ for (String name: uniformValues.keySet()) {
749
+ int loc = getUniformLoc(name);
750
+ if (loc == -1) {
751
+ PGraphics.showWarning("The shader doesn't have a uniform called \"" +
752
+ name + "\" OR the uniform was removed during " +
753
+ "compilation because it was unused.");
754
+ continue;
755
+ }
756
+ UniformValue val = uniformValues.get(name);
757
+ if (val.type == UniformValue.INT1) {
758
+ int[] v = ((int[])val.value);
759
+ pgl.uniform1i(loc, v[0]);
760
+ } else if (val.type == UniformValue.INT2) {
761
+ int[] v = ((int[])val.value);
762
+ pgl.uniform2i(loc, v[0], v[1]);
763
+ } else if (val.type == UniformValue.INT3) {
764
+ int[] v = ((int[])val.value);
765
+ pgl.uniform3i(loc, v[0], v[1], v[2]);
766
+ } else if (val.type == UniformValue.INT4) {
767
+ int[] v = ((int[])val.value);
768
+ pgl.uniform4i(loc, v[0], v[1], v[2], v[3]);
769
+ } else if (val.type == UniformValue.FLOAT1) {
770
+ float[] v = ((float[])val.value);
771
+ pgl.uniform1f(loc, v[0]);
772
+ } else if (val.type == UniformValue.FLOAT2) {
773
+ float[] v = ((float[])val.value);
774
+ pgl.uniform2f(loc, v[0], v[1]);
775
+ } else if (val.type == UniformValue.FLOAT3) {
776
+ float[] v = ((float[])val.value);
777
+ pgl.uniform3f(loc, v[0], v[1], v[2]);
778
+ } else if (val.type == UniformValue.FLOAT4) {
779
+ float[] v = ((float[])val.value);
780
+ pgl.uniform4f(loc, v[0], v[1], v[2], v[3]);
781
+ } else if (val.type == UniformValue.INT1VEC) {
782
+ int[] v = ((int[])val.value);
783
+ updateIntBuffer(v);
784
+ pgl.uniform1iv(loc, v.length, intBuffer);
785
+ } else if (val.type == UniformValue.INT2VEC) {
786
+ int[] v = ((int[])val.value);
787
+ updateIntBuffer(v);
788
+ pgl.uniform2iv(loc, v.length / 2, intBuffer);
789
+ } else if (val.type == UniformValue.INT3VEC) {
790
+ int[] v = ((int[])val.value);
791
+ updateIntBuffer(v);
792
+ pgl.uniform3iv(loc, v.length / 3, intBuffer);
793
+ } else if (val.type == UniformValue.INT4VEC) {
794
+ int[] v = ((int[])val.value);
795
+ updateIntBuffer(v);
796
+ pgl.uniform4iv(loc, v.length / 4, intBuffer);
797
+ } else if (val.type == UniformValue.FLOAT1VEC) {
798
+ float[] v = ((float[])val.value);
799
+ updateFloatBuffer(v);
800
+ pgl.uniform1fv(loc, v.length, floatBuffer);
801
+ } else if (val.type == UniformValue.FLOAT2VEC) {
802
+ float[] v = ((float[])val.value);
803
+ updateFloatBuffer(v);
804
+ pgl.uniform2fv(loc, v.length / 2, floatBuffer);
805
+ } else if (val.type == UniformValue.FLOAT3VEC) {
806
+ float[] v = ((float[])val.value);
807
+ updateFloatBuffer(v);
808
+ pgl.uniform3fv(loc, v.length / 3, floatBuffer);
809
+ } else if (val.type == UniformValue.FLOAT4VEC) {
810
+ float[] v = ((float[])val.value);
811
+ updateFloatBuffer(v);
812
+ pgl.uniform4fv(loc, v.length / 4, floatBuffer);
813
+ } else if (val.type == UniformValue.MAT2) {
814
+ float[] v = ((float[])val.value);
815
+ updateFloatBuffer(v);
816
+ pgl.uniformMatrix2fv(loc, 1, false, floatBuffer);
817
+ } else if (val.type == UniformValue.MAT3) {
818
+ float[] v = ((float[])val.value);
819
+ updateFloatBuffer(v);
820
+ pgl.uniformMatrix3fv(loc, 1, false, floatBuffer);
821
+ } else if (val.type == UniformValue.MAT4) {
822
+ float[] v = ((float[])val.value);
823
+ updateFloatBuffer(v);
824
+ pgl.uniformMatrix4fv(loc, 1, false, floatBuffer);
825
+ } else if (val.type == UniformValue.SAMPLER2D) {
826
+ PImage img = (PImage)val.value;
827
+ Texture tex = currentPG.getTexture(img);
828
+
829
+ if (textures == null) textures = new HashMap<Integer, Texture>();
830
+ textures.put(loc, tex);
831
+
832
+ if (texUnits == null) texUnits = new HashMap<Integer, Integer>();
833
+ if (texUnits.containsKey(loc)) {
834
+ unit = texUnits.get(loc);
835
+ pgl.uniform1i(loc, unit);
836
+ } else {
837
+ texUnits.put(loc, unit);
838
+ pgl.uniform1i(loc, unit);
839
+ }
840
+ unit++;
841
+ }
842
+ }
843
+ uniformValues.clear();
844
+ }
845
+ }
846
+
847
+
848
+ protected void updateIntBuffer(int[] vec) {
849
+ intBuffer = PGL.updateIntBuffer(intBuffer, vec, false);
850
+ }
851
+
852
+
853
+ protected void updateFloatBuffer(float[] vec) {
854
+ floatBuffer = PGL.updateFloatBuffer(floatBuffer, vec, false);
855
+ }
856
+
857
+
858
+ protected void bindTextures() {
859
+ if (textures != null && texUnits != null) {
860
+ for (int loc: textures.keySet()) {
861
+ Texture tex = textures.get(loc);
862
+ Integer unit = texUnits.get(loc);
863
+ if (unit != null) {
864
+ pgl.activeTexture(PGL.TEXTURE0 + unit);
865
+ tex.bind();
866
+ } else {
867
+ throw new RuntimeException("Cannot find unit for texture " + tex);
868
+ }
869
+ }
870
+ }
871
+ }
872
+
873
+
874
+ protected void unbindTextures() {
875
+ if (textures != null && texUnits != null) {
876
+ for (int loc: textures.keySet()) {
877
+ Texture tex = textures.get(loc);
878
+ Integer unit = texUnits.get(loc);
879
+ if (unit != null) {
880
+ pgl.activeTexture(PGL.TEXTURE0 + unit);
881
+ tex.unbind();
882
+ } else {
883
+ throw new RuntimeException("Cannot find unit for texture " + tex);
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 ? false : true;
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 ? false : true;
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
+ * @param shaderSource a string containing the shader's code
992
+ */
993
+ protected boolean compileFragmentShader() {
994
+ pgl.shaderSource(glFragment, PApplet.join(fragmentShaderSource, "\n"));
995
+ pgl.compileShader(glFragment);
996
+
997
+ pgl.getShaderiv(glFragment, PGL.COMPILE_STATUS, intBuffer);
998
+ boolean compiled = intBuffer.get(0) == 0 ? false : true;
999
+ if (!compiled) {
1000
+ PGraphics.showException("Cannot compile fragment shader:\n" +
1001
+ pgl.getShaderInfoLog(glFragment));
1002
+ return false;
1003
+ } else {
1004
+ return true;
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 (int i = 0; i < source.length; i++) {
1022
+ String line = source[i].trim();
1023
+
1024
+ if (PApplet.match(line, colorShaderDefRegexp) != null)
1025
+ return PShader.COLOR;
1026
+ else if (PApplet.match(line, lightShaderDefRegexp) != null)
1027
+ return PShader.LIGHT;
1028
+ else if (PApplet.match(line, texShaderDefRegexp) != null)
1029
+ return PShader.TEXTURE;
1030
+ else if (PApplet.match(line, texlightShaderDefRegexp) != null)
1031
+ return PShader.TEXLIGHT;
1032
+ else if (PApplet.match(line, polyShaderDefRegexp) != null)
1033
+ return PShader.POLY;
1034
+ else if (PApplet.match(line, triShaderAttrRegexp) != null)
1035
+ return PShader.POLY;
1036
+ else if (PApplet.match(line, quadShaderAttrRegexp) != null)
1037
+ return PShader.POLY;
1038
+ else if (PApplet.match(line, pointShaderDefRegexp) != null)
1039
+ return PShader.POINT;
1040
+ else if (PApplet.match(line, lineShaderDefRegexp) != null)
1041
+ return PShader.LINE;
1042
+ else if (PApplet.match(line, pointShaderAttrRegexp) != null)
1043
+ return PShader.POINT;
1044
+ else if (PApplet.match(line, pointShaderInRegexp) != null)
1045
+ return PShader.POINT;
1046
+ else if (PApplet.match(line, lineShaderAttrRegexp) != null)
1047
+ return PShader.LINE;
1048
+ else if (PApplet.match(line, lineShaderInRegexp) != null)
1049
+ return PShader.LINE;
1050
+ }
1051
+ return defaultType;
1052
+ }
1053
+
1054
+
1055
+ // ***************************************************************************
1056
+ //
1057
+ // Processing specific
1058
+
1059
+
1060
+ protected int getType() {
1061
+ return type;
1062
+ }
1063
+
1064
+
1065
+ protected void setType(int type) {
1066
+ this.type = type;
1067
+ }
1068
+
1069
+
1070
+ protected boolean hasType() {
1071
+ return POINT <= type && type <= TEXLIGHT;
1072
+ }
1073
+
1074
+
1075
+ protected boolean isPointShader() {
1076
+ return type == POINT;
1077
+ }
1078
+
1079
+
1080
+ protected boolean isLineShader() {
1081
+ return type == LINE;
1082
+ }
1083
+
1084
+
1085
+ protected boolean isPolyShader() {
1086
+ return POLY <= type && type <= TEXLIGHT;
1087
+ }
1088
+
1089
+
1090
+ protected boolean checkPolyType(int type) {
1091
+ if (getType() == PShader.POLY) return true;
1092
+
1093
+ if (getType() != type) {
1094
+ if (type == TEXLIGHT) {
1095
+ PGraphics.showWarning(PGraphicsOpenGL.NO_TEXLIGHT_SHADER_ERROR);
1096
+ } else if (type == LIGHT) {
1097
+ PGraphics.showWarning(PGraphicsOpenGL.NO_LIGHT_SHADER_ERROR);
1098
+ } else if (type == TEXTURE) {
1099
+ PGraphics.showWarning(PGraphicsOpenGL.NO_TEXTURE_SHADER_ERROR);
1100
+ } else if (type == COLOR) {
1101
+ PGraphics.showWarning(PGraphicsOpenGL.NO_COLOR_SHADER_ERROR);
1102
+ }
1103
+ return false;
1104
+ }
1105
+
1106
+ return true;
1107
+ }
1108
+
1109
+
1110
+ protected int getLastTexUnit() {
1111
+ return texUnits == null ? -1 : texUnits.size() - 1;
1112
+ }
1113
+
1114
+
1115
+ protected void setRenderer(PGraphicsOpenGL pg) {
1116
+ this.currentPG = pg;
1117
+ }
1118
+
1119
+
1120
+ protected void loadAttributes() {
1121
+ if (loadedAttributes) return;
1122
+
1123
+ vertexLoc = getAttributeLoc("vertex");
1124
+ if (vertexLoc == -1) vertexLoc = getAttributeLoc("position");
1125
+
1126
+ colorLoc = getAttributeLoc("color");
1127
+ texCoordLoc = getAttributeLoc("texCoord");
1128
+ normalLoc = getAttributeLoc("normal");
1129
+
1130
+ ambientLoc = getAttributeLoc("ambient");
1131
+ specularLoc = getAttributeLoc("specular");
1132
+ emissiveLoc = getAttributeLoc("emissive");
1133
+ shininessLoc = getAttributeLoc("shininess");
1134
+
1135
+ directionLoc = getAttributeLoc("direction");
1136
+
1137
+ offsetLoc = getAttributeLoc("offset");
1138
+
1139
+ directionLoc = getAttributeLoc("direction");
1140
+ offsetLoc = getAttributeLoc("offset");
1141
+
1142
+ loadedAttributes = true;
1143
+ }
1144
+
1145
+
1146
+ protected void loadUniforms() {
1147
+ if (loadedUniforms) return;
1148
+ transformMatLoc = getUniformLoc("transform");
1149
+ if (transformMatLoc == -1)
1150
+ transformMatLoc = getUniformLoc("transformMatrix");
1151
+
1152
+ modelviewMatLoc = getUniformLoc("modelview");
1153
+ if (modelviewMatLoc == -1)
1154
+ modelviewMatLoc = getUniformLoc("modelviewMatrix");
1155
+
1156
+ projectionMatLoc = getUniformLoc("projection");
1157
+ if (projectionMatLoc == -1)
1158
+ projectionMatLoc = getUniformLoc("projectionMatrix");
1159
+
1160
+ viewportLoc = getUniformLoc("viewport");
1161
+ resolutionLoc = getUniformLoc("resolution");
1162
+ ppixelsLoc = getUniformLoc("ppixels");
1163
+
1164
+ normalMatLoc = getUniformLoc("normalMatrix");
1165
+
1166
+ lightCountLoc = getUniformLoc("lightCount");
1167
+ lightPositionLoc = getUniformLoc("lightPosition");
1168
+ lightNormalLoc = getUniformLoc("lightNormal");
1169
+ lightAmbientLoc = getUniformLoc("lightAmbient");
1170
+ lightDiffuseLoc = getUniformLoc("lightDiffuse");
1171
+ lightSpecularLoc = getUniformLoc("lightSpecular");
1172
+ lightFalloffLoc = getUniformLoc("lightFalloff");
1173
+ lightSpotLoc = getUniformLoc("lightSpot");
1174
+
1175
+ textureLoc = getUniformLoc("texture");
1176
+ if (textureLoc == -1) {
1177
+ textureLoc = getUniformLoc("texMap");
1178
+ }
1179
+
1180
+ texMatrixLoc = getUniformLoc("texMatrix");
1181
+ texOffsetLoc = getUniformLoc("texOffset");
1182
+
1183
+ perspectiveLoc = getUniformLoc("perspective");
1184
+ scaleLoc = getUniformLoc("scale");
1185
+ loadedUniforms = true;
1186
+ }
1187
+
1188
+
1189
+ protected void setCommonUniforms() {
1190
+ if (-1 < transformMatLoc) {
1191
+ currentPG.updateGLProjmodelview();
1192
+ setUniformMatrix(transformMatLoc, currentPG.glProjmodelview);
1193
+ }
1194
+
1195
+ if (-1 < modelviewMatLoc) {
1196
+ currentPG.updateGLModelview();
1197
+ setUniformMatrix(modelviewMatLoc, currentPG.glModelview);
1198
+ }
1199
+
1200
+ if (-1 < projectionMatLoc) {
1201
+ currentPG.updateGLProjection();
1202
+ setUniformMatrix(projectionMatLoc, currentPG.glProjection);
1203
+ }
1204
+
1205
+ if (-1 < viewportLoc) {
1206
+ float x = currentPG.viewport.get(0);
1207
+ float y = currentPG.viewport.get(1);
1208
+ float w = currentPG.viewport.get(2);
1209
+ float h = currentPG.viewport.get(3);
1210
+ setUniformValue(viewportLoc, x, y, w, h);
1211
+ }
1212
+
1213
+ if (-1 < resolutionLoc) {
1214
+ float w = currentPG.viewport.get(2);
1215
+ float h = currentPG.viewport.get(3);
1216
+ setUniformValue(resolutionLoc, w, h);
1217
+ }
1218
+
1219
+ if (-1 < ppixelsLoc) {
1220
+ ppixelsUnit = getLastTexUnit() + 1;
1221
+ setUniformValue(ppixelsLoc, ppixelsUnit);
1222
+ pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
1223
+ currentPG.bindFrontTexture();
1224
+ } else {
1225
+ ppixelsUnit = -1;
1226
+ }
1227
+ }
1228
+
1229
+
1230
+ protected void bindTyped() {
1231
+ if (currentPG == null) {
1232
+ setRenderer(primaryPG.getCurrentPG());
1233
+ loadAttributes();
1234
+ loadUniforms();
1235
+ }
1236
+ setCommonUniforms();
1237
+
1238
+ if (-1 < vertexLoc) pgl.enableVertexAttribArray(vertexLoc);
1239
+ if (-1 < colorLoc) pgl.enableVertexAttribArray(colorLoc);
1240
+ if (-1 < texCoordLoc) pgl.enableVertexAttribArray(texCoordLoc);
1241
+ if (-1 < normalLoc) pgl.enableVertexAttribArray(normalLoc);
1242
+
1243
+ if (-1 < normalMatLoc) {
1244
+ currentPG.updateGLNormal();
1245
+ setUniformMatrix(normalMatLoc, currentPG.glNormal);
1246
+ }
1247
+
1248
+ if (-1 < ambientLoc) pgl.enableVertexAttribArray(ambientLoc);
1249
+ if (-1 < specularLoc) pgl.enableVertexAttribArray(specularLoc);
1250
+ if (-1 < emissiveLoc) pgl.enableVertexAttribArray(emissiveLoc);
1251
+ if (-1 < shininessLoc) pgl.enableVertexAttribArray(shininessLoc);
1252
+
1253
+ int count = currentPG.lightCount;
1254
+ setUniformValue(lightCountLoc, count);
1255
+ if (0 < count) {
1256
+ setUniformVector(lightPositionLoc, currentPG.lightPosition, 4, count);
1257
+ setUniformVector(lightNormalLoc, currentPG.lightNormal, 3, count);
1258
+ setUniformVector(lightAmbientLoc, currentPG.lightAmbient, 3, count);
1259
+ setUniformVector(lightDiffuseLoc, currentPG.lightDiffuse, 3, count);
1260
+ setUniformVector(lightSpecularLoc, currentPG.lightSpecular, 3, count);
1261
+ setUniformVector(lightFalloffLoc, currentPG.lightFalloffCoefficients,
1262
+ 3, count);
1263
+ setUniformVector(lightSpotLoc, currentPG.lightSpotParameters, 2, count);
1264
+ }
1265
+
1266
+ if (-1 < directionLoc) pgl.enableVertexAttribArray(directionLoc);
1267
+
1268
+ if (-1 < offsetLoc) pgl.enableVertexAttribArray(offsetLoc);
1269
+
1270
+ if (-1 < perspectiveLoc) {
1271
+ if (currentPG.getHint(ENABLE_STROKE_PERSPECTIVE) &&
1272
+ currentPG.nonOrthoProjection()) {
1273
+ setUniformValue(perspectiveLoc, 1);
1274
+ } else {
1275
+ setUniformValue(perspectiveLoc, 0);
1276
+ }
1277
+ }
1278
+
1279
+ if (-1 < scaleLoc) {
1280
+ if (currentPG.getHint(DISABLE_OPTIMIZED_STROKE)) {
1281
+ setUniformValue(scaleLoc, 1.0f, 1.0f, 1.0f);
1282
+ } else {
1283
+ float f = PGL.STROKE_DISPLACEMENT;
1284
+ if (currentPG.orthoProjection()) {
1285
+ setUniformValue(scaleLoc, 1, 1, f);
1286
+ } else {
1287
+ setUniformValue(scaleLoc, f, f, f);
1288
+ }
1289
+ }
1290
+ }
1291
+ }
1292
+
1293
+ protected void unbindTyped() {
1294
+ if (-1 < offsetLoc) pgl.disableVertexAttribArray(offsetLoc);
1295
+
1296
+ if (-1 < directionLoc) pgl.disableVertexAttribArray(directionLoc);
1297
+
1298
+ if (-1 < textureLoc && texture != null) {
1299
+ pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1300
+ texture.unbind();
1301
+ pgl.activeTexture(PGL.TEXTURE0);
1302
+ texture = null;
1303
+ }
1304
+
1305
+ if (-1 < ambientLoc) pgl.disableVertexAttribArray(ambientLoc);
1306
+ if (-1 < specularLoc) pgl.disableVertexAttribArray(specularLoc);
1307
+ if (-1 < emissiveLoc) pgl.disableVertexAttribArray(emissiveLoc);
1308
+ if (-1 < shininessLoc) pgl.disableVertexAttribArray(shininessLoc);
1309
+
1310
+ if (-1 < vertexLoc) pgl.disableVertexAttribArray(vertexLoc);
1311
+ if (-1 < colorLoc) pgl.disableVertexAttribArray(colorLoc);
1312
+ if (-1 < texCoordLoc) pgl.disableVertexAttribArray(texCoordLoc);
1313
+ if (-1 < normalLoc) pgl.disableVertexAttribArray(normalLoc);
1314
+
1315
+ if (-1 < ppixelsLoc) {
1316
+ pgl.enableFBOLayer();
1317
+ pgl.activeTexture(PGL.TEXTURE0 + ppixelsUnit);
1318
+ currentPG.unbindFrontTexture();
1319
+ pgl.activeTexture(PGL.TEXTURE0);
1320
+ }
1321
+
1322
+ pgl.bindBuffer(PGL.ARRAY_BUFFER, 0);
1323
+ }
1324
+
1325
+ protected void setTexture(Texture tex) {
1326
+ texture = tex;
1327
+
1328
+ float scaleu = 1;
1329
+ float scalev = 1;
1330
+ float dispu = 0;
1331
+ float dispv = 0;
1332
+
1333
+ if (tex != null) {
1334
+ if (tex.invertedX()) {
1335
+ scaleu = -1;
1336
+ dispu = 1;
1337
+ }
1338
+
1339
+ if (tex.invertedY()) {
1340
+ scalev = -1;
1341
+ dispv = 1;
1342
+ }
1343
+
1344
+ scaleu *= tex.maxTexcoordU();
1345
+ dispu *= tex.maxTexcoordU();
1346
+ scalev *= tex.maxTexcoordV();
1347
+ dispv *= tex.maxTexcoordV();
1348
+
1349
+ setUniformValue(texOffsetLoc, 1.0f / tex.width, 1.0f / tex.height);
1350
+
1351
+ if (-1 < textureLoc) {
1352
+ texUnit = -1 < ppixelsUnit ? ppixelsUnit + 1 : getLastTexUnit() + 1;
1353
+ setUniformValue(textureLoc, texUnit);
1354
+ pgl.activeTexture(PGL.TEXTURE0 + texUnit);
1355
+ tex.bind();
1356
+ }
1357
+ }
1358
+
1359
+ if (-1 < texMatrixLoc) {
1360
+ if (tcmat == null) {
1361
+ tcmat = new float[16];
1362
+ }
1363
+ tcmat[0] = scaleu; tcmat[4] = 0; tcmat[ 8] = 0; tcmat[12] = dispu;
1364
+ tcmat[1] = 0; tcmat[5] = scalev; tcmat[ 9] = 0; tcmat[13] = dispv;
1365
+ tcmat[2] = 0; tcmat[6] = 0; tcmat[10] = 0; tcmat[14] = 0;
1366
+ tcmat[3] = 0; tcmat[7] = 0; tcmat[11] = 0; tcmat[15] = 0;
1367
+ setUniformMatrix(texMatrixLoc, tcmat);
1368
+ }
1369
+ }
1370
+
1371
+
1372
+ protected boolean supportsTexturing() {
1373
+ return -1 < textureLoc;
1374
+ }
1375
+
1376
+ protected boolean supportLighting() {
1377
+ return -1 < lightCountLoc || -1 < lightPositionLoc || -1 < lightNormalLoc;
1378
+ }
1379
+
1380
+ protected boolean accessTexCoords() {
1381
+ return -1 < texCoordLoc;
1382
+ }
1383
+
1384
+ protected boolean accessNormals() {
1385
+ return -1 < normalLoc;
1386
+ }
1387
+
1388
+ protected boolean accessLightAttribs() {
1389
+ return -1 < ambientLoc || -1 < specularLoc || -1 < emissiveLoc ||
1390
+ -1 < shininessLoc;
1391
+ }
1392
+
1393
+ protected void setVertexAttribute(int vboId, int size, int type,
1394
+ int stride, int offset) {
1395
+ setAttributeVBO(vertexLoc, vboId, size, type, false, stride, offset);
1396
+ }
1397
+
1398
+ protected void setColorAttribute(int vboId, int size, int type,
1399
+ int stride, int offset) {
1400
+ setAttributeVBO(colorLoc, vboId, size, type, true, stride, offset);
1401
+ }
1402
+
1403
+ protected void setNormalAttribute(int vboId, int size, int type,
1404
+ int stride, int offset) {
1405
+ setAttributeVBO(normalLoc, vboId, size, type, false, stride, offset);
1406
+ }
1407
+
1408
+ protected void setTexcoordAttribute(int vboId, int size, int type,
1409
+ int stride, int offset) {
1410
+ setAttributeVBO(texCoordLoc, vboId, size, type, false, stride, offset);
1411
+ }
1412
+
1413
+ protected void setAmbientAttribute(int vboId, int size, int type,
1414
+ int stride, int offset) {
1415
+ setAttributeVBO(ambientLoc, vboId, size, type, true, stride, offset);
1416
+ }
1417
+
1418
+ protected void setSpecularAttribute(int vboId, int size, int type,
1419
+ int stride, int offset) {
1420
+ setAttributeVBO(specularLoc, vboId, size, type, true, stride, offset);
1421
+ }
1422
+
1423
+ protected void setEmissiveAttribute(int vboId, int size, int type,
1424
+ int stride, int offset) {
1425
+ setAttributeVBO(emissiveLoc, vboId, size, type, true, stride, offset);
1426
+ }
1427
+
1428
+ protected void setShininessAttribute(int vboId, int size, int type,
1429
+ int stride, int offset) {
1430
+ setAttributeVBO(shininessLoc, vboId, size, type, false, stride, offset);
1431
+ }
1432
+
1433
+ protected void setLineAttribute(int vboId, int size, int type,
1434
+ int stride, int offset) {
1435
+ setAttributeVBO(directionLoc, vboId, size, type, false, stride, offset);
1436
+ }
1437
+
1438
+ protected void setPointAttribute(int vboId, int size, int type,
1439
+ int stride, int offset) {
1440
+ setAttributeVBO(offsetLoc, vboId, size, type, false, stride, offset);
1441
+ }
1442
+
1443
+
1444
+ // ***************************************************************************
1445
+ //
1446
+ // Class to store a user-specified value for a uniform parameter
1447
+ // in the shader
1448
+ protected static class UniformValue {
1449
+ static final int INT1 = 0;
1450
+ static final int INT2 = 1;
1451
+ static final int INT3 = 2;
1452
+ static final int INT4 = 3;
1453
+ static final int FLOAT1 = 4;
1454
+ static final int FLOAT2 = 5;
1455
+ static final int FLOAT3 = 6;
1456
+ static final int FLOAT4 = 7;
1457
+ static final int INT1VEC = 8;
1458
+ static final int INT2VEC = 9;
1459
+ static final int INT3VEC = 10;
1460
+ static final int INT4VEC = 11;
1461
+ static final int FLOAT1VEC = 12;
1462
+ static final int FLOAT2VEC = 13;
1463
+ static final int FLOAT3VEC = 14;
1464
+ static final int FLOAT4VEC = 15;
1465
+ static final int MAT2 = 16;
1466
+ static final int MAT3 = 17;
1467
+ static final int MAT4 = 18;
1468
+ static final int SAMPLER2D = 19;
1469
+
1470
+ int type;
1471
+ Object value;
1472
+
1473
+ UniformValue(int type, Object value) {
1474
+ this.type = type;
1475
+ this.value = value;
1476
+ }
1477
+ }
1478
+ }