propane 3.6.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 (161) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -1
  3. data/.travis.yml +1 -1
  4. data/CHANGELOG.md +5 -1
  5. data/README.md +6 -13
  6. data/Rakefile +7 -6
  7. data/lib/java/japplemenubar/JAppleMenuBar.java +88 -0
  8. data/lib/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  9. data/lib/java/monkstone/ColorUtil.java +127 -0
  10. data/lib/java/monkstone/MathToolModule.java +287 -0
  11. data/lib/java/monkstone/PropaneLibrary.java +46 -0
  12. data/lib/java/monkstone/core/LibraryProxy.java +136 -0
  13. data/lib/java/monkstone/fastmath/DegLutTables.java +111 -0
  14. data/lib/java/monkstone/fastmath/Deglut.java +71 -0
  15. data/lib/java/monkstone/fastmath/package-info.java +6 -0
  16. data/lib/java/monkstone/filechooser/Chooser.java +39 -0
  17. data/lib/java/monkstone/noise/FastTerrain.java +874 -0
  18. data/lib/java/monkstone/noise/Noise.java +90 -0
  19. data/lib/java/monkstone/noise/NoiseGenerator.java +75 -0
  20. data/lib/java/monkstone/noise/NoiseMode.java +28 -0
  21. data/lib/java/monkstone/noise/OpenSimplex2F.java +881 -0
  22. data/lib/java/monkstone/noise/OpenSimplex2S.java +1106 -0
  23. data/lib/java/monkstone/noise/SmoothTerrain.java +1099 -0
  24. data/lib/java/monkstone/slider/CustomHorizontalSlider.java +164 -0
  25. data/lib/java/monkstone/slider/CustomVerticalSlider.java +178 -0
  26. data/lib/java/monkstone/slider/SimpleHorizontalSlider.java +145 -0
  27. data/lib/java/monkstone/slider/SimpleSlider.java +166 -0
  28. data/lib/java/monkstone/slider/SimpleVerticalSlider.java +157 -0
  29. data/lib/java/monkstone/slider/Slider.java +61 -0
  30. data/lib/java/monkstone/slider/SliderBar.java +245 -0
  31. data/lib/java/monkstone/slider/SliderGroup.java +56 -0
  32. data/lib/java/monkstone/slider/WheelHandler.java +35 -0
  33. data/lib/java/monkstone/vecmath/GfxRender.java +86 -0
  34. data/lib/java/monkstone/vecmath/JRender.java +56 -0
  35. data/lib/java/monkstone/vecmath/ShapeRender.java +87 -0
  36. data/lib/java/monkstone/vecmath/package-info.java +20 -0
  37. data/lib/java/monkstone/vecmath/vec2/Vec2.java +802 -0
  38. data/lib/java/monkstone/vecmath/vec2/package-info.java +6 -0
  39. data/lib/java/monkstone/vecmath/vec3/Vec3.java +727 -0
  40. data/lib/java/monkstone/vecmath/vec3/package-info.java +6 -0
  41. data/lib/java/monkstone/videoevent/CaptureEvent.java +27 -0
  42. data/lib/java/monkstone/videoevent/MovieEvent.java +32 -0
  43. data/lib/java/monkstone/videoevent/package-info.java +20 -0
  44. data/lib/java/processing/awt/PGraphicsJava2D.java +3040 -0
  45. data/lib/java/processing/awt/PImageAWT.java +377 -0
  46. data/lib/java/processing/awt/PShapeJava2D.java +387 -0
  47. data/lib/java/processing/awt/PSurfaceAWT.java +1581 -0
  48. data/lib/java/processing/awt/ShimAWT.java +581 -0
  49. data/lib/java/processing/core/PApplet.java +15156 -0
  50. data/lib/java/processing/core/PConstants.java +523 -0
  51. data/lib/java/processing/core/PFont.java +1126 -0
  52. data/lib/java/processing/core/PGraphics.java +8600 -0
  53. data/lib/java/processing/core/PImage.java +3377 -0
  54. data/lib/java/processing/core/PMatrix.java +208 -0
  55. data/lib/java/processing/core/PMatrix2D.java +562 -0
  56. data/lib/java/processing/core/PMatrix3D.java +890 -0
  57. data/lib/java/processing/core/PShape.java +3561 -0
  58. data/lib/java/processing/core/PShapeOBJ.java +483 -0
  59. data/lib/java/processing/core/PShapeSVG.java +2016 -0
  60. data/lib/java/processing/core/PStyle.java +63 -0
  61. data/lib/java/processing/core/PSurface.java +198 -0
  62. data/lib/java/processing/core/PSurfaceNone.java +431 -0
  63. data/lib/java/processing/core/PVector.java +1066 -0
  64. data/lib/java/processing/core/ThinkDifferent.java +115 -0
  65. data/lib/java/processing/data/DoubleDict.java +850 -0
  66. data/lib/java/processing/data/DoubleList.java +928 -0
  67. data/lib/java/processing/data/FloatDict.java +847 -0
  68. data/lib/java/processing/data/FloatList.java +936 -0
  69. data/lib/java/processing/data/IntDict.java +807 -0
  70. data/lib/java/processing/data/IntList.java +936 -0
  71. data/lib/java/processing/data/JSONArray.java +1260 -0
  72. data/lib/java/processing/data/JSONObject.java +2282 -0
  73. data/lib/java/processing/data/JSONTokener.java +435 -0
  74. data/lib/java/processing/data/LongDict.java +802 -0
  75. data/lib/java/processing/data/LongList.java +937 -0
  76. data/lib/java/processing/data/Sort.java +46 -0
  77. data/lib/java/processing/data/StringDict.java +613 -0
  78. data/lib/java/processing/data/StringList.java +800 -0
  79. data/lib/java/processing/data/Table.java +4936 -0
  80. data/lib/java/processing/data/TableRow.java +198 -0
  81. data/lib/java/processing/data/XML.java +1156 -0
  82. data/lib/java/processing/dxf/RawDXF.java +404 -0
  83. data/lib/java/processing/event/Event.java +125 -0
  84. data/lib/java/processing/event/KeyEvent.java +70 -0
  85. data/lib/java/processing/event/MouseEvent.java +114 -0
  86. data/lib/java/processing/event/TouchEvent.java +57 -0
  87. data/lib/java/processing/javafx/PGraphicsFX2D.java +32 -0
  88. data/lib/java/processing/javafx/PSurfaceFX.java +173 -0
  89. data/lib/java/processing/net/Client.java +744 -0
  90. data/lib/java/processing/net/Server.java +388 -0
  91. data/lib/java/processing/opengl/FontTexture.java +378 -0
  92. data/lib/java/processing/opengl/FrameBuffer.java +513 -0
  93. data/lib/java/processing/opengl/LinePath.java +627 -0
  94. data/lib/java/processing/opengl/LineStroker.java +681 -0
  95. data/lib/java/processing/opengl/PGL.java +3483 -0
  96. data/lib/java/processing/opengl/PGraphics2D.java +615 -0
  97. data/lib/java/processing/opengl/PGraphics3D.java +281 -0
  98. data/lib/java/processing/opengl/PGraphicsOpenGL.java +13753 -0
  99. data/lib/java/processing/opengl/PJOGL.java +2008 -0
  100. data/lib/java/processing/opengl/PShader.java +1484 -0
  101. data/lib/java/processing/opengl/PShapeOpenGL.java +5269 -0
  102. data/lib/java/processing/opengl/PSurfaceJOGL.java +1385 -0
  103. data/lib/java/processing/opengl/Texture.java +1696 -0
  104. data/lib/java/processing/opengl/VertexBuffer.java +88 -0
  105. data/lib/java/processing/opengl/cursors/arrow.png +0 -0
  106. data/lib/java/processing/opengl/cursors/cross.png +0 -0
  107. data/lib/java/processing/opengl/cursors/hand.png +0 -0
  108. data/lib/java/processing/opengl/cursors/license.txt +27 -0
  109. data/lib/java/processing/opengl/cursors/move.png +0 -0
  110. data/lib/java/processing/opengl/cursors/text.png +0 -0
  111. data/lib/java/processing/opengl/cursors/wait.png +0 -0
  112. data/lib/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
  113. data/lib/java/processing/opengl/shaders/ColorVert.glsl +34 -0
  114. data/lib/java/processing/opengl/shaders/LightFrag.glsl +33 -0
  115. data/lib/java/processing/opengl/shaders/LightVert.glsl +151 -0
  116. data/lib/java/processing/opengl/shaders/LineFrag.glsl +32 -0
  117. data/lib/java/processing/opengl/shaders/LineVert.glsl +100 -0
  118. data/lib/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
  119. data/lib/java/processing/opengl/shaders/PointFrag.glsl +32 -0
  120. data/lib/java/processing/opengl/shaders/PointVert.glsl +56 -0
  121. data/lib/java/processing/opengl/shaders/TexFrag.glsl +37 -0
  122. data/lib/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
  123. data/lib/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
  124. data/lib/java/processing/opengl/shaders/TexVert.glsl +38 -0
  125. data/lib/java/processing/pdf/PGraphicsPDF.java +581 -0
  126. data/lib/java/processing/svg/PGraphicsSVG.java +378 -0
  127. data/lib/propane/app.rb +9 -10
  128. data/lib/propane/runner.rb +10 -12
  129. data/lib/propane/version.rb +1 -1
  130. data/library/pdf/pdf.rb +7 -0
  131. data/library/svg/svg.rb +7 -0
  132. data/mvnw +3 -3
  133. data/mvnw.cmd +2 -2
  134. data/pom.rb +30 -3
  135. data/pom.xml +54 -3
  136. data/propane.gemspec +7 -3
  137. data/src/main/java/monkstone/FastNoiseModuleJava.java +127 -0
  138. data/src/main/java/monkstone/MathToolModule.java +30 -30
  139. data/src/main/java/monkstone/PropaneLibrary.java +2 -0
  140. data/src/main/java/monkstone/SmoothNoiseModuleJava.java +127 -0
  141. data/src/main/java/monkstone/fastmath/DegLutTables.java +111 -0
  142. data/src/main/java/monkstone/fastmath/Deglut.java +6 -56
  143. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  144. data/src/main/java/monkstone/noise/OpenSimplex2F.java +813 -0
  145. data/src/main/java/monkstone/noise/OpenSimplex2S.java +1138 -0
  146. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  147. data/src/main/java/monkstone/vecmath/JRender.java +6 -6
  148. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +20 -19
  149. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +12 -12
  150. data/src/main/java/processing/awt/PGraphicsJava2D.java +11 -3
  151. data/src/main/java/processing/core/PApplet.java +13242 -13374
  152. data/src/main/java/processing/core/PConstants.java +155 -163
  153. data/src/main/java/processing/core/PGraphics.java +118 -111
  154. data/src/main/java/processing/opengl/PJOGL.java +6 -5
  155. data/src/main/java/processing/pdf/PGraphicsPDF.java +581 -0
  156. data/src/main/java/processing/svg/PGraphicsSVG.java +378 -0
  157. data/test/deglut_spec_test.rb +2 -2
  158. data/vendors/Rakefile +1 -1
  159. metadata +146 -17
  160. data/library/simplex_noise/simplex_noise.rb +0 -5
  161. data/src/main/java/monkstone/noise/SimplexNoise.java +0 -436
@@ -0,0 +1,483 @@
1
+ package processing.core;
2
+
3
+ import java.io.BufferedReader;
4
+ import java.io.File;
5
+ import java.io.IOException;
6
+ import java.util.ArrayList;
7
+ import java.util.HashMap;
8
+ import java.util.Map;
9
+
10
+ /**
11
+ * This class is not part of the Processing API and should not be used
12
+ * directly. Instead, use loadShape() and methods like it, which will make
13
+ * use of this class. Using this class directly will cause your code to break
14
+ * when combined with future versions of Processing.
15
+ * <p>
16
+ * OBJ loading implemented using code from Saito's OBJLoader library:
17
+ * http://code.google.com/p/saitoobjloader/
18
+ * and OBJReader from Ahmet Kizilay
19
+ * http://www.openprocessing.org/visuals/?visualID=191
20
+ *
21
+ */
22
+ public class PShapeOBJ extends PShape {
23
+
24
+ /**
25
+ * Initializes a new OBJ Object with the given filename.
26
+ * @param parent
27
+ * @param filename
28
+ */
29
+ public PShapeOBJ(PApplet parent, String filename) {
30
+ this(parent, parent.createReader(filename), getBasePath(parent, filename));
31
+ }
32
+
33
+ public PShapeOBJ(PApplet parent, BufferedReader reader) {
34
+ this(parent, reader, "");
35
+ }
36
+
37
+ public PShapeOBJ(PApplet parent, BufferedReader reader, String basePath) {
38
+ ArrayList<OBJFace> faces = new ArrayList<>();
39
+ ArrayList<OBJMaterial> materials = new ArrayList<>();
40
+ ArrayList<PVector> coords = new ArrayList<>();
41
+ ArrayList<PVector> normals = new ArrayList<>();
42
+ ArrayList<PVector> texcoords = new ArrayList<>();
43
+ parseOBJ(parent, basePath, reader,
44
+ faces, materials, coords, normals, texcoords);
45
+
46
+ // The OBJ geometry is stored with each face in a separate child shape.
47
+ parent = null;
48
+ family = GROUP;
49
+ addChildren(faces, materials, coords, normals, texcoords);
50
+ }
51
+
52
+
53
+ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
54
+ ArrayList<PVector> coords,
55
+ ArrayList<PVector> normals,
56
+ ArrayList<PVector> texcoords) {
57
+ family = GEOMETRY;
58
+ switch (face.vertIdx.size()) {
59
+ case 3:
60
+ kind = TRIANGLES;
61
+ break;
62
+ case 4:
63
+ kind = QUADS;
64
+ break;
65
+ default:
66
+ kind = POLYGON;
67
+ break;
68
+ }
69
+
70
+ stroke = false;
71
+ fill = true;
72
+
73
+ // Setting material properties for the new face
74
+ fillColor = rgbaValue(mtl.kd);
75
+ ambientColor = rgbaValue(mtl.ka);
76
+ specularColor = rgbaValue(mtl.ks);
77
+ shininess = mtl.ns;
78
+ if (mtl.kdMap != null) {
79
+ // If current material is textured, then tinting the texture using the
80
+ // diffuse color.
81
+ tintColor = rgbaValue(mtl.kd, mtl.d);
82
+ }
83
+
84
+ vertexCount = face.vertIdx.size();
85
+ vertices = new float[vertexCount][12];
86
+ for (int j = 0; j < face.vertIdx.size(); j++){
87
+ int vertIdx, normIdx, texIdx;
88
+ PVector vert, norms, tex;
89
+
90
+ vert = norms = tex = null;
91
+
92
+ vertIdx = face.vertIdx.get(j) - 1;
93
+ vert = coords.get(vertIdx);
94
+
95
+ if (j < face.normIdx.size()) {
96
+ normIdx = face.normIdx.get(j) - 1;
97
+ if (-1 < normIdx) {
98
+ norms = normals.get(normIdx);
99
+ }
100
+ }
101
+
102
+ if (j < face.texIdx.size()) {
103
+ texIdx = face.texIdx.get(j) - 1;
104
+ if (-1 < texIdx) {
105
+ tex = texcoords.get(texIdx);
106
+ }
107
+ }
108
+
109
+ vertices[j][X] = vert.x;
110
+ vertices[j][Y] = vert.y;
111
+ vertices[j][Z] = vert.z;
112
+
113
+ vertices[j][PGraphics.R] = mtl.kd.x;
114
+ vertices[j][PGraphics.G] = mtl.kd.y;
115
+ vertices[j][PGraphics.B] = mtl.kd.z;
116
+ vertices[j][PGraphics.A] = 1;
117
+
118
+ if (norms != null) {
119
+ vertices[j][PGraphics.NX] = norms.x;
120
+ vertices[j][PGraphics.NY] = norms.y;
121
+ vertices[j][PGraphics.NZ] = norms.z;
122
+ }
123
+
124
+ if (tex != null) {
125
+ vertices[j][PGraphics.U] = tex.x;
126
+ vertices[j][PGraphics.V] = tex.y;
127
+ }
128
+
129
+ if (mtl != null && mtl.kdMap != null) {
130
+ image = mtl.kdMap;
131
+ }
132
+ }
133
+ }
134
+
135
+
136
+ protected void addChildren(ArrayList<OBJFace> faces,
137
+ ArrayList<OBJMaterial> materials,
138
+ ArrayList<PVector> coords,
139
+ ArrayList<PVector> normals,
140
+ ArrayList<PVector> texcoords) {
141
+ int mtlIdxCur = -1;
142
+ OBJMaterial mtl = null;
143
+ for (int i = 0; i < faces.size(); i++) {
144
+ OBJFace face = faces.get(i);
145
+
146
+ // Getting current material.
147
+ if (mtlIdxCur != face.matIdx || face.matIdx == -1) {
148
+ // To make sure that at least we get the default material
149
+ mtlIdxCur = PApplet.max(0, face.matIdx);
150
+ mtl = materials.get(mtlIdxCur);
151
+ }
152
+
153
+ // Creating child shape for current face.
154
+ PShape child = new PShapeOBJ(face, mtl, coords, normals, texcoords);
155
+ addChild(child);
156
+ }
157
+ }
158
+
159
+
160
+ static protected void parseOBJ(PApplet parent, String path,
161
+ BufferedReader reader,
162
+ ArrayList<OBJFace> faces,
163
+ ArrayList<OBJMaterial> materials,
164
+ ArrayList<PVector> coords,
165
+ ArrayList<PVector> normals,
166
+ ArrayList<PVector> texcoords) {
167
+ Map<String, Integer> mtlTable = new HashMap<>();
168
+ int mtlIdxCur = -1;
169
+ boolean readv, readvn, readvt;
170
+ try {
171
+
172
+ readv = readvn = readvt = false;
173
+ String line;
174
+ String gname = "object";
175
+ while ((line = reader.readLine()) != null) {
176
+ // Parse the line.
177
+ line = line.trim();
178
+ if (line.equals("") || line.indexOf('#') == 0) {
179
+ // Empty line of comment, ignore line
180
+ continue;
181
+ }
182
+
183
+ // The below patch/hack comes from Carlos Tomas Marti and is a
184
+ // fix for single backslashes in Rhino obj files
185
+
186
+ // BEGINNING OF RHINO OBJ FILES HACK
187
+ // Statements can be broken in multiple lines using '\' at the
188
+ // end of a line.
189
+ // In regular expressions, the backslash is also an escape
190
+ // character.
191
+ // The regular expression \\ matches a single backslash. This
192
+ // regular expression as a Java string, becomes "\\\\".
193
+ // That's right: 4 backslashes to match a single one.
194
+ while (line.contains("\\")) {
195
+ line = line.split("\\\\")[0];
196
+ final String s = reader.readLine();
197
+ if (s != null)
198
+ line += s;
199
+ }
200
+ // END OF RHINO OBJ FILES HACK
201
+
202
+ String[] parts = line.split("\\s+");
203
+ // if not a blank line, process the line.
204
+ if (parts.length > 0) {
205
+ switch (parts[0]) {
206
+ case "v":
207
+ {
208
+ // vertex
209
+ PVector tempv = new PVector(Float.parseFloat(parts[1]),
210
+ Float.parseFloat(parts[2]),
211
+ Float.parseFloat(parts[3]));
212
+ coords.add(tempv);
213
+ readv = true;
214
+ break;
215
+ }
216
+ case "vn":
217
+ // normal
218
+ PVector tempn = new PVector(Float.parseFloat(parts[1]),
219
+ Float.parseFloat(parts[2]),
220
+ Float.parseFloat(parts[3]));
221
+ normals.add(tempn);
222
+ readvn = true;
223
+ break;
224
+ case "vt":
225
+ {
226
+ // uv, inverting v to take into account Processing's inverted Y axis
227
+ // with respect to OpenGL.
228
+ PVector tempv = new PVector(Float.parseFloat(parts[1]),
229
+ 1 - Float.parseFloat(parts[2]));
230
+ texcoords.add(tempv);
231
+ readvt = true;
232
+ break;
233
+ }
234
+ // Object name is ignored, for now.
235
+ case "o":
236
+ break;
237
+ case "mtllib":
238
+ if (parts[1] != null) {
239
+ String fn = parts[1];
240
+ if (!fn.contains(File.separator) && !path.equals("")) {
241
+ // Relative file name, adding the base path.
242
+ fn = path + File.separator + fn;
243
+ }
244
+ BufferedReader mreader = parent.createReader(fn);
245
+ if (mreader != null) {
246
+ parseMTL(parent, fn, path, mreader, materials, mtlTable);
247
+ mreader.close();
248
+ }
249
+ } break;
250
+ case "g":
251
+ gname = 1 < parts.length ? parts[1] : "";
252
+ break;
253
+ case "usemtl":
254
+ // Getting index of current active material (will be applied on
255
+ // all subsequent faces).
256
+ if (parts[1] != null) {
257
+ String mtlname = parts[1];
258
+ if (mtlTable.containsKey(mtlname)) {
259
+ Integer tempInt = mtlTable.get(mtlname);
260
+ mtlIdxCur = tempInt;
261
+ } else {
262
+ mtlIdxCur = -1;
263
+ }
264
+ } break;
265
+ case "f":
266
+ // Face setting
267
+ OBJFace face = new OBJFace();
268
+ face.matIdx = mtlIdxCur;
269
+ face.name = gname;
270
+ for (int i = 1; i < parts.length; i++) {
271
+ String seg = parts[i];
272
+
273
+ if (seg.indexOf("/") > 0) {
274
+ String[] forder = seg.split("/");
275
+
276
+ if (forder.length > 2) {
277
+ // Getting vertex and texture and normal indexes.
278
+ if (forder[0].length() > 0 && readv) {
279
+ face.vertIdx.add(Integer.valueOf(forder[0]));
280
+ }
281
+
282
+ if (forder[1].length() > 0 && readvt) {
283
+ face.texIdx.add(Integer.valueOf(forder[1]));
284
+ }
285
+
286
+ if (forder[2].length() > 0 && readvn) {
287
+ face.normIdx.add(Integer.valueOf(forder[2]));
288
+ }
289
+ } else if (forder.length > 1) {
290
+ // Getting vertex and texture/normal indexes.
291
+ if (forder[0].length() > 0 && readv) {
292
+ face.vertIdx.add(Integer.valueOf(forder[0]));
293
+ }
294
+
295
+ if (forder[1].length() > 0) {
296
+ if (readvt) {
297
+ face.texIdx.add(Integer.valueOf(forder[1]));
298
+ } else if (readvn) {
299
+ face.normIdx.add(Integer.valueOf(forder[1]));
300
+ }
301
+
302
+ }
303
+
304
+ } else if (forder.length > 0) {
305
+ // Getting vertex index only.
306
+ if (forder[0].length() > 0 && readv) {
307
+ face.vertIdx.add(Integer.valueOf(forder[0]));
308
+ }
309
+ }
310
+ } else {
311
+ // Getting vertex index only.
312
+ if (seg.length() > 0 && readv) {
313
+ face.vertIdx.add(Integer.valueOf(seg));
314
+ }
315
+ }
316
+ } faces.add(face);
317
+ break;
318
+ default:
319
+ break;
320
+ }
321
+ }
322
+ }
323
+
324
+ if (materials.isEmpty()) {
325
+ // No materials definition so far. Adding one default material.
326
+ OBJMaterial defMtl = new OBJMaterial();
327
+ materials.add(defMtl);
328
+ }
329
+
330
+ } catch (IOException | NumberFormatException e) {
331
+ }
332
+ }
333
+
334
+
335
+ static protected void parseMTL(PApplet parent, String mtlfn, String path,
336
+ BufferedReader reader,
337
+ ArrayList<OBJMaterial> materials,
338
+ Map<String, Integer> materialsHash) {
339
+ try {
340
+ String line;
341
+ OBJMaterial currentMtl = null;
342
+ while ((line = reader.readLine()) != null) {
343
+ // Parse the line
344
+ line = line.trim();
345
+ String[] parts = line.split("\\s+");
346
+ if (parts.length > 0) {
347
+ // Extract the material data.
348
+ if (parts[0].equals("newmtl")) {
349
+ // Starting new material.
350
+ String mtlname = parts[1];
351
+ currentMtl = addMaterial(mtlname, materials, materialsHash);
352
+ } else {
353
+ if (currentMtl == null) {
354
+ currentMtl = addMaterial("material" + materials.size(),
355
+ materials, materialsHash);
356
+ }
357
+ if (parts[0].equals("map_Kd") && parts.length > 1) {
358
+ // Loading texture map.
359
+ String texname = parts[1];
360
+ if (!texname.contains(File.separator) && !path.equals("")) {
361
+ // Relative file name, adding the base path.
362
+ texname = path + File.separator + texname;
363
+ }
364
+
365
+ File file = new File(parent.dataPath(texname));
366
+ if (file.exists()) {
367
+ currentMtl.kdMap = parent.loadImage(texname);
368
+ } else {
369
+ System.err.println("The texture map \"" + texname + "\" " +
370
+ "in the materials definition file \"" + mtlfn + "\" " +
371
+ "is missing or inaccessible, make sure " +
372
+ "the URL is valid or that the file has been " +
373
+ "added to your sketch and is readable.");
374
+ }
375
+ } else if (parts[0].equals("Ka") && parts.length > 3) {
376
+ // The ambient color of the material
377
+ currentMtl.ka.x = Float.parseFloat(parts[1]);
378
+ currentMtl.ka.y = Float.parseFloat(parts[2]);
379
+ currentMtl.ka.z = Float.parseFloat(parts[3]);
380
+ } else if (parts[0].equals("Kd") && parts.length > 3) {
381
+ // The diffuse color of the material
382
+ currentMtl.kd.x = Float.parseFloat(parts[1]);
383
+ currentMtl.kd.y = Float.parseFloat(parts[2]);
384
+ currentMtl.kd.z = Float.parseFloat(parts[3]);
385
+ } else if (parts[0].equals("Ks") && parts.length > 3) {
386
+ // The specular color weighted by the specular coefficient
387
+ currentMtl.ks.x = Float.parseFloat(parts[1]);
388
+ currentMtl.ks.y = Float.parseFloat(parts[2]);
389
+ currentMtl.ks.z = Float.parseFloat(parts[3]);
390
+ } else if ((parts[0].equals("d") ||
391
+ parts[0].equals("Tr")) && parts.length > 1) {
392
+ // Reading the alpha transparency.
393
+ currentMtl.d = Float.parseFloat(parts[1]);
394
+ } else if (parts[0].equals("Ns") && parts.length > 1) {
395
+ // The specular component of the Phong shading model
396
+ currentMtl.ns = Float.parseFloat(parts[1]);
397
+ }
398
+ }
399
+ }
400
+ }
401
+ } catch (IOException | NumberFormatException e) {
402
+ }
403
+ }
404
+
405
+ protected static OBJMaterial addMaterial(String mtlname,
406
+ ArrayList<OBJMaterial> materials,
407
+ Map<String, Integer> materialsHash) {
408
+ OBJMaterial currentMtl = new OBJMaterial(mtlname);
409
+ materialsHash.put(mtlname, materials.size());
410
+ materials.add(currentMtl);
411
+ return currentMtl;
412
+ }
413
+
414
+ protected static int rgbaValue(PVector color) {
415
+ return 0xFF000000 | ((int)(color.x * 255) << 16) |
416
+ ((int)(color.y * 255) << 8) |
417
+ (int)(color.z * 255);
418
+ }
419
+
420
+
421
+ protected static int rgbaValue(PVector color, float alpha) {
422
+ return ((int)(alpha * 255) << 24) |
423
+ ((int)(color.x * 255) << 16) |
424
+ ((int)(color.y * 255) << 8) |
425
+ (int)(color.z * 255);
426
+ }
427
+
428
+
429
+ // Stores a face from an OBJ file
430
+ static protected class OBJFace {
431
+ ArrayList<Integer> vertIdx;
432
+ ArrayList<Integer> texIdx;
433
+ ArrayList<Integer> normIdx;
434
+ int matIdx;
435
+ String name;
436
+
437
+ OBJFace() {
438
+ vertIdx = new ArrayList<>();
439
+ texIdx = new ArrayList<>();
440
+ normIdx = new ArrayList<>();
441
+ matIdx = -1;
442
+ name = "";
443
+ }
444
+ }
445
+
446
+
447
+ static protected String getBasePath(PApplet parent, String filename) {
448
+ // Obtaining the path
449
+ File file = new File(parent.dataPath(filename));
450
+ if (!file.exists()) {
451
+ file = parent.sketchFile(filename);
452
+ }
453
+ String absolutePath = file.getAbsolutePath();
454
+ return absolutePath.substring(0,
455
+ absolutePath.lastIndexOf(File.separator));
456
+ }
457
+
458
+
459
+ // Stores a material defined in an MTL file.
460
+ static protected class OBJMaterial {
461
+ String name;
462
+ PVector ka;
463
+ PVector kd;
464
+ PVector ks;
465
+ float d;
466
+ float ns;
467
+ PImage kdMap;
468
+
469
+ OBJMaterial() {
470
+ this("default");
471
+ }
472
+
473
+ OBJMaterial(String name) {
474
+ this.name = name;
475
+ ka = new PVector(0.5f, 0.5f, 0.5f);
476
+ kd = new PVector(0.5f, 0.5f, 0.5f);
477
+ ks = new PVector(0.5f, 0.5f, 0.5f);
478
+ d = 1.0f;
479
+ ns = 0.0f;
480
+ kdMap = null;
481
+ }
482
+ }
483
+ }