如何在JOGL中移动相机眼睛位置?

时间:2016-01-28 20:58:37

标签: java opengl jogl

我试图在慢跑中移动相机,但我的代码似乎没有做任何事情。如何使用键盘输入移动相机?我正在画一个旋转的六边形,我想移动相机,但我似乎无法让它工作。这是我的代码:

import javax.media.opengl.*;
import javax.media.opengl.awt.*;
import com.sun.opengl.util.*;
import com.sun.opengl.util.gl2.GLUT;
import javax.media.opengl.glu.*;

import java.awt.event.*;
import java.awt.*;
import javax.swing.*;

public class Rotation2 extends JFrame implements GLEventListener, KeyListener
{
    GLCanvas canvas;
    Animator an;

    public Rotation2()
    {

        canvas=new GLCanvas();
        an=new Animator(canvas);
        add(canvas);
        canvas.addGLEventListener(this);
        canvas.addKeyListener(this);
        setSize(600,400);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        an.start();

    }
    double eyeX = 0;
    double eyeY = 0;
    double eyeZ = 0;

    public void init(GLAutoDrawable drawable)
    {
        GL2 gl=drawable.getGL().getGL2();
        GLU glu = new GLU();
        gl.glClearColor(0.0f,0.0f,0.0f,0.0f);
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glOrtho(-300,300,-200,200,200,-200);
        //gl.glLoadIdentity();
        gl.glMatrixMode(GL2.GL_MODELVIEW);
        //glu.gluLookAt(5, 5, 5,0, 0, 0, 1, 100, 0);
        gl.glClearDepth(1.0f);                   // Set background depth to farthest
        gl.glEnable(GL2.GL_DEPTH_TEST);   // Enable depth testing for z-culling
        gl.glDepthFunc(GL2.GL_LEQUAL);    // Set the type of depth-test
    }
    double x=0;

    public void display(GLAutoDrawable drawable)
    {
        GL2 gl=drawable.getGL().getGL2();
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
        gl.glEnable(GL2.GL_DEPTH_TEST);
        gl.glLoadIdentity();

        GLU glu=new GLU();

        //gl.glPushMatrix();
        gl.glScaled(2,2,2);
        gl.glRotated(x,1,1,1);
        x+=.05;

        glu.gluLookAt(eyeX, eyeY, 2,0, 0, 0, 1, 100, 0);

        gl.glColor3f(0,1f,0);
        // front
        gl.glBegin(GL2.GL_POLYGON);
        gl.glVertex3d(25,50,25);
        gl.glVertex3d(-25,50,25);
        gl.glVertex3d(-50,0,25);
        gl.glVertex3d(-25,-50,25);
        gl.glVertex3d(25,-50,25);
        gl.glVertex3d(50,0,25);
        gl.glEnd();
        //left
        gl.glColor3f(1f,1f,0);
        gl.glBegin(GL2.GL_POLYGON);
        gl.glVertex3d(25,50,-25);
        gl.glVertex3d(-25,50,-25);
        gl.glVertex3d(-50,0,-25);
        gl.glVertex3d(-25,-50,-25);
        gl.glVertex3d(25,-50,-25);
        gl.glVertex3d(50,0,-25);

        gl.glEnd();


        gl.glColor3f(1f,0f,0);
        gl.glBegin(GL2.GL_POLYGON);

        gl.glVertex3d(25,50,25);
        gl.glVertex3d(25,50,-25);
        gl.glVertex3d(-25,50,-25);
        gl.glVertex3d(-25,50,25);

        gl.glEnd();

        gl.glColor3f(0f,0f,1f);
        gl.glBegin(GL2.GL_POLYGON);

        gl.glVertex3d(-25,50,25);
        gl.glVertex3d(-25,50,-25);
        gl.glVertex3d(-50,0,-25);
        gl.glVertex3d(-50,0,25);

        gl.glEnd();

        gl.glColor3f(1f,1f,1f);
        gl.glBegin(GL2.GL_POLYGON);

        gl.glVertex3d(-50,0,25);
        gl.glVertex3d(-50,0,-25);
        gl.glVertex3d(-25,-50,-25);
        gl.glVertex3d(-25,-50,25);

        gl.glEnd();

        gl.glColor3f(0,1f,1f);

        gl.glBegin(GL2.GL_POLYGON);

        gl.glVertex3d(-25,-50,25);
        gl.glVertex3d(-25,-50,-25);
        gl.glVertex3d(25,-50,-25);
        gl.glVertex3d(25,-50,25);
        gl.glEnd();

        gl.glColor3f(1,0f,1f);

        gl.glBegin(GL2.GL_POLYGON);
        gl.glVertex3d(25,-50,25);
        gl.glVertex3d(25,-50,-25);
        gl.glVertex3d(50,0,-25);
        gl.glVertex3d(50,0,25);

        gl.glEnd();

        gl.glColor3f(.2f,1f,.2f);

        gl.glBegin(GL2.GL_POLYGON);

        gl.glVertex3d(50,0,25);
        gl.glVertex3d(50,0,-25);
        gl.glVertex3d(25,50,-25);
        gl.glVertex3d(25,50,25);

        gl.glEnd();
        //gl.glPopMatrix();


    }

    public void reshape(GLAutoDrawable drawable,int x,int y,int width,int height)
    {}
    public void dispose(GLAutoDrawable drawable)
    {}


    public static void main(String[] ar)
    {

        new Rotation2();
    }

    @Override
    public void keyTyped(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_X) {
            eyeX++;
        }
        else if (e.getKeyCode() == KeyEvent.VK_Y) {
            eyeY++;
        }
        else if(e.getKeyCode() == KeyEvent.VK_Z) {
            eyeZ++;
        }
        else {

        }
    }

    @Override
    public void keyPressed(KeyEvent e) {

    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

1 个答案:

答案 0 :(得分:1)

这是弃用的opengl,算了吧。

你也在使用一个非常古老的jogl,更新它,继续jogamp.org,在“Builds / Download”下单击“zip”,然后下载jogamp-all-platforms.7z解压缩并按照说明设置jogl { {3}}

拿这个here

它已使用keyListener,只需复制您为keyTyped编写的代码并将其粘贴到keyPressed下(您必须使用jogamp keyListener,您在样本中使用的内容)而是java awt one)。

如果您有任何问题,请告诉我们:)