Skip to content

Commit dd8661f

Browse files
author
monkstone
committed
add povwriter, make mesh_to_shape public
1 parent 061736c commit dd8661f

File tree

9 files changed

+1350
-4
lines changed

9 files changed

+1350
-4
lines changed

src/toxi/processing/MeshToVBO.java

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,22 @@
11
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
2+
* This library adds translate from toxiclibs Mesh to processing PShape (vbo)
3+
* Copyright (c) 2015 Martin Prout
4+
*
5+
* This library is free software; you can redistribute it and/or modify it under
6+
* the terms of the GNU Lesser General Public License as published by the Free
7+
* Software Foundation; either version 2.1 of the License, or (at your option)
8+
* any later version.
9+
*
10+
* http://creativecommons.org/licenses/LGPL/2.1/
11+
*
12+
* This library is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15+
* details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
520
*/
621
package toxi.processing;
722

@@ -20,11 +35,21 @@ public class MeshToVBO {
2035

2136
private final PApplet app;
2237

38+
/**
39+
*
40+
* @param app PApplet
41+
*/
2342
public MeshToVBO(PApplet app) {
2443
this.app = app;
2544
}
2645

27-
PShape meshToShape(Mesh3D mesh, boolean smooth) {
46+
/**
47+
* Use default smooth (smooth = false) for cubes etc
48+
* @param mesh Toxiclibs mesh
49+
* @param smooth boolean
50+
* @return
51+
*/
52+
public PShape meshToShape(Mesh3D mesh, boolean smooth) {
2853
PShape retained = app.createShape();
2954
retained.beginShape(PConstants.TRIANGLE);
3055
if (smooth) {
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* This library adds PovRAY export facility to toxiclibscore
3+
* Copyright (c) 2015 Martin Prout
4+
*
5+
* This library is free software; you can redistribute it and/or modify it under
6+
* the terms of the GNU Lesser General Public License as published by the Free
7+
* Software Foundation; either version 2.1 of the License, or (at your option)
8+
* any later version.
9+
*
10+
* http://creativecommons.org/licenses/LGPL/2.1/
11+
*
12+
* This library is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15+
* details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
package toxi.processing;
23+
24+
import toxi.geom.Vec3D;
25+
26+
/**
27+
*
28+
* @author Martin Prout
29+
*/
30+
public interface POVInterface {
31+
32+
/**
33+
* Output start of face_indices
34+
*
35+
* @param count
36+
*/
37+
void beginIndices(int count);
38+
39+
/**
40+
* Begin the mesh2 output as a PovRAY declaration
41+
*
42+
* @param name
43+
*/
44+
void beginMesh2(String name);
45+
46+
/**
47+
* Output start of normal_vectors
48+
*
49+
* @param count
50+
*/
51+
void beginNormals(int count);
52+
53+
/**
54+
* close the mesh declaration
55+
*/
56+
void endSave();
57+
58+
/**
59+
* End the current section ie vertex_vector, normal_vector or face_indices
60+
*/
61+
void endSection();
62+
63+
/**
64+
* Write face indices as as vector
65+
*
66+
* @param a
67+
* @param b
68+
* @param c
69+
*/
70+
void face(int a, int b, int c);
71+
72+
/**
73+
* Track the number of normals written to file
74+
*
75+
* @return current normal offset
76+
*/
77+
int getCurrNormalOffset();
78+
79+
/**
80+
* Track the number of vertices written to file
81+
*
82+
* @return vertex offset
83+
*/
84+
int getCurrVertexOffset();
85+
86+
/**
87+
* Required to keep in sync with current option
88+
*
89+
* @return option Textures
90+
*/
91+
Textures getTexture();
92+
93+
/**
94+
* Write normal as PovRAY vector
95+
*
96+
* @param n
97+
*/
98+
void normal(Vec3D n);
99+
100+
/**
101+
* Required to keep in sync with current option
102+
*
103+
* @param opt
104+
*/
105+
void setTexture(Textures opt);
106+
107+
/**
108+
* Used to output total count vertex_vector, normal_vector & face_indices
109+
*
110+
* @param count
111+
*/
112+
void total(int count);
113+
114+
/**
115+
* Write vertex as PovRAY vector
116+
*
117+
* @param v
118+
*/
119+
void vertex(Vec3D v);
120+
121+
}

src/toxi/processing/POVMesh.java

Lines changed: 219 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,219 @@
1+
/*
2+
* This library adds PovRAY export facility to toxiclibscore
3+
* Copyright (c) 2015 Martin Prout
4+
*
5+
* This library is free software; you can redistribute it and/or modify it under
6+
* the terms of the GNU Lesser General Public License as published by the Free
7+
* Software Foundation; either version 2.1 of the License, or (at your option)
8+
* any later version.
9+
*
10+
* http://creativecommons.org/licenses/LGPL/2.1/
11+
*
12+
* This library is distributed in the hope that it will be useful, but WITHOUT
13+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14+
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
15+
* details.
16+
*
17+
* You should have received a copy of the GNU Lesser General Public License
18+
* along with this library; if not, write to the Free Software Foundation, Inc.,
19+
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
package toxi.processing;
22+
23+
import java.io.File;
24+
import processing.core.PApplet;
25+
import toxi.geom.AABB;
26+
import toxi.geom.Vec3D;
27+
28+
/**
29+
* This class provides access to underlying TriangleMesh parameters to allow
30+
* export in PovRAY mesh2 format (with or without normals)
31+
*
32+
* @author Martin Prout
33+
*/
34+
public class POVMesh {
35+
36+
private POVWriter pov;
37+
private Textures opt;
38+
private final PApplet parent;
39+
/**
40+
*
41+
*/
42+
static String VERSION = "0.58";
43+
44+
/**
45+
* Default constructor this mesh no texture
46+
*
47+
* @param app
48+
*/
49+
public POVMesh(PApplet app) {
50+
parent = app;
51+
parent.registerMethod("dispose", this);
52+
}
53+
54+
/**
55+
* Allows the option to change texture option per mesh
56+
*
57+
* @param opt
58+
*/
59+
public void setTexture(Textures opt) {
60+
this.opt = opt;
61+
}
62+
63+
/**
64+
* Saves the mesh as PovRAY mesh2 format by appending it to the given mesh
65+
* {@link POVWriter} instance. Saves normals.
66+
*
67+
* @param meshArray
68+
*/
69+
public void saveAsPOV(toxi.geom.mesh.TriangleMesh[] meshArray) {
70+
saveAsMesh(meshArray, true);
71+
}
72+
73+
/**
74+
* Saves the mesh as PovRAY mesh2 format by appending it to the given mesh
75+
* {@link POVWriter} instance. Saves normals.
76+
*
77+
* @param mesh
78+
*/
79+
public void saveAsPOV(toxi.geom.mesh.TriangleMesh mesh) {
80+
saveAsPOV(mesh, true);
81+
}
82+
83+
/**
84+
* Saves the mesh as PovRAY mesh2 format to the given {@link PrintWriter}.
85+
* Without normals (when saveNormal is false), checks if option has changed,
86+
* changes option if required (implies serial use of saveAsPov)
87+
*
88+
* @param mesh
89+
* @param saveNormals boolean
90+
*/
91+
public void saveAsPOV(toxi.geom.mesh.TriangleMesh mesh, boolean saveNormals) {
92+
AABB boundingBox = mesh.getBoundingBox();
93+
Vec3D min = boundingBox.getMin();
94+
Vec3D max = boundingBox.getMax();
95+
pov.boundingBox(min, max);
96+
if (opt != pov.getTexture()) {
97+
pov.setTexture(opt);
98+
}
99+
pov.beginMesh2(mesh.name);
100+
int vOffset = pov.getCurrVertexOffset();
101+
// vertices
102+
pov.total(mesh.vertices.size());
103+
mesh.vertices.values().stream().forEach((v) -> {
104+
pov.vertex(v);
105+
});
106+
pov.endSection();
107+
// faces
108+
if (saveNormals) {
109+
// normals
110+
pov.beginNormals(mesh.vertices.size());
111+
mesh.vertices.values().stream().forEach((v) -> {
112+
pov.normal(v.normal.getNormalized());
113+
});
114+
pov.endSection();
115+
}
116+
pov.beginIndices(mesh.faces.size());
117+
mesh.faces.stream().forEach((f) -> {
118+
pov.face(f.b.id + vOffset, f.a.id + vOffset, f.c.id + vOffset);
119+
});
120+
pov.endSection();
121+
pov.endSave();
122+
}
123+
124+
/**
125+
* Saves the mesh as PovRAY mesh2 format to the given {@link PrintWriter}.
126+
* Without normals (when saveNormal is false) Check on option is included
127+
* for completeness
128+
*
129+
* @param meshArray
130+
* @param saveNormals boolean
131+
*/
132+
public void saveAsMesh(toxi.geom.mesh.TriangleMesh[] meshArray, boolean saveNormals) {
133+
AABB boundingBox;
134+
Vec3D min;
135+
Vec3D max;
136+
137+
if (opt != pov.getTexture()) {
138+
pov.setTexture(opt);
139+
}
140+
for (toxi.geom.mesh.TriangleMesh mesh : meshArray) {
141+
pov.beginMesh2(mesh.name);
142+
boundingBox = mesh.getBoundingBox();
143+
min = boundingBox.getMin();
144+
max = boundingBox.getMax();
145+
pov.boundingBox(min, max);
146+
int vOffset = pov.getCurrVertexOffset();
147+
// vertices
148+
pov.total(mesh.vertices.size());
149+
mesh.vertices.values().stream().forEach((v) -> {
150+
pov.vertex(v);
151+
});
152+
pov.endSection();
153+
// faces
154+
if (saveNormals) {
155+
// normals
156+
pov.beginNormals(mesh.vertices.size());
157+
mesh.vertices.values().stream().forEach((v) -> {
158+
pov.normal(v.normal.getNormalized());
159+
});
160+
pov.endSection();
161+
}
162+
pov.beginIndices(mesh.faces.size());
163+
mesh.faces.stream().forEach((f) -> {
164+
pov.face(f.b.id + vOffset, f.a.id + vOffset, f.c.id + vOffset);
165+
});
166+
pov.endSection();
167+
pov.endSave();
168+
}
169+
}
170+
171+
/**
172+
* Start writing *.inc file
173+
*
174+
* @param fileName main.inc File
175+
*/
176+
public void beginSave(File fileName) {
177+
opt = Textures.RAW;
178+
this.pov = new POVWriter(fileName);
179+
pov.beginForeground();
180+
}
181+
182+
/**
183+
* Finish writing *.inc file and close PrintWriter
184+
*/
185+
public void endSave() {
186+
pov.endForeground();
187+
}
188+
189+
// /**
190+
// *
191+
// * @param e
192+
// */
193+
// public void keyEvent(KeyEvent e) {
194+
// if (e.getAction() == KeyEvent.RELEASED) {
195+
// switch (e.getKey()) {
196+
// case 't':
197+
// case 'T':
198+
// status = Tracing.EXPORTING;
199+
// System.out.println("tracing");
200+
// break;
201+
// }
202+
// }
203+
// }
204+
/**
205+
* Required by processing
206+
*/
207+
public void dispose() {
208+
endSave();
209+
}
210+
211+
/**
212+
* Required by processing
213+
*
214+
* @return version String
215+
*/
216+
public String version() {
217+
return VERSION;
218+
}
219+
}

0 commit comments

Comments
 (0)