2ec797f5fdf29453a686c98311521efef891a019
[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 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.glColor3f(0,0,0);
87         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
88         synchronized(this) {
89             for(Mesh.T t : mesh) t.glTriangle(gl, null);
90             for(Mesh.Vertex v : mesh.vertices()) {
91                 Point p = v.getPoint();
92                 gl.glColor3f(1,1,1);
93                 gl.glBegin(gl.GL_POINTS);
94                 p.glVertex(gl);
95                 gl.glEnd();
96                 gl.glFlush();
97
98                 Point projected = projection.times(p);
99                 gl.glReadPixels((int)projected.x-1, (int)projected.y-1, 3, 3, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, buf);
100
101                 boolean vis = false;
102                 for(int j=0; j<9*4; j++) vis |= buf.get(j)!=0;
103                 v.visible = vis;
104                 if (vis) {
105                     gl.glColor3f(0,0,0);
106                     gl.glBegin(gl.GL_POINTS);
107                     p.glVertex(gl);
108                     gl.glEnd();
109                 }
110             }
111         }
112         gl.glShadeModel(GL.GL_SMOOTH);
113         gl.glEnable(GL.GL_LIGHTING);
114         gl.glDrawBuffer(GL.GL_FRONT);
115     }
116
117
118     /** return the position of the mouse as a point in window-space */
119     public Point getMouse() {
120         return new Point(mousex, glcanvas.getHeight()-mousey, 0);
121     }
122
123     public void mouseWheelMoved(MouseWheelEvent e) {
124         tz -= e.getWheelRotation();
125     }
126
127     public void keyTyped(KeyEvent e)  { }
128     public void keyPressed(KeyEvent e)  {
129         switch(e.getKeyCode()) {
130             case KeyEvent.VK_CONTROL: control = true; break;
131             case KeyEvent.VK_ALT: alt = true; break;
132             case KeyEvent.VK_SHIFT: shift = true; break;
133         }
134     }
135
136     public void keyReleased(KeyEvent e) {
137         switch(e.getKeyCode()) {
138             case KeyEvent.VK_CONTROL: control = false; break;
139             case KeyEvent.VK_ALT: alt = false; break;
140             case KeyEvent.VK_SHIFT: shift = false; break;
141         }
142     }
143
144     public void mouseClicked(MouseEvent e) { }
145     public void mouseEntered(MouseEvent e) { }
146     public void mouseExited(MouseEvent e) { }
147     public void mousePressed(MouseEvent e) {
148         clickPoint = getMouse();
149         clickClosest = closest == null ? null : closest.getPoint();
150     }
151
152     public void mouseReleased(MouseEvent e) {
153         clickPoint = null;
154         clickClosest = null;
155     }
156
157     public void mouseMoved(MouseEvent e) {
158         mousex = e.getX();
159         mousey = e.getY();
160     }
161
162     public void mouseDragged(MouseEvent e) {
163         if (shift) {
164             /*
165             tx += (mousex - e.getX())/(float)20;
166             ty += (mousey - e.getY())/(float)20;
167             */
168             if (closest != null && projection != null && clickClosest != null) {
169                 synchronized(this) {
170                     Vec d1 = projection.inverse().times(getMouse()).minus(projection.inverse().times(clickPoint));
171                     Vec delta = d1.plus(clickClosest).minus(closest.getPoint());
172                     //System.out.println(delta + " " + closest.getPoint());
173                     System.out.println(getMouse().minus(clickPoint));
174                     closest.move(Matrix.translate(delta), true);
175                 }
176             }
177         } else {
178             anglex -= mousex - e.getX();
179             angley += mousey - e.getY();
180         }
181         mousex = e.getX();
182         mousey = e.getY();
183     }
184
185     public Viewer(JFrame f) {
186         this.f = f;
187         glcanvas = new GLCanvas();
188         glcanvas.addGLEventListener(this);
189         f.add(glcanvas, BorderLayout.CENTER);
190         glcanvas.addMouseListener(this);
191         glcanvas.addMouseMotionListener(this);
192         glcanvas.addMouseWheelListener(this);
193         glcanvas.addKeyListener(this);
194     }
195
196     public void repaint() {
197         glcanvas.repaint();
198     }
199 }