package cg; //--------------------------------------------------------------------- import javax.swing.JFrame; import com.jogamp.opengl.GL2; import com.jogamp.opengl.GLAutoDrawable; import com.jogamp.opengl.GLCapabilities; import com.jogamp.opengl.GLEventListener; import com.jogamp.opengl.GLProfile; import com.jogamp.opengl.awt.GLCanvas; import com.jogamp.opengl.glu.GLU; import com.jogamp.opengl.util.FPSAnimator; import java.awt.Font; import com.jogamp.opengl.util.awt.TextRenderer; //--------------------------------------------------------------------- public class Main implements GLEventListener { private GLU glu = new GLU(); private float rtri = 0.0f; private float tri = 0.0f; final long initialTime = System.currentTimeMillis(); long lastTime = initialTime; int frames; int fps; TextRenderer renderer; //--------------------------------------------------------------------- public static void main(String[] args) { final GLProfile profile = GLProfile.get(GLProfile.GL2); GLCapabilities capabilities = new GLCapabilities(profile); final GLCanvas glcanvas = new GLCanvas(capabilities); final int width = 800; final int height = 600; Main m = new Main(); glcanvas.addGLEventListener(m); final JFrame frame = new JFrame ("OpenGL Fenster"); frame.getContentPane().add(glcanvas); frame.setSize(width, height); frame.setVisible(true); final FPSAnimator animator = new FPSAnimator( glcanvas, 60,true ); animator.start(); } //--------------------------------------------------------------------- @Override public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); gl.glClearColor(0.000f, 0.749f, 1.000f, 1.0f); gl.glEnable(GL2.GL_DEPTH_TEST); renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 50)); } //--------------------------------------------------------------------- @Override public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); long currentTime = System.currentTimeMillis(); if (currentTime - lastTime > 1000) { fps = (int) (frames / ((currentTime - lastTime) / 1000.0f)); lastTime = currentTime; frames = 0; } gl.glClear( GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT ); gl.glLoadIdentity(); gl.glTranslatef( 0.0f, 0.0f, -6.0f - tri ); gl.glRotatef( rtri, 1.0f, 1.0f, 1.0f ); gl.glBegin(GL2.GL_QUADS); gl.glColor3f( 1f,0f,0f ); //red color gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Top) gl.glVertex3f( -1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top) gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Bottom Left Of The Quad (Top) gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Bottom Right Of The Quad (Top) gl.glColor3f( 0f,1f,0f ); //green color gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Top Right Of The Quad gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Top Left Of The Quad gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad gl.glColor3f( 0f,0f,1f ); //blue color gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Front) gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Front) gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad gl.glColor3f( 1f,1f,0f ); //yellow (red + green) gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Back) gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Back) gl.glColor3f( 1f,0f,1f ); //purple (red + green) gl.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Left) gl.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Left) gl.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad gl.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad gl.glColor3f( 0f,1f, 1f ); //sky blue (blue +green) gl.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Right) gl.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Left Of The Quad gl.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad gl.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad gl.glEnd(); //--------------------------------------------------------------------- renderer.beginRendering(drawable.getSurfaceWidth(), drawable.getSurfaceHeight()); gl.glMatrixMode(GL2.GL_MODELVIEW); gl.glRotatef(45.0f, 0.0f, 0.0f, 1.0f); renderer.setColor(1.0f, 0.5f, 0.0f, 0.8f); renderer.draw(" fps: " + fps, 150, 100); renderer.endRendering(); //--------------------------------------------------------------------- rtri +=0.2f; tri +=0.0002f; frames++; } //--------------------------------------------------------------------- @Override public void dispose(GLAutoDrawable drawable) {} @Override public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) { final GL2 gl = drawable.getGL().getGL2(); final float h = ( float ) width / ( float ) height; gl.glViewport( 0, 0, width, height ); gl.glMatrixMode( GL2.GL_PROJECTION ); gl.glLoadIdentity(); glu.gluPerspective( 45.0f, h, 1.0, 20.0 ); gl.glMatrixMode( GL2.GL_MODELVIEW ); gl.glLoadIdentity(); } }