我正在使用 LWJGL/Open GL 来绘制一个立方体。问题是它没有按正确的顺序绘制面,也没有在正确的位置渲染立方体!这是 Block.java:
package src;
import static org.lwjgl.opengl.GL11.GL_QUADS;
import static org.lwjgl.opengl.GL11.glBegin;
import static org.lwjgl.opengl.GL11.glColor3f;
import static org.lwjgl.opengl.GL11.glTranslatef;
import static org.lwjgl.opengl.GL11.glVertex3f;
import static org.lwjgl.opengl.GL11.*;
public class Block {
private float x;
private float y;
private float z;
private Chunk chunk;
public Block(float arg1, float arg2, float arg3, Chunk arg4) {
x = arg1;
y = arg2;
z = arg3;
chunk = arg4;
}
public void renderBlock() {
// glTranslatef(0, 0, 0);
// Texture tx =
chunk.getWorld().getGame().getTexture(0).bind();
glBegin(GL_QUADS);
{
// Top Face
glTexCoord2f(0, 0);
glVertex3f(x - 1, y + 1, z - 1);
glTexCoord2f(0, 1);
glVertex3f(x, y + 1, z - 1);
glTexCoord2f(1, 1);
glVertex3f(x, y + 1, z);
glTexCoord2f(1, 0);
glVertex3f(x - 1, y + 1, z);
// Bottom Face
glTexCoord2f(0, 0);
glVertex3f(x - 1, y, z - 1);
glTexCoord2f(0, 1);
glVertex3f(x, y, z - 1);
glTexCoord2f(1, 1);
glVertex3f(x, y, z);
glTexCoord2f(1, 0);
glVertex3f(x - 1, y, z);
// Front Face
glTexCoord2f(0, 0);
glVertex3f(x, y + 1, z - 1);
glTexCoord2f(0, 1);
glVertex3f(x, y, z - 1);
glTexCoord2f(1, 1);
glVertex3f(x, y, z);
glTexCoord2f(1, 0);
glVertex3f(x, y + 1, z);
// Back Face
glTexCoord2f(0, 0);
glVertex3f(x - 1, y + 1, z - 1);
glTexCoord2f(0, 1);
glVertex3f(x - 1, y, z - 1);
glTexCoord2f(1, 1);
glVertex3f(x - 1, y, z);
glTexCoord2f(1, 0);
glVertex3f(x - 1, y + 1, z);
// BackFace
glTexCoord2f(0, 0);
glVertex3f(x - 1, y + 1, z - 1);
glTexCoord2f(0, 1);
glVertex3f(x, y + 1, z - 1);
glTexCoord2f(1, 1);
glVertex3f(x, y, z - 1);
glTexCoord2f(1, 0);
glVertex3f(x - 1, y, z - 1);
// FrontFace
glTexCoord2f(0, 0);
glVertex3f(x - 1, y + 1, z);
glTexCoord2f(1, 0);
glVertex3f(x, y + 1, z);
glTexCoord2f(1, 1);
glVertex3f(x, y, z);
glTexCoord2f(0, 1);
glVertex3f(x - 1, y, z);
}
glEnd();
}
public float getX() {
return x;
}
public float getY() {
return y;
}
public float getZ() {
return z;
}
}
这是 Render.java:
package src;
import static org.lwjgl.opengl.GL11.GL_COLOR_BUFFER_BIT;
import static org.lwjgl.opengl.GL11.GL_DEPTH_TEST;
import static org.lwjgl.opengl.GL11.glClear;
import static org.lwjgl.opengl.GL11.glEnable;
import static org.lwjgl.opengl.GL11.glEnd;
import static org.lwjgl.opengl.GL11.glLoadIdentity;
import static org.lwjgl.opengl.GL11.glPopMatrix;
import static org.lwjgl.opengl.GL11.glPushMatrix;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
public class Render {
Game game;
public Render(Game arggame) {
game = arggame;
}
public void initDisplay() {
try {
Display.setDisplayMode(new DisplayMode(1280, 720));
Display.create();
} catch (LWJGLException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void renderLoop() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
game.cam.useView();
glPushMatrix();
{
renderBlocks(game.world);
glEnd();
}
glPopMatrix();
Display.update();
}
public void renderBlocks(World arg1world) {
for (int i = 0; i < 125; i++) {
if (arg1world.chunkIndex[i] != null) {
for (int j = 0; j < 32768; j++) {
if (arg1world.chunkIndex[i].blockIndex[j] != null) {
arg1world.chunkIndex[i].blockIndex[j].renderBlock();
}
}
}
}
}
}
块似乎比应该的要低 2 个块,我认为可以通过编辑 glVertex3f 来修复它,但是为什么它不能正确渲染,因为它使用了位置变量。glTranslate 只会让一切变得更糟(做一些指数变化)。