/** * */ package cg; import java.awt.Font; import javax.swing.JFrame; import com.jogamp.opengl.GL2; // GL2 constants 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; // GL Utilities Library import com.jogamp.opengl.util.FPSAnimator; // Verwendung dieser Klasse ermöglicht die Animation sowie auch ihre Steuerung import com.jogamp.opengl.util.awt.TextRenderer; /** * @author Alex tavkhelidze * */ /** * JOGL Example #: 3D Shapes - animated solid cube with some text rendered in the corner */ public class Main implements GLEventListener { /** * @param args */ private GLU glu = new GLU(); private float rtri = 0.0f; private float tri = 0.0f; // remember the initial time for animation final long initialTime = System.currentTimeMillis(); // remember the last time for measuring FPS long lastTime = initialTime; // count the frames that were rendered int frames; // current number of frames per second int fps; // used to render text in the GLCanvas TextRenderer renderer; /** The entry main() method to setup the top-level container and animator */ 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); // Instanziierung der Klasse FPSAnimator für die Animationsunterstützung final FPSAnimator animator = new FPSAnimator( glcanvas, 60,true ); // Initiierung der Animation animator.start(); } // ------ Implement methods declared in GLEventListener ------ /** * Called back immediately after the OpenGL context is initialized. Can be used * to perform one-time initialization. Run only once. */ @Override public void init(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); // create a TextRenderer for printing text to the GLCanvas renderer = new TextRenderer(new Font("SansSerif", Font.BOLD, 20)); gl.glClearColor(0.000f, 0.749f, 1.000f, 1.0f); gl.glEnable( GL2.GL_DEPTH_TEST ); // Aktivierung des Tiefentests - sonst wird der Würfel "hohl" } /** * Called back by the animator to perform rendering. */ @Override public void display(GLAutoDrawable drawable) { GL2 gl = drawable.getGL().getGL2(); // calculate time since start of the simulation long currentTime = System.currentTimeMillis(); float time = (currentTime - initialTime) / 1000.0f; frames++; // measure FPS once per second if (currentTime - lastTime > 1000) { // measure FPS over a range of frames // if (frames > 100) { fps = (int) (frames / ((currentTime - lastTime) / 1000.0f)); System.out.println("time = " + (int) time + ", fps = " + fps); // set time lastTime = currentTime; // reset frame count 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 ); // Verschiebung der Würfel immer weiter (mit jedem Animationsschritt) in die Tiefe des Fensters gl.glRotatef( rtri, 1.0f, 1.0f, 1.0f ); // Rotation der Würfel mit der Winkelgröße, die für jeden Schritt gleich dem Wert der Variable rtri ist // --- call your draw code here // ... transformations, objects, ... //drawing the cube in all dimensions gl.glBegin( GL2.GL_QUADS ); // Start Drawing The Cube 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(); // Done Drawing The Quad // draw the FPS on top of the screen renderer.beginRendering(drawable.getSurfaceWidth(), drawable.getSurfaceHeight()); renderer.setColor(1.0f, 0.5f, 0.0f, 0.8f); renderer.draw(" fps: " + fps, 0, 20); // 0 und 20 stehen für X- und Y-Koordinaten des Schaufensters (Viewport) renderer.endRendering(); // --- end of your draw code rtri +=0.2f; // Inkrementierung des Wertes der Variable rtri um 0.2 (float) Einheiten für jeden Animationsschritt tri +=0.0002f; } /** * Called back before the OpenGL context is destroyed. Release resource such as buffers. */ @Override public void dispose(GLAutoDrawable drawable) { // TODO Auto-generated method stub } /** * Call-back handler for window re-size event. Also called when the drawable is * first set to visible. */ @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(); } } // ------ End of Code ------