Java  1.0
ogl.java
Go to the documentation of this file.
1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.media.opengl.*;
4 import javax.media.opengl.awt.GLCanvas;
5 import javax.media.opengl.fixedfunc.*;
6 import com.jogamp.opengl.util.Animator;
7 
12 public class JOGLQuad implements GLEventListener {
13 
14  private float rotateT = 0.0f;
15 
16  @Override
17  public void display(GLAutoDrawable gLDrawable) {
18  final GL2 gl = gLDrawable.getGL().getGL2();
19  gl.glClear(GL.GL_COLOR_BUFFER_BIT);
20  gl.glClear(GL.GL_DEPTH_BUFFER_BIT);
21  gl.glLoadIdentity();
22  gl.glTranslatef(0.0f, 0.0f, -5.0f);
23 
24  // rotate on the three axis
25  gl.glRotatef(rotateT, 1.0f, 0.0f, 0.0f);
26  gl.glRotatef(rotateT, 0.0f, 1.0f, 0.0f);
27  gl.glRotatef(rotateT, 0.0f, 0.0f, 1.0f);
28 
29  // Draw A Quad
30  gl.glBegin(GL2.GL_QUADS);
31  gl.glColor3f(0.0f, 1.0f, 1.0f); // set the color of the quad
32  gl.glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
33  gl.glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
34  gl.glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
35  gl.glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
36  // Done Drawing The Quad
37  gl.glEnd();
38 
39  // increasing rotation for the next iteration
40  rotateT += 0.2f;
41  }
42 
43  @Override
44  public void init(GLAutoDrawable glDrawable) {
45  GL2 gl = glDrawable.getGL().getGL2();
46  gl.glShadeModel(GLLightingFunc.GL_SMOOTH);
47  gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
48  gl.glClearDepth(1.0f);
49  gl.glEnable(GL.GL_DEPTH_TEST);
50  gl.glDepthFunc(GL.GL_LEQUAL);
51  gl.glHint(GL2ES1.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);
52  }
53 
54  @Override
55  public void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height) {
56  GL2 gl = gLDrawable.getGL().getGL2();
57  final float aspect = (float) width / (float) height;
58  gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
59  gl.glLoadIdentity();
60  final float fh = 0.5f;
61  final float fw = fh * aspect;
62  gl.glFrustumf(-fw, fw, -fh, fh, 1.0f, 1000.0f);
63  gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
64  gl.glLoadIdentity();
65  }
66 
67  @Override
68  public void dispose(GLAutoDrawable gLDrawable) {
69  }
70 
71  public static void main(String[] args) {
72  final GLCanvas canvas = new GLCanvas();
73  final Frame frame = new Frame("Jogl Quad drawing");
74  final Animator animator = new Animator(canvas);
75  canvas.addGLEventListener(new JOGLQuad());
76  frame.add(canvas);
77  frame.setSize(640, 480);
78  frame.setResizable(false);
79  frame.addWindowListener(new WindowAdapter() {
80  public void windowClosing(WindowEvent e) {
81  animator.stop();
82  frame.dispose();
83  System.exit(0);
84  }
85  });
86  frame.setVisible(true);
87  animator.start();
88  canvas.requestFocus();
89  }
90 }
JOGLQuad.dispose
void dispose(GLAutoDrawable gLDrawable)
Definition: ogl.java:68
JOGLQuad.reshape
void reshape(GLAutoDrawable gLDrawable, int x, int y, int width, int height)
Definition: ogl.java:55
JOGLQuad.display
void display(GLAutoDrawable gLDrawable)
Definition: ogl.java:17
JOGLQuad.main
static void main(String[] args)
Definition: ogl.java:71
JOGLQuad
Self-contained example (within a single class only to keep it simple) displaying a rotating quad.
Definition: ogl.java:12
JOGLQuad.init
void init(GLAutoDrawable glDrawable)
Definition: ogl.java:44
JOGLQuad.rotateT
float rotateT
Definition: ogl.java:14