English 中文(简体)
采用TAO和开放式GL TK框架在C#建立VBO
原标题:Trying to set VBO:s in C# using TAO and OpenGL TK framework

我试图将我的vert、指数和正文点定在VBO:并用我的手提方法加以提炼,在TAO/OpenGL TK framwork下的C#中。 但我的屏幕只显示一个黑色屏幕。 我在没有VBO的情况下进行了检测,然后运行。 但是,凭借我的vert、指数和正文,我可以取得成功。

我的法典:

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中发挥作用,但并非在这里。 因此,我猜测我错过了装束的 st。

最佳回答

实际上,我也期待着这一周。 现在我有一个正确的工作解决办法。

你们需要了解几个问题,以成功创建<代码>Vertex Buffer Object。 第一个问题是人们会说什么东西。 页: 1 页: 1 GL_INDEX_ARRAY 当你利用彩色阵列冲破你vert之间的彩色时。

另一个问题是<代码>。 金框架使用<条码>,以抵消而不是普通<条码>0>。 页: 1

另一方面,它希望你把两个不同的概念结合起来。 你试图把你的数据装入一个缓冲地带,如果你重新使用你的固定代码,你将完全成功。 这里的问题是你重新使用错误的<代码>。 GL_ARRAY_BUFFER的指挥。 www.un.org/Depts/DGACM/index_spanish.htm 页: 1

届时,我将使用标准<代码>Vertex Arrays和另一个使用<代码>Vertex Buffer Object的办法。 我希望,这将明确。

www.un.org/Depts/DGACM/index_spanish.htm

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

www.un.org/Depts/DGACM/index_french.htm

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

我希望这一点是有用的,但我却想看到这一点。

问题回答

评论/修改。 我不准备对此进行测试,但很接近:

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

    }

这里有一个基于壳牌回答的班级例子:

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




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签