0

我有一些代码应该创建一个新的 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 代码不正确?

4

1 回答 1

0

我认为您的问题之一是在 makeChunk() 中创建了许多 CubeVBO,但是您将它们全部分配给同一个变量,因此当方法结束时,vertexVBO 仅引用您创建的最后一个。在 render() 中,您正在绑定和绘制最终的 CubeVBO。

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

于 2013-03-03T06:42:39.040 回答