如何让相机沿着旋转的方向移动?

How to make the camera move in the direction of the rotation?

如何让相机沿着旋转的方向移动?如何计算相机的位置以跟随旋转? 这是我的代码:

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 = null;
    Animator an;

    public Rotation2()
    {   
        canvas=new GLCanvas();
        an=new Animator(canvas);
        add(canvas);
        canvas.addGLEventListener(this);
        canvas.addKeyListener(this);
        setSize(1280,900);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        an.start();
        requestFocus();

    }
    float xPosition = 0;
    float zPosition = 0;

    float red = 0;
    float green = 0;
    float blue = 1;


    public void init(GLAutoDrawable drawable)
    {
        GL2 gl = drawable.getGL().getGL2();
        GLU glu = new GLU();
        gl.glMatrixMode(GL2.GL_PROJECTION);
        gl.glLoadIdentity();

        double w = drawable.getWidth();
        double h = drawable.getHeight();
        double aspect = w/h;
        glu.gluPerspective(60.0, aspect, 2.0, 20.0);

    }
    double zRot = 0;
    double xRot = 0;

    public void display(GLAutoDrawable drawable)
    {
        GL2 gl=drawable.getGL().getGL2();
        GLU glu = new GLU();
        GLUT glut = new GLUT();

        gl.glMatrixMode(GL2.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glClearColor(0f,0f,0f,0f);
        //xPosition+=0.005;
        //zPosition+=0.005;
        gl.glRotated(zRot, 0, 1, 0);
        gl.glRotated(xRot, 1, 0, 0);
        glu.gluLookAt(xPosition, 0, zPosition,
                      xPosition, 0, (zPosition+20),
                      0, 1, 0);

        gl.glClear(GL2.GL_COLOR_BUFFER_BIT);

        red = 0.0f;
        green = 0.0f;
        blue = 0.9f;
        gl.glColor3f(red, green, blue);
//transforming the place the next shape
//will be drawn.
        gl.glTranslated(2, 0, 2);
//We use wire here because default
//lighting is not good enough to
//use when rendering the solid version
        glut.glutWireIcosahedron();
//more shapes to navigate through
        gl.glTranslated(-4, 0, 0);
        glut.glutWireIcosahedron();
        red = 0.0f;
        green = 0.9f;
        blue = 0.1f;
        gl.glColor3f(red, green, blue);
        gl.glTranslated(4, 0, 4);
        glut.glutWireIcosahedron();
        gl.glTranslated(-4, 0, 0);
        glut.glutWireIcosahedron();
        red = 0.9f;
        green = 0.0f;
        blue = 0.1f;
        gl.glColor3f(red, green, blue);
        gl.glTranslated(4, 0, 4);
        glut.glutWireIcosahedron();
        gl.glTranslated(-4, 0, 0);
        glut.glutWireIcosahedron();
        red = 0.9f;
        green = 0.0f;
        blue = 0.9f;
        gl.glColor3f(red, green, blue);
        gl.glTranslated(4, 0, 4);
        glut.glutWireIcosahedron();
        gl.glTranslated(-4, 0, 0);
        glut.glutWireIcosahedron();


    }

    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) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_W) {
            zPosition++;
        }
        else if (e.getKeyCode() == KeyEvent.VK_S) {
            zPosition--;
        }
        else if(e.getKeyCode() == KeyEvent.VK_A) {
            xPosition++;
        }
        else if (e.getKeyCode() == KeyEvent.VK_D) {
            xPosition--;
        }
        if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
            zRot+=5;
        }
        else if (e.getKeyCode() == KeyEvent.VK_LEFT) {
            zRot-=5;
        }
        else if (e.getKeyCode() == KeyEvent.VK_UP) {
            xRot-=5;
        }
        else if (e.getKeyCode() == KeyEvent.VK_DOWN) {
            xRot+=5;
        }
        canvas.repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}

p.s。您必须单击 canvas 才能开始工作。

elect是对的,你要了解基本概念,不能对计算机图形学的概念不了解就胡乱摆弄。您可以阅读 OpenGL 红皮书,其代码示例已移植到 JOGL 并可在 Github here 上找到。

我自己的第一人称射击游戏最古老的 alpha 版本 类 展示了如何实现相机,但不能向上或向下看: GameGLView GameController GameModel

P.S:我必须尽快更新它以使其适用于 JOGL 2.3.2 及更高版本。它适用于 JOGL 2.3.1。

P.P.S: 我昨天(2016 年 2 月 4 日)更新了上面的源代码,使其可以与 JOGL 2.3.2 一起使用。