我在这里设置了一个主类,它从ArrayList渲染块但是我只看到最后一个要渲染的。为什么我只看到ArrayList中的最后一个块?
这是我在我的主类中渲染块的地方:
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);
}
}
}
然后我在制作每个块时使用了这个Block类:
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);
}
}
答案 0 :(得分:1)
你只看到最后一个的原因是因为所有“块”都在彼此之上!
在Block
课程中,您设置了x
,y
,z
,sx
,sy
,{{1}变量为静态,它们不应该是!
基本上,static关键字表示该值是共享的,因此如果您在一个地方更改sz
,它也会影响其他所有内容。
建议了解所有Java关键字或至少所有基本关键字,因为您会跳到像OpenGL一样先进的东西。