VBO - 立方体偏移

时间:2013-03-03 02:21:14

标签: java opengl lwjgl vbo cube

我有一些代码应该创建一个新的VBO并用我使用偏移而不是glTranslate创建的多维数据集填充它。我有一个cubeVBO类,它创建一个带偏移的新多维数据集,但由于某种原因,代码只被调用一次。看起来剔除工作也不正常,因为当我就在他们面前时,立方体的面部会消失。这是班级:

public class CubeVBO {

public int vboVHandle;
int offset = Block.size;
int count = 0;
public FloatBuffer vertices;

public CubeVBO(float x, float z, float y){
    vertices = BufferUtils.createFloatBuffer(3 * 4 * 6);
    vertices.put(new float[] {
            x + offset, y + offset, z, 
            x - offset, y + offset, z,
            x - offset, y + offset, z - Block.size, 
            x + offset, y + offset, z - Block.size, 
            x + offset, y - offset, z - Block.size, 
            x - offset, y - offset, z - Block.size, 
            x - offset, y - offset, z, 
            x + offset, y - offset, z, 
            x + offset, y + offset, z - Block.size, 
            x - offset, y + offset, z - Block.size, 
            x - offset, y - offset, z - Block.size, 
            x + offset, y - offset, z - Block.size, 
            x + offset, y - offset, z, 
            x - offset, y - offset, z, 
            x - offset, y + offset, z, 
            x + offset, y + offset, z, 
            x - offset, y + offset, z - Block.size, 
            x - offset, y + offset, z, 
            x - offset, y - offset, z, 
            x - offset, y - offset, z - Block.size, 
            x + offset, y + offset, z, 
            x + offset, y + offset, z - Block.size, 
            x + offset, y - offset,
            z - Block.size, x + offset, y - offset, z });
    vertices.flip();

    vboVHandle = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vboVHandle);
    glBufferData(GL_ARRAY_BUFFER, vertices, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

} 我在这堂课中使用它:

public class BaseChunk {

// x - left/right y - up/down y - forward/backward

VBO vbo;
CubeVBO vertexVBO;

public static final int CHUNK_X = 16;
public static final int CHUNK_Y = 16;
public static final int CHUNK_Z = 16;

public boolean rebuild = false;

FloatBuffer blockPositionData;
FloatBuffer vertexColorData;

private Block[][][] blocks;
private int vboBlockData;

public BaseChunk() {

    blocks = new Block[CHUNK_X][CHUNK_Z][CHUNK_Y];
    blockPositionData = BufferUtils.createFloatBuffer((CHUNK_X * CHUNK_Z * CHUNK_Y) * 6 * 12);

    for (int xx = 0; xx < CHUNK_X; xx++) {
        for (int zz = 0; zz < CHUNK_Z; zz++) {
            for (int yy = 0; yy < CHUNK_Y; yy++) {
                blocks[xx][zz][yy] = Block.Grass;
            }
        }
    }
}

public void rebuildChunk(int id) {
    if (rebuild) {

    }
}

public void makeChunk(){
    for(int x = 0; x <= 16; x++){
        for(int z = 0; z <= 16; z++){
            for(int y = 0; y <= 16; y++){
                vertexVBO = new CubeVBO((float) 16 + x, (float) 16 + z, (float) 16 + y);
            }
        }
    }

    vboBlockData = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vboBlockData);
    glBufferData(GL_ARRAY_BUFFER, blockPositionData, GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

public void render() {
    vbo = new VBO();
    glBindBuffer(GL_ARRAY_BUFFER, vertexVBO.vboVHandle);
    glVertexPointer(3, GL_FLOAT, 0, 0L);

    glBindBuffer(GL_ARRAY_BUFFER, vbo.vboCHandle);
    glColorPointer(3, GL_FLOAT, 0, 0);

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
                glPushMatrix();
                //glTranslatef(0, 0, 0);
                glDrawArrays(GL_QUADS, 0, 24);
                glPopMatrix();
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);
}

我的补偿不正确吗?或者我的VBO代码不正确?

1 个答案:

答案 0 :(得分:0)

我认为你的一个问题是你在makeChunk()中创建了很多CubeVBO,但你将它们全部分配给同一个变量,所以当方法结束时,只有你创建的最后一个被vertexVBO引用。在render()中,您绑定并绘制最终的CubeVBO。

你需要创建一个包含所有立方体顶点的大VBO,并绑定并绘制它。不要忘记更新glDrawArrays的count参数。