OpenGL顶点缓冲区未绘制(LWJGL)

时间:2011-10-07 22:07:30

标签: opengl lwjgl vertex-buffer

我一直在努力实现更好的绘制方法,并尝试使用Vertex Buffers。 现在,我一直在关注LWJGL Wiki [Using Vertex Buffer Objects (VBO)]中的教程,但它对我不起作用,根本没有绘制(据我所见)。 如果我用glVertex3d()绘制,则一切正常。

这就是它的样子:

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL12.*;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBVertexBufferObject;
import org.lwjgl.opengl.GLContext;

public abstract class Shape {

public void render()
{
    if(vertexBufferID > 0 && indexBufferID > 0)
    {
        glEnableClientState(GL_VERTEX_ARRAY);
        ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, vertexBufferID);
        glVertexPointer(3, GL_FLOAT, 0, 0);

        if(colorBufferID > 0)
        {
            glEnableClientState(GL_COLOR_ARRAY);
            ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, colorBufferID);
            glColorPointer(4, GL_FLOAT, 0, 0);
        }

        if(textureCoordBufferID > 0)
        {
            glEnableClientState(GL_TEXTURE_COORD_ARRAY);
            ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, textureCoordBufferID);
            glColorPointer(2, GL_FLOAT, 0, 0);
        }

        if(normalBufferID > 0)
        {
            glEnableClientState(GL_NORMAL_ARRAY);
            ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, normalBufferID);
            glNormalPointer(GL_FLOAT, 0, 0);
        }

        ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, indexBufferID);
        glDrawRangeElements(drawMode, 0, verticesNumber, verticesNumber,
                            GL_UNSIGNED_INT, 0);

        glDisableClientState(GL_VERTEX_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_TEXTURE_COORD_ARRAY);
        glDisableClientState(GL_NORMAL_ARRAY);
    }
}

public static int createVBOID()
{
      if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object) {
        IntBuffer buffer = BufferUtils.createIntBuffer(1);
        ARBVertexBufferObject.glGenBuffersARB(buffer);
        return buffer.get(0);
      }
      return 0;
}

public static void bufferData(int id, FloatBuffer buffer)
{
      if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
      {
          ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, id);
          ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ARRAY_BUFFER_ARB, buffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
      }
}

public static void bufferElementData(int id, IntBuffer buffer)
{
    if (GLContext.getCapabilities().GL_ARB_vertex_buffer_object)
    {
        ARBVertexBufferObject.glBindBufferARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, id);
        ARBVertexBufferObject.glBufferDataARB(ARBVertexBufferObject.GL_ELEMENT_ARRAY_BUFFER_ARB, buffer, ARBVertexBufferObject.GL_STATIC_DRAW_ARB);
    }
}

@Override
protected void finalize() throws Throwable
{
    if(vertexBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(vertexBufferID);
    if(colorBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(colorBufferID);
    if(textureCoordBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(textureCoordBufferID);
    if(indexBufferID > 0) ARBVertexBufferObject.glDeleteBuffersARB(indexBufferID);

    super.finalize();
}

protected int verticesNumber;
protected int drawMode;

protected int vertexBufferID;
protected int colorBufferID;
protected int textureCoordBufferID;
protected int normalBufferID;
protected int indexBufferID;
}

子类,实际填写内容:

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

public class ShapeCube extends Shape{

public ShapeCube()
{       
    drawMode = GL11.GL_QUADS;

    verticesNumber = 6 * 4;

    FloatBuffer vertices = BufferUtils.createFloatBuffer(verticesNumber * 3);
    IntBuffer indices = BufferUtils.createIntBuffer(verticesNumber);

    for(float x = -0.5F; x < 1; x++) for(float y = -0.5F; y < 1; y++) for(float z = -0.5F; z < 1; z++)
    {
        vertices.put(new float[]{x, y, z});
    }

    indices.put(new int[]{0, 1, 2, 3, 4, 5, 6, 7});
    indices.put(new int[]{0, 1, 4, 5, 2, 3, 6, 7});
    indices.put(new int[]{0, 2, 4, 6, 1, 3, 5, 7});

    vertexBufferID = createVBOID();
    bufferData(vertexBufferID, vertices);

    indexBufferID = createVBOID();
    bufferElementData(indexBufferID, indices);
}
}

正如我所说,这段代码并没有为我绘制任何东西(我可以看到)。我确定我在正确的位置调用正确的代码,它应该绘制一个简单的彩色立方体。 我很确定我一定会错过一些简单的东西,但我对Vertex Buffer对象没有任何经验。

1 个答案:

答案 0 :(得分:2)

我现在已经找到了答案。对于每个人都在想,我一直在使用的缓冲区的位置必须设置为零。然后它完美地工作:)