【发布时间】:2016-01-29 08:51:56
【问题描述】:
如何使相机沿旋转方向移动?如何计算相机的位置以跟随旋转? 这是我的代码:
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) {
}
}
附言您必须单击画布才能开始工作。
【问题讨论】:
-
而不是旋转你的相机 - 你为什么不直接在你的对象上使用变换来改变相机瞄准的点并反射相机角度呢?您可以使用世界上的各种转换矩阵来做到这一点。
-
恐怕我不知道该怎么做,而且听起来似乎不是我想要的。我希望能够像在 FPS 游戏中那样在物体周围移动。
-
乔伊,除了学习和理解事物的运作方式之外别无他法,对不起,这不是你 10 分钟就能学会的东西
-
@elect 我习惯于看教程和学习东西,我正在寻找有关如何为 FPS 相机建模的指针,我还没有研究过任何与之相关的东西,我只知道如何绘制多边形和使用转换函数。 JOGL上的资源太可怕了!我找不到一本好书或类似的东西:D