我想创建一个使用LWJGL的FPS相机,但我的代码似乎不工作。 -_- 它只是在屏幕前创建一个白色块,玩家不能移动或旋转。 问题是什么?LWJGL 3D运动
public class LocalWindow
{
public void Launch()
{
try
{
Display.setDisplayMode (new DisplayMode (800, 600));
Display.setTitle("Historica");
Display.create();
}
catch (LWJGLException e)
{
e.printStackTrace();
}
}
public void Render()
{
initMatrix();
while (!Display.isCloseRequested())
//&& !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
{
initControls();
frameRefresh();
initCamera();
Block s = new Block(0, 0, -4);
s.setBlock();
Display.update();
}
}
private void initCamera()
{
Camera player = new Camera();
player.setView();
}
private void frameRefresh()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
}
private void initControls()
{
Mouse.setGrabbed(true);
Controls controls = new Controls();
controls.moveForward(0.1f);
controls.moveBackward(0.1f);
controls.strafeLeft(0.1f);
controls.strafeRight(0.1f);
controls.lookingAround(0.1f);
}
private void initMatrix()
{
Matrix matrix = new Matrix();
matrix.initMatrix();
}
}
Camera类:
public class Camera {
public float x;
public float y;
public float z;
public float rx;
public float ry;
public float rz;
public float fov = 70;
public float aspect = (float)Display.getWidth()/(float)Display.getHeight();
public float near = 0.3f;
public float far = 1000;
public float dx;
public float dy;
public void setView(){
glRotatef(rx,1,0,0);
glRotatef(ry,0,1,0);
glRotatef(rz,0,0,1);
glTranslatef(x,y,z);
}
}
的控件类:
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
public class Controls extends Camera {
boolean W = Keyboard.isKeyDown(Keyboard.KEY_W);
boolean S = Keyboard.isKeyDown(Keyboard.KEY_S);
boolean A = Keyboard.isKeyDown(Keyboard.KEY_A);
boolean D = Keyboard.isKeyDown(Keyboard.KEY_D);
public void moveForward(float amount)
{
if(W)
{
x += amount * Math.cos(Math.toRadians(ry + 90));
z += amount * Math.sin(Math.toRadians(ry + 90));
}
}
public void moveBackward(float amount)
{
if(S)
{
x -= amount * Math.cos(Math.toRadians(ry + 90));
z -= amount * Math.sin(Math.toRadians(ry + 90));
}
}
public void strafeLeft(float amount)
{
if(A);
{
x -= amount * (float)Math.sin(Math.toRadians(ry-90));
z += amount * (float)Math.cos(Math.toRadians(ry-90));
}
}
public void strafeRight(float amount)
{
if(D)
{
x -= amount * (float)Math.sin(Math.toRadians(ry+90));
z += amount * (float)Math.cos(Math.toRadians(ry+90));
}
}
public void lookingAround(float amount)
{
dx = Mouse.getDX();
dy = Mouse.getDY();
ry += dx * amount;
rx -= dy * amount;
}
}
Matrix类:
public class Matrix extends Camera{
public void initMatrix(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
}
}
类块的工作原理完全正确(它创建在给定的坐标的三维块)。
如果你是新的OpenGL,那么你不应该开始与直接模式管道 – 2014-12-02 19:17:18