在LWJGL中没有正确创建VBO

时间:2014-05-17 03:38:38

标签: java opengl graphics lwjgl vbo

我使用顶点缓冲对象设置了2D游戏,并且每个项目都有一个'在游戏发布之前,游戏中有一个for循环创建的缓冲区。我发现前两个硬币'物品(它们模糊地看起来像硬币......)被成功创建,显示它们的纹理,但其他两个只是空白的立方体...这让我相信我正确绑定顶点,因为它显示了四边形,但纹理没有正确缓冲。

这是我的生成代码:

 public void generateWorldLegacy() {

    for(Item item : world.items) {

        item.load();
                    //This makes the item class load its image 

         for(int i = 0; i < item.textureId.length; i++) {

             item.vboBuffer[i] = BufferUtils.createFloatBuffer(amountOfVertices * vertexSize);

             item.vboBuffer[i].rewind();

             item.vboBuffer[i].put(

                         new float[] {

                         - item.width/2, - item.height/2,
                         + item.width/2, - item.height/2,
                         + item.width/2, + item.height/2,
                         - item.width/2, + item.height/2

             });

             item.vboBuffer[i].flip();

                 //TODO I have to make these public, put all the floatbuffers into arrays so I can dispose of them when the process is terminated

                 item.textureBuffer[i] = BufferUtils.createFloatBuffer(amountOfVertices * textureSize);

                 item.textureBuffer[i].rewind();

                 item.textureBuffer[i].put(new float[] {

                            0, 1,
                            1, 1,
                            1, 0, 
                            0, 0

                 });

                 item.textureBuffer[i].flip();

                 item.vboId[i] = glGenBuffers();
                 glBindBuffer(GL_ARRAY_BUFFER, item.vboId[i]);
                 glBufferData(GL_ARRAY_BUFFER, item.vboBuffer[i], GL_STATIC_DRAW);
                 glBindBuffer(GL_ARRAY_BUFFER, 0);

                 item.textureId[i] = glGenBuffers();
                 glBindBuffer(GL_ARRAY_BUFFER, item.textureId[i]);
                 glBufferData(GL_ARRAY_BUFFER, item.textureBuffer[i],       GL_STATIC_DRAW);
                 glBindBuffer(GL_ARRAY_BUFFER, 0);

             System.out.println(item.textureId[i] + "\n");

         }

    }

}

这是我的渲染代码(对于项目):

    public void renderWorld() {

    glTranslatef(-player.position.x, -player.position.y, 0);

    for(Item i : world.items) {

        glTranslatef(i.position.x, i.position.y, 0);

        if(i.position.y - horizon < player.position.y) { 

        glBindTexture(GL_TEXTURE_2D, i.vboId[i.spritePointer]);

        // vertices
        glBindBuffer(GL_ARRAY_BUFFER, i.vboId[i.spritePointer]);
        //glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
        glVertexPointer(vertexSize, GL_FLOAT, 0, 0);
        // texCoords
        glBindBuffer(GL_ARRAY_BUFFER, i.textureId[i.spritePointer]);
        //glBufferData(GL_ARRAY_BUFFER, textureData, GL_STATIC_DRAW);
        glTexCoordPointer(textureSize, GL_FLOAT, 0, 0);
        // unbind VBO

        glBindBuffer(GL_ARRAY_BUFFER, 0);

        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);

        glDrawArrays(GL_QUADS, 0, amountOfVertices);

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);

        }

        glTranslatef(-i.position.x, -i.position.y, 0);

    }



}

如果我需要发布我的商品代码和我的播放器&#39;渲染代码,我会,但我不认为这是问题的原因。

以下是我的结果图片:

https://www.dropbox.com/s/rvqopq2tmtmf8xd/Screen%20Shot%202014-05-16%20at%209.16.40%20PM.png

1 个答案:

答案 0 :(得分:1)

您的对象ID混淆了。看看这两行代码:

glBindTexture(GL_TEXTURE_2D, i.vboId[i.spritePointer]);
glBindBuffer(GL_ARRAY_BUFFER, i.vboId[i.spritePointer]);

您使用相同的ID来绑定纹理和缓冲区。