尝试使用 TAO 和 OpenGL TK 框架在 C# 中设置 VBO:s

发布于 2024-10-02 18:18:59 字数 5758 浏览 9 评论 0原文

我试图将我的顶点、索引和纹理点设置为 VBO:s 并使用我的绘制方法绘制它们,所有这些都在 TAO/OpenGL TK 框架下的 C# 中进行。但我的屏幕只显示黑屏。我已经在没有 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 环境中工作,但在这里不行。所以我猜我错过了一些绑定的东西。

I'm trying to set my vertices, indices and texture points to VBO:s and the draw them with my draw method, all this in C# under TAO/OpenGL TK framwork. But my screen shows only a black screen. I've tested without VBO:s, and then it works. But with my vertices, indices and texture points I can't success.

My code:

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();

    }

The vertices/indices/texture points works in my C++ VBO enviroment, but not here. So I'm guessing I had missed something with the bind stuff.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吻安 2024-10-09 18:18:59

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

要成功创建顶点缓冲区对象,您需要注意几个问题。第一个问题是人们把事情搞砸了。 GL_INDEX_ARRAY 不适用于您的索引。当您使用颜色数组来平滑顶点之间的颜色时,应使用 GL_INDEX_ARRAY 。

另一个问题是 Tao 框架 使用 IntPtr.Zero 作为偏移量,而不是普通的 0。当 glDrawElements 调用中的最后一个参数是 Vertex Buffer Object 时,您将使用 Vertex Array 样式。

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

话虽如此,我将展示一种使用标准顶点数组的解决方案,以及另一种使用顶点缓冲区对象的解决方案。我希望这能澄清事情。

顶点数组

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);            
}

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

Actually, I've been looking for this some weeks too. Now I have a correct working solution.

There are several problems you need to be aware of to successfully create a Vertex Buffer Object. The first problem is that people mess things up. GL_INDEX_ARRAY isn't for your indices. You shall use GL_INDEX_ARRAY when you use a color array for smoothing out the color(s) between your verts.

Another problem is that the Tao framework uses IntPtr.Zero for an offset and not an ordinary 0. You're using the last parameter in the glDrawElements call as Vertex Array style when it's a Vertex Buffer Object.

On the other hand, it looks like you're mixing two different concepts together. You're trying to load your data into a buffer, which you totally will success with if you're using your posted code. The problem here though is that you're using wrong GL_ARRAY_BUFFER command. For your indices you should use GL_ELEMENT_ARRAY_BUFFER.

With that said I will show one solution using standard Vertex Arrays and another using Vertex Buffer Objects. I hope this will clear things out.

Vertex Arrays:

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);
}

Vertex Buffer Objects:

//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);            
}

I hope this was useful, it took me a while to figure this out.

短叹 2024-10-09 18:18:59

内联评论/修改。我还没有准备好测试这个,但它已经很接近了:

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();

    }

Comments/modifications inline. I'm not set up to test this, but it's close:

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();

    }
浮云落日 2024-10-09 18:18:59

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

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); 
    }
}

Here's a working class example based on Shelly's answer:

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); 
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文