到目前为止, 我们已经学习了如何在JOGL中创建不同的几何形状, 例如三角形, 立方体和长方体。现在, 我们将创建一个明轮并尝试旋转它。
文章图片
旋转桨轮的3D图像
要通过编码旋转明轮, 请参考以下示例:-
【JOGL 3D桨轮】JPaddle.java
package com.srcmini.jogl;
import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.JFrame;
import com.jogamp.opengl.util.FPSAnimator;
public class JPaddle implements GLEventListener {private float rotation = 0.0f;
@Override
public void display( GLAutoDrawable drawable ) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef( 0f, 0f, -2.0f );
gl.glRotatef(rotation, 1f, 0f, 0f);
gl.glColor3f(1f, 0f, 0f);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(-0.5, 0.3, 0.8);
gl.glVertex3d(0.5, 0.3, 0.8);
gl.glVertex3d(0.8, 0.7, 0.8);
gl.glVertex3d(-0.8, 0.7, 0.8);
gl.glEnd();
int paddles=40;
for (int i = 0;
i <
paddles;
i++) {
gl.glRotated(360.0/paddles, 1, 0, 0);
gl.glBegin(GL2.GL_POLYGON);
gl.glVertex3d(-0.5, 0.3, 0.8);
gl.glVertex3d(0.5, 0.3, 0.8);
gl.glVertex3d(0.8, 0.7, 0.8);
gl.glVertex3d(-0.8, 0.7, 0.8);
gl.glEnd();
}rotation -= 0.2f;
}public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {}
public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
}
@Override
public void dispose(GLAutoDrawable arg0) {}
@Override
public void init(GLAutoDrawable arg0) {}public static void main( String[] args ) {
final GLProfile gp = GLProfile.get( GLProfile.GL2 );
GLCapabilities cap = new GLCapabilities( gp );
final GLCanvas gc = new GLCanvas(cap);
JPaddle paddle = new JPaddle();
gc.addGLEventListener( paddle );
gc.setSize( 400, 400 );
final JFrame frame = new JFrame ("Motor Paddle");
frame.add(gc);
frame.setSize(600, 500);
frame.setVisible(true);
final FPSAnimator animator = new FPSAnimator(gc, 200, true);
animator.start();
}
}
输出:
文章图片