在 OpenGL 中,我一直在尝试旋转相机,以便看到整个 3D 空间。我研究了在平移点旋转空间,但我得到的只是相机围绕中心旋转。
这是我用来旋转视图的代码(在 Java 中):
游戏循环
public Game(){
try {
Display.setDisplayMode(new DisplayMode(1280, 720));
Display.setTitle("Survive the Planets");
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
}
//Initialize OpenGL
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50f, 1280f / 720f, 1.0f, 10000);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
Planet planet = Planet.generateCreative();
planet.viewPlanet();
while(!Display.isCloseRequested()){
//render
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
planet.render();
//update
if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)){
Display.destroy();
System.exit(0);
return;
}
Camera.rotate(1, 1, 1);
Display.update();
Display.sync(240);
}
Display.destroy();
}
相机类
public class Camera {
private static float x, y, z, rX, rY, rZ;
public static void translate(float x, float y, float z){
Camera.x += x;
Camera.y += y;
Camera.z += z;
glTranslatef(x, y, z);
}
public static void rotate(float x, float y, float z){
glRotatef(x, 1, 0, 0);
glRotatef(y, 0, 1, 0);
glRotatef(z, 0, 0, 1);
Camera.rX += x;
Camera.rY += y;
Camera.rZ += z;
}
public static void setLocation(float x, float y, float z){
float dx = x - Camera.x;
float dy = y - Camera.y;
float dz = z - Camera.z;
glTranslatef(dx, dy, dz);
Camera.x = x;
Camera.y = y;
Camera.z = z;
}
public static void setRotation(float x, float y, float z){
float dx = x - Camera.rX;
float dy = y - Camera.rY;
float dz = z - Camera.rZ;
glRotatef(dx, 1, 0, 0);
glRotatef(dy, 0, 1, 0);
glRotatef(dz, 0, 0, 1);
Camera.rX = x;
Camera.rY = y;
Camera.rZ = z;
}
}
行星类:
public class Planet {
private List<Block> blocks;
private PlanetReference ref;
private Planet(){ }
public void render() {
for(Block block : blocks){
block.render();
}
}
public static Planet generate(PlanetReference ref){
Planet p = new Planet();
p.ref = ref;
List<Block> blocks = new CopyOnWriteArrayList<Block>();
System.out.println("Genrating planet " + ref.getName() + "... ");
//world generation
int r = ref.getRadius() * 32;
int i = 0;
for(int x = -r; x != r; x += 32){
for(int y = -r; y != r; y += 32){
for(int z = -r; z != r; z += 32){
i += 1;
Cube cube = new Cube(x, y, z, 32);
if(Game.getDistance(cube.getCenter()) < r){
Block block = new Block(p, Material.DIRT, cube.getLocation());
blocks.add(block);
}
}
}
}
p.blocks = blocks;
System.out.println(blocks.size() + " blocks where generated. " + i);
return p;
}
public void viewPlanet(){
Camera.setLocation(0, 0, -(ref.getRadius() * 96));
}
public static Planet generateCreative() {
return Planet.generate(new PlanetReference("Creative", 16, 9, 64));
}
}
关于我应该做什么的任何建议?如果您需要更多代码,请发表评论。