JOGL 3D浆轮

到目前为止,我们已经学习了如何在 JOGL 中创建不同的几何形状,如三角形、立方体和长方体。现在,我们将创建一个3D桨轮并尝试旋转它。

要通过编码3D旋转桨轮,请查看以下示例:

package com.yiidian;

import com.jogamp.opengl.util.FPSAnimator;

import javax.media.opengl.*;
import javax.media.opengl.awt.GLCanvas;
import javax.swing.*;

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("yiidian.com:Motor Paddle");
        frame.add(gc);
        frame.setSize(600, 500);
        frame.setVisible(true);

        final FPSAnimator animator = new FPSAnimator(gc, 200, true);
        animator.start();
    }

}  

输出结果为:

热门文章

优秀文章