2

我正在尝试将我的顶点、索引和纹理点设置为 VBO:s 并使用我的 draw 方法绘制它们,所有这些都在 C# 中的 TAO/OpenGL TK 框架下。但我的屏幕只显示黑屏。我已经在没有 VBO:s 的情况下进行了测试,然后它就可以工作了。但是对于我的顶点、索引和纹理点,我无法成功。

我的代码:

private float[] vertices;
private byte[] indices;
private float[] texture;

private int[] mVertexBuffer;
private int[] mIndicesBuffer;
private int[] mTextureBuffer;

//...Constructor start
vertices = new float[] {
                                            -1.0f, -1.0f, 1.0f,
                                            1.0f, -1.0f, 1.0f,
                                            -1.0f, 1.0f, 1.0f,
                                            1.0f, 1.0f, 1.0f,

                                            1.0f, -1.0f, 1.0f,
                                            1.0f, -1.0f, -1.0f, 
                                            1.0f, 1.0f, 1.0f, 
                                            1.0f, 1.0f, -1.0f,

                                            1.0f, -1.0f, -1.0f, 
                                            -1.0f, -1.0f, -1.0f, 
                                            1.0f, 1.0f, -1.0f, 
                                            -1.0f, 1.0f, -1.0f,

                                            -1.0f, -1.0f, -1.0f, 
                                            -1.0f, -1.0f, 1.0f, 
                                            -1.0f, 1.0f, -1.0f, 
                                            -1.0f, 1.0f, 1.0f,

                                            -1.0f, -1.0f, -1.0f, 
                                            1.0f, -1.0f, -1.0f, 
                                            -1.0f, -1.0f, 1.0f, 
                                            1.0f, -1.0f, 1.0f,

                                            -1.0f, 1.0f, 1.0f, 
                                            1.0f, 1.0f, 1.0f, 
                                            -1.0f, 1.0f, -1.0f, 
                                            1.0f, 1.0f, -1.0f, 
                                            };

texture = new float[] {
                                                                                           0.0f, 1.0f,                                                1.0f, 1.0f,                                                0.0f, 0.0f,                                                 1.0f, 0.0f,                                                 
                                            0.0f, 1.0f, 
                                            1.0f, 1.0f, 
                                            0.0f, 0.0f, 
                                            1.0f, 0.0f, 

                                            0.0f, 1.0f, 
                                            1.0f, 1.0f, 
                                            0.0f, 0.0f, 
                                            1.0f, 0.0f, 

                                            0.0f, 1.0f, 
                                            1.0f, 1.0f, 
                                            0.0f, 0.0f, 
                                            1.0f, 0.0f, 

                                            0.0f, 1.0f, 
                                            1.0f, 1.0f, 
                                            0.0f, 0.0f, 
                                            1.0f, 0.0f, 

                                            0.0f, 1.0f, 
                                            1.0f, 1.0f, 
                                            0.0f, 0.0f, 
                                            1.0f, 0.0f,  
                                            };

indices = new byte[] {
                                            0, 1, 3, 0, 3, 2,                                                4, 5, 7, 4, 7, 6,
                                            8, 9, 11, 8, 11, 10,
                                            12, 13, 15, 12, 15, 14, 
                                            16, 17, 19, 16, 19, 18, 
                                             20, 21, 23, 20, 23, 22, 
                                            };

mVertexBuffer = new int[1];
mIndicesBuffer = new int[1];
mTextureBuffer = new int[1];

//...Constructor end

public void setBuffers() {
        gl.glGenBuffersARB(1, mVertexBuffer);
        gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, mVertexBuffer[0]);
        gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB,
        (IntPtr)(vertices.Length * sizeof(float)),
  vertices, GL.GL_STATIC_DRAW_ARB);

        gl.glGenBuffersARB(1, mIndicesBuffer);
        gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, mIndicesBuffer[0]);
        gl.glBufferDataARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB,
         (IntPtr)(indices.Length * sizeof(float)),
  indices, GL.GL_DYNAMIC_DRAW_ARB);

        gl.glGenBuffersARB(1, mTextureBuffer);
        gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, mTextureBuffer[0]);
        gl.glBufferDataARB(GL.GL_ARRAY_BUFFER_ARB,
         (IntPtr)(texture.Length * sizeof(float)),
  texture, GL.GL_STATIC_DRAW_ARB);
  }

    public void draw()
    {
        gl.glBegin(gl.GL_TRIANGLES);

        gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);

        gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, mVertexBuffer[0]);
        gl.glVertexPointer(3, GL.GL_FLOAT, 0, mVertexBuffer);
        gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, mTextureBuffer[0]);
        gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, mTextureBuffer);

        gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, mIndicesBuffer[0]);

        gl.glDrawElements(GL.GL_TRIANGLES, indices.Length,
 gl.GL_UNSIGNED_BYTE, mIndicesBuffer);

        gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
        gl.glEnd();

    }

顶点/索引/纹理点在我的 C++ VBO 环境中工作,但不是在这里。所以我猜我错过了绑定的东西。

4

3 回答 3

6

实际上,我也一直在寻找这个几个星期。现在我有一个正确的工作解决方案。

要成功创建Vertex Buffer Object. 第一个问题是人们把事情搞砸了。GL_INDEX_ARRAY不适合你的indicesGL_INDEX_ARRAY当你使用颜色数组来平滑你的顶点之间的颜色时,你应该使用它。

另一个问题是Tao framework使用IntPtr.Zero偏移量而不是普通的0. 当glDrawElements调用中的最后一个参数是.Vertex ArrayVertex Buffer Object

另一方面,看起来您将两个不同的概念混合在一起。您正在尝试将数据加载到缓冲区中,如果您使用发布的代码,您将完全成功。但是这里的问题是您使用了错误的GL_ARRAY_BUFFER命令。对于您indices,您应该使用GL_ELEMENT_ARRAY_BUFFER.

话虽如此,我将展示一种使用标准的解决方案Vertex Arrays和另一种使用Vertex Buffer Objects. 我希望这能解决问题。

顶点数组

public void draw()
{
     gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
     gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY);

     gl.glVertexPointer(3, gl.GL_FLOAT, 0, verticesArray);
     gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, textureArray);

     //With Vertex Arrays, the last parameter is your pointer to your indices
     gl.glDrawElements(gl.GL_TRIANGLES, indices.Length,
                               gl.GL_UNSIGNED_SHORT, indicesArray);

     gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY);
     gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
}

顶点缓冲对象

//This should be used in the constructor 
//or in another method than the draw method
mVertexBuffer = new int[1];
mTextureBuffer = new int[1];
mIndexBuffer = new int[1];

gl.glGenBuffers(1, mVertexBuffer);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, mVertexBuffer[0]);
gl.glBufferData(gl.GL_ARRAY_BUFFER,
     (IntPtr)(verticesArray.Length * sizeof(float)),
                  verticesArray, gl.GL_STATIC_DRAW);

gl.glGenBuffers(1, mTextureBuffer);
gl.glBindBuffer(gl.GL_ARRAY_BUFFER, mTextureBuffer[0]);
gl.glBufferData(gl.GL_ARRAY_BUFFER,
     (IntPtr)(textureArray.Length * sizeof(float)),
                  textureArray, gl.GL_STATIC_DRAW);

gl.glGenBuffers(1, mIndexBuffer);
gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer[0]);
gl.glBufferData(gl.GL_ELEMENT_ARRAY_BUFFER,
     (IntPtr)(indicesArray.Length * sizeof(short)),
                  indicesArray, gl.GL_STATIC_DRAW);

//Your draw method
public void draw()
{     
    gl.glEnableClientState(gl.GL_VERTEX_ARRAY);
    gl.glEnableClientState(gl.GL_TEXTURE_COORD_ARRAY);

    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, mVertexBuffer[0]);

    //Notice: IntPtr.Zero is used...
    gl.glVertexPointer(3, gl.GL_FLOAT, 0, IntPtr.Zero);

    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, mTextureBuffer[0]);

    //Notice: IntPtr.Zero is used...
    gl.glTexCoordPointer(2, gl.GL_FLOAT, 0, IntPtr.Zero); 
    gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer[0]);

    //Notice: IntPtr.Zero is used. Last parameter is
    //an offset using Vertex Buffer Objects and in Vertex Arrays
    //it is a pointer
    gl.glDrawElements(gl.GL_TRIANGLES, indices.Length,
             gl.GL_UNSIGNED_SHORT, IntPtr.Zero); 

    //Remember to unbind your buffer to prevent it to destroy
    //other draw calls or objects
    gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0);
    gl.glBindBuffer(gl.GL_ELEMENT_ARRAY_BUFFER, 0);

    gl.glDisableClientState(gl.GL_VERTEX_ARRAY);
    gl.glDisableClientState(gl.GL_TEXTURE_COORD_ARRAY);            
}

我希望这很有用,我花了一段时间才弄清楚。

于 2010-11-28T08:19:20.513 回答
1

内联评论/修改。我没有准备好对此进行测试,但它已经接近了:

public void draw()
    {
        //gl.glBegin(gl.GL_TRIANGLES);  // no glBegin/glEnd at all -- that's immediate mode

        gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
        gl.glEnableClientState(GL.GL_TEXTURE_COORD_ARRAY);
        gl.glEnableClientState(GL.GL_INDEX_ARRAY);  // also using index array

        gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, mVertexBuffer[0]);
        gl.glVertexPointer(3, GL.GL_FLOAT, 0, 0);  // with vertex arrays the last argument is a pointer, with VBOs it's an *offset*
        gl.glBindBufferARB(GL.GL_ARRAY_BUFFER_ARB, mTextureBuffer[0]);
        gl.glTexCoordPointer(2, GL.GL_FLOAT, 0, 0);  // ditto

        gl.glBindBufferARB(GL.GL_ELEMENT_ARRAY_BUFFER_ARB, mIndicesBuffer[0]);

        //gl.glDrawElements(GL.GL_TRIANGLES, indices.Length, gl.GL_UNSIGNED_BYTE, mIndicesBuffer);  // this is vertex-array style
        gl.glIndexPointer(GL.GL_UNSIGNED_BYTE, 0, 0); // as above for all pointers
        gl.glDrawArrays(GL.GL_TRIANGLES, 0, indices.length);

        // You can probably leave these enabled to save time:
        gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
        gl.glDisableClientState(GL.GL_TEXTURE_COORD_ARRAY);
        gl.glDisableClientState(GL.GL_INDEX_ARRAY);            
        //gl.glEnd();

    }
于 2010-11-20T18:58:32.640 回答
1

这是一个基于雪莉回答的工人阶级示例:

class Vbo {
    private float[] vertices;
    private ushort[] indices;
    private float[] texture;

    private int[] mVertexBuffer;
    private int[] mIndicesBuffer;
    private int[] mTextureBuffer;

    public Vbo() {
        vertices = new float[] {
                                        -1.0f, -1.0f, 1.0f,
                                        1.0f, -1.0f, 1.0f,
                                        -1.0f, 1.0f, 1.0f,
                                        1.0f, 1.0f, 1.0f,

                                        1.0f, -1.0f, 1.0f,
                                        1.0f, -1.0f, -1.0f, 
                                        1.0f, 1.0f, 1.0f, 
                                        1.0f, 1.0f, -1.0f,

                                        1.0f, -1.0f, -1.0f, 
                                        -1.0f, -1.0f, -1.0f, 
                                        1.0f, 1.0f, -1.0f, 
                                        -1.0f, 1.0f, -1.0f,

                                        -1.0f, -1.0f, -1.0f, 
                                        -1.0f, -1.0f, 1.0f, 
                                        -1.0f, 1.0f, -1.0f, 
                                        -1.0f, 1.0f, 1.0f,

                                        -1.0f, -1.0f, -1.0f, 
                                        1.0f, -1.0f, -1.0f, 
                                        -1.0f, -1.0f, 1.0f, 
                                        1.0f, -1.0f, 1.0f,

                                        -1.0f, 1.0f, 1.0f, 
                                        1.0f, 1.0f, 1.0f, 
                                        -1.0f, 1.0f, -1.0f, 
                                        1.0f, 1.0f, -1.0f, 
                                        };

        texture = new float[] {
                                                                                       0.0f, 1.0f,                                                1.0f, 1.0f,                                                0.0f, 0.0f,                                                 1.0f, 0.0f,                                                 
                                        0.0f, 1.0f, 
                                        1.0f, 1.0f, 
                                        0.0f, 0.0f, 
                                        1.0f, 0.0f, 

                                        0.0f, 1.0f, 
                                        1.0f, 1.0f, 
                                        0.0f, 0.0f, 
                                        1.0f, 0.0f, 

                                        0.0f, 1.0f, 
                                        1.0f, 1.0f, 
                                        0.0f, 0.0f, 
                                        1.0f, 0.0f, 

                                        0.0f, 1.0f, 
                                        1.0f, 1.0f, 
                                        0.0f, 0.0f, 
                                        1.0f, 0.0f, 

                                        0.0f, 1.0f, 
                                        1.0f, 1.0f, 
                                        0.0f, 0.0f, 
                                        1.0f, 0.0f,  
                                        };

        indices = new ushort[] {
                                        0, 1, 3, 0, 3, 2,                                                4, 5, 7, 4, 7, 6,
                                        8, 9, 11, 8, 11, 10,
                                        12, 13, 15, 12, 15, 14, 
                                        16, 17, 19, 16, 19, 18, 
                                         20, 21, 23, 20, 23, 22, 
                                        };

        for (int i = 0; i < vertices.Length; i++) {
            vertices[i] *= 2;
        }

        mVertexBuffer = new int[1];
        mIndicesBuffer = new int[1];
        mTextureBuffer = new int[1];



        Gl.glGenBuffers(1, mVertexBuffer);
        Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, mVertexBuffer[0]);
        Gl.glBufferData(Gl.GL_ARRAY_BUFFER,
             (IntPtr)(vertices.Length * sizeof(float)),
                          vertices, Gl.GL_STATIC_DRAW);

        Gl.glGenBuffers(1, mTextureBuffer);
        Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, mTextureBuffer[0]);
        Gl.glBufferData(Gl.GL_ARRAY_BUFFER,
             (IntPtr)(texture.Length * sizeof(float)),
                          texture, Gl.GL_STATIC_DRAW);

        Gl.glGenBuffers(1, mIndicesBuffer);
        Gl.glBindBuffer(Gl.GL_ELEMENT_ARRAY_BUFFER, mIndicesBuffer[0]);
        Gl.glBufferData(Gl.GL_ELEMENT_ARRAY_BUFFER,
             (IntPtr)(indices.Length * sizeof(ushort)),
                          indices, Gl.GL_STATIC_DRAW);   
    }

    public void Render() {
        Gl.glEnableClientState(Gl.GL_VERTEX_ARRAY);
        Gl.glEnableClientState(Gl.GL_TEXTURE_COORD_ARRAY);

        Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, mVertexBuffer[0]);

        //Notice: IntPtr.Zero is used...
        Gl.glVertexPointer(3, Gl.GL_FLOAT, 0, IntPtr.Zero);

        Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, mTextureBuffer[0]);

        //Notice: IntPtr.Zero is used...
        Gl.glTexCoordPointer(2, Gl.GL_FLOAT, 0, IntPtr.Zero);
        Gl.glBindBuffer(Gl.GL_ELEMENT_ARRAY_BUFFER, mIndicesBuffer[0]);

        //Notice: IntPtr.Zero is used. Last parameter is
        //an offset using Vertex Buffer Objects and in Vertex Arrays
        //it is a pointer
        Gl.glDrawElements(Gl.GL_TRIANGLES, indices.Length, Gl.GL_UNSIGNED_SHORT, IntPtr.Zero);

        //Remember to unbind your buffer to prevent it to destroy
        //other draw calls or objects
        Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, 0);
        Gl.glBindBuffer(Gl.GL_ELEMENT_ARRAY_BUFFER, 0);

        Gl.glDisableClientState(Gl.GL_VERTEX_ARRAY);
        Gl.glDisableClientState(Gl.GL_TEXTURE_COORD_ARRAY); 
    }
}
于 2013-05-28T01:32:32.930 回答