【JOGL 3D立方体】在本节中, 我们将学习如何创建多维数据集。因此, 为此首先创建六个相同大小的四边形, 并将它们定位为通过连接它们将得到一个合适的立方体。我们还将旋转此立方体以查看其所有侧面。
要创建旋转的多维数据集, 请通过以下示例:-
J3DCube.java
package com.srcmini.jogl;
import javax.media.opengl.GL2;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.media.opengl.glu.GLU;
import javax.swing.JFrame;
import com.jogamp.opengl.util.FPSAnimator;
public class J3DCube implements GLEventListener {private GLU glu = new GLU();
private float rotation = 0.0f;
@Overridepublic 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, -2.0f );
gl.glRotatef(rotation, 1.0f, 1.0f, 1.0f);
gl.glBegin(GL2.GL_QUADS);
gl.glColor3f(0f, 0f, 1f);
//Blue color//Top Quadrilateralgl.glVertex3f(0.5f, 0.5f, -0.5f);
//Upper Rightgl.glVertex3f( -0.5f, 0.5f, -0.5f);
// Upper Leftgl.glVertex3f( -0.5f, 0.5f, 0.5f );
// Bottom Leftgl.glVertex3f( 0.5f, 0.5f, 0.5f );
// Bottom Right//Below Quadrilateralgl.glColor3f( 1f, 0f, 0f );
//Red colorgl.glVertex3f( 0.5f, -0.5f, 0.5f );
// Upper Right gl.glVertex3f( -0.5f, -0.5f, 0.5f );
// Upper Left gl.glVertex3f( -0.5f, -0.5f, -0.5f );
// Bottom Left gl.glVertex3f( 0.5f, -0.5f, -0.5f );
// Bottom Right //Front Quadrilateralgl.glColor3f( 0f, 1f, 0f );
//Green colorgl.glVertex3f( 0.5f, 0.5f, 0.5f );
// Upper Right gl.glVertex3f( -0.5f, 0.5f, 0.5f );
// Upper Left gl.glVertex3f( -0.5f, -0.5f, 0.5f );
// Bottom Left gl.glVertex3f( 0.5f, -0.5f, 0.5f );
// Bottom Right//Back Quadrilateralgl.glColor3f( 1f, 1f, 0f );
//Yellowgl.glVertex3f( 0.5f, -0.5f, -0.5f );
// Bottom Left gl.glVertex3f( -0.5f, -0.5f, -0.5f );
// Bottom Right gl.glVertex3f( -0.5f, 0.5f, -0.5f );
// Upper Right gl.glVertex3f( 0.5f, 0.5f, -0.5f );
// Upper Left //Left Quadrilateralgl.glColor3f( 1f, 0f, 1f );
//Purplegl.glVertex3f( -0.5f, 0.5f, 0.5f );
// Upper Rightgl.glVertex3f( -0.5f, 0.5f, -0.5f );
// Upper Left gl.glVertex3f( -0.5f, -0.5f, -0.5f );
// Bottom Left gl.glVertex3f( -0.5f, -0.5f, 0.5f );
// Bottom Right //Right Quadrilateralgl.glColor3f( 0f, 1f, 1f );
//Cyangl.glVertex3f( 0.5f, 0.5f, -0.5f );
// Upper Right gl.glVertex3f( 0.5f, 0.5f, 0.5f );
// Upper Left gl.glVertex3f( 0.5f, -0.5f, 0.5f );
// Bottom Left gl.glVertex3f( 0.5f, -0.5f, -0.5f );
// Bottom Right gl.glEnd();
gl.glFlush();
rotation += 0.6f;
}@Overridepublic void dispose( GLAutoDrawable drawable ) {}@Overridepublic void init( GLAutoDrawable drawable ) { }@Overridepublic void reshape( GLAutoDrawable drawable, int x, int y, int width, int height ) { final GL2 gl = drawable.getGL().getGL2();
if( height <
= 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 gp = GLProfile.get( GLProfile.GL2 );
GLCapabilities cap = new GLCapabilities(gp );
final GLCanvas gc = new GLCanvas(cap);
J3DCube cube = new J3DCube();
gc.addGLEventListener( cube );
gc.setSize( 400, 400 );
final JFrame frame = new JFrame( " 3D cube" );
frame.add(gc);
frame.setSize(600, 500);
frame.setVisible(true);
final FPSAnimator animator = new FPSAnimator(gc, 200, true);
animator.start();
} }
输出:
文章图片