1
\$\begingroup\$

I have drawn a triangle and a square using open gl and everything was fine.

I have then tried to draw the same triangle and square, while implementing VAO's and now I only get a white triangle - that is not the triangle I am attempting to draw. As far as I can see I am binding everything correctly in the onLoad method.

The square doesnt draw at all. Only the triangle which is not right.

If anyone could point out what I am missing or point me to somewhere I can understand this problem and fix (tutorial or other documentation) I would be very grateful.

Here is the code

    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();
    }
}

}

EDIT: Here is what it looks like

\$\endgroup\$
  • \$\begingroup\$ care to add a screen shot of your wrong triangle? \$\endgroup\$ – ratchet freak Apr 19 '15 at 10:15
1
\$\begingroup\$

You draw with

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

however the indices you use are for PrimitiveType.Triangles

Replace that with

GL.BindVertexArray(mVertexArrayObjectIDs[1]);
GL.DrawElements(PrimitiveType.Triangles, 6, DrawElementsType.UnsignedInt, 0);
\$\endgroup\$
  • \$\begingroup\$ What do you mean ? I am using the tIndices? Why is that when I bind them on line 62? GL.BindVertexArray(mVertexArrayObjectIDs[1]); \$\endgroup\$ – user1937185 Apr 20 '15 at 13:54
  • \$\begingroup\$ @Minanda Those indices are meant to be drawn with PrimitiveType.Triangles. \$\endgroup\$ – ratchet freak Apr 20 '15 at 13:56
  • \$\begingroup\$ Ah I see what you mean, I previously had that indices as {0,1,2,3}; which made it triangle fan. I changed that to try it with triangle. Neither works they both give the same result. \$\endgroup\$ – user1937185 Apr 20 '15 at 13:59
0
\$\begingroup\$

I was not using the shader after I loaded it.

Added GL.UseProgram(mShader.ShaderProgramID);

After shader loading code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.