2 import java.awt.event.*;
4 import javax.media.opengl.*;
5 import javax.media.opengl.glu.*;
7 public class Main implements GLEventListener {
9 private Geom geom = new Geom();
11 /** magnification factor */
12 private static final float MAG = 1;
14 Geom.V[] translations;
17 public Main(StlFile stlf) {
19 Geom.P ltf = geom.newP(-0.2, 0.1, 0.1);
20 Geom.P mtf = geom.newP( 0.0, 0.1, 0.1);
21 Geom.P rtf = geom.newP( 0.2, 0.1, 0.1);
22 Geom.P ltn = geom.newP(-0.2, 0.1, -0.1);
23 Geom.P mtn = geom.newP( 0.0, 0.1, -0.1);
24 Geom.P rtn = geom.newP( 0.2, 0.1, -0.1);
25 Geom.P lbf = geom.newP(-0.2, -0.1, 0.1);
26 Geom.P mbf = geom.newP( 0.0, -0.1, 0.1);
27 Geom.P rbf = geom.newP( 0.2, -0.1, 0.1);
28 Geom.P lbn = geom.newP(-0.2, -0.1, -0.1);
29 Geom.P mbn = geom.newP( 0.0, -0.1, -0.1);
30 Geom.P rbn = geom.newP( 0.2, -0.1, -0.1);
32 points = new Geom.P[] {
47 translations = new Geom.V[] {
48 geom.new V(-0.2, 0.2, 0),
49 geom.new V( 0.2, 0.2, 0),
50 geom.new V(-0.2, -0.2, 0),
51 geom.new V( 0.2, -0.2, 0),
52 geom.new V( 0.4, 0, 0),
53 geom.new V(-0.4, 0, 0),
54 geom.new V( 0, 0, 0.2),
55 geom.new V( 0, 0, -0.2),
58 for(Geom.V v : translations) {
59 for(Geom.P p1 : points) {
60 for(Geom.P p2 : points) {
61 if (p1.plus(v).minus(p2).mag() < Geom.EPSILON) {
62 System.out.println("bind " + p1 + " to " + p2);
70 geom.newT(ltf, mtf, mtn);
71 geom.newT(mtn, ltn, ltf);
72 geom.newT(mtf, rtf, rtn);
73 geom.newT(rtn, mtn, mtf);
75 // bottom (swap normals)
76 geom.newT(mbf, lbf, mbn);
77 geom.newT(lbn, mbn, lbf);
78 geom.newT(rbf, mbf, rbn);
79 geom.newT(mbn, rbn, mbf);
82 geom.newT(ltf, ltn, lbn);
83 geom.newT(lbn, lbf, ltf);
85 // right (swap normals)
86 geom.newT(rtn, rtf, rbn);
87 geom.newT(rbf, rbn, rtf);
90 geom.newT(ltn, mtn, mbn);
91 geom.newT(ltn, mbn, lbn);
92 geom.newT(mtn, rtn, rbn);
93 geom.newT(mtn, rbn, mbn);
96 geom.newT(mtf, ltf, mbf);
97 geom.newT(mbf, ltf, lbf);
98 geom.newT(rtf, mtf, rbf);
99 geom.newT(rbf, mtf, mbf);
103 for(int i=0; i<stlf.coordArray.length; i+=3) {
104 Geom.P p0 = geom.newP(stlf.coordArray[i+0].x * MAG, stlf.coordArray[i+0].y * MAG, stlf.coordArray[i+0].z * MAG);
105 Geom.P p1 = geom.newP(stlf.coordArray[i+1].x * MAG, stlf.coordArray[i+1].y * MAG, stlf.coordArray[i+1].z * MAG);
106 Geom.P p2 = geom.newP(stlf.coordArray[i+2].x * MAG, stlf.coordArray[i+2].y * MAG, stlf.coordArray[i+2].z * MAG);
107 Geom.V n = geom.new V(stlf.normArray[i/3].x * MAG, stlf.normArray[i/3].y * MAG, stlf.normArray[i/3].z * MAG);
108 Geom.T t = geom.newT(geom.newE(p0, p1), geom.newE(p1, p2), geom.newE(p2, p0), n);
113 mtf.move(geom.new V(0, (float)-0.05, (float)0.05));
118 * Take care of initialization here.
120 public void init(GLAutoDrawable gld) {
123 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
124 gl.glClearDepth(1.0);
125 gl.glViewport(0, 0, 500, 300);
126 gl.glMatrixMode(GL.GL_PROJECTION);
127 gl.glEnable(GL.GL_DEPTH_TEST);
128 gl.glDepthFunc(GL.GL_LEQUAL);
131 gl.glRotatef((float)45, 1, 1, 1);
133 //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
137 public static int angle = 0;
139 public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
140 public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
141 public void display(GLAutoDrawable drawable) {
142 GL gl = drawable.getGL();
143 gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
144 gl.glPointSize(5.0f);
146 gl.glRotatef(angle, 1, 1, 1);
148 gl.glBegin(GL.GL_TRIANGLES);
152 for(Geom.V v1 : translations) {
153 if (v1.z==0 && v1.y==0) continue;
154 Geom.V v = v1.times((float)1.1);
155 gl.glTranslatef(v.x, v.y, v.z);
157 gl.glTranslatef(-v.x, -v.y, -v.z);
161 private void draw(GL gl, boolean triangles) {
165 for(Geom.T t : geom) {
166 if (red < 0.15) red = 1.0f;
167 if (green < 0.15) green = 1.0f;
168 if (blue < 0.15) blue = 1.0f;
172 gl.glColor3f(red, green, blue);
173 //gl.glBegin(GL.GL_LINES);
175 gl.glBegin(GL.GL_TRIANGLES);
179 gl.glBegin(GL.GL_LINES);
180 t.e1.p1.glVertex(gl);
181 t.e1.p2.glVertex(gl);
182 t.e2.p1.glVertex(gl);
183 t.e2.p2.glVertex(gl);
184 t.e3.p1.glVertex(gl);
185 t.e3.p2.glVertex(gl);
189 Geom.P centroid = t.centroid();
190 gl.glBegin(GL.GL_LINES);
191 gl.glColor3f(1, 1, 1);
192 centroid.glVertex(gl);
193 centroid.plus(t.norm().times(t.diameter())).glVertex(gl);
195 t.p1().plus(t.p1().norm().times(t.diameter())).glVertex(gl);
201 public static void main(String[] s) throws Exception {
202 StlFile stlf = new StlFile();
203 stlf.load("teapot.stl");
204 Main main = new Main(stlf);
205 Frame f = new Frame();
206 GLCapabilities glcaps = new GLCapabilities();
207 GLCanvas glcanvas = new GLCanvas();
208 glcanvas.addGLEventListener(main);
209 f.add(glcanvas, BorderLayout.CENTER);