2 import java.awt.event.*;
4 import javax.media.opengl.*;
5 import javax.media.opengl.glu.*;
7 // FEATURE: check google's 3D warehouse for sample shapes
9 public class Main implements GLEventListener {
11 private Geom geom = new Geom();
13 /** magnification factor */
14 private static final float MAG = 1;
16 Geom.V[] translations;
19 public Main(StlFile stlf) {
21 Geom.P ltf = geom.newP(-0.2, 0.1, 0.1);
22 Geom.P mtf = geom.newP( 0.0, 0.1, 0.1);
23 Geom.P rtf = geom.newP( 0.2, 0.1, 0.1);
24 Geom.P ltn = geom.newP(-0.2, 0.1, -0.1);
25 Geom.P mtn = geom.newP( 0.0, 0.1, -0.1);
26 Geom.P rtn = geom.newP( 0.2, 0.1, -0.1);
27 Geom.P lbf = geom.newP(-0.2, -0.1, 0.1);
28 Geom.P mbf = geom.newP( 0.0, -0.1, 0.1);
29 Geom.P rbf = geom.newP( 0.2, -0.1, 0.1);
30 Geom.P lbn = geom.newP(-0.2, -0.1, -0.1);
31 Geom.P mbn = geom.newP( 0.0, -0.1, -0.1);
32 Geom.P rbn = geom.newP( 0.2, -0.1, -0.1);
34 points = new Geom.P[] {
49 translations = new Geom.V[] {
50 geom.new V(-0.2, 0.2, 0),
51 geom.new V( 0.2, 0.2, 0),
52 geom.new V(-0.2, -0.2, 0),
53 geom.new V( 0.2, -0.2, 0),
54 geom.new V( 0.4, 0, 0),
55 geom.new V(-0.4, 0, 0),
56 geom.new V( 0, 0, 0.2),
57 geom.new V( 0, 0, -0.2),
61 geom.newT(ltf, mtf, mtn);
62 geom.newT(mtn, ltn, ltf);
63 geom.newT(mtf, rtf, rtn);
64 geom.newT(rtn, mtn, mtf);
66 // bottom (swap normals)
67 geom.newT(mbf, lbf, mbn);
68 geom.newT(lbn, mbn, lbf);
69 geom.newT(rbf, mbf, rbn);
70 geom.newT(mbn, rbn, mbf);
73 geom.newT(ltf, ltn, lbn);
74 geom.newT(lbn, lbf, ltf);
76 // right (swap normals)
77 geom.newT(rtn, rtf, rbn);
78 geom.newT(rbf, rbn, rtf);
81 geom.newT(ltn, mtn, mbn);
82 geom.newT(ltn, mbn, lbn);
83 geom.newT(mtn, rtn, rbn);
84 geom.newT(mtn, rbn, mbn);
87 geom.newT(mtf, ltf, mbf);
88 geom.newT(mbf, ltf, lbf);
89 geom.newT(rtf, mtf, rbf);
90 geom.newT(rbf, mtf, mbf);
92 for(Geom.V v : translations) {
93 for(Geom.T t1 : geom) {
94 for(Geom.T t2 : geom) {
97 if ((t1.p1().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
98 (t1.p2().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
99 (t1.p3().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
100 t1.e1().bind(t2.e3().pair);
101 t1.e2().bind(t2.e2().pair);
102 t1.e3().bind(t2.e1().pair);
104 if ((t1.p2().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
105 (t1.p3().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
106 (t1.p1().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
107 t1.e2().bind(t2.e3().pair);
108 t1.e3().bind(t2.e2().pair);
109 t1.e1().bind(t2.e1().pair);
111 if ((t1.p3().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
112 (t1.p1().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
113 (t1.p2().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
114 t1.e3().bind(t2.e3().pair);
115 t1.e1().bind(t2.e2().pair);
116 t1.e2().bind(t2.e1().pair);
122 //Geom.P mid = geom.newE(ltf, lbn).shatter();
127 //mid.move(geom.new V((float)-0.05,0,0));
129 //mtf.move(geom.new V(0, (float)-0.05, (float)0.05));
132 for(Geom.T t : geom) {
138 mtf.move(geom.new V(0, (float)-0.05, (float)0.00));
142 for(int i=0; i<stlf.coordArray.length; i+=3) {
143 Geom.P p0 = geom.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
144 Geom.P p1 = geom.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
145 Geom.P p2 = geom.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
146 Geom.V n = geom.new V(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y * MAG, stlf.normArray[i/3].z * MAG);
147 Geom.T t = geom.newT(p0, p1, p2, n);
155 * Take care of initialization here.
157 public void init(GLAutoDrawable gld) {
160 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
161 gl.glClearDepth(1.0);
162 gl.glViewport(0, 0, 500, 300);
163 gl.glMatrixMode(GL.GL_PROJECTION);
164 gl.glEnable(GL.GL_DEPTH_TEST);
165 gl.glDepthFunc(GL.GL_LEQUAL);
168 gl.glRotatef((float)45, 1, 1, 1);
170 //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
174 public static int angle = 0;
176 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
177 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
178 public void display(GLAutoDrawable drawable) {
179 GL gl = drawable.getGL();
180 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
181 gl.glPointSize(5.0f);
183 gl.glRotatef(angle, 1, 1, 1);
185 gl.glBegin(GL.GL_TRIANGLES);
189 for(Geom.V v1 : translations) {
190 if (v1.z==0 && v1.y==0) continue;
191 Geom.V v = v1.times((float)1.1);
192 gl.glTranslatef(v.x, v.y, v.z);
194 gl.glTranslatef(-v.x, -v.y, -v.z);
199 private void draw(GL gl, boolean triangles) {
203 for(Geom.T t : geom) {
204 if (red < 0.15) red = 1.0f;
205 if (green < 0.15) green = 1.0f;
206 if (blue < 0.15) blue = 1.0f;
210 gl.glColor3f(red, green, blue);
212 case 0: gl.glColor3f((float)0.25, (float)0.25, (float)0.75); break;
213 case 1: gl.glColor3f((float)0.25, (float)0.75, (float)0.25); break;
214 case 2: gl.glColor3f((float)0.75, (float)0.25, (float)0.25); break;
215 case 3: gl.glColor3f((float)0.50, (float)0.50, (float)0.50); break;
217 //gl.glBegin(GL.GL_LINES);
219 gl.glBegin(GL.GL_TRIANGLES);
223 gl.glBegin(GL.GL_LINES);
224 t.e1().p1.glVertex(gl);
225 t.e1().p2.glVertex(gl);
226 t.e2().p1.glVertex(gl);
227 t.e2().p2.glVertex(gl);
228 t.e3().p1.glVertex(gl);
229 t.e3().p2.glVertex(gl);
233 Geom.P centroid = t.centroid();
234 gl.glBegin(GL.GL_LINES);
235 gl.glColor3f(1, 1, 1);
237 centroid.glVertex(gl);
238 centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
242 t.p1().plus(t.p1().norm().times(t.diameter())).glVertex(gl);
244 t.p2().plus(t.p2().norm().times(t.diameter())).glVertex(gl);
246 t.p3().plus(t.p3().norm().times(t.diameter())).glVertex(gl);
253 public static void main(String[] s) throws Exception {
254 StlFile stlf = new StlFile();
255 stlf.load("teapot.stl");
256 Main main = new Main(stlf);
257 Frame f = new Frame();
258 GLCapabilities glcaps = new GLCapabilities();
259 GLCanvas glcanvas = new GLCanvas();
260 glcanvas.addGLEventListener(main);
261 f.add(glcanvas, BorderLayout.CENTER);