init
[anneal.git] / src / Geom.java
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4 import javax.media.opengl.*;
5 import javax.media.opengl.glu.*;
6
7 public class Geom implements GLEventListener {
8
9     public static StlFile stlf = null;
10
11     /**
12      * Take care of initialization here.
13      */
14     public void init(GLAutoDrawable gld) {
15         GL gl = gld.getGL();
16         GLU glu = new GLU();
17         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
18         gl.glViewport(0, 0, 500, 300);
19         gl.glMatrixMode(GL.GL_PROJECTION);
20         gl.glLoadIdentity();
21         //glu.gluOrtho2D(0.0, 500.0, 0.0, 300.0);
22         display(gld);
23     }
24
25
26     /**
27      * Take care of drawing here.
28      */
29     public void display(GLAutoDrawable drawable) {
30         float red = 0.0f;
31         float green = 0.0f;
32         float blue = 0.0f;
33         GL gl = drawable.getGL();
34         gl.glClear(GL.GL_COLOR_BUFFER_BIT);
35         gl.glPointSize(5.0f);
36
37         for(int i=0; i<stlf.coordArray.length; i+=3) {
38             red -= .09f;
39             green -= .12f;
40             blue -= .15f;
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);
45
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);
51             gl.glEnd();
52         }
53     }
54
55
56     public void reshape(
57                         GLAutoDrawable drawable,
58                         int x,
59                         int y,
60                         int width,
61                      int height
62                         ) {}
63     public void displayChanged(
64                                GLAutoDrawable drawable,
65                                boolean modeChanged,
66                             boolean deviceChanged
67                                ) {}
68
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; }
74     }
75
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; }
83     }
84
85     /** an edge */
86     public static class E {
87         public final P p1, p2;
88         private T t1, t2;
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;
92             E e = (E)o;
93             if (this.p1 == e.p1 && this.p2 == e.p2) return true;
94             if (this.p2 == e.p1 && this.p1 == e.p2) return true;
95             return false;
96         }
97         public P shared(E e) { return null; }
98     }
99
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)) {
109                 this.e1 = e1;
110                 this.e2 = e2;
111                 this.e3 = e3;
112             } else {
113                 this.e1 = e3;
114                 this.e2 = e2;
115                 this.e3 = e1;
116             }
117         }
118         public V norm() {
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();
123         }
124         public T(E e1, E e2, E e3) { this.e1 = e1; this.e2 = e2; this.e3 = e3; }
125     }
126
127     /** matrix */
128     public static class M {
129     }
130
131     public static void main(String[] s) throws Exception {
132         stlf = new StlFile();
133         stlf.load("teapot.stl");
134         /*
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] + ")");
140         }
141
142     gi.setCoordinates();
143     gi.setNormals(normArray);
144     gi.setStripCounts(stripCounts);
145
146         GeometryInfo gi = ;
147         ga.convertToIndexedTriangles();
148         GeometryArray ga = ;
149         ga.convertToIndexedTriangles();
150         */
151
152         Geom geom = new Geom();
153         
154
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);
160         f.show();
161         f.setSize(500, 300);
162     }
163
164 }