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

时间:2016-01-29 08:51:57

标签: java opengl camera 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 = 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。您必须单击画布才能开始工作。

1 个答案:

答案 0 :(得分:2)

选举是对的,你必须要理解基本概念,你不能仅仅修补对计算机图形概念的理解。您可以阅读OpenGL Red Book,其代码示例已移植到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一起使用。

相关问题