我正在尝试在 OpenGL 中使用 VBO 试验绘图方法。许多人通常使用 1 个 vbo 来存储一个对象数据数组。我试图做一些完全相反的事情,将多个对象数据存储到 1 个 vbo 中,然后绘制它。我为什么要这样做背后有故事。有时我想将许多对象组合为一个对象。但是我的代码并没有做到公正。以下是我的伪代码:
//Data
int[] _vBO = new int[1]; //vertex buffer objects
_triangleVertices = new float[]
{ 0.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, //first triangle lineloop, positions in the middle of the screen.
3.0f, 1.0f, 0.0f, 2.0f, -1.0f, 0.0f, 4.0f, -1.0f, 0.0f, //second triangle lineloop, positions on the left side of the first triangle.
-3.0f, 1.0f, 0.0f, -4.0f, -1.0f, 0.0f, -2.0f, -1.0f, 0.0f }; //third triangle lineloop, positions on the right side of the first triangle.
//Setting Up
void init()
{
GL.GenBuffers(1, _vBO);
GL.BindBuffer(BufferTarget.ArrayBuffer, _vBO[0]);
GL.BufferData(BufferTarget.ArrayBuffer, new IntPtr(sizeof(float) * _triangleVertices.Length), _triangleVertices, BufferUsageHint.StaticDraw);
GL.VertexPointer(3, VertexPointerType.Float, 0, 0);
GL.EnableClientState(Array.VertexArray);
}
//Drawing
void display()
{
GL.Clear(ClearBufferMask.ColorBufferBit);
GL.Clear(ClearBufferMask.DepthBufferBit);
//setting up camera and projection.
float[] eyes = { 0.0f, 0.0f, -10.0f };
float[] target = { 0.0f, 0.0f, 0.0f };
Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView(0.785398163f, 4.0f / 3.0f, 0.1f, 100f); //45 degree = 0.785398163 rads
Matrix4 view = Matrix4.LookAt(eyes[0], eyes[1], eyes[2], target[0], target[1], target[2], 0, 1, 0);
Matrix4 model = Matrix4.Identity;
Matrix4 MV = view * model;
GL.MatrixMode(MatrixMode.Projection);
GL.LoadIdentity();
GL.LoadMatrix(ref projection);
GL.MatrixMode(MatrixMode.Modelview);
GL.LoadIdentity();
GL.LoadMatrix(ref MV);
GL.Viewport(0, 0, glControlWindow.Width, glControlWindow.Height);
GL.Enable(EnableCap.DepthTest); //Enable correct Z Drawings
GL.DepthFunc(DepthFunction.Less); //Enable correct Z Drawings
GL.MatrixMode(MatrixMode.Modelview);
//Draw
drawTriangleLineLoops();
//Finally...
GraphicsContext.CurrentContext.VSync = true; //Caps frame rate as to not over run GPU
glControlWindow.SwapBuffers(); //Takes from the 'GL' and puts into control
}
void drawTriangleLineLoops()
{
GL.BindBuffer(BufferTarget.ArrayBuffer, _vBO[0]);
GL.VertexPointer(3, VertexPointerType.Float, 0, 0);
GL.EnableClientState(ArrayCap.VertexArray);
GL.DrawArrays(BeginMode.LineLoop, 0, 3); //drawing first triangle lineloop, first vertex starts at index 0.
GL.DrawArrays(BeginMode.LineLoop, 9, 3); //drawing second triangle lineloop, first vertex starts at index 9.
GL.DrawArrays(BeginMode.LineLoop, 18, 3); //drawing third triangle lineloop, first vertex starts at index 18.
GL.DisableClientState(ArrayCap.VertexArray);
}
我希望绘制 3 个不同的三角形,但只绘制了一个。我不知道出了什么问题。