使用VBO中的OpenTK渲染三角形

时间:2015-07-24 05:53:42

标签: c# opengl opentk

我无法使用OpenTK中的VBO为我的生活渲染三角形。我在glControl_Load()事件中将我的数据加载到VBO。运行时,我得到一个没有三角形的背景屏幕。数据来自网格m.OpenGLArrays(输出数据,输出索引)输出浮点数和整数列表。顶点T1v1,T1v2,T1v3,T2v1,T2v2,T2v3,....的浮点列表,每个三角形的所有三个顶点都背靠背。

然而,当我评论"中间"时给出一个带有代码的空白屏幕。渲染代码一切都很好.... ???我做错了什么?

    private void glControl_Load(object sender, EventArgs e)
    {
        loaded = true;
        glControl.MouseMove += new MouseEventHandler(glControl_MouseMove);
        glControl.MouseWheel += new MouseEventHandler(glControl_MouseWheel);

        GL.ClearColor(Color.DarkSlateGray);
        GL.Color3(1f, 1f, 1f);

        m.OpenGLArrays(out data, out indices);
        this.indicesSize = (uint)indices.Length;
        GL.GenBuffers(1, out VBOid[0]);
        GL.GenBuffers(1, out VBOid[1]);

        SetupViewport();
    }

    private void SetupViewport()
    {
        if (this.WindowState == FormWindowState.Minimized) return;
        glControl.Width = this.Width - 32;
        glControl.Height = this.Height - 80;
        Frame_label.Location = new System.Drawing.Point(glControl.Width / 2, glControl.Height + 25);
        GL.MatrixMode(MatrixMode.Projection);
        //GL.LoadIdentity();
        GL.Ortho(0, glControl.Width, 0, glControl.Height, -1, 1); // Bottom-left corner pixel has coordinate (0, 0)
        GL.Viewport(0, 0, glControl.Width, glControl.Height); // Use all of the glControl painting area
        GL.Enable(EnableCap.DepthTest);

        GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid[0]);
        GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(data.Length * sizeof(float)), data, BufferUsageHint.StaticDraw);
        GL.BindBuffer(BufferTarget.ArrayBuffer, 0);

        float aspect_ratio = this.Width / (float)this.Height;
        projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 1024);
        GL.MatrixMode(MatrixMode.Projection);
        GL.LoadMatrix(ref projection);
    }

    private void glControl_Paint(object sender, PaintEventArgs e)
    {
        if (loaded)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit |
                     ClearBufferMask.DepthBufferBit |
                     ClearBufferMask.StencilBufferBit);

            modelview = Matrix4.LookAt(0f, 0f, -200f + zoomFactor, 0, 0, 0, 0.0f, 1.0f, 0.0f);
            var aspect_ratio = Width / (float)Height;
            projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspect_ratio, 1, 512);

            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadMatrix(ref projection);
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadMatrix(ref modelview);
            GL.Rotate(angleY, 1.0f, 0, 0);
            GL.Rotate(angleX, 0, 1.0f, 0);

            GL.EnableClientState(ArrayCap.VertexArray);

            GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid[0]);

            GL.Color3(Color.Yellow);

            GL.VertexPointer(3, VertexPointerType.Float, Vector3.SizeInBytes, new IntPtr(0));
            GL.DrawArrays(PrimitiveType.Triangles, 0, data.Length);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
            GL.DisableClientState(ArrayCap.VertexArray);

            //GL.Color3(Color.Yellow);
            //GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill);
            //GL.Begin(PrimitiveType.Triangles);

            //for (int i = 0; i < this.md.mesh.Count; i++)
            //{
            //    GL.Normal3(this.md.mesh[i].normal);
            //    GL.Vertex3(this.md.mesh[i].vertices[0]);
            //    GL.Vertex3(this.md.mesh[i].vertices[1]);
            //    GL.Vertex3(this.md.mesh[i].vertices[2]);
            //}
            //GL.End();
            //GL.EndList();

            glControl.SwapBuffers();

            Frame_label.Text = "Frame: " + frameNum++;
        }
    }

1 个答案:

答案 0 :(得分:-1)

如果某些事情看起来不对,那么它可能不是。我严肃地质疑我对opengl的理解,花了几个小时看这个。然而,忘记迭代for循环中的count变量以将网格从一个对象传递到另一个对象只是一个简单的错误。每个三角形都有相同的顶点!在调试时总是期待意外!