0
votes

I cannot render triangles for the life of me with a VBO in OpenTK. I am loading my data to the VBO in glControl_Load() event. I get a background screen with no triangles when running. The data is a from a mesh m.OpenGLArrays(out data, out indices) outputs a list of floats and ints. The list of floats for the vertices T1v1, T1v2, T1v3, T2v1, T2v2, T2v3, .... , all three vertices for each triangle back to back.

However given a blank screen with the code when I comment the "intermediate" rendering code everything renders fine....??? What am I doing wrong?

    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
You're using deprecated OpenGL: opengl.org/wiki/Legacy_OpenGL - Felix K.

1 Answers

-1
votes

If something doesn't seem right, then it probably isn't. I seriously questioned my understanding of opengl and spent hours looking at this. However it was just a simple error of forgetting to iterate a count variable in a for loop to transfer the mesh from one object to another. Each triangle had identical vertices! Always expect the unexpected when it comes to debugging!