我正在製作一個基於體素的遊戲,並且爲了它的需要,我創建了一個塊渲染引擎。如何停止渲染不可見面
問題是,我需要生成很多立方體。每次我渲染16x16x16塊以上的塊時,我的FPS幾乎沒有下降,因爲它渲染了所有這些立方體的全部6個面。這是24 576四邊形,我不想那樣。因此,我的問題是,如何停止渲染不可見的頂點(或四邊形),並因此提高我的遊戲性能?
下面是渲染塊類:
public void renderBlock(int posx, int posy, int posz) {
try{
//t.bind();
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);// or even GL_FRONT_AND_BACK */);
glPushMatrix();
GL11.glTranslatef((2*posx+0.5f),(2*posy+0.5f),(2*posz+0.5f)); // Move Right 1.5 Units And Into The Screen 6.0
GL11.glRotatef(rquad,1.0f,1.0f,1.0f);
glBegin(GL_QUADS); // Draw A Quad
GL11.glColor3f(0.5f, 0.4f, 0.4f); // Set The Color To Green
GL11.glTexCoord2f(0,0);
GL11.glVertex3f(1f, 1f,-1f); // Top Right Of The Quad (Top)
GL11.glTexCoord2f(1,0);
GL11.glVertex3f(-1f, 1f,-1f); // Top Left Of The Quad (Top)
GL11.glTexCoord2f(1,1);
GL11.glVertex3f(-1f, 1f, 1f); // Bottom Left Of The Quad (Top)
GL11.glTexCoord2f(0,1);
GL11.glVertex3f(1f, 1f, 1f); // Bottom Right Of The Quad (Top)
//GL11.glColor3f(1.2f,0.5f,0.9f); // Set The Color To Orange
GL11.glTexCoord2f(0,0);
GL11.glVertex3f(1f,-1f, 1f); // Top Right Of The Quad (Bottom)
GL11.glTexCoord2f(0,1);
GL11.glVertex3f(-1f,-1f, 1f); // Top Left Of The Quad (Bottom)
GL11.glTexCoord2f(1,1);
GL11.glVertex3f(-1f,-1f,-1f); // Bottom Left Of The Quad (Bottom)
GL11.glTexCoord2f(1,0);
GL11.glVertex3f(1f,-1f,-1f); // Bottom Right Of The Quad (Bottom)
//GL11.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
GL11.glTexCoord2f(0,0);
GL11.glVertex3f(1f, 1f, 1f); // Top Right Of The Quad (Front)
GL11.glTexCoord2f(1,0);
GL11.glVertex3f(-1f, 1f, 1f); // Top Left Of The Quad (Front)
GL11.glTexCoord2f(1,1);
GL11.glVertex3f(-1f,-1f, 1f); // Bottom Left Of The Quad (Front)
GL11.glTexCoord2f(0,1);
GL11.glVertex3f(1f,-1f, 1f); // Bottom Right Of The Quad (Front)
//GL11.glColor3f(1f,0.5f,0.0f); // Set The Color To Yellow
GL11.glTexCoord2f(0,0);
GL11.glVertex3f(1f,-1f,-1f); // Bottom Left Of The Quad (Back)
GL11.glTexCoord2f(1,0);
GL11.glVertex3f(-1f,-1f,-1f); // Bottom Right Of The Quad (Back)
GL11.glTexCoord2f(1,1);
GL11.glVertex3f(-1f, 1f,-1f); // Top Right Of The Quad (Back)
GL11.glTexCoord2f(0,1);
GL11.glVertex3f(1f, 1f,-1f); // Top Left Of The Quad (Back)
//GL11.glColor3f(0.0f,0.0f,0.3f); // Set The Color To Blue
GL11.glTexCoord2f(0,1);
GL11.glVertex3f(-1f, 1f, 1f); // Top Right Of The Quad (Left)
GL11.glTexCoord2f(1,1);
GL11.glVertex3f(-1f, 1f,-1f); // Top Left Of The Quad (Left)
GL11.glTexCoord2f(1,0);
GL11.glVertex3f(-1f,-1f,-1f); // Bottom Left Of The Quad (Left)
GL11.glTexCoord2f(0,0);
GL11.glVertex3f(-1f,-1f, 1f); // Bottom Right Of The Quad (Left)
//GL11.glColor3f(0.5f,0.0f,0.5f); // Set The Color To Violet
GL11.glTexCoord2f(0,0);
GL11.glVertex3f(1f, 1f,-1f); // Top Right Of The Quad (Right)
GL11.glTexCoord2f(1,0);
GL11.glVertex3f(1f, 1f, 1f); // Top Left Of The Quad (Right)
GL11.glTexCoord2f(1,1);
GL11.glVertex3f(1f,-1f, 1f); // Bottom Left Of The Quad (Right)
GL11.glTexCoord2f(0,1);
GL11.glVertex3f(1f,-1f,-1f); // Bottom Right Of The Quad (Right)
//rquad+=0.0001f;
glEnd();
glPopMatrix();
}catch(NullPointerException t){t.printStackTrace(); System.out.println("rendering block failed");}
}
這裏是代碼,使他們:
private void render() {
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
for(int y=0; y<32; y++){
for(int x=0; x<16; x++){
for(int z=0; z<16; z++) {
b.renderBlock(x, y, z);
}
}
}
}
任何人都可以幫忙嗎? – TheMorfeus 2012-04-06 15:49:59
可能的重複[如何去除單位立方體世界中的臉部去除Minecraft?](http://stackoverflow.com/questions/6319655/how-to-do-face-removal-in-a-unit- cube-world-a-la-minecraft) – 2012-04-06 17:25:08