2013-05-07 92 views
1

我在這裏設置了一個主類,它從ArrayList呈現塊,但是我只看到最後一個被渲染。爲什麼我只能看到ArrayList中的最後一個塊?LWJGL 3D渲染不起作用

這是我渲染我的塊在我的主類:

glPushMatrix(); 
glTranslatef(0, 0, -10); 
Brick.bind(); 
glBegin(GL_QUADS); 
    Block b; 
    for(int i = 0; i < blocks.size(); i++) 
    { 
     b = blocks.get(i); 
     b.render(); 
    } 
glEnd(); 

glPopMatrix(); Display.update();

這是我生成我塊:

public static void generateMap() 
    { 
     Block b; 
     for(float i = 0; i < 99; i += 3) 
     { 
      for(float r = 0; r < 99; r += 3) 
      { 
       b = new Block(i, 0f, r, 3f, 3f, 3f); 
       blocks.add(b); 
      } 
     } 
    } 

然後,我有我做每塊時,此塊類:

import static org.lwjgl.opengl.GL11.glTexCoord2f; 
import static org.lwjgl.opengl.GL11.glVertex3f; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.util.logging.Level; 
import java.util.logging.Logger; 

import org.newdawn.slick.opengl.Texture; 
import org.newdawn.slick.opengl.TextureLoader; 


public class Block 
{ 
    protected static float x; 
    protected static float y; 
    protected static float z; 
    protected static float sx; 
    protected static float sy; 
    protected static float sz; 

    public Block(float x, float y, float z, float sx, float sy, float sz) 
    { 
     this.x = x; 
     this.y = y; 
     this.z = z; 
     this.sx = sx; 
     this.sy = sy; 
     this.sz = sz; 
    } 
    public static void render() 
    { 
     glTexCoord2f(0,0); glVertex3f(x-sx,y,z-sz); 
     glTexCoord2f(0,1); glVertex3f(x,y,z-sz); 
     glTexCoord2f(1,1); glVertex3f(x,y,z); 
     glTexCoord2f(1,0); glVertex3f(x-sx,y,z); 
     //Bottom 
     glTexCoord2f(0,0); glVertex3f(x-sx,y-sy,z-sz); 
     glTexCoord2f(0,1); glVertex3f(x,y-sx,z-sz); 
     glTexCoord2f(1,1); glVertex3f(x,y-sx,z); 
     glTexCoord2f(1,0); glVertex3f(x-sx,y-sy,z); 
     //Front 
     glTexCoord2f(0,0); glVertex3f(x,y,z);    
     glTexCoord2f(0,1); glVertex3f(x,y-sy,z); 
     glTexCoord2f(1,1); glVertex3f(x-sx,y-sy,z); 
     glTexCoord2f(1,0); glVertex3f(x-sx,y,z); 
     //Back 
     glTexCoord2f(0,0); glVertex3f(x,y,z-sz); 
     glTexCoord2f(0,1); glVertex3f(x,y-sy,z-sz); 
     glTexCoord2f(1,1); glVertex3f(x-sx,y-sy,z-sz); 
     glTexCoord2f(1,0); glVertex3f(x-sx,y,z-sz); 
     //Left 
     glTexCoord2f(0,0); glVertex3f(x-sx,y,z); 
     glTexCoord2f(0,1); glVertex3f(x-sx,y-sy,z); 
     glTexCoord2f(1,1); glVertex3f(x-sx,y-sy,z-sz); 
     glTexCoord2f(1,0); glVertex3f(x-sx,y,z-sz); 
     //Right 
     glTexCoord2f(0,0); glVertex3f(x,y,z); 
     glTexCoord2f(0,1); glVertex3f(x,y-sy,z); 
     glTexCoord2f(1,1); glVertex3f(x,y-sy,z-sz); 
     glTexCoord2f(1,0); glVertex3f(x,y,z-sz); 
    } 
} 

回答

1

的原因,你只能看到最後一個,是因爲所有的「塊」都在彼此之上!

在你設置了xyzsxsysz變量爲靜態的Block類,他們不應該!

基本上,static關鍵字表示該值是共享的,因此如果在一個位置更改x,它也會影響其他所有內容。

建議學習所有的Java關鍵字或至少所有的基本一次,因爲你跳到像OpenGL一樣先進的東西。