Java 在Opengl中使用θ和φ角进行旋转

Java 在Opengl中使用θ和φ角进行旋转,java,opengl,matrix,opengl-es,rotation,Java,Opengl,Matrix,Opengl Es,Rotation,我正在尝试用OpenGL移动和旋转球体。为此,我保存我的矩阵状态,加载单位矩阵,然后转换到我想要的点,最后,旋转并推动状态,如上面代码中所示。因为这些过程是从结束到开始进行的,所以旋转是从(0,0,0)开始的,并且只有在这之后,旋转才会移动到所需的点。平移很好,但旋转有问题。出于这个目的,我想用两个角度来描述旋转:θ和φ(只是按照你在这里看到的物理惯例)。如果我先做φ的旋转,然后再做θ的旋转,结果不同于按相反的顺序做的。我知道这是因为矩阵积。另外,我已经读到,有一个问题与欧拉角万向节锁,我应该使

我正在尝试用OpenGL移动和旋转球体。为此,我保存我的矩阵状态,加载单位矩阵,然后转换到我想要的点,最后,旋转并推动状态,如上面代码中所示。因为这些过程是从结束到开始进行的,所以旋转是从(0,0,0)开始的,并且只有在这之后,旋转才会移动到所需的点。平移很好,但旋转有问题。出于这个目的,我想用两个角度来描述旋转:θ和φ(只是按照你在这里看到的物理惯例)。如果我先做φ的旋转,然后再做θ的旋转,结果不同于按相反的顺序做的。我知道这是因为矩阵积。另外,我已经读到,有一个问题与欧拉角万向节锁,我应该使用四元数,但我仍然没有这么清楚。我将数据视为正交视图,由于上述原因,我不确定我所做的是否正确,或者这只是我尝试的角度带来的一些运气。你将如何进行

    gl.glPushMatrix();
    gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
    gl.glTranslatef(x, y, z);
    gl.glRotatef(theta, 0,1,0); 
    gl.glRotatef(phi, 0,0,1);

    GLUquadric earth = glu.gluNewQuadric();
    glu.gluQuadricTexture(earth, true);
    glu.gluQuadricDrawStyle(earth, GLU.GLU_FILL);
    glu.gluQuadricNormals(earth, GLU.GLU_FLAT);
    glu.gluQuadricOrientation(earth, GLU.GLU_OUTSIDE);          
    glu.gluSphere(earth, radius, slices, stacks);        
    glu.gluDeleteQuadric(earth);
    gl.glPopMatrix();

我的理解是,在这个旋转之后,gl.glRotatef(θ,0,1,0)向量(0,0,1)也改变了。对于代码gl.glRotatef(φ,0,0,1)在对象局部坐标中工作。我想你想在全球空间做这件事。我认为若你们把向量(0,0,1)旋转0,1,0到θ度,然后用新的向量进行第二次旋转

以下是伪代码示例:

gl.glRotatef(theta, 0,1,0); 
glm::mat4 rotationMatrix = glm::rotate(glm::mat4(1.0f), theta, glm::vec3(0.0, 1.0, 0.0));
glm::mat4 rotateVector = rotationMatrix * glm::vec4(0.0, 0.0, 1.0));
gl.glRotatef(phi, rotateVector.x, rotateVector.y, rotateVector.z);

我认为这段代码无法编译。我试图说明我使用函数的意思

最后,我改变了主意,不再使用ortho,而是尝试使用透视法来理解问题。起初,我注意到我的粒子旋转了一个额外的角度。但最后,我认为这是因为纹理是如何添加到粒子的。我尝试了几件像这里说的那样的事情:但这是不可能的。在尝试了很多东西之后,我试着画轴线,经过数小时的测试,我发现我忘记了Math.sin和Math.cos是弧度,而不是像OpenGl那样的度数。当我意识到这一点时,我注意到用线条来表达是完美的。所有这些都是在全球坐标系下发生的。简言之:

    gl.glRotatef(theta, 0, 1, 0);
    gl.glRotatef(phi, 0, 0, 1);
没关系。我让我的代码让我明白这一点。请将“路由到图像”更改为png图像,该图像是一个正方形,分为4种不同颜色(如windows标志)。你可以用油漆做

import java.awt.BorderLayout;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;

import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.glu.GLU;
import com.jogamp.opengl.glu.GLUquadric;
import com.jogamp.opengl.awt.GLCanvas;

import javax.swing.JFrame;

import com.jogamp.opengl.util.FPSAnimator;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureData;
import com.jogamp.opengl.util.texture.TextureIO;

/**
 * Test developed using:
 * 
 * A minimal JOGL demo.
 * 
 * @author <a href="mailto:kain@land-of-kain.de">Kai Ruhl</a>
 * @since 26 Feb 2009
 */
public class test3d extends GLCanvas implements GLEventListener {

    /** Serial version UID. */
    private static final long serialVersionUID = 1L;

    /** The GL unit (helper class). */
    private GLU glu;

    /** The frames per second setting. */
    private int fps = 24;

    /** The OpenGL animator. */
    private FPSAnimator animator;

    /** The earth texture. */
    private Texture earthTexture;

    private int see_ = 0;

    /**
     * A new mini starter.
     */
    public test3d(int see) {
        addGLEventListener(this);
        see_ = see;
    }

    /**
     * Sets up the screen.
     * 
     * @see javax.media.opengl.GLEventListener#init(javax.media.opengl.GLAutoDrawable)
     */
    public void init(GLAutoDrawable drawable) {
        final GL2 gl = (GL2) drawable.getGL();

        // Enable z- (depth) buffer for hidden surface removal. 
        gl.glEnable(GL.GL_DEPTH_TEST);
        gl.glDepthFunc(GL.GL_LEQUAL);

        // Enable smooth shading.
        gl.glShadeModel(GL2.GL_SMOOTH);

        // Define "clear" color.
        gl.glClearColor(0f, 0f, 0f, 0f);

        // We want a nice perspective.
        gl.glHint(GL2.GL_PERSPECTIVE_CORRECTION_HINT, GL.GL_NICEST);

        // Create GLU.
        glu = new GLU();
        GLProfile profile = GLProfile.get(GLProfile.GL2);
        // Load earth texture.
        try {
            FileInputStream stream = new FileInputStream(ROUTE TO IMAGE);
            TextureData data = TextureIO.newTextureData(profile, stream, false, "png");
            earthTexture = TextureIO.newTexture(data);
        }
        catch (IOException exc) {
            exc.printStackTrace();
            System.exit(1);
        }

        // Start animator.
        animator = new FPSAnimator(this, fps);
        animator.start();
    }

    /**
     * The only method that you should implement by yourself.
     * 
     * @see javax.media.opengl.GLEventListener#display(javax.media.opengl.GLAutoDrawable)
     */
    public void display(GLAutoDrawable drawable) {
        if (!animator.isAnimating()) {
            return;
        }
        final GL2 gl = (GL2) drawable.getGL();

        // Clear screen.
        gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

        // Set camera.
        setCamera(gl, glu, 30);

        // Prepare light parameters.
        float SHINE_ALL_DIRECTIONS = 1;
        float[] lightPos = {0, 0, 0, SHINE_ALL_DIRECTIONS};
        float[] lightColorAmbient = {1f, 1f, 1f, 1f};
        float[] lightColorSpecular = {1f, 1f, 1f, 1f};

        // Set light parameters.
        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_POSITION, lightPos, 0);
        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_AMBIENT, lightColorAmbient, 0);
        gl.glLightfv(GL2.GL_LIGHT1, GL2.GL_SPECULAR, lightColorSpecular, 0);

        // Enable lighting in GL.
        gl.glEnable(GL2.GL_LIGHT1);
        gl.glEnable(GL2.GL_LIGHTING);

        // Set material properties.
        float[] rgba = {1f, 0f, 0f};
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f);

        gl.glBegin(gl.GL_LINES);        //RED == X
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(10, 0, 0);
        gl.glEnd();

        rgba[0] = 0f;
        rgba[1] = 1f;
        rgba[2] = 0f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f);

        gl.glBegin(gl.GL_LINES);        //GREEN == Y
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(0, 10, 0);
        gl.glEnd();


        rgba[0] = 0f;
        rgba[1] = 0f;
        rgba[2] = 1f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f);
        gl.glBegin(gl.GL_LINES);        //BLUE == Z
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(0, 0, 10);
        gl.glEnd();     

        // Set material properties.
        float theta = 45f;
        float phi = 270f;
        /*
        ////////////////////////////////////////////////////LINES 1 ROT//////////////////////////////////////////////////////////
        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
        rgba[0] = 1f;
        rgba[1] = 0f;
        rgba[2] = 1f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f);

        gl.glRotatef(phi, 0, 0, 1);

        gl.glBegin(gl.GL_LINES);        //VIOLET == X'
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(10, 0, 0);
        gl.glEnd();

        gl.glPopMatrix();


        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
        rgba[0] = 1f;
        rgba[1] = 1f;
        rgba[2] = 0f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f);

        gl.glRotatef(phi, 0, 0, 1);

        gl.glBegin(gl.GL_LINES);        //YELLOW == Y'
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(0, 10, 0);
        gl.glEnd();

        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
        rgba[0] = 0f;
        rgba[1] = 1f;
        rgba[2] = 1f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f); 

        gl.glRotatef(phi, 0, 0, 1);

        gl.glBegin(gl.GL_LINES);        //CYAN == Z'
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(0, 0, 10);
        gl.glEnd();     

        gl.glPopMatrix();
        */

        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
        rgba[0] = 1f;
        rgba[1] = 1f;
        rgba[2] = 1f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f); 

        //gl.glRotatef(theta, (float) Math.sin(phi), (float) Math.cos(phi), 0);
        gl.glTranslatef(3, 0, 0);
        gl.glRotatef(theta, 0, 1, 0);
        gl.glRotatef(phi, 0, 0, 1);
        gl.glBegin(gl.GL_LINES);        //WHITE == NEW Y'
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(0, 0, 10.f);
        gl.glEnd();

        gl.glPopMatrix();


        ////////////////////////////////////////////////////LINES 2 ROT//////////////////////////////////////////////////////////
        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
        rgba[0] = 1f;
        rgba[1] = 0f;
        rgba[2] = 1f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f);

        //gl.glRotatef(theta, (float) -Math.sin(phi), (float) Math.cos(phi),0); 
        //gl.glRotatef(theta, (float) Math.sin(phi*Math.PI/180), (float) Math.cos(phi*Math.PI/180),0);    
        gl.glRotatef(theta, 0, 1, 0);
        gl.glRotatef(phi, 0, 0, 1);

        gl.glBegin(gl.GL_LINES);        //VIOLET == X'
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(10, 0, 0);
        gl.glEnd();

        gl.glPopMatrix();


        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
        rgba[0] = 1f;
        rgba[1] = 1f;
        rgba[2] = 0f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f);

        //gl.glRotatef(theta, (float) -Math.sin(phi), (float) Math.cos(phi),0); 
        //gl.glRotatef(theta, (float) Math.sin(phi*Math.PI/180), (float) Math.cos(phi*Math.PI/180),0);    
        gl.glRotatef(theta, 0, 1, 0);
        gl.glRotatef(phi, 0, 0, 1);

        gl.glBegin(gl.GL_LINES);        //YELLOW == Y'
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(0, 10, 0);
        gl.glEnd();

        gl.glPopMatrix();

        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
        rgba[0] = 0f;
        rgba[1] = 1f;
        rgba[2] = 1f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f); 

        //gl.glRotatef(theta, (float) -Math.sin(phi), (float) Math.cos(phi),0); 
        //gl.glRotatef(theta, (float) Math.sin(phi*Math.PI/180), (float) Math.cos(phi*Math.PI/180),0); 
        gl.glRotatef(theta, 0, 1, 0);
        gl.glRotatef(phi, 0, 0, 1);

        gl.glBegin(gl.GL_LINES);        //CYAN == Z'
            gl.glVertex3f(0, 0, 0);
            gl.glVertex3f(0, 0, 10);
        gl.glEnd();     

        gl.glPopMatrix();

        //////////////////////////////////////////SPHERES////////////////////////////////////////////

        // Apply texture.

        rgba[0] = 1f;
        rgba[1] = 1f;
        rgba[2] = 1f;
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_AMBIENT, rgba, 0);
        gl.glMaterialfv(GL.GL_FRONT, GL2.GL_SPECULAR, rgba, 0);
        gl.glMaterialf(GL.GL_FRONT, GL2.GL_SHININESS, 1f);

        earthTexture.enable(gl);
        earthTexture.bind(gl);

        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)

        GLUquadric earth = glu.gluNewQuadric();
        glu.gluQuadricTexture(earth, true);
        glu.gluQuadricDrawStyle(earth, GLU.GLU_FILL);
        glu.gluQuadricNormals(earth, GLU.GLU_FLAT);
        glu.gluQuadricOrientation(earth, GLU.GLU_OUTSIDE);          
        glu.gluSphere(earth, 0.5, 64, 64);        
        glu.gluDeleteQuadric(earth);
        gl.glPopMatrix();

        earthTexture.disable(gl);

        earthTexture.enable(gl);
        earthTexture.bind(gl);



        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
        gl.glTranslatef(1.5f, 0, 0);    
        gl.glRotatef(phi, 0,0,1);

        GLUquadric earth1 = glu.gluNewQuadric();
        glu.gluQuadricTexture(earth1, true);
        glu.gluQuadricDrawStyle(earth1, GLU.GLU_FILL);
        glu.gluQuadricNormals(earth1, GLU.GLU_FLAT);
        glu.gluQuadricOrientation(earth1, GLU.GLU_OUTSIDE);         
        glu.gluSphere(earth1, 0.5, 64, 64);        
        glu.gluDeleteQuadric(earth1);
        gl.glPopMatrix();

        earthTexture.disable(gl);

        earthTexture.enable(gl);
        earthTexture.bind(gl);

        gl.glPushMatrix();
        gl.glLoadIdentity();            //I reset the matrix. Now the translation and rotation is from the origin (0,0,0)
        gl.glTranslatef(3.f, 0, 0);         
        gl.glRotatef(theta, 0, 1,0);    
        gl.glRotatef(phi, 0,0,1);

        GLUquadric earth2 = glu.gluNewQuadric();
        glu.gluQuadricTexture(earth2, true);
        glu.gluQuadricDrawStyle(earth2, GLU.GLU_FILL);
        glu.gluQuadricNormals(earth2, GLU.GLU_FLAT);
        glu.gluQuadricOrientation(earth2, GLU.GLU_OUTSIDE);         
        glu.gluSphere(earth2, 0.5, 64, 64);        
        glu.gluDeleteQuadric(earth2);
        gl.glPopMatrix();

        earthTexture.disable(gl);
    }

    /**
     * Resizes the screen.
     * 
     * @see javax.media.opengl.GLEventListener#reshape(javax.media.opengl.GLAutoDrawable,
     *      int, int, int, int)
     */
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
        final GL gl = drawable.getGL();
        gl.glViewport(0, 0, width, height);
    }

    /**
     * Changing devices is not supported.
     * 
     * @see javax.media.opengl.GLEventListener#displayChanged(javax.media.opengl.GLAutoDrawable,
     *      boolean, boolean)
     */
    public void displayChanged(GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged) {
        throw new UnsupportedOperationException("Changing display is not supported.");
    }

    /**
     * @param gl The GL context.
     * @param glu The GL unit.
     * @param distance The distance from the screen.
     */
    private void setCamera(GL2 gl, GLU glu, float distance) {
        // Change to projection matrix.
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();

        // Perspective.
        float widthHeightRatio = (float) getWidth() / (float) getHeight();
        glu.gluPerspective(45, widthHeightRatio, 1, 10000);
        if (see_ == 0){
            glu.gluLookAt(10, 10, 10, 0, 0, 0, 0, 0, 1);        
        }else if (see_ == 1){
            glu.gluLookAt(10, 0, 0, 0, 0, 0, 0, 0, 1);      
        }else if (see_ == 2){
            glu.gluLookAt(0, 10, 0, 0, 0, 0, 0, 0, 1);     
        }else if (see_ == 3){
            glu.gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);     
        }
        // Change back to model view matrix.
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
    }

    /**
     * Starts the JOGL mini demo.
     * 
     * @param args Command line args.
     */
    public final static void main(String[] args) {
        GLProfile profile = GLProfile.get(GLProfile.GL2);
        GLCapabilities capabilities = new GLCapabilities(profile);
        capabilities.setRedBits(8);
        capabilities.setBlueBits(8);
        capabilities.setGreenBits(8);
        capabilities.setAlphaBits(8);         

        int size = 640;

        test3d canvas = new test3d(0);        
        JFrame frame = new JFrame("Mini JOGL Demo (breed)");
        frame.getContentPane().add(canvas, BorderLayout.CENTER);
        frame.setBounds(0, 200, size, size);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

        test3d canvas2 = new test3d(1);        
        JFrame frame2 = new JFrame("Mini JOGL Demo (breed)");
        frame2.getContentPane().add(canvas2, BorderLayout.CENTER);
        frame2.setBounds(0, 200, size, size);
        frame2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame2.setVisible(true);

        test3d canvas3 = new test3d(2);        
        JFrame frame3 = new JFrame("Mini JOGL Demo (breed)");
        frame3.getContentPane().add(canvas3, BorderLayout.CENTER);
        frame3.setBounds(0, 200, size, size);
        frame3.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame3.setVisible(true);

        test3d canvas4 = new test3d(3);        
        JFrame frame4 = new JFrame("Mini JOGL Demo (breed)");
        frame4.getContentPane().add(canvas4, BorderLayout.CENTER);
        frame4.setBounds(0, 200, size, size);
        frame4.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame4.setVisible(true);

    }

    @Override
    public void dispose(GLAutoDrawable drawable) {
        // TODO Auto-generated method stub

    }

}
导入java.awt.BorderLayout;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.util.ArrayList;
导入com.jogamp.opengl.GL;
导入com.jogamp.opengl.GL2;
导入com.jogamp.opengl.GLAutoDrawable;
导入com.jogamp.opengl.gl功能;
导入com.jogamp.opengl.GLEventListener;
导入com.jogamp.opengl.GLProfile;
导入com.jogamp.opengl.glu.glu;
导入com.jogamp.opengl.glu.gluquadrac;
导入com.jogamp.opengl.awt.GLCanvas;
导入javax.swing.JFrame;
导入com.jogamp.opengl.util.FPSAnimator;
导入com.jogamp.opengl.util.texture.texture;
导入com.jogamp.opengl.util.texture.TextureData;
导入com.jogamp.opengl.util.texture.TextureIO;
/**
*使用以下工具开发的测试:
* 
*一个最小的慢跑演示。
* 
*@作者
*@自2009年2月26日起
*/
公共类test3d扩展了GLCanvas实现了GLEventListener{
/**串行版本UID*/
私有静态最终长serialVersionUID=1L;
/**GL单元(辅助对象类)*/
私有GLU-GLU;
/**每秒帧数设置*/
私有整数fps=24;
/**OpenGL动画师*/
私人动画师;
/**地球的结构*/
私人纹理;
私有int see=0;
/**
*新的迷你起动器。
*/
公共test3d(int-see){
addGLEventListener(本);
见;
}
/**
*设置屏幕。
* 
*@see javax.media.opengl.GLEventListener#init(javax.media.opengl.GLAutoDrawable)
*/
公共void init(GLAutoDrawable){
最终GL2 gl=(GL2)drawable.getGL();
//启用z-(深度)缓冲区以删除隐藏曲面。
gl.glEnable(gl.gl\U深度测试);
gl.glDepthFunc(gl.gl_LEQUAL);
//启用平滑着色。
gl.glShadeModel(GL2.glu平滑);
//定义“清晰”颜色。
gl.glClearColor(0f,0f,0f,0f);
//我们需要一个好的视角。
gl.glHint(GL2.gl\u透视图\u校正\u提示,gl.gl\u最佳);
//创建GLU。
glu=新的glu();
GLProfile=GLProfile.get(GLProfile.GL2);
//加载地球纹理。
试一试{
FileInputStream=新的FileInputStream(路由到图像);
TextureData=TextureIO.newTextureData(配置文件,流,假,“png”);
earthTexture=TextureIO.newTexture(数据);
}
捕获(IOException){
exc.printStackTrace();
系统出口(1);
}
//启动动画师。
animator=新的fps animator(此,fps);
animator.start();
}
/**
*您应该自己实现的唯一方法。
* 
*@see javax.media.opengl.GLEventListener#display(javax.media.opengl.GLAutoDrawable)
*/
公共空白显示(GLAutoDrawable){
如果(!animator.isAnimating()){
返回;
}
最终GL2 gl=(GL2)drawable.getGL();
//清除屏幕。
gl.glClear(gl.gl_颜色_缓冲_位| gl.gl_深度_缓冲_位);
//设置摄像机。
设置摄像头(gl、glu、30);
//准备灯光参数。
所有方向的浮动发光=1;
float[]lightPos={0,0,0,照亮所有方向};
float[]lightColorAmbient={1f,1f,1f,1f};
float[]lightColorSpecular={1f,1f,1f,1f};
//设置灯光参数。
gl.glLightfv(GL2.gl_LIGHT1,GL2.gl_位置,lightPos,0);
gl.glLightfv(GL2.gl_LIGHT1,GL2.gl_AMBIENT,lightcoloranbient,0);
gl.glLightfv(GL2.gl_LIGHT1,GL2.gl_SPECUL