2 import java.awt.event.*;
4 import javax.media.opengl.*;
5 import javax.media.opengl.glu.*;
7 public class Geom implements GLEventListener {
9 public static StlFile stlf = null;
12 * Take care of initialization here.
14 public void init(GLAutoDrawable gld) {
17 gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
18 gl.glViewport(0, 0, 500, 300);
19 gl.glMatrixMode(GL.GL_PROJECTION);
21 //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
27 * Take care of drawing here.
29 public void display(GLAutoDrawable drawable) {
33 GL gl = drawable.getGL();
34 gl.glClear(GL.GL_COLOR_BUFFER_BIT);
37 for(int i=0; i<stlf.coordArray.length; i+=3) {
41 if (red < 0.15) red = 1.0f;
42 if (green < 0.15) green = 1.0f;
43 if (blue < 0.15) blue = 1.0f;
44 gl.glColor3f(red, green, blue);
46 gl.glBegin(GL.GL_TRIANGLES);
47 for(int j=0; j<3; j++)
48 gl.glVertex3f(stlf.coordArray[i+j].x,
49 stlf.coordArray[i+j].y,
50 stlf.coordArray[i+j].z);
57 GLAutoDrawable drawable,
63 public void displayChanged(
64 GLAutoDrawable drawable,
69 /** point in 3-space */
70 public static class P {
71 public final float x, y, z;
72 public P(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
73 public V minus(P p) { return null; }
76 /** vector in 3-space */
77 public static class V {
78 private final float x, y, z;
79 public V(float x, float y, float z) { this.x = x; this.y = y; this.z = z; }
80 public V norm() { return null; /* FIXME */ }
81 public boolean sameDirection(V v) { throw new Error(); }
82 public V cross(V v) { return null; }
86 public static class E {
87 public final P p1, p2;
89 public E(P p1, P p2) { this.p1 = p1; this.p2 = p2; }
90 public boolean equals(Object o) {
91 if (o==null || !(o instanceof E)) return false;
93 if (this.p1 == e.p1 && this.p2 == e.p2) return true;
94 if (this.p2 == e.p1 && this.p1 == e.p2) return true;
97 public P shared(E e) { return null; }
100 /** a triangle (face) */
101 public static class T {
102 public final E e1, e2, e3;
103 public T(E e1, E e2, E e3, V normal) {
104 P p1 = e1.shared(e2);
105 P p2 = e2.shared(e3);
106 P p3 = e3.shared(e1);
107 V norm = p2.minus(p1).cross(p3.minus(p1));
108 if (norm.sameDirection(normal)) {
119 P p1 = e1.shared(e2);
120 P p2 = e2.shared(e3);
121 P p3 = e3.shared(e1);
122 return p2.minus(p1).cross(p3.minus(p1)).norm();
124 public T(E e1, E e2, E e3) { this.e1 = e1; this.e2 = e2; this.e3 = e3; }
128 public static class M {
131 public static void main(String[] s) throws Exception {
132 stlf = new StlFile();
133 stlf.load("teapot.stl");
135 for(int i=0; i<stlf.coordArray.length; i+=3) {
136 System.out.println("triangle: (" +
137 stlf.coordArray[i+0] + ", " +
138 stlf.coordArray[i+1] + ", " +
139 stlf.coordArray[i+2] + ")");
143 gi.setNormals(normArray);
144 gi.setStripCounts(stripCounts);
147 ga.convertToIndexedTriangles();
149 ga.convertToIndexedTriangles();
152 Geom geom = new Geom();
155 Frame f = new Frame();
156 GLCapabilities glcaps = new GLCapabilities();
157 GLCanvas glcanvas = new GLCanvas();
158 glcanvas.addGLEventListener(geom);
159 f.add(glcanvas, BorderLayout.CENTER);