2 import java.awt.event.*;
4 import javax.media.opengl.*;
5 import javax.media.opengl.glu.*;
8 // FEATURE: check google's 3D warehouse for sample shapes
10 public class Main implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener {
13 boolean shift = false;
14 boolean control = false;
16 public void mouseWheelMoved(MouseWheelEvent e) {
17 tz -= e.getWheelRotation() ;
20 public void keyTyped(KeyEvent e) { }
21 public void keyPressed(KeyEvent e) {
22 switch(e.getKeyCode()) {
23 case KeyEvent.VK_CONTROL: control = true; break;
24 case KeyEvent.VK_ALT: alt = true; break;
25 case KeyEvent.VK_SHIFT: shift = true; break;
28 public void keyReleased(KeyEvent e) {
29 switch(e.getKeyCode()) {
30 case KeyEvent.VK_CONTROL: control = false; break;
31 case KeyEvent.VK_ALT: alt = false; break;
32 case KeyEvent.VK_SHIFT: shift = false; break;
36 public void mouseClicked(MouseEvent e) { }
37 public void mouseEntered(MouseEvent e) { }
38 public void mouseExited(MouseEvent e) { }
39 public void mousePressed(MouseEvent e) { }
40 public void mouseReleased(MouseEvent e) { }
44 public void mouseMoved(MouseEvent e) {
54 public void mouseDragged(MouseEvent e) {
56 tx += (mousex - e.getX())/(float)20;
57 ty += (mousey - e.getY())/(float)20;
59 anglex -= mousex - e.getX();
60 angley += mousey - e.getY();
66 private Geom geom = new Geom();
68 /** magnification factor */
69 private static final float MAG = 1;
71 Geom.V[] translations;
74 public Main(StlFile stlf) {
76 Geom.P ltf = geom.newP(-0.2, 0.1, 0.1);
77 Geom.P mtf = geom.newP( 0.0, 0.1, 0.1);
78 Geom.P rtf = geom.newP( 0.2, 0.1, 0.1);
79 Geom.P ltn = geom.newP(-0.2, 0.1, -0.1);
80 Geom.P mtn = geom.newP( 0.0, 0.1, -0.1);
81 Geom.P rtn = geom.newP( 0.2, 0.1, -0.1);
82 Geom.P lbf = geom.newP(-0.2, -0.1, 0.1);
83 Geom.P mbf = geom.newP( 0.0, -0.1, 0.1);
84 Geom.P rbf = geom.newP( 0.2, -0.1, 0.1);
85 Geom.P lbn = geom.newP(-0.2, -0.1, -0.1);
86 Geom.P mbn = geom.newP( 0.0, -0.1, -0.1);
87 Geom.P rbn = geom.newP( 0.2, -0.1, -0.1);
89 points = new Geom.P[] {
104 translations = new Geom.V[] {
105 geom.new V(-0.2, 0.2, 0),
106 geom.new V( 0.2, 0.2, 0),
107 geom.new V(-0.2, -0.2, 0),
108 geom.new V( 0.2, -0.2, 0),
109 geom.new V( 0.4, 0, 0),
110 geom.new V(-0.4, 0, 0),
111 geom.new V( 0, 0, 0.2),
112 geom.new V( 0, 0, -0.2),
116 geom.newT(ltf, mtf, mtn);
117 geom.newT(mtn, ltn, ltf);
118 geom.newT(mtf, rtf, rtn);
119 geom.newT(rtn, mtn, mtf);
121 // bottom (swap normals)
122 geom.newT(mbf, lbf, mbn);
123 geom.newT(lbn, mbn, lbf);
124 geom.newT(rbf, mbf, rbn);
125 geom.newT(mbn, rbn, mbf);
128 geom.newT(ltf, ltn, lbn);
129 geom.newT(lbn, lbf, ltf);
131 // right (swap normals)
132 geom.newT(rtn, rtf, rbn);
133 geom.newT(rbf, rbn, rtf);
136 geom.newT(ltn, mtn, mbn);
137 geom.newT(ltn, mbn, lbn);
138 geom.newT(mtn, rtn, rbn);
139 geom.newT(mtn, rbn, mbn);
142 geom.newT(mtf, ltf, mbf);
143 geom.newT(mbf, ltf, lbf);
144 geom.newT(rtf, mtf, rbf);
145 geom.newT(rbf, mtf, mbf);
147 for(Geom.V v : translations) {
148 for(Geom.T t1 : geom) {
149 for(Geom.T t2 : geom) {
150 if (t1==t2) continue;
152 if ((t1.p1().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
153 (t1.p2().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
154 (t1.p3().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
155 t1.e1().bind(t2.e3().pair);
156 t1.e2().bind(t2.e2().pair);
157 t1.e3().bind(t2.e1().pair);
159 if ((t1.p2().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
160 (t1.p3().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
161 (t1.p1().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
162 t1.e2().bind(t2.e3().pair);
163 t1.e3().bind(t2.e2().pair);
164 t1.e1().bind(t2.e1().pair);
166 if ((t1.p3().plus(v).minus(t2.p1()).mag() < Geom.EPSILON) &&
167 (t1.p1().plus(v).minus(t2.p3()).mag() < Geom.EPSILON) &&
168 (t1.p2().plus(v).minus(t2.p2()).mag() < Geom.EPSILON)) {
169 t1.e3().bind(t2.e3().pair);
170 t1.e1().bind(t2.e2().pair);
171 t1.e2().bind(t2.e1().pair);
177 //Geom.P mid = geom.newE(ltf, lbn).shatter();
182 //mid.move(geom.new V((float)-0.05,0,0));
184 //mtf.move(geom.new V(0, (float)-0.05, (float)0.05));
187 for(Geom.T t : geom) {
193 for(int i=0; i<100; i++) {
200 for(int i=0; i<stlf.coordArray.length; i+=3) {
201 Geom.P p0 = geom.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
202 Geom.P p1 = geom.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
203 Geom.P p2 = geom.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
204 Geom.V n = geom.new V(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y * MAG, stlf.normArray[i/3].z * MAG);
205 Geom.T t = geom.newT(p0, p1, p2, n);
211 Random random = new Random();
214 int which = Math.abs(random.nextInt()) % points.length;
215 float r1 = Math.abs(random.nextFloat());
216 r1 = r1 - (float)Math.floor(r1);
217 r1 = r1 * (float)0.01;
218 r1 = r1 - (float)0.005;
219 switch(Math.abs(random.nextInt()) % 3) {
220 case 0: points[which].move(geom.new V((float)0, (float)r1, (float)0.00)); break;
221 case 1: points[which].move(geom.new V((float)r1, (float)0, (float)0.00)); break;
222 case 2: points[which].move(geom.new V((float)0, (float)0, (float)r1)); break;
227 * Take care of initialization here.
229 public void init(GLAutoDrawable gld) {
231 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
232 gl.glViewport(0, 0, 500, 300);
233 gl.glMatrixMode(GL.GL_PROJECTION);
234 gl.glEnable(GL.GL_DEPTH_TEST);
235 gl.glClearDepth(1.0);
236 gl.glDepthFunc(GL.GL_LEQUAL);
238 //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
242 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
243 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
244 public void display(GLAutoDrawable drawable) {
245 GL gl = drawable.getGL();
247 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
248 gl.glPointSize(5.0f);
250 glu.gluPerspective(50-tz, ((float)drawable.getWidth())/drawable.getHeight(), 0.5, 10);
251 glu.gluLookAt(0, 0, -1, 0, 0, 0, 0, 1, 0);
252 gl.glTranslatef(tx/(float)20, ty/(float)20, 0);
253 gl.glRotatef(anglex, 0, 1, 0);
254 gl.glRotatef(angley, 1, 0, 0);
256 gl.glBegin(GL.GL_TRIANGLES);
260 for(Geom.V v1 : translations) {
261 if (v1.z==0 && v1.y==0) continue;
262 Geom.V v = v1.times((float)1.1);
263 gl.glTranslatef(v.x, v.y, v.z);
265 gl.glTranslatef(-v.x, -v.y, -v.z);
270 private void draw(GL gl, boolean triangles) {
274 for(Geom.T t : geom) {
275 if (red < 0.15) red = 1.0f;
276 if (green < 0.15) green = 1.0f;
277 if (blue < 0.15) blue = 1.0f;
281 gl.glColor3f(red, green, blue);
283 case 0: gl.glColor3f((float)0.25, (float)0.25, (float)0.75); break;
284 case 1: gl.glColor3f((float)0.25, (float)0.75, (float)0.25); break;
285 case 2: gl.glColor3f((float)0.75, (float)0.25, (float)0.25); break;
286 case 3: gl.glColor3f((float)0.50, (float)0.50, (float)0.50); break;
288 //gl.glBegin(GL.GL_LINES);
290 gl.glBegin(GL.GL_TRIANGLES);
294 gl.glBegin(GL.GL_LINES);
295 t.e1().p1.glVertex(gl);
296 t.e1().p2.glVertex(gl);
297 t.e2().p1.glVertex(gl);
298 t.e2().p2.glVertex(gl);
299 t.e3().p1.glVertex(gl);
300 t.e3().p2.glVertex(gl);
304 Geom.P centroid = t.centroid();
305 gl.glBegin(GL.GL_LINES);
306 gl.glColor3f(1, 1, 1);
308 centroid.glVertex(gl);
309 centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
313 t.p1().plus(t.p1().norm().times(t.diameter())).glVertex(gl);
315 t.p2().plus(t.p2().norm().times(t.diameter())).glVertex(gl);
317 t.p3().plus(t.p3().norm().times(t.diameter())).glVertex(gl);
324 public static void main(String[] s) throws Exception {
325 StlFile stlf = new StlFile();
326 stlf.load("teapot.stl");
327 Main main = new Main(stlf);
328 Frame f = new Frame();
329 GLCapabilities glcaps = new GLCapabilities();
330 GLCanvas glcanvas = new GLCanvas();
331 glcanvas.addGLEventListener(main);
332 f.add(glcanvas, BorderLayout.CENTER);
338 glcanvas.addMouseListener(main);
339 glcanvas.addMouseMotionListener(main);
340 glcanvas.addMouseWheelListener(main);
341 glcanvas.addKeyListener(main);