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());
101 t1.e2().bind(t2.e2());
102 t1.e3().bind(t2.e1());
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());
108 t1.e3().bind(t2.e2());
109 t1.e1().bind(t2.e1());
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());
115 t1.e1().bind(t2.e2());
116 t1.e2().bind(t2.e1());
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));
130 mtf.move(geom.new V(0, (float)-0.05, (float)0.00));
134 for(int i=0; i<stlf.coordArray.length; i+=3) {
135 Geom.P p0 = geom.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
136 Geom.P p1 = geom.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
137 Geom.P p2 = geom.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
138 Geom.V n = geom.new V(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y * MAG, stlf.normArray[i/3].z * MAG);
139 Geom.T t = geom.newT(p0, p1, p2, n);
147 * Take care of initialization here.
149 public void init(GLAutoDrawable gld) {
152 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
153 gl.glClearDepth(1.0);
154 gl.glViewport(0, 0, 500, 300);
155 gl.glMatrixMode(GL.GL_PROJECTION);
156 gl.glEnable(GL.GL_DEPTH_TEST);
157 gl.glDepthFunc(GL.GL_LEQUAL);
160 gl.glRotatef((float)45, 1, 1, 1);
162 //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
166 public static int angle = 0;
168 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
169 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
170 public void display(GLAutoDrawable drawable) {
171 GL gl = drawable.getGL();
172 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
173 gl.glPointSize(5.0f);
175 gl.glRotatef(angle, 1, 1, 1);
177 gl.glBegin(GL.GL_TRIANGLES);
181 for(Geom.V v1 : translations) {
182 if (v1.z==0 && v1.y==0) continue;
183 Geom.V v = v1.times((float)1.1);
184 gl.glTranslatef(v.x, v.y, v.z);
186 gl.glTranslatef(-v.x, -v.y, -v.z);
191 private void draw(GL gl, boolean triangles) {
195 for(Geom.T t : geom) {
196 if (red < 0.15) red = 1.0f;
197 if (green < 0.15) green = 1.0f;
198 if (blue < 0.15) blue = 1.0f;
202 gl.glColor3f(red, green, blue);
204 case 0: gl.glColor3f((float)0.25, (float)0.25, (float)0.75); break;
205 case 1: gl.glColor3f((float)0.25, (float)0.75, (float)0.25); break;
206 case 2: gl.glColor3f((float)0.75, (float)0.25, (float)0.25); break;
207 case 3: gl.glColor3f((float)0.50, (float)0.50, (float)0.50); break;
209 //gl.glBegin(GL.GL_LINES);
211 gl.glBegin(GL.GL_TRIANGLES);
215 gl.glBegin(GL.GL_LINES);
216 t.e1().p1.glVertex(gl);
217 t.e1().p2.glVertex(gl);
218 t.e2().p1.glVertex(gl);
219 t.e2().p2.glVertex(gl);
220 t.e3().p1.glVertex(gl);
221 t.e3().p2.glVertex(gl);
225 Geom.P centroid = t.centroid();
226 gl.glBegin(GL.GL_LINES);
227 gl.glColor3f(1, 1, 1);
229 centroid.glVertex(gl);
230 centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
234 t.p1().plus(t.p1().norm().times(t.diameter())).glVertex(gl);
236 t.p2().plus(t.p2().norm().times(t.diameter())).glVertex(gl);
238 t.p3().plus(t.p3().norm().times(t.diameter())).glVertex(gl);
245 public static void main(String[] s) throws Exception {
246 StlFile stlf = new StlFile();
247 stlf.load("teapot.stl");
248 Main main = new Main(stlf);
249 Frame f = new Frame();
250 GLCapabilities glcaps = new GLCapabilities();
251 GLCanvas glcanvas = new GLCanvas();
252 glcanvas.addGLEventListener(main);
253 f.add(glcanvas, BorderLayout.CENTER);