OpenGL顶点数组对象(VAO)

我画了一个三角形和一个正方形使用开放式的gl和一切都很好。

然后我试图绘制相同的三角形和正方形,同时实现VAO,现在我只得到一个白色的三角形 – 这不是我正在试图绘制的三角形。 据我所见,我在onLoad方法中正确地绑定了一切。

广场并没有画。 只有那个不正确的三角形

如果有人能指出我缺less的东西或指向某处,我可以理解这个问题并修复(教程或其他文档),我将非常感激。

这是代码

using Labs.Utility; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; using System; namespace Labs.Lab2 { class Lab2_1Window : GameWindow { private int[] mVertexArrayObjectIDs = new int[2]; private int[] mTriangleVertexBufferObjectIDArray = new int[2]; private int[] mSquareVertexBufferObjectIDArray = new int[2]; private ShaderUtility mShader; public Lab2_1Window() : base( 800, // Width 600, // Height GraphicsMode.Default, "Lab 2_1 Linking to Shaders and VAOs", GameWindowFlags.Default, DisplayDevice.Default, 3, // major 3, // minor GraphicsContextFlags.ForwardCompatible ) { } protected override void OnLoad(EventArgs e) { GL.ClearColor(Color4.CadetBlue); GL.Enable(EnableCap.DepthTest); #region Shader Loading Code mShader = new ShaderUtility(@"Lab2/Shaders/vLab21.vert", @"Lab2/Shaders/fSimple.frag"); #endregion int vColourLocation = GL.GetAttribLocation(mShader.ShaderProgramID, "vColour"); int vPositionLocation = GL.GetAttribLocation(mShader.ShaderProgramID, "vPosition"); //square float[] sVertices = new float[] { -0.2f, -0.4f, 0.2f, 0.0f, 1.0f, 0.2f, 0.8f, -0.4f, 0.2f, 0.0f, 1.0f, 1.0f, 0.8f, 0.6f, 0.2f, 0.0f, 1.0f, 0.4f, -0.2f, 0.6f, 0.2f, 0.0f, 1.0f, 0.0f}; uint[] sIndices = new uint[] { 0, 1, 2, 0,3,2}; GL.GenVertexArrays(2, mVertexArrayObjectIDs); GL.BindVertexArray(mVertexArrayObjectIDs[1]); GL.GenBuffers(2, mSquareVertexBufferObjectIDArray); GL.BindBuffer(BufferTarget.ArrayBuffer, mSquareVertexBufferObjectIDArray[0]); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(sVertices.Length * sizeof(float)), sVertices, BufferUsageHint.StaticDraw); int sSize; GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out sSize); if (sVertices.Length * sizeof(float) != sSize) { throw new ApplicationException("Vertex data not loaded onto graphics card correctly"); } GL.BindBuffer(BufferTarget.ElementArrayBuffer, mSquareVertexBufferObjectIDArray[1]); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(sIndices.Length * sizeof(int)), sIndices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out sSize); if (sIndices.Length * sizeof(int) != sSize) { throw new ApplicationException("Index data not loaded onto graphics card correctly"); } GL.VertexAttribPointer(vPositionLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0); GL.VertexAttribPointer(vColourLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 3 * sizeof(float)); //GL.EnableVertexAttribArray(vColourLocation); //GL.EnableVertexAttribArray(vPositionLocation); //triangle float[] tVertices = new float[] { -0.8f, 0.8f, 0.4f, 1.0f, 0.0f, 0.6f, -0.6f, -0.4f, 0.4f, -0.2f, 0.5f, 1.0f, 0.2f, 0.2f, 0.4f, -1.0f, 0.0f, 1.0f}; uint[] tIndices = new uint[] { 0, 1, 2 }; //GL.GenVertexArrays(2, mVertexArrayObjectIDs); GL.BindVertexArray(mVertexArrayObjectIDs[0]); GL.GenBuffers(2, mTriangleVertexBufferObjectIDArray); GL.BindBuffer(BufferTarget.ArrayBuffer, mTriangleVertexBufferObjectIDArray[0]); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(tVertices.Length * sizeof(float)), tVertices, BufferUsageHint.StaticDraw); int size; GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size); if (tVertices.Length * sizeof(float) != size) { throw new ApplicationException("Vertex data not loaded onto graphics card correctly"); } GL.BindBuffer(BufferTarget.ElementArrayBuffer, mTriangleVertexBufferObjectIDArray[1]); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(tIndices.Length * sizeof(int)), tIndices, BufferUsageHint.StaticDraw); GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size); if (tIndices.Length * sizeof(int) != size) { throw new ApplicationException("Index data not loaded onto graphics card correctly"); } GL.VertexAttribPointer(vPositionLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 0); GL.VertexAttribPointer(vColourLocation, 3, VertexAttribPointerType.Float, false, 6 * sizeof(float), 3 * sizeof(float)); GL.EnableVertexAttribArray(vColourLocation); GL.EnableVertexAttribArray(vPositionLocation); base.OnLoad(e); } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.BindVertexArray(mVertexArrayObjectIDs[1]); GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0); GL.BindVertexArray(mVertexArrayObjectIDs[0]); GL.DrawElements(PrimitiveType.Triangles, 3, DrawElementsType.UnsignedInt, 0); GL.BindVertexArray(0); this.SwapBuffers(); } protected override void OnUnload(EventArgs e) { base.OnUnload(e); GL.BindVertexArray(0); GL.DeleteVertexArrays(2, mVertexArrayObjectIDs); GL.DeleteBuffers(2, mTriangleVertexBufferObjectIDArray); GL.DeleteBuffers(2, mSquareVertexBufferObjectIDArray); GL.UseProgram(0); mShader.Delete(); } } 

}

编辑: 这是它的样子

你用画

 GL.BindVertexArray(mVertexArrayObjectIDs[1]); GL.DrawElements(PrimitiveType.TriangleFan, 4, DrawElementsType.UnsignedInt, 0); 

但是您使用的索引是PrimitiveType.Triangles

把它replace成

 GL.BindVertexArray(mVertexArrayObjectIDs[1]); GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0); 

我加载后没有使用着色器。

添加了GL.UseProgram(mShader.ShaderProgramID);

在着色器加载代码之后。