Home > Software design >  glsl VertexShader/FragmentShader refuse to compile in OpenTk c#
glsl VertexShader/FragmentShader refuse to compile in OpenTk c#

Time:07-23

I have followed this tutorial to try and learn OpenTK (https://www.youtube.com/watch?v=elEyLoQQ67o&t=1182s) My code is pretty much identical to his but when the program is run, both the fragment-shader and the pixel-shader fail to compile with the vague error "ERROR: 0:8: 'f' : syntax error syntax error". And when the program is linked it randomly gives the error "Attached fragment shader is not compiled." or "Attached vertex shader is not compiled." every time the program is run. I am completely stumped and have no idea what is causing the ERROR since i can find no error with the code and the shader is as simple as can be.

The code is suppose to draw a triangle to the screen.

 string vertexShaderCode = 
        @"
        #version 460 core

        layout (location = 0) in vec3 aPosition;

        void main()
        {
            gl_Position = vec4(aPosition, 1f);
        }
        
        ";

        string fragmentShaderCode =
        @"
        #version 460 core

        out vec4 fragColor;

        void main()
        {
            fragColor = vec4(0f, 0f, 0.5f, 1f);
        }
        ";

        int vertexShader = GL.CreateShader(ShaderType.VertexShader);
        GL.ShaderSource(vertexShader, vertexShaderCode);
        GL.CompileShader(vertexShader);

        GL.GetShader(vertexShader, ShaderParameter.CompileStatus, out int d);
        if (d == 0)
        {
            string infoLog = GL.GetShaderInfoLog(vertexShader);
            System.Console.WriteLine(infoLog);
        }

        int fragmentShader = GL.CreateShader(ShaderType.FragmentShader);
        GL.ShaderSource(fragmentShader, fragmentShaderCode);
        GL.CompileShader(fragmentShader);

        GL.GetShader(fragmentShader, ShaderParameter.CompileStatus, out int a);
        if (a == 0)
        {
            string infoLog = GL.GetShaderInfoLog(fragmentShader);
            System.Console.WriteLine(infoLog);
        }



        this.shaderProgram = GL.CreateProgram();
        GL.AttachShader(this.shaderProgram, vertexShader);
        GL.AttachShader(this.shaderProgram, fragmentShader);

        GL.LinkProgram(this.shaderProgram);

        int linkStatus = 0;
        GL.GetProgram(this.shaderProgram, GetProgramParameterName.LinkStatus, out linkStatus);
        string infolog = GL.GetProgramInfoLog(this.shaderProgram);
        Console.WriteLine(infolog);

        GL.DetachShader(this.shaderProgram, vertexShader);
        GL.DetachShader(this.shaderProgram, fragmentShader);

        GL.DeleteShader(vertexShader);
        GL.DeleteShader(fragmentShader);

Another thing, altough the shaders fail to compile, a triangle is still drawn to the screen which shouldn't be possible right? The only explenation i can think of is that the hardware has some kind of base vertex-shader that it resorts to if a custom vertex-shader is not given.

CodePudding user response:

1f is not a valid GLSL syntax (this is not C or C ). Either 1.0f, 1.0 or simply 1. In GLSL 1.0 is a single-precision floating point literal, for double-precision you have to use the suffix lf (e.g. 1.0lf). See alos The OpenGL® Shading Language, Version 4.60.7 - 4.1.4. Floats.

gl_Position = vec4(aPosition, 1f);

gl_Position = vec4(aPosition, 1);

fragColor = vec4(0f, 0f, 0.5f, 1f);

fragColor = vec4(0, 0, 0.5, 1);
  • Related