I have found the following C++ code in this tutorial to draw a triangle with OpenGL 4:
float points[] = {
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
GLuint vbo = 0;
glGenBuffers (1, &vbo);
glBindBuffer (GL_ARRAY_BUFFER, vbo);
glBufferData (GL_ARRAY_BUFFER, 9 * sizeof (float), points, GL_STATIC_DRAW);
I use C# and OpenTK, so I tried to translate the code:
float[] points =
{
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
uint[] vbo = {0};
GL.GenBuffers(1, vbo);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]);
GL.BufferData(BufferTarget.ArrayBuffer, 9 * sizeof(float), ppoints, BufferUsageHint.StaticDraw);
The problem is that GL.GenBuffers()
and GL.BufferData()
require pointers (Visual Studio shows *int
or out int
and ref float*
, IntPtr
).
I've tried to fix this, but it didn't work:
float[] points =
{
0.0f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};
uint[] vbo = {0};
GL.GenBuffers(1, vbo);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo[0]);
unsafe
{
fixed (float* ppoints = points)
{
GL.BufferData(BufferTarget.ArrayBuffer, 9 * sizeof(float), ppoints, BufferUsageHint.StaticDraw);
}
}
So my question is how to use these methods in a good C# style?