0

I am trying to draw the triangle with the colours and verticies specified but currently it seems like its picking some colour numbers for the positions and is not doing what its supposed to do

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Graphics;

namespace newTriangle
{
    class Program
    {
        static void Main(string[] args)
        {
            MyWindow myWindow = new MyWindow();
            myWindow.Run();
        }
    }

    class MyWindow : GameWindow
    {
        private uint[] vertexBufferObjectIDs = new uint[2];
        private int vertexArrayID, vertexShaderID, fragmentShaderID, shaderProgramID;

        public MyWindow()
            : base(800, // Width
                600, // Height
                GraphicsMode.Default,
                "My OpenTK Window",
                GameWindowFlags.Default,
                DisplayDevice.Default,
                3, // major
                0, // minor
                GraphicsContextFlags.ForwardCompatible) { }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            GL.ClearColor(Color4.CornflowerBlue);

            GL.GenVertexArrays(1, out vertexArrayID);
            GL.BindVertexArray(vertexArrayID);

            ushort[] indices = new ushort[] { 0, 1, 2 };
            float[] vertices = new float[] {-1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
                                            0.0f, -1.0f, 1.0f, 0.0f, 0.0f,
                                            1.0f, 1.0f, 0.0f, 0.0f, 1.0f };

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

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, vertexBufferObjectIDs[1]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(ushort)), indices, BufferUsageHint.StaticDraw);

            GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, true, 5 * sizeof(float), 0);
            GL.EnableVertexAttribArray(0);

            GL.VertexAttribPointer(1, 3, VertexAttribPointerType.Float, true, 5 * sizeof(float), 2 * sizeof(float));
            GL.EnableVertexAttribArray(1);

            vertexShaderID = GL.CreateShader(ShaderType.VertexShader);
            string vertShaderText =
                @"
                            #version 150

                            in vec3 position;
                            in vec3 colour;
                            out vec3 Colour;
                            void main()
                            {
                            Colour = colour;
                            gl_Position = vec4(position, 1) ;
                            }";

            GL.ShaderSource(vertexShaderID, vertShaderText);
            GL.CompileShader(vertexShaderID);

            fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader);
            string fragShaderText =
                @"
                            #version 150
                            in vec3 Colour;
                            out vec4 outputF;
                            void main() 
                            {
                            outputF = vec4(Colour, 1.0);
                            }";
            GL.ShaderSource(fragmentShaderID, fragShaderText);
            GL.CompileShader(fragmentShaderID);

            shaderProgramID = GL.CreateProgram();
            GL.AttachShader(shaderProgramID, fragmentShaderID);
            GL.AttachShader(shaderProgramID, vertexShaderID);
            GL.LinkProgram(shaderProgramID);
            GL.UseProgram(shaderProgramID);
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            GL.DeleteBuffers(vertexBufferObjectIDs.Length, vertexBufferObjectIDs);
            GL.DeleteVertexArrays(1, ref vertexArrayID);

            GL.UseProgram(0); GL.DetachShader(shaderProgramID, vertexShaderID);
            GL.DetachShader(shaderProgramID, fragmentShaderID);
            GL.DeleteShader(fragmentShaderID);
            GL.DeleteShader(vertexShaderID);
            GL.DeleteProgram(shaderProgramID);
        }

        protected override void OnRenderFrame(FrameEventArgs e)
        {
            base.OnRenderFrame(e);

            GL.Clear(ClearBufferMask.ColorBufferBit);

            GL.DrawElements(BeginMode.Triangles, 3, DrawElementsType.UnsignedShort, IntPtr.Zero);

            this.SwapBuffers();
        }
    }
}

can anyone see my mistake?

genpfault
  • 51,148
  • 11
  • 85
  • 139
Sweeney
  • 3
  • 2

2 Answers2

0
vec4(position, 1)

In GLSL integers are not automatically type-promoted.

Kinda weird, because you got it right in your fragment shader.

Try this:

vec4(position, 1.0)
genpfault
  • 51,148
  • 11
  • 85
  • 139
0

You didn't link up the locations of the attributes. You can fix it by using the appropriate calls to BindAttribLocation​, or use layout qualifiers with locations. Also, position is a vec3 but you give it only 2 floats.

Using layout qualifiers is an easy fix:

layout(location = 0) in vec2 position;
layout(location = 1) in vec3 colour;

That gives me this picture: https://i.stack.imgur.com/wxWou.png which looks like it's probably what you had in mind.

harold
  • 61,398
  • 6
  • 86
  • 164