1 package gears;
2 
3 import java.awt.*;
4 import java.awt.event.*;
5 
6 import javax.media.opengl.*;
7 import com.sun.opengl.util.*;
8 
9 /**
10  * slightly modified Gears demo used as jackpot testing purposes.
11  * - - -
12  * Gears.java <BR>
13  * author: Brian Paul (converted to Java by Ron Cemer and Sven Goethel) <P>
14  *
15  * This version is equal to Brian Paul's version 1.2 1999/10/21
16  */
17 
18 public class Gears implements GLEventListener, MouseListener, MouseMotionListener {
main(String[] args)19   public static void main(String[] args) {
20 
21     Frame frame = new Frame("Gear Demo");
22     GLCanvas canvas = new GLCanvas(new GLCapabilities());
23 
24     canvas.addGLEventListener(new Gears());
25     frame.add(canvas);
26     frame.setSize(300, 300);
27     final Animator animator = new Animator(canvas);
28     frame.addWindowListener(new WindowAdapter() {
29         public void windowClosing(WindowEvent e) {
30           // Run this on another thread than the AWT event queue to
31           // make sure the call to Animator.stop() completes before
32           // exiting
33           new Thread(new Runnable() {
34               public void run() {
35                 animator.stop();
36                 System.exit(0);
37               }
38             }).start();
39         }
40       });
41     frame.show();
42     animator.start();
43   }
44 
45   private float view_rotx = 20.0f, view_roty = 30.0f, view_rotz = 0.0f;
46   private int gear1, gear2, gear3;
47   private float angle = 0.0f;
48 
49   private int prevMouseX, prevMouseY;
50   private boolean mouseRButtonDown = false;
51 
init(GLAutoDrawable drawable)52   public void init(GLAutoDrawable drawable) {
53     // Use debug pipeline
54     drawable.setGL(new DebugGL(drawable.getGL()));
55 
56     GL gl = drawable.getGL();
57 
58     System.err.println("INIT GL IS: " + gl.getClass().getName());
59 
60     gl.setSwapInterval(1);
61 
62     float pos[] = { 5.0f, 5.0f, 10.0f, 0.0f };
63     float red[] = { 0.8f, 0.1f, 0.0f, 1.0f };
64     float green[] = { 0.0f, 0.8f, 0.2f, 1.0f };
65     float blue[] = { 0.2f, 0.2f, 1.0f, 1.0f };
66 
67     gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, pos, 0);
68     gl.glEnable(GL.GL_CULL_FACE);
69     gl.glEnable(GL.GL_LIGHTING);
70     gl.glEnable(GL.GL_LIGHT0);
71     gl.glEnable(GL.GL_DEPTH_TEST);
72 
73     /* make the gears */
74     gear1 = gl.glGenLists(1);
75     gl.glNewList(gear1, GL.GL_COMPILE);
76     gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, red, 0);
77     gear(gl, 1.0f, 4.0f, 1.0f, 20, 0.7f);
78     gl.glEndList();
79 
80     gear2 = gl.glGenLists(1);
81     gl.glNewList(gear2, GL.GL_COMPILE);
82     gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, green, 0);
83     gear(gl, 0.5f, 2.0f, 2.0f, 10, 0.7f);
84     gl.glEndList();
85 
86     gear3 = gl.glGenLists(1);
87     gl.glNewList(gear3, GL.GL_COMPILE);
88     gl.glMaterialfv(GL.GL_FRONT, GL.GL_AMBIENT_AND_DIFFUSE, blue, 0);
89     gear(gl, 1.3f, 2.0f, 0.5f, 10, 0.7f);
90     gl.glEndList();
91 
92     gl.glEnable(GL.GL_NORMALIZE);
93 
94     drawable.addMouseListener(this);
95     drawable.addMouseMotionListener(this);
96   }
97 
reshape(GLAutoDrawable drawable, int x, int y, int width, int height)98   public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
99     GL gl = drawable.getGL();
100 
101     float h = (float)height / (float)width;
102 
103     gl.glMatrixMode(GL.GL_PROJECTION);
104 
105     System.err.println("GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
106     System.err.println("GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
107     System.err.println("GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
108     gl.glLoadIdentity();
109     gl.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f);
110     gl.glMatrixMode(GL.GL_MODELVIEW);
111     gl.glLoadIdentity();
112     gl.glTranslatef(0.0f, 0.0f, -40.0f);
113   }
114 
display(GLAutoDrawable drawable)115   public void display(GLAutoDrawable drawable) {
116     angle += 2.0f;
117 
118     GL gl = drawable.getGL();
119     if ((drawable instanceof GLJPanel) &&
120         !((GLJPanel) drawable).isOpaque() &&
121         ((GLJPanel) drawable).shouldPreserveColorBufferIfTranslucent()) {
122       gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
123     } else {
124       gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
125     }
126 
127     gl.glPushMatrix();
128     gl.glRotatef(view_rotx, 1.0f, 0.0f, 0.0f);
129     gl.glRotatef(view_roty, 0.0f, 1.0f, 0.0f);
130     gl.glRotatef(view_rotz, 0.0f, 0.0f, 1.0f);
131 
132     gl.glPushMatrix();
133     gl.glTranslatef(-3.0f, -2.0f, 0.0f);
134     gl.glRotatef(angle, 0.0f, 0.0f, 1.0f);
135     gl.glCallList(gear1);
136     gl.glPopMatrix();
137 
138     gl.glPushMatrix();
139     gl.glTranslatef(3.1f, -2.0f, 0.0f);
140     gl.glRotatef(-2.0f * angle - 9.0f, 0.0f, 0.0f, 1.0f);
141     gl.glCallList(gear2);
142     gl.glPopMatrix();
143 
144     gl.glPushMatrix();
145     gl.glTranslatef(-3.1f, 4.2f, 0.0f);
146     gl.glRotatef(-2.0f * angle - 25.0f, 0.0f, 0.0f, 1.0f);
147     gl.glCallList(gear3);
148     gl.glPopMatrix();
149 
150     gl.glPopMatrix();
151   }
152 
displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)153   public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {}
154 
gear(GL gl, float inner_radius, float outer_radius, float width, int teeth, float tooth_depth)155   private void gear(GL gl,
156                     float inner_radius,
157                     float outer_radius,
158                     float width,
159                     int teeth,
160                     float tooth_depth)
161   {
162     int i;
163     float r0, r1, r2;
164     float angle, da;
165     float u, v, len;
166 
167     r0 = inner_radius;
168     r1 = outer_radius - tooth_depth / 2.0f;
169     r2 = outer_radius + tooth_depth / 2.0f;
170 
171     da = 2.0f * (float) Math.PI / teeth / 4.0f;
172 
173     gl.glShadeModel(GL.GL_FLAT);
174 
175     gl.glNormal3f(0.0f, 0.0f, 1.0f);
176 
177     /* draw front face */
178     gl.glBegin(GL.GL_QUAD_STRIP);
179     for (i = 0; i <= teeth; i++)
180       {
181         angle = i * 2.0f * (float) Math.PI / teeth;
182         gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), width * 0.5f);
183         gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), width * 0.5f);
184         if(i < teeth)
185           {
186             gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), width * 0.5f);
187             gl.glVertex3f(r1 * (float)Math.cos(angle + 3.0f * da), r1 * (float)Math.sin(angle + 3.0f * da), width * 0.5f);
188           }
189       }
190     gl.glEnd();
191 
192     /* draw front sides of teeth */
193     gl.glBegin(GL.GL_QUADS);
194     for (i = 0; i < teeth; i++)
195       {
196         angle = i * 2.0f * (float) Math.PI / teeth;
197         gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), width * 0.5f);
198         gl.glVertex3f(r2 * (float)Math.cos(angle + da), r2 * (float)Math.sin(angle + da), width * 0.5f);
199         gl.glVertex3f(r2 * (float)Math.cos(angle + 2.0f * da), r2 * (float)Math.sin(angle + 2.0f * da), width * 0.5f);
200         gl.glVertex3f(r1 * (float)Math.cos(angle + 3.0f * da), r1 * (float)Math.sin(angle + 3.0f * da), width * 0.5f);
201       }
202     gl.glEnd();
203 
204     /* draw back face */
205     gl.glBegin(GL.GL_QUAD_STRIP);
206     for (i = 0; i <= teeth; i++)
207       {
208         angle = i * 2.0f * (float) Math.PI / teeth;
209         gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), -width * 0.5f);
210         gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), -width * 0.5f);
211         gl.glVertex3f(r1 * (float)Math.cos(angle + 3 * da), r1 * (float)Math.sin(angle + 3 * da), -width * 0.5f);
212         gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), -width * 0.5f);
213       }
214     gl.glEnd();
215 
216     /* draw back sides of teeth */
217     gl.glBegin(GL.GL_QUADS);
218     for (i = 0; i < teeth; i++)
219       {
220         angle = i * 2.0f * (float) Math.PI / teeth;
221         gl.glVertex3f(r1 * (float)Math.cos(angle + 3 * da), r1 * (float)Math.sin(angle + 3 * da), -width * 0.5f);
222         gl.glVertex3f(r2 * (float)Math.cos(angle + 2 * da), r2 * (float)Math.sin(angle + 2 * da), -width * 0.5f);
223         gl.glVertex3f(r2 * (float)Math.cos(angle + da), r2 * (float)Math.sin(angle + da), -width * 0.5f);
224         gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), -width * 0.5f);
225       }
226     gl.glEnd();
227 
228     /* draw outward faces of teeth */
229     gl.glBegin(GL.GL_QUAD_STRIP);
230     for (i = 0; i < teeth; i++)
231       {
232         angle = i * 2.0f * (float) Math.PI / teeth;
233         gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), width * 0.5f);
234         gl.glVertex3f(r1 * (float)Math.cos(angle), r1 * (float)Math.sin(angle), -width * 0.5f);
235         u = r2 * (float)Math.cos(angle + da) - r1 * (float)Math.cos(angle);
236         v = r2 * (float)Math.sin(angle + da) - r1 * (float)Math.sin(angle);
237         len = (float)Math.sqrt(u * u + v * v);
238         u /= len;
239         v /= len;
240         gl.glNormal3f(v, -u, 0.0f);
241         gl.glVertex3f(r2 * (float)Math.cos(angle + da), r2 * (float)Math.sin(angle + da), width * 0.5f);
242         gl.glVertex3f(r2 * (float)Math.cos(angle + da), r2 * (float)Math.sin(angle + da), -width * 0.5f);
243         gl.glNormal3f((float)Math.cos(angle), (float)Math.sin(angle), 0.0f);
244         gl.glVertex3f(r2 * (float)Math.cos(angle + 2 * da), r2 * (float)Math.sin(angle + 2 * da), width * 0.5f);
245         gl.glVertex3f(r2 * (float)Math.cos(angle + 2 * da), r2 * (float)Math.sin(angle + 2 * da), -width * 0.5f);
246         u = r1 * (float)Math.cos(angle + 3 * da) - r2 * (float)Math.cos(angle + 2 * da);
247         v = r1 * (float)Math.sin(angle + 3 * da) - r2 * (float)Math.sin(angle + 2 * da);
248         gl.glNormal3f(v, -u, 0.0f);
249         gl.glVertex3f(r1 * (float)Math.cos(angle + 3 * da), r1 * (float)Math.sin(angle + 3 * da), width * 0.5f);
250         gl.glVertex3f(r1 * (float)Math.cos(angle + 3 * da), r1 * (float)Math.sin(angle + 3 * da), -width * 0.5f);
251         gl.glNormal3f((float)Math.cos(angle), (float)Math.sin(angle), 0.0f);
252       }
253     gl.glVertex3f(r1 * (float)Math.cos(0), r1 * (float)Math.sin(0), width * 0.5f);
254     gl.glVertex3f(r1 * (float)Math.cos(0), r1 * (float)Math.sin(0), -width * 0.5f);
255     gl.glEnd();
256 
257     gl.glShadeModel(GL.GL_SMOOTH);
258 
259     /* draw inside radius cylinder */
260     gl.glBegin(GL.GL_QUAD_STRIP);
261     for (i = 0; i <= teeth; i++)
262       {
263         angle = i * 2.0f * (float) Math.PI / teeth;
264         gl.glNormal3f(-(float)Math.cos(angle), -(float)Math.sin(angle), 0.0f);
265         gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), -width * 0.5f);
266         gl.glVertex3f(r0 * (float)Math.cos(angle), r0 * (float)Math.sin(angle), width * 0.5f);
267       }
268     gl.glEnd();
269   }
270 
271   // Methods required for the implementation of MouseListener
mouseEntered(MouseEvent e)272   public void mouseEntered(MouseEvent e) {}
mouseExited(MouseEvent e)273   public void mouseExited(MouseEvent e) {}
274 
mousePressed(MouseEvent e)275   public void mousePressed(MouseEvent e) {
276     prevMouseX = e.getX();
277     prevMouseY = e.getY();
278     if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
279       mouseRButtonDown = true;
280     }
281   }
282 
mouseReleased(MouseEvent e)283   public void mouseReleased(MouseEvent e) {
284     if ((e.getModifiers() & e.BUTTON3_MASK) != 0) {
285       mouseRButtonDown = false;
286     }
287   }
288 
mouseClicked(MouseEvent e)289   public void mouseClicked(MouseEvent e) {}
290 
291   // Methods required for the implementation of MouseMotionListener
mouseDragged(MouseEvent e)292   public void mouseDragged(MouseEvent e) {
293     int x = e.getX();
294     int y = e.getY();
295     Dimension size = e.getComponent().getSize();
296 
297     float thetaY = 360.0f * ( (float)(x-prevMouseX)/(float)size.width);
298     float thetaX = 360.0f * ( (float)(prevMouseY-y)/(float)size.height);
299 
300     prevMouseX = x;
301     prevMouseY = y;
302 
303     view_rotx += thetaX;
304     view_roty += thetaY;
305   }
306 
mouseMoved(MouseEvent e)307   public void mouseMoved(MouseEvent e) {}
308 }
309 
310