Guest User

Untitled

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