我正在尝试将我的顶点、索引和纹理点设置为 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 环境中工作,但不是在这里。所以我猜我错过了绑定的东西。