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,544 @@
1
+ package processing.core;
2
+
3
+ import java.io.BufferedReader;
4
+ import java.io.File;
5
+ import java.util.ArrayList;
6
+ import java.util.HashMap;
7
+ import java.util.Map;
8
+
9
+ /**
10
+ * This class is not part of the Processing API and should not be used
11
+ * directly. Instead, use loadShape() and methods like it, which will make
12
+ * use of this class. Using this class directly will cause your code to break
13
+ * when combined with future versions of Processing.
14
+ * <p>
15
+ * OBJ loading implemented using code from Saito's OBJLoader library:
16
+ * http://code.google.com/p/saitoobjloader/
17
+ * and OBJReader from Ahmet Kizilay
18
+ * http://www.openprocessing.org/visuals/?visualID=191
19
+ *
20
+ */
21
+ public class PShapeOBJ extends PShape {
22
+
23
+ /**
24
+ * Initializes a new OBJ Object with the given filename.
25
+ * @param parent
26
+ * @param filename
27
+ */
28
+ public PShapeOBJ(PApplet parent, String filename) {
29
+ this(parent, parent.createReader(filename), getBasePath(parent, filename));
30
+ }
31
+
32
+ /**
33
+ *
34
+ * @param parent
35
+ * @param reader
36
+ */
37
+ public PShapeOBJ(PApplet parent, BufferedReader reader) {
38
+ this(parent, reader, "");
39
+ }
40
+
41
+ /**
42
+ *
43
+ * @param parent
44
+ * @param reader
45
+ * @param basePath
46
+ */
47
+ public PShapeOBJ(PApplet parent, BufferedReader reader, String basePath) {
48
+ ArrayList<OBJFace> faces = new ArrayList<OBJFace>();
49
+ ArrayList<OBJMaterial> materials = new ArrayList<OBJMaterial>();
50
+ ArrayList<PVector> coords = new ArrayList<PVector>();
51
+ ArrayList<PVector> normals = new ArrayList<PVector>();
52
+ ArrayList<PVector> texcoords = new ArrayList<PVector>();
53
+ parseOBJ(parent, basePath, reader,
54
+ faces, materials, coords, normals, texcoords);
55
+
56
+ // The OBJ geometry is stored with each face in a separate child shape.
57
+ parent = null;
58
+ family = GROUP;
59
+ addChildren(faces, materials, coords, normals, texcoords);
60
+ }
61
+
62
+ /**
63
+ *
64
+ * @param face
65
+ * @param mtl
66
+ * @param coords
67
+ * @param normals
68
+ * @param texcoords
69
+ */
70
+ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
71
+ ArrayList<PVector> coords,
72
+ ArrayList<PVector> normals,
73
+ ArrayList<PVector> texcoords) {
74
+ family = GEOMETRY;
75
+ if (face.vertIdx.size() == 3) {
76
+ kind = TRIANGLES;
77
+ } else if (face.vertIdx.size() == 4) {
78
+ kind = QUADS;
79
+ } else {
80
+ kind = POLYGON;
81
+ }
82
+
83
+ stroke = false;
84
+ fill = true;
85
+
86
+ // Setting material properties for the new face
87
+ fillColor = rgbaValue(mtl.kd);
88
+ ambientColor = rgbaValue(mtl.ka);
89
+ specularColor = rgbaValue(mtl.ks);
90
+ shininess = mtl.ns;
91
+ if (mtl.kdMap != null) {
92
+ // If current material is textured, then tinting the texture using the
93
+ // diffuse color.
94
+ tintColor = rgbaValue(mtl.kd, mtl.d);
95
+ }
96
+
97
+ vertexCount = face.vertIdx.size();
98
+ vertices = new float[vertexCount][12];
99
+ for (int j = 0; j < face.vertIdx.size(); j++){
100
+ int vertIdx, normIdx, texIdx;
101
+ PVector vert, norms, tex;
102
+
103
+ vert = norms = tex = null;
104
+
105
+ vertIdx = face.vertIdx.get(j).intValue() - 1;
106
+ vert = coords.get(vertIdx);
107
+
108
+ if (j < face.normIdx.size()) {
109
+ normIdx = face.normIdx.get(j).intValue() - 1;
110
+ if (-1 < normIdx) {
111
+ norms = normals.get(normIdx);
112
+ }
113
+ }
114
+
115
+ if (j < face.texIdx.size()) {
116
+ texIdx = face.texIdx.get(j).intValue() - 1;
117
+ if (-1 < texIdx) {
118
+ tex = texcoords.get(texIdx);
119
+ }
120
+ }
121
+
122
+ vertices[j][X] = vert.x;
123
+ vertices[j][Y] = vert.y;
124
+ vertices[j][Z] = vert.z;
125
+
126
+ vertices[j][PGraphics.R] = mtl.kd.x;
127
+ vertices[j][PGraphics.G] = mtl.kd.y;
128
+ vertices[j][PGraphics.B] = mtl.kd.z;
129
+ vertices[j][PGraphics.A] = 1;
130
+
131
+ if (norms != null) {
132
+ vertices[j][PGraphics.NX] = norms.x;
133
+ vertices[j][PGraphics.NY] = norms.y;
134
+ vertices[j][PGraphics.NZ] = norms.z;
135
+ }
136
+
137
+ if (tex != null) {
138
+ vertices[j][PGraphics.U] = tex.x;
139
+ vertices[j][PGraphics.V] = tex.y;
140
+ }
141
+
142
+ if (mtl != null && mtl.kdMap != null) {
143
+ image = mtl.kdMap;
144
+ }
145
+ }
146
+ }
147
+
148
+ /**
149
+ *
150
+ * @param faces
151
+ * @param materials
152
+ * @param coords
153
+ * @param normals
154
+ * @param texcoords
155
+ */
156
+ protected void addChildren(ArrayList<OBJFace> faces,
157
+ ArrayList<OBJMaterial> materials,
158
+ ArrayList<PVector> coords,
159
+ ArrayList<PVector> normals,
160
+ ArrayList<PVector> texcoords) {
161
+ int mtlIdxCur = -1;
162
+ OBJMaterial mtl = null;
163
+ for (int i = 0; i < faces.size(); i++) {
164
+ OBJFace face = faces.get(i);
165
+
166
+ // Getting current material.
167
+ if (mtlIdxCur != face.matIdx || face.matIdx == -1) {
168
+ // To make sure that at least we get the default material
169
+ mtlIdxCur = PApplet.max(0, face.matIdx);
170
+ mtl = materials.get(mtlIdxCur);
171
+ }
172
+
173
+ // Creating child shape for current face.
174
+ PShape child = new PShapeOBJ(face, mtl, coords, normals, texcoords);
175
+ addChild(child);
176
+ }
177
+ }
178
+
179
+ /**
180
+ *
181
+ * @param parent
182
+ * @param path
183
+ * @param reader
184
+ * @param faces
185
+ * @param materials
186
+ * @param coords
187
+ * @param normals
188
+ * @param texcoords
189
+ */
190
+ static protected void parseOBJ(PApplet parent, String path,
191
+ BufferedReader reader,
192
+ ArrayList<OBJFace> faces,
193
+ ArrayList<OBJMaterial> materials,
194
+ ArrayList<PVector> coords,
195
+ ArrayList<PVector> normals,
196
+ ArrayList<PVector> texcoords) {
197
+ Map<String, Integer> mtlTable = new HashMap<String, Integer>();
198
+ int mtlIdxCur = -1;
199
+ boolean readv, readvn, readvt;
200
+ try {
201
+
202
+ readv = readvn = readvt = false;
203
+ String line;
204
+ String gname = "object";
205
+ while ((line = reader.readLine()) != null) {
206
+ // Parse the line.
207
+ line = line.trim();
208
+ if (line.equals("") || line.indexOf('#') == 0) {
209
+ // Empty line of comment, ignore line
210
+ continue;
211
+ }
212
+
213
+ // The below patch/hack comes from Carlos Tomas Marti and is a
214
+ // fix for single backslashes in Rhino obj files
215
+
216
+ // BEGINNING OF RHINO OBJ FILES HACK
217
+ // Statements can be broken in multiple lines using '\' at the
218
+ // end of a line.
219
+ // In regular expressions, the backslash is also an escape
220
+ // character.
221
+ // The regular expression \\ matches a single backslash. This
222
+ // regular expression as a Java string, becomes "\\\\".
223
+ // That's right: 4 backslashes to match a single one.
224
+ while (line.contains("\\")) {
225
+ line = line.split("\\\\")[0];
226
+ final String s = reader.readLine();
227
+ if (s != null)
228
+ line += s;
229
+ }
230
+ // END OF RHINO OBJ FILES HACK
231
+
232
+ String[] parts = line.split("\\s+");
233
+ // if not a blank line, process the line.
234
+ if (parts.length > 0) {
235
+ if (parts[0].equals("v")) {
236
+ // vertex
237
+ PVector tempv = new PVector(Float.valueOf(parts[1]).floatValue(),
238
+ Float.valueOf(parts[2]).floatValue(),
239
+ Float.valueOf(parts[3]).floatValue());
240
+ coords.add(tempv);
241
+ readv = true;
242
+ } else if (parts[0].equals("vn")) {
243
+ // normal
244
+ PVector tempn = new PVector(Float.valueOf(parts[1]).floatValue(),
245
+ Float.valueOf(parts[2]).floatValue(),
246
+ Float.valueOf(parts[3]).floatValue());
247
+ normals.add(tempn);
248
+ readvn = true;
249
+ } else if (parts[0].equals("vt")) {
250
+ // uv, inverting v to take into account Processing's inverted Y axis
251
+ // with respect to OpenGL.
252
+ PVector tempv = new PVector(Float.valueOf(parts[1]).floatValue(),
253
+ 1 - Float.valueOf(parts[2]).
254
+ floatValue());
255
+ texcoords.add(tempv);
256
+ readvt = true;
257
+ } else if (parts[0].equals("o")) {
258
+ // Object name is ignored, for now.
259
+ } else if (parts[0].equals("mtllib")) {
260
+ if (parts[1] != null) {
261
+ String fn = parts[1];
262
+ if (fn.indexOf(File.separator) == -1 && !path.equals("")) {
263
+ // Relative file name, adding the base path.
264
+ fn = path + File.separator + fn;
265
+ }
266
+ BufferedReader mreader = parent.createReader(fn);
267
+ if (mreader != null) {
268
+ parseMTL(parent, fn, path, mreader, materials, mtlTable);
269
+ mreader.close();
270
+ }
271
+ }
272
+ } else if (parts[0].equals("g")) {
273
+ gname = 1 < parts.length ? parts[1] : "";
274
+ } else if (parts[0].equals("usemtl")) {
275
+ // Getting index of current active material (will be applied on
276
+ // all subsequent faces).
277
+ if (parts[1] != null) {
278
+ String mtlname = parts[1];
279
+ if (mtlTable.containsKey(mtlname)) {
280
+ Integer tempInt = mtlTable.get(mtlname);
281
+ mtlIdxCur = tempInt.intValue();
282
+ } else {
283
+ mtlIdxCur = -1;
284
+ }
285
+ }
286
+ } else if (parts[0].equals("f")) {
287
+ // Face setting
288
+ OBJFace face = new OBJFace();
289
+ face.matIdx = mtlIdxCur;
290
+ face.name = gname;
291
+
292
+ for (int i = 1; i < parts.length; i++) {
293
+ String seg = parts[i];
294
+
295
+ if (seg.indexOf("/") > 0) {
296
+ String[] forder = seg.split("/");
297
+
298
+ if (forder.length > 2) {
299
+ // Getting vertex and texture and normal indexes.
300
+ if (forder[0].length() > 0 && readv) {
301
+ face.vertIdx.add(Integer.valueOf(forder[0]));
302
+ }
303
+
304
+ if (forder[1].length() > 0 && readvt) {
305
+ face.texIdx.add(Integer.valueOf(forder[1]));
306
+ }
307
+
308
+ if (forder[2].length() > 0 && readvn) {
309
+ face.normIdx.add(Integer.valueOf(forder[2]));
310
+ }
311
+ } else if (forder.length > 1) {
312
+ // Getting vertex and texture/normal indexes.
313
+ if (forder[0].length() > 0 && readv) {
314
+ face.vertIdx.add(Integer.valueOf(forder[0]));
315
+ }
316
+
317
+ if (forder[1].length() > 0) {
318
+ if (readvt) {
319
+ face.texIdx.add(Integer.valueOf(forder[1]));
320
+ } else if (readvn) {
321
+ face.normIdx.add(Integer.valueOf(forder[1]));
322
+ }
323
+
324
+ }
325
+
326
+ } else if (forder.length > 0) {
327
+ // Getting vertex index only.
328
+ if (forder[0].length() > 0 && readv) {
329
+ face.vertIdx.add(Integer.valueOf(forder[0]));
330
+ }
331
+ }
332
+ } else {
333
+ // Getting vertex index only.
334
+ if (seg.length() > 0 && readv) {
335
+ face.vertIdx.add(Integer.valueOf(seg));
336
+ }
337
+ }
338
+ }
339
+
340
+ faces.add(face);
341
+ }
342
+ }
343
+ }
344
+
345
+ if (materials.size() == 0) {
346
+ // No materials definition so far. Adding one default material.
347
+ OBJMaterial defMtl = new OBJMaterial();
348
+ materials.add(defMtl);
349
+ }
350
+
351
+ } catch (Exception e) {
352
+ e.printStackTrace();
353
+ }
354
+ }
355
+
356
+ /**
357
+ *
358
+ * @param parent
359
+ * @param mtlfn
360
+ * @param path
361
+ * @param reader
362
+ * @param materials
363
+ * @param materialsHash
364
+ */
365
+ static protected void parseMTL(PApplet parent, String mtlfn, String path,
366
+ BufferedReader reader,
367
+ ArrayList<OBJMaterial> materials,
368
+ Map<String, Integer> materialsHash) {
369
+ try {
370
+ String line;
371
+ OBJMaterial currentMtl = null;
372
+ while ((line = reader.readLine()) != null) {
373
+ // Parse the line
374
+ line = line.trim();
375
+ String parts[] = line.split("\\s+");
376
+ if (parts.length > 0) {
377
+ // Extract the material data.
378
+ if (parts[0].equals("newmtl")) {
379
+ // Starting new material.
380
+ String mtlname = parts[1];
381
+ currentMtl = addMaterial(mtlname, materials, materialsHash);
382
+ } else {
383
+ if (currentMtl == null) {
384
+ currentMtl = addMaterial("material" + materials.size(),
385
+ materials, materialsHash);
386
+ }
387
+ if (parts[0].equals("map_Kd") && parts.length > 1) {
388
+ // Loading texture map.
389
+ String texname = parts[1];
390
+ if (texname.indexOf(File.separator) == -1 && !path.equals("")) {
391
+ // Relative file name, adding the base path.
392
+ texname = path + File.separator + texname;
393
+ }
394
+
395
+ File file = new File(parent.dataPath(texname));
396
+ if (file.exists()) {
397
+ currentMtl.kdMap = parent.loadImage(texname);
398
+ } else {
399
+ System.err.println("The texture map \"" + texname + "\" " +
400
+ "in the materials definition file \"" + mtlfn + "\" " +
401
+ "is missing or inaccessible, make sure " +
402
+ "the URL is valid or that the file has been " +
403
+ "added to your sketch and is readable.");
404
+ }
405
+ } else if (parts[0].equals("Ka") && parts.length > 3) {
406
+ // The ambient color of the material
407
+ currentMtl.ka.x = Float.valueOf(parts[1]).floatValue();
408
+ currentMtl.ka.y = Float.valueOf(parts[2]).floatValue();
409
+ currentMtl.ka.z = Float.valueOf(parts[3]).floatValue();
410
+ } else if (parts[0].equals("Kd") && parts.length > 3) {
411
+ // The diffuse color of the material
412
+ currentMtl.kd.x = Float.valueOf(parts[1]).floatValue();
413
+ currentMtl.kd.y = Float.valueOf(parts[2]).floatValue();
414
+ currentMtl.kd.z = Float.valueOf(parts[3]).floatValue();
415
+ } else if (parts[0].equals("Ks") && parts.length > 3) {
416
+ // The specular color weighted by the specular coefficient
417
+ currentMtl.ks.x = Float.valueOf(parts[1]).floatValue();
418
+ currentMtl.ks.y = Float.valueOf(parts[2]).floatValue();
419
+ currentMtl.ks.z = Float.valueOf(parts[3]).floatValue();
420
+ } else if ((parts[0].equals("d") ||
421
+ parts[0].equals("Tr")) && parts.length > 1) {
422
+ // Reading the alpha transparency.
423
+ currentMtl.d = Float.valueOf(parts[1]).floatValue();
424
+ } else if (parts[0].equals("Ns") && parts.length > 1) {
425
+ // The specular component of the Phong shading model
426
+ currentMtl.ns = Float.valueOf(parts[1]).floatValue();
427
+ }
428
+ }
429
+ }
430
+ }
431
+ } catch (Exception e) {
432
+ e.printStackTrace();
433
+ }
434
+ }
435
+
436
+ /**
437
+ *
438
+ * @param mtlname
439
+ * @param materials
440
+ * @param materialsHash
441
+ * @return
442
+ */
443
+ protected static OBJMaterial addMaterial(String mtlname,
444
+ ArrayList<OBJMaterial> materials,
445
+ Map<String, Integer> materialsHash) {
446
+ OBJMaterial currentMtl = new OBJMaterial(mtlname);
447
+ materialsHash.put(mtlname, Integer.valueOf(materials.size()));
448
+ materials.add(currentMtl);
449
+ return currentMtl;
450
+ }
451
+
452
+ /**
453
+ *
454
+ * @param color
455
+ * @return
456
+ */
457
+ protected static int rgbaValue(PVector color) {
458
+ return 0xFF000000 | ((int)(color.x * 255) << 16) |
459
+ ((int)(color.y * 255) << 8) |
460
+ (int)(color.z * 255);
461
+ }
462
+
463
+ /**
464
+ *
465
+ * @param color
466
+ * @param alpha
467
+ * @return
468
+ */
469
+ protected static int rgbaValue(PVector color, float alpha) {
470
+ return ((int)(alpha * 255) << 24) |
471
+ ((int)(color.x * 255) << 16) |
472
+ ((int)(color.y * 255) << 8) |
473
+ (int)(color.z * 255);
474
+ }
475
+
476
+
477
+ // Stores a face from an OBJ file
478
+
479
+ /**
480
+ *
481
+ */
482
+ static protected class OBJFace {
483
+ ArrayList<Integer> vertIdx;
484
+ ArrayList<Integer> texIdx;
485
+ ArrayList<Integer> normIdx;
486
+ int matIdx;
487
+ String name;
488
+
489
+ OBJFace() {
490
+ vertIdx = new ArrayList<Integer>();
491
+ texIdx = new ArrayList<Integer>();
492
+ normIdx = new ArrayList<Integer>();
493
+ matIdx = -1;
494
+ name = "";
495
+ }
496
+ }
497
+
498
+ /**
499
+ *
500
+ * @param parent
501
+ * @param filename
502
+ * @return
503
+ */
504
+ static protected String getBasePath(PApplet parent, String filename) {
505
+ // Obtaining the path
506
+ File file = new File(parent.dataPath(filename));
507
+ if (!file.exists()) {
508
+ file = parent.sketchFile(filename);
509
+ }
510
+ String absolutePath = file.getAbsolutePath();
511
+ return absolutePath.substring(0,
512
+ absolutePath.lastIndexOf(File.separator));
513
+ }
514
+
515
+
516
+ // Stores a material defined in an MTL file.
517
+
518
+ /**
519
+ *
520
+ */
521
+ static protected class OBJMaterial {
522
+ String name;
523
+ PVector ka;
524
+ PVector kd;
525
+ PVector ks;
526
+ float d;
527
+ float ns;
528
+ PImage kdMap;
529
+
530
+ OBJMaterial() {
531
+ this("default");
532
+ }
533
+
534
+ OBJMaterial(String name) {
535
+ this.name = name;
536
+ ka = new PVector(0.5f, 0.5f, 0.5f);
537
+ kd = new PVector(0.5f, 0.5f, 0.5f);
538
+ ks = new PVector(0.5f, 0.5f, 0.5f);
539
+ d = 1.0f;
540
+ ns = 0.0f;
541
+ kdMap = null;
542
+ }
543
+ }
544
+ }