checkpoint
[anneal.git] / src / edu / berkeley / qfat / Viewer.java
1 package edu.berkeley.qfat;
2 import java.io.*;
3 import java.nio.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import javax.media.opengl.*;
8 import javax.media.opengl.glu.*;
9 import com.sun.opengl.util.*;
10 import java.util.*;
11 import edu.berkeley.qfat.geom.*;
12 import edu.berkeley.qfat.geom.Point;
13
14 public abstract class Viewer implements GLEventListener, MouseListener, MouseMotionListener, KeyListener, MouseWheelListener  {
15
16     Mesh.Vertex closest = null;
17
18     public Mesh tile = new Mesh(false);
19     public Mesh goal = new Mesh(false);
20
21     private Matrix projection = null;
22
23     JFrame f;
24     GLCanvas glcanvas;
25     Point clickPoint = null;
26     Point clickClosest = null;
27
28     int mousex;
29     int mousey;
30     float tx = 0;
31     float ty = 0;
32     float tz = 0;
33     float anglex = 0;
34     float angley = 0;
35
36     boolean alt = false;
37     boolean shift = false;
38     boolean control = false;
39
40     public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { }
41     public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) { }
42
43     public void init(GLAutoDrawable gld) {
44         GL gl = gld.getGL();
45         gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
46         gl.glViewport(0, 0, 500, 300);
47         gl.glEnable(GL.GL_DEPTH_TEST);
48         gl.glClearDepth(1.0);
49         gl.glDepthFunc(GL.GL_LEQUAL);
50         gl.glMatrixMode(GL.GL_PROJECTION);
51         gl.glLoadIdentity();
52         gl.glMatrixMode(GL.GL_MODELVIEW);
53
54         float mat_specular[] = { 0.5f, 0.5f, 0.5f, 0.5f };
55         float mat_shininess[] = { 50.0f };
56         gl.glShadeModel(GL.GL_SMOOTH);
57         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_DIFFUSE, mat_specular, 0);
58         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SPECULAR, mat_specular, 0);  
59         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT, new float[] { 0.3f, 0.3f, 0.3f, 0.3f }, 0);  
60         //gl.glMaterialfv(GL.GL_FRONT, GL.GL_SHININESS, mat_shininess, 0);
61         gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, new float[] { 1.0f,    4.0f,  -10.0f, 0.0f }, 0);
62         gl.glLightfv(GL.GL_LIGHT1, GL.GL_POSITION, new float[] { -10.0f, 10.0f,   10.0f, 0.0f }, 0);
63         gl.glLightfv(GL.GL_LIGHT2, GL.GL_POSITION, new float[] { 10.0f, -10.0f,   10.0f, 0.0f }, 0);
64         gl.glLightfv(GL.GL_LIGHT3, GL.GL_POSITION, new float[] { 10.0f,  10.0f,  -10.0f, 0.0f }, 0);
65         gl.glLightfv(GL.GL_LIGHT4, GL.GL_POSITION, new float[] { -10.0f, 10.0f,  -10.0f, 0.0f }, 0);
66         gl.glLightfv(GL.GL_LIGHT5, GL.GL_POSITION, new float[] { 10.0f, -10.0f,  -10.0f, 0.0f }, 0);
67         gl.glEnable(GL.GL_LIGHTING);
68         gl.glEnable(GL.GL_LIGHT0);
69
70         gl.glColorMaterial(GL.GL_FRONT_AND_BACK, GL.GL_AMBIENT_AND_DIFFUSE);
71         gl.glEnable(GL.GL_COLOR_MATERIAL);
72
73         display(gld);
74     }
75
76     public abstract void _display(GLAutoDrawable drawable, GL gl);
77     public final void display(GLAutoDrawable drawable) {
78         GL gl = drawable.getGL();
79         _display(drawable, gl);
80         projection = Matrix.getProjectionMatrix(gl);
81     }
82
83     protected synchronized void updateVisibility(GL gl, Mesh mesh) {
84         Matrix projection = Matrix.getProjectionMatrix(gl);
85         IntBuffer buf = ByteBuffer.allocateDirect(9*4*4).order(ByteOrder.nativeOrder()).asIntBuffer();
86         gl.glFlush();
87         gl.glDrawBuffer(GL.GL_BACK);
88         gl.glReadBuffer( GL.GL_BACK );
89         gl.glPixelStorei( GL.GL_PACK_ALIGNMENT, 1);
90         gl.glFlush();
91         gl.glDisable(GL.GL_LIGHTING);
92         gl.glShadeModel(GL.GL_FLAT);
93         gl.glColor3f(0,0,0);
94         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
95         for(Mesh.T t : mesh) t.glTriangle(gl, null);
96         for(Mesh.Vertex v : mesh.vertices()) {
97             Point p = v.getPoint();
98             gl.glColor3f(1,1,1);
99             gl.glBegin(gl.GL_POINTS);
100             p.glVertex(gl);
101             gl.glEnd();
102             gl.glFlush();
103             
104             Point projected = projection.times(p);
105             gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
106             
107             boolean vis = false;
108             for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
109             v.visible = vis;
110             if (vis) {
111                 gl.glColor3f(0,0,0);
112                 gl.glBegin(gl.GL_POINTS);
113                 p.glVertex(gl);
114                 gl.glEnd();
115             }
116         }
117         gl.glShadeModel(GL.GL_SMOOTH);
118         gl.glEnable(GL.GL_LIGHTING);
119         gl.glDrawBuffer(GL.GL_FRONT);
120     }
121
122
123     /** return the position of the mouse as a point in window-space */
124     public Point getMouse() {
125         return new Point(mousex, glcanvas.getHeight()-mousey, 0);
126     }
127
128     public void mouseWheelMoved(MouseWheelEvent e) {
129         tz -= e.getWheelRotation();
130     }
131
132     public void keyTyped(KeyEvent e)  { }
133     public void keyPressed(KeyEvent e)  {
134         switch(e.getKeyCode()) {
135             case KeyEvent.VK_CONTROL: control = true; break;
136             case KeyEvent.VK_ALT: alt = true; break;
137             case KeyEvent.VK_SHIFT: shift = true; break;
138         }
139     }
140
141     public void keyReleased(KeyEvent e) {
142         switch(e.getKeyCode()) {
143             case KeyEvent.VK_CONTROL: control = false; break;
144             case KeyEvent.VK_ALT: alt = false; break;
145             case KeyEvent.VK_SHIFT: shift = false; break;
146         }
147     }
148
149     public void mouseClicked(MouseEvent e) { }
150     public void mouseEntered(MouseEvent e) { }
151     public void mouseExited(MouseEvent e) { }
152     public void mousePressed(MouseEvent e) {
153         clickPoint = getMouse();
154         clickClosest = closest == null ? null : closest.getPoint();
155     }
156
157     public void mouseReleased(MouseEvent e) {
158         clickPoint = null;
159         clickClosest = null;
160     }
161
162     public void mouseMoved(MouseEvent e) {
163         mousex = e.getX();
164         mousey = e.getY();
165     }
166
167     public void mouseDragged(MouseEvent e) {
168         if (shift) {
169             /*
170             tx += (mousex - e.getX())/(float)20;
171             ty += (mousey - e.getY())/(float)20;
172             */
173             if (closest != null && projection != null && clickClosest != null) {
174                 synchronized(this) {
175                     Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
176                     Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
177                     //System.out.println(delta + " " + closest.getPoint());
178                     System.out.println(getMouse().minus(clickPoint));
179                     closest.move(Matrix.translate(delta), false);
180                 }
181             }
182         } else {
183             anglex -= mousex - e.getX();
184             angley += mousey - e.getY();
185         }
186         mousex = e.getX();
187         mousey = e.getY();
188     }
189
190     public Viewer(JFrame f) {
191         this.f = f;
192         glcanvas = new GLCanvas();
193         glcanvas.addGLEventListener(this);
194         f.add(glcanvas, BorderLayout.CENTER);
195         glcanvas.addMouseListener(this);
196         glcanvas.addMouseMotionListener(this);
197         glcanvas.addMouseWheelListener(this);
198         glcanvas.addKeyListener(this);
199     }
200
201     public void repaint() {
202         glcanvas.repaint();
203     }
204 }