Trying to set VBO:s in C# using TAO / OpenGL TK framework

Started by
13 comments, last by JulianAssange 13 years, 5 months ago
Hello! 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 framework. 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.

Thanks in advance! Any help would be appreciated!

[Edited by - JulianAssange on November 19, 2010 1:39:30 PM]
Advertisement
Your VBO code looks right to me from an OpenGL perspective (don't know anything about C# though). You've tested this on the exact same platform using immediate mode with these vertices and got it to work?

Also a minor issue but you're defining your index buffer as indices.length * sizeof(float), you probably want it to be sizeof(byte). I don't think that would cause your problem though.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Quote:Original post by karwosts
Your VBO code looks right to me from an OpenGL perspective (don't know anything about C# though). You've tested this on the exact same platform using immediate mode with these vertices and got it to work?

Also a minor issue but you're defining your index buffer as indices.length * sizeof(float), you probably want it to be sizeof(byte). I don't think that would cause your problem though.


Yep, it works perfect in my mobile enviroment.

This is really confusing; I've worked on this specific problem in some days now...

Do you think that it could be any problem with the indices buffer when I'm binding it in my draw function? That was a part I added on my own.
Ideas:

Can you just draw a single triangle without indices? (glDrawArrays)
If that works, can you then draw your triangle using trivial indices (indices = {0,1,2}).

Doing that should help you narrow down your problem.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
I'm not really sure with what you mean with trivial indices, however, now I've tried this (with changes from previous code):

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


gl.glDrawArrays(GL.GL_TRIANGLES, 0, vertices.Length);
//gl.glDrawElements(GL.GL_TRIANGLES, indices.Length, GL.GL_UNSIGNED_BYTE, mIndicesBuffer);
...

and I have also shorten down my vertices float to 3 faces like this:

-1.0f, -1.0f, 1.0f,
1.0f, -1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
I'm assuming that your single triangle is still not working?

Trivial indices I just meant keep the same triangle vertices, but instead of glDrawArrays, use glDrawElements, but assign your indices to be (0,1,2). It should be the same as glDrawArrays, but just a chance to flush out anything you had wrong with your indices array setup.
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Also, are you checking for errors? (glGetError)
[size=2]My Projects:
[size=2]Portfolio Map for Android - Free Visual Portfolio Tracker
[size=2]Electron Flux for Android - Free Puzzle/Logic Game
Quote:Original post by karwosts
I'm assuming that your single triangle is still not working?

Trivial indices I just meant keep the same triangle vertices, but instead of glDrawArrays, use glDrawElements, but assign your indices to be (0,1,2). It should be the same as glDrawArrays, but just a chance to flush out anything you had wrong with your indices array setup.


Oh yes, I forgot. :) It's still not working.
Quote:Original post by karwosts
Also, are you checking for errors? (glGetError)


I did this before, but not now. When I had problem to set the VBO (not drawing them) it throwed me an exception. When I got it to work (with setting the VBO, not drawing them) and got a black screen I just assumed that I've got no error and I was almost sure I've missed something between the lines.

Hm, I can't still figure this out. I have a feeling that it is the indices buffer of some kind that mess this up.

Really stuck here, though.

This topic is closed to new replies.

Advertisement