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