/**
 * 
 */
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;

//Verwendung dieser Klasse ermöglicht die Animation sowie auch ihre Steuerung
import com.jogamp.opengl.util.FPSAnimator; 

/**
 * @author Alex Tavkhelidze
 *
 */

/**
 * JOGL Example #: 3D Shapes - animated solid cube
 */

public class Main implements GLEventListener {

	/**
	 * @param args
	 */
	
	private GLU glu = new GLU();
	private float rtri = 0.0f;
	private float tri = 0.0f;

		final long initialTime = System.currentTimeMillis();
	
	 /** 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();
	}
	
	 /**
	    * 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();
		
		gl.glClearColor(0.000f, 0.749f, 1.000f, 1.0f);
		
	    gl.glEnable( GL2.GL_DEPTH_TEST );
	}

	/**
	    * Called back by the animator to perform rendering.
	    */
	@Override
	public void display(GLAutoDrawable drawable) {
		
		GL2 gl = drawable.getGL().getGL2();
		
		long currentTime = System.currentTimeMillis();
		// Simulationsdauer
		float time = (currentTime - initialTime) / 1000.0f;
		
	      gl.glClear( GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );     
	      
	      gl.glMatrixMode( GL2.GL_MODELVIEW );
	      gl.glLoadIdentity();
	      
	      // Verschiebung des Würfels immer weiter (mit jedem Animationsschritt) in die Tiefe des Fensters
	      gl.glTranslatef( 0.0f, 0.0f, -6.0f - tri );
	      
	      // Rotation des Würfels um die Winkelgröße rtri
	      gl.glRotatef( rtri, 1.0f, 1.0f, 1.0f ); 
		
	      // Start Drawing The Cube	      
	      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 
	      
	      // Done Drawing The Quad
	      gl.glEnd(); 	
	      
	   // Inkrementierung für jeden Animationsschritt  
		rtri +=0.8f; 
		tri +=0.02f;
	}
	
	@Override
	public void dispose(GLAutoDrawable drawable) {
		// TODO Auto-generated method stub
		
	}

	@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.glMatrixMode( GL2.GL_PROJECTION );
	      gl.glLoadIdentity();
	      glu.gluPerspective( 45.0f, h, 1.0, 20.0 ); 
	      
	}

}