In this project you will create a unique 3 scene composed of OpenGL graphic comp
ID: 3809888 • Letter: I
Question
In this project you will create a unique 3 scene composed of OpenGL graphic components using transformation methods.
Requirements:
1. Using Netbeans or Eclipse, develop a JOGL application that displays a unique 3D scene. The scene has the following specifications:
a. Size: 640x480
b. Includes at least 6 different shapes
c. Uses at least 6 different transformation methods
2. Use Java and JOGL for your implementation of OpenGL
3. All Java source code should be written using Google Java style guide.
4. Prepare, conduct and document a test plan verifying each method is functioning properly. (Hint: Using JUNIT tests are recommended)
Explanation / Answer
import java.awt.DisplayMode;
import javax.media.opengl.GL2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import com.jogamp.opengl.util.FPSAnimator;
public class Cube implements GLEventListener {
public static DisplayMode dm, dm_old;
private GLU glu = new GLU();
private float rquad = 0.0f;
@Override
public void display( GLAutoDrawable drawable ) {
final GL2 glogic = drawable.getGL().getGL2();
glogic.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT );
glogic.glLoadIdentity();
glogic.glTranslatef( 0f, 0f, -5.0f );
// Rotate The Cube On X, Y & Z
glogic.glRotatef(rquad, 1.0f, 1.0f, 1.0f);
//giving different colors to different sides
glogic.glBegin(GL2.GL_QUADS); // Start Drawing The Cube
glogic.glColor3f(1f,0f,0f); //red color
glogic.glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
glogic.glVertex3f( -1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
glogic.glVertex3f( -1.0f, 1.0f, 1.0f ); // Bottom Left Of The Quad (Top)
glogic.glVertex3f( 1.0f, 1.0f, 1.0f ); // Bottom Right Of The Quad (Top)
glogic.glColor3f( 0f,1f,0f ); //green color
glogic.glVertex3f( 1.0f, -1.0f, 1.0f ); // Top Right Of The Quad
glogic.glVertex3f( -1.0f, -1.0f, 1.0f ); // Top Left Of The Quad
glogic.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
glogic.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad
glogic.glColor3f( 0f,0f,1f ); //blue color
glogic.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Front)
glogic.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Front)
glogic.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad
glogic.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad
glogic.glColor3f( 1f,1f,0f ); //yellow (red + green)
glogic.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
glogic.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad
glogic.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Back)
glogic.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Back)
glogic.glColor3f( 1f,0f,1f ); //purple (red + green)
glogic.glVertex3f( -1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Left)
glogic.glVertex3f( -1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Left)
glogic.glVertex3f( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad
glogic.glVertex3f( -1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad
glogic.glColor3f( 0f,1f, 1f ); //sky blue (blue +green)
glogic.glVertex3f( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Right)
glogic.glVertex3f( 1.0f, 1.0f, 1.0f ); // Top Left Of The Quad
glogic.glVertex3f( 1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad
glogic.glVertex3f( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad
glogic.glEnd(); // Done Drawing The Quad
glogic.glFlush();
rquad -= 0.15f;
}
@Override
public void dispose( GLAutoDrawable drawable ) {
// TODO Auto-generated method stub
}
@Override
public void init( GLAutoDrawable drawable ) {
final GL2 glogic = drawable.getGL().getGL2();
glogic.glShadeModel( GL2.GL_SMOOTH );
glogic.glClearColor( 0f, 0f, 0f, 0f );
glogic.glClearDepth( 1.0f );
glogic.glEnable( GL2.GL_DEPTH_TEST );
glogic.glDepthFunc( GL2.GL_LEQUAL );
glogic.glHint( GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL2.GL_NICEST );
}
@Override
public void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) {
// TODO Auto-generated method stub
final GL2 glogic = drawable.getGL().getGL2();
if( height lt;= 0 )
height = 1;
final float h = ( float ) width / ( float ) height;
glogic.glViewport( 0, 0, width, height );
glogic.glMatrixMode( GL2.GL_PROJECTION );
gluPerspective.glLoadIdentity();
glu.gluPerspective( 45.0f, h, 1.0, 20.0 );
glogic.glMatrixMode( GL2.GL_MODELVIEW );
glogic.glLoadIdentity();
}
public static void main( String[] args ) {
final GLProfile profile = GLProfile.get( GLProfile.GL2 );
GLCapabilities capabilities = new GLCapabilities( profile );
// The canvas
final GLCanvas glcanvas = new GLCanvas( capabilities );
Cube cube = new Cube();
glcanvas.addGLEventListener( cube );
glcanvas.setSize( 640, 480 );
final JFrame frame = new JFrame ( " Multicolored cube" );
frame.getContentPane().add( glcanvas );
frame.setSize( frame.getContentPane().getPreferredSize() );
frame.setVisible( true );
final FPSAnimator animator = new FPSAnimator(glcanvas, 300,true);
animator.start();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.