fixed vertex normals
[anneal.git] / src / Main.java
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 import javax.media.opengl.*;
5 import javax.media.opengl.glu.*;
6
7 // FEATURE: check google's 3D warehouse for sample shapes
8
9 public class Main implements GLEventListener {
10
11     private Geom geom = new Geom();
12
13     /** magnification factor */
14     private static final float MAG = 1;
15
16     Geom.V[] translations;
17     Geom.P[] points;
18
19     public Main(StlFile stlf) {
20
21
22         Geom.P ltf = geom.newP(-0.2,  0.1,  0.1);
23         Geom.P mtf = geom.newP( 0.0,  0.1,  0.1);
24         Geom.P rtf = geom.newP( 0.2,  0.1,  0.1);
25         Geom.P ltn = geom.newP(-0.2,  0.1, -0.1);
26         Geom.P mtn = geom.newP( 0.0,  0.1, -0.1);
27         Geom.P rtn = geom.newP( 0.2,  0.1, -0.1);
28         Geom.P lbf = geom.newP(-0.2, -0.1,  0.1);
29         Geom.P mbf = geom.newP( 0.0, -0.1,  0.1);
30         Geom.P rbf = geom.newP( 0.2, -0.1,  0.1);
31         Geom.P lbn = geom.newP(-0.2, -0.1, -0.1);
32         Geom.P mbn = geom.newP( 0.0, -0.1, -0.1);
33         Geom.P rbn = geom.newP( 0.2, -0.1, -0.1);
34         
35         points = new Geom.P[] {
36             ltf,
37             mtf,
38             rtf,
39             ltn,
40             mtn,
41             rtn,
42             lbf,
43             mbf,
44             rbf,
45             lbn,
46             mbn,
47             rbn
48         };
49
50         translations = new Geom.V[] {
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.2, -0.2,    0),
55             geom.new V( 0.4,    0,    0),
56             geom.new V(-0.4,    0,    0),
57             geom.new V(   0,    0,  0.2),
58             geom.new V(   0,    0, -0.2),
59         };
60
61         // top
62         geom.newT(ltf, mtf, mtn);
63         geom.newT(mtn, ltn, ltf);
64         geom.newT(mtf, rtf, rtn);
65         geom.newT(rtn, mtn, mtf);
66
67         // bottom (swap normals)
68         geom.newT(mbf, lbf, mbn);
69         geom.newT(lbn, mbn, lbf);
70         geom.newT(rbf, mbf, rbn);
71         geom.newT(mbn, rbn, mbf);
72         
73         // left
74         geom.newT(ltf, ltn, lbn);
75         geom.newT(lbn, lbf, ltf);
76
77         // right (swap normals)
78         geom.newT(rtn, rtf, rbn);
79         geom.newT(rbf, rbn, rtf);
80
81         // front
82         geom.newT(ltn, mtn, mbn);
83         geom.newT(ltn, mbn, lbn);
84         geom.newT(mtn, rtn, rbn);
85         geom.newT(mtn, rbn, mbn);
86
87         // back
88         geom.newT(mtf, ltf, mbf);
89         geom.newT(mbf, ltf, lbf);
90         geom.newT(rtf, mtf, rbf);
91         geom.newT(rbf, mtf, mbf);
92
93         for(Geom.V v : translations) {
94             for(Geom.T t1 : geom) {
95                 for(Geom.T t2 : geom) {
96                     if (t1==t2) continue;
97
98                     if ((t1.p1().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
99                         (t1.p2().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
100                         (t1.p3().plus(v).minus(t2.p2()).mag() < Geom.EPSILON))
101                         t1.bind(t2, 0);
102                     if ((t1.p2().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
103                         (t1.p3().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
104                         (t1.p1().plus(v).minus(t2.p2()).mag() < Geom.EPSILON))
105                         t1.bind(t2, 1);
106                     if ((t1.p3().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
107                         (t1.p1().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
108                         (t1.p2().plus(v).minus(t2.p2()).mag() < Geom.EPSILON))
109                         t1.bind(t2, 2);
110                 }
111             }
112         }
113
114         //Geom.P mid = geom.newE(ltf, lbn).shatter();
115
116         //tx.e2.shatter();
117         //tx.e3.shatter();
118
119         //mid.move(geom.new V((float)-0.05,0,0));
120
121         //mtf.move(geom.new V(0, (float)-0.05, (float)0.05));
122         mtf.move(geom.new V(0, (float)-0.05, (float)0.00));
123
124         /*
125
126         for(int i=0; i<stlf.coordArray.length; i+=3) {
127             Geom.P p0 = geom.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
128             Geom.P p1 = geom.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
129             Geom.P p2 = geom.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
130             Geom.V n  = geom.new V(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y  * MAG, stlf.normArray[i/3].z * MAG);
131             Geom.T t  = geom.newT(geom.newE(p0, p1), geom.newE(p1, p2), geom.newE(p2, p0), n);
132         }
133
134         */
135
136     }
137
138     /**
139      * Take care of initialization here.
140      */
141     public void init(GLAutoDrawable gld) {
142         GL gl = gld.getGL();
143         GLU glu = new GLU();
144         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
145         gl.glClearDepth(1.0);
146         gl.glViewport(0, 0, 500, 300);
147         gl.glMatrixMode(GL.GL_PROJECTION);
148         gl.glEnable(GL.GL_DEPTH_TEST);
149         gl.glDepthFunc(GL.GL_LEQUAL);
150         
151         gl.glLoadIdentity();
152         gl.glRotatef((float)45, 1, 1, 1);
153
154         //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
155         display(gld);
156     }
157
158     public static int angle = 0;
159
160     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
161     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
162     public void display(GLAutoDrawable drawable) {
163         GL gl = drawable.getGL();
164         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
165         gl.glPointSize(5.0f);
166         gl.glLoadIdentity();
167         gl.glRotatef(angle, 1, 1, 1);
168
169         gl.glBegin(GL.GL_TRIANGLES);
170         draw(gl, true);
171         gl.glEnd();
172         /*        
173         for(Geom.V v1 : translations) {
174             if (v1.z==0 && v1.y==0) continue;
175             Geom.V v = v1.times((float)1.1);
176             gl.glTranslatef(v.x, v.y, v.z);
177             //draw(gl, false);
178             gl.glTranslatef(-v.x, -v.y, -v.z);
179         }
180         */
181     }
182
183     private void draw(GL gl, boolean triangles) {
184         float red = 0.0f;
185         float green = 0.0f;
186         float blue = 0.0f;
187         for(Geom.T t : geom) {
188             if (red < 0.15) red = 1.0f;
189             if (green < 0.15) green = 1.0f;
190             if (blue < 0.15) blue = 1.0f;
191             red -= .09f;
192             green -= .12f;
193             blue -= .15f;
194             gl.glColor3f(red, green, blue);
195             switch(t.color) {
196                 case 0: gl.glColor3f((float)0.25, (float)0.25, (float)0.75); break;
197                 case 1: gl.glColor3f((float)0.25, (float)0.75, (float)0.25); break;
198                 case 2: gl.glColor3f((float)0.75, (float)0.25, (float)0.25); break;
199                 case 3: gl.glColor3f((float)0.50, (float)0.50, (float)0.50); break;
200             }
201             //gl.glBegin(GL.GL_LINES);
202             if (triangles) {
203                 gl.glBegin(GL.GL_TRIANGLES);
204                 t.glVertices(gl);
205                 gl.glEnd();
206             } else {
207                 gl.glBegin(GL.GL_LINES);
208                 t.e1().p1.glVertex(gl);
209                 t.e1().p2.glVertex(gl);
210                 t.e2().p1.glVertex(gl);
211                 t.e2().p2.glVertex(gl);
212                 t.e3().p1.glVertex(gl);
213                 t.e3().p2.glVertex(gl);
214                 gl.glEnd();
215             }
216
217             Geom.P centroid = t.centroid();
218             gl.glBegin(GL.GL_LINES);
219             gl.glColor3f(1, 1, 1);
220             /*
221             centroid.glVertex(gl);
222             centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
223             */
224             t.p1().glVertex(gl);
225             t.p1().plus(t.p1().norm().times(t.diameter())).glVertex(gl);
226             t.p2().glVertex(gl);
227             t.p2().plus(t.p2().norm().times(t.diameter())).glVertex(gl);
228             t.p3().glVertex(gl);
229             t.p3().plus(t.p3().norm().times(t.diameter())).glVertex(gl);
230
231             gl.glEnd();
232
233         }
234     }
235
236     public static void main(String[] s) throws Exception {
237         StlFile stlf = new StlFile();
238         stlf.load("teapot.stl");
239         Main main = new Main(stlf);
240         Frame f = new Frame();
241         GLCapabilities glcaps = new GLCapabilities();
242         GLCanvas glcanvas = new GLCanvas();
243         glcanvas.addGLEventListener(main);
244         f.add(glcanvas, BorderLayout.CENTER);
245         f.pack();
246         f.show();
247         f.setSize(900, 900);
248         f.doLayout();
249
250         while(true) {
251             Thread.sleep(100);
252             angle+=5;
253             glcanvas.repaint();
254         }
255
256     }
257
258 }