【问题标题】:Rendering triangles with OpenTK from VBO使用 VBO 的 OpenTK 渲染三角形
【发布时间】:2015-07-24 05:53:41
【问题描述】:

我无法在 OpenTK 中使用 VBO 渲染三角形。我在 glControl_Load() 事件中将我的数据加载到 VBO。运行时我得到一个没有三角形的背景屏幕。数据来自网格 m.OpenGLArrays(out data, out indices) 输出浮点数和整数列表。顶点 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++;
        }
    }

【问题讨论】:

标签: c# opengl opentk


【解决方案1】:

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

【讨论】:

  • 如果您已经发现错误,您的回答/问题不太可能帮助其他人,如果您可以删除它们会很好。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 2021-04-08
  • 1970-01-01
相关资源
最近更新 更多