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) {
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);
35 points = new Geom.P[] {
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),
62 geom.newT(ltf, mtf, mtn);
63 geom.newT(mtn, ltn, ltf);
64 geom.newT(mtf, rtf, rtn);
65 geom.newT(rtn, mtn, mtf);
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);
74 geom.newT(ltf, ltn, lbn);
75 geom.newT(lbn, lbf, ltf);
77 // right (swap normals)
78 geom.newT(rtn, rtf, rbn);
79 geom.newT(rbf, rbn, rtf);
82 geom.newT(ltn, mtn, mbn);
83 geom.newT(ltn, mbn, lbn);
84 geom.newT(mtn, rtn, rbn);
85 geom.newT(mtn, rbn, mbn);
88 geom.newT(mtf, ltf, mbf);
89 geom.newT(mbf, ltf, lbf);
90 geom.newT(rtf, mtf, rbf);
91 geom.newT(rbf, mtf, mbf);
93 for(Geom.V v : translations) {
94 for(Geom.T t1 : geom) {
95 for(Geom.T t2 : geom) {
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))
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))
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))
114 //Geom.P mid = geom.newE(ltf, lbn).shatter();
119 //mid.move(geom.new V((float)-0.05,0,0));
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));
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(p0, p1, p2, n);
139 * Take care of initialization here.
141 public void init(GLAutoDrawable gld) {
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);
152 gl.glRotatef((float)45, 1, 1, 1);
154 //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
158 public static int angle = 0;
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);
167 gl.glRotatef(angle, 1, 1, 1);
169 gl.glBegin(GL.GL_TRIANGLES);
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);
178 gl.glTranslatef(-v.x, -v.y, -v.z);
183 private void draw(GL gl, boolean triangles) {
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;
194 gl.glColor3f(red, green, blue);
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;
201 //gl.glBegin(GL.GL_LINES);
203 gl.glBegin(GL.GL_TRIANGLES);
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);
217 Geom.P centroid = t.centroid();
218 gl.glBegin(GL.GL_LINES);
219 gl.glColor3f(1, 1, 1);
221 centroid.glVertex(gl);
222 centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
226 t.p1().plus(t.p1().norm().times(t.diameter())).glVertex(gl);
228 t.p2().plus(t.p2().norm().times(t.diameter())).glVertex(gl);
230 t.p3().plus(t.p3().norm().times(t.diameter())).glVertex(gl);
237 public static void main(String[] s) throws Exception {
238 StlFile stlf = new StlFile();
239 stlf.load("teapot.stl");
240 Main main = new Main(stlf);
241 Frame f = new Frame();
242 GLCapabilities glcaps = new GLCapabilities();
243 GLCanvas glcanvas = new GLCanvas();
244 glcanvas.addGLEventListener(main);
245 f.add(glcanvas, BorderLayout.CENTER);