How to implement rotation algorithm for cube in OpenGL Java? -
here code created cube in opengl java, how implement algorithm rotation on ox
, oy
, oz
axis without using predefinition function. example how rotate on origin point or point.
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 gl = drawable.getgl().getgl2(); gl.glclear(gl2.gl_color_buffer_bit | gl2.gl_depth_buffer_bit ); gl.glloadidentity(); gl.gltranslatef( 0f, 0f, -5.0f ); // rotate cube on x, y & z gl.glrotatef(rquad, 1.0f, 1.0f, 1.0f); //giving different colors different sides gl.glbegin(gl2.gl_quads); // start drawing cube gl.glcolor3f(1f,0f,0f); //red color gl.glvertex3f(1.0f, 1.0f, -1.0f); // top right of quad (top) gl.glvertex3f( -1.0f, 1.0f, -1.0f); // top left of quad (top) gl.glvertex3f( -1.0f, 1.0f, 1.0f ); // bottom left of quad (top) gl.glvertex3f( 1.0f, 1.0f, 1.0f ); // bottom right of quad (top) gl.glcolor3f( 0f,1f,0f ); //green color gl.glvertex3f( 1.0f, -1.0f, 1.0f ); // top right of quad gl.glvertex3f( -1.0f, -1.0f, 1.0f ); // top left of quad gl.glvertex3f( -1.0f, -1.0f, -1.0f ); // bottom left of quad gl.glvertex3f( 1.0f, -1.0f, -1.0f ); // bottom right of quad gl.glcolor3f( 0f,0f,1f ); //blue color gl.glvertex3f( 1.0f, 1.0f, 1.0f ); // top right of quad (front) gl.glvertex3f( -1.0f, 1.0f, 1.0f ); // top left of quad (front) gl.glvertex3f( -1.0f, -1.0f, 1.0f ); // bottom left of quad gl.glvertex3f( 1.0f, -1.0f, 1.0f ); // bottom right of quad gl.glcolor3f( 1f,1f,0f ); //yellow (red + green) gl.glvertex3f( 1.0f, -1.0f, -1.0f ); // bottom left of quad gl.glvertex3f( -1.0f, -1.0f, -1.0f ); // bottom right of quad gl.glvertex3f( -1.0f, 1.0f, -1.0f ); // top right of quad (back) gl.glvertex3f( 1.0f, 1.0f, -1.0f ); // top left of quad (back) gl.glcolor3f( 1f,0f,1f ); //purple (red + green) gl.glvertex3f( -1.0f, 1.0f, 1.0f ); // top right of quad (left) gl.glvertex3f( -1.0f, 1.0f, -1.0f ); // top left of quad (left) gl.glvertex3f( -1.0f, -1.0f, -1.0f ); // bottom left of quad gl.glvertex3f( -1.0f, -1.0f, 1.0f ); // bottom right of quad gl.glcolor3f( 0f,1f, 1f ); //sky blue (blue +green) gl.glvertex3f( 1.0f, 1.0f, -1.0f ); // top right of quad (right) gl.glvertex3f( 1.0f, 1.0f, 1.0f ); // top left of quad gl.glvertex3f( 1.0f, -1.0f, 1.0f ); // bottom left of quad gl.glvertex3f( 1.0f, -1.0f, -1.0f ); // bottom right of quad gl.glend(); // done drawing quad gl.glflush(); rquad -= 0.15f; } @override public void dispose( glautodrawable drawable ) { // todo auto-generated method stub } @override public void init( glautodrawable drawable ) { final gl2 gl = drawable.getgl().getgl2(); gl.glshademodel( gl2.gl_smooth ); gl.glclearcolor( 0f, 0f, 0f, 0f ); gl.glcleardepth( 1.0f ); gl.glenable( gl2.gl_depth_test ); gl.gldepthfunc( gl2.gl_lequal ); gl.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 gl = drawable.getgl().getgl2(); if( height lt;= 0 ) height = 1; 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(); } public static void main( string[] args ) { final glprofile profile = glprofile.get( glprofile.gl2 ); glcapabilities capabilities = new glcapabilities( profile ); // canvas final glcanvas glcanvas = new glcanvas( capabilities ); cube cube = new cube(); glcanvas.addgleventlistener( cube ); glcanvas.setsize( 400, 400 ); 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(); } }
Comments
Post a Comment