Java 怎么设置OpenGL Canvas 3D

2019年7月29日20:02:32Java 怎么设置OpenGL Canvas 3D已关闭评论 437 5997字阅读19分59秒

1.简介

Java 怎么设置OpenGL Canvas 3D?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */

import static javax.media.opengl.GL.GL_LINES;
import static javax.media.opengl.fixedfunc.GLMatrixFunc.GL_MODELVIEW;
import static javax.media.opengl.fixedfunc.GLMatrixFunc.GL_PROJECTION;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;

import javax.media.opengl.GL;
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 javax.swing.SwingUtilities;

import movement.state.OneData;

import com.jogamp.opengl.util.awt.TextRenderer;

public class Canvas3D extends GLCanvas implements GLEventListener {

    private static final long serialVersionUID = 1L;

    public OneData oneData;

    public static void main(String[] args) {
        System.out.println("One frame rendered!");
        startFramedOne();
    }

    public static Canvas3D startFramedOne() {
        final Canvas3D canvas2D = new Canvas3D();

        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                canvas2D.setPreferredSize(new Dimension(
                        canvas2D.oneData.screenWidth,
                        canvas2D.oneData.screenHeight));

                final JFrame jFrame = new JFrame();
                jFrame.getContentPane().add(canvas2D);

                jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                
                Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
                jFrame.setLocation(dim.width / 2
                        - canvas2D.oneData.screenWidth / 2, dim.height / 2
                        - canvas2D.oneData.screenHeight / 2);

                jFrame.setTitle(canvas2D.oneData.TITLE);
                jFrame.pack();
                canvas2D.requestFocusInWindow();

                jFrame.setVisible(true);
                canvas2D.display();

                synchronized (canvas2D) {
                    canvas2D.notifyAll();
                }
            }
        });

        return canvas2D;
    }

    private static GLCapabilities getCapabilities() {
        GLCapabilities capabilities = new GLCapabilities(
                GLProfile.getDefault());

        capabilities.setHardwareAccelerated(true);
        capabilities.setDoubleBuffered(true);

        
        capabilities.setNumSamples(2);
        capabilities.setSampleBuffers(true);

        return capabilities;
    }

    public Canvas3D() {
        super(getCapabilities());
        this.addGLEventListener(this);
        this.oneData = new OneData();
    }

    @Override
    public void init(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClearColor(0, 0, 0, 1);

        gl.glEnable(GL.GL_DEPTH_TEST);

        gl.glViewport(0, 0, oneData.width, oneData.height);

        gl.glMatrixMode(GL_PROJECTION);
        gl.glLoadIdentity();
        new GLU().gluPerspective(45.0, (float) oneData.width
                / oneData.height, 0.1, 10000.0); 

        
        gl.glMatrixMode(GL_MODELVIEW);
        
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width,
            int height) {
        this.oneData.screenWidth = width;
        this.oneData.screenHeight = height;

        if (height == 0)
            height = 1; 
    }

    @Override
    public void display(GLAutoDrawable drawable) {
        GL2 gl = drawable.getGL().getGL2();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        
        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glRotatef(15, 1, 0, 0);
        gl.glTranslatef(0, 0, 1);
        RenderingHelper.drawParallelepiped(gl, 1f, 1.5f, .75f);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glRotatef(15, 1, 0, 0);
        gl.glTranslatef(0, 0, 1);

        RenderingHelper.drawBoneLine(gl, 0, -3, 0, 0, 1, 0);

        
        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glRotatef(15, 1, 0, 0);
        gl.glTranslatef(.9f, .4f, 1.75f);
        gl.glRotatef(-4, 0, 0, 1);

        RenderingHelper.drawParallelepiped(gl, 0.5f, .35f, .25f,
                new float[] { .5f, .5f, .1f });

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glRotatef(15, 1, 0, 0);
        gl.glTranslatef(-.9f, .4f, 1.75f);
        gl.glRotatef(3, 0, 0, 1);

        RenderingHelper.drawParallelepiped(gl, 0.5f, .3f, .2f, new float[] {
                .5f, .5f, .1f });

        
        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(0, -3.8f, 0);

        RenderingHelper.drawParallelepiped(gl, 2.3f, 1f, 1.2f);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(0, -3.8f, 0);

        RenderingHelper.drawBoneLine(gl, -3.35f, 0, 0, 3.35f, 0, 0);

        
        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(0, -5, 0);

        RenderingHelper.drawParallelepiped(gl, 1f, 1.8f, .7f);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(0, -5, 0);

        RenderingHelper.drawBoneLine(gl, 0, -3, 0, 0, 2, 0);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(0, -8, 0);

        RenderingHelper.drawBoneLine(gl, -1.4f, 0, -.3f, 1.7f, 0, .3f);

        
        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(4, -7.5f, 0);
        gl.glRotatef(10, 0, 0, 1);

        RenderingHelper.drawParallelepiped(gl, .8f, 1.5f, .7f);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(4, -7.5f, 0);
        gl.glRotatef(10, 0, 0, 1);

        RenderingHelper.drawBoneLine(gl, 0, 3.75f, 0, 0, 0, 0);

        
        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(-4.1f, -6.6f, 1.37f);
        gl.glRotatef(-30, 0, 0, 1);
        gl.glRotatef(-70, 1, 0, 0);

        RenderingHelper.drawParallelepiped(gl, .8f, 1.5f, .7f);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(-4, -7.5f, 0);
        gl.glRotatef(-10, 0, 0, 1);

        RenderingHelper.drawBoneLine(gl, 0, 3.75f, 0, 0, 1, 0);

        
        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(1.7f, -12, .3f);

        RenderingHelper.drawParallelepiped(gl, .8f, 1.3f, .7f);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(1.7f, -13, 0.6f);

        RenderingHelper.drawParallelepiped(gl, .8f, .3f, .7f);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(1.7f, -8, .3f);

        RenderingHelper.drawBoneLine(gl, 0, 0, 0, 0, -3, 0);

        
        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(-1.4f, -12, -.3f);

        RenderingHelper.drawParallelepiped(gl, .8f, 1.3f, .7f);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(-1.4f, -13, 0);

        RenderingHelper.drawParallelepiped(gl, .8f, .3f, .7f);

        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(-1.4f, -8, -.3f);

        RenderingHelper.drawBoneLine(gl, 0, 0, 0, 0, -3, 0);

        
        RenderingHelper
                .setDefaultCamera(gl, oneData.angleX, oneData.angleY);
        gl.glTranslatef(0, -17.3f, 0);

        RenderingHelper.drawParallelepiped(gl, 4, 4, 4, new float[] { 0.1f,
                0.8f, .4f });
    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
    }

}

 
继续阅读
编程十万个怎么办