Search code examples
c#openglshadervboopentk

error c0000: syntax error, unexpected '?' at token '?'


Alright I searched other peoples questions and could not find a solution to my problem. I am using OpenTK in C# and GLSL 330. It is producing the error message error c0000: syntax error, unexpected '?' at token '?'

For some reason it doesn't like something I'm doing. So, here is my code I hope someone can tell me what I'm doing wrong.

public static string vertexShaderSource = @"
#version 330

uniform mat4 pvm;

in vec4 Position;
in vec2 texCoord;

out vec2 texCoordV;

void main()
{
    texCoordV = texCoord;
    gl_Position = Position * pvm;
}";

public static string fragmentShaderSource = @"
#version 330

in vec2 texCoordV;

out vec4 colorOut;

void main()
{
    colorOut = vec4(texCoord, 0.0, 0.0);    
}";


    public void Initalize()
    {
        style = GUI_Skin.styles[0];
        vertices = new Vector3[6];
        vertices[0] = new Vector3(0, 0, 0f);
        vertices[1] = new Vector3(100, 0, 0f);
        vertices[2] = new Vector3(0, 100, 0f);
        vertices[3] = new Vector3(100, 0, 0f);
        vertices[4] = new Vector3(0, 100, 0f);
        vertices[5] = new Vector3(100, 100, 0f);

        GL.GenBuffers(1, out vertHandle);
        GL.BindBuffer(BufferTarget.ArrayBuffer, vertHandle);
        GL.BufferData<Vector3>(BufferTarget.ArrayBuffer,
                               new IntPtr(vertices.Length * Vector3.SizeInBytes),
                               vertices, BufferUsageHint.StaticDraw);

        texCoords = new Vector2[6];
        texCoords[0] = new Vector2(0,0);
        texCoords[1] = new Vector2(1, 0);
        texCoords[2] = new Vector2(0, 1);
        texCoords[3] = new Vector2(1, 0);
        texCoords[4] = new Vector2(0, 1);
        texCoords[5] = new Vector2(1, 1);

        GL.GenBuffers(1, out texHandle);
        GL.BindBuffer(BufferTarget.ArrayBuffer, texHandle);
        GL.BufferData<Vector2>(BufferTarget.ArrayBuffer,
                               new IntPtr(texCoords.Length * Vector2.SizeInBytes),
                               texCoords, BufferUsageHint.StaticDraw);
    }

    public void Draw()
    {
        GL.EnableVertexAttribArray(vertHandle);
        GL.BindBuffer(BufferTarget.ArrayBuffer, vertHandle);
        GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);

        GL.EnableVertexAttribArray(texHandle);
        GL.BindBuffer(BufferTarget.ArrayBuffer, texHandle);
        GL.VertexAttribPointer(0, 2, VertexAttribPointerType.Float, false, Vector2.SizeInBytes, 0);

        GL.DrawArrays(PrimitiveType.Triangles, 0, 6);

        GL.DisableVertexAttribArray(vertHandle);
        GL.DisableVertexAttribArray(texHandle);
    }

Solution

  • Alright so the issues have been fixed. Thanks to the helpful comments above.

    Lets start with the shader. The @ symbol before the string declaration had to be removed and after every line \n had to be inserted. Also, I was calling transpose when I draw with the shader. Which could be fixed by changing the order of matrices.

        public static void Run()
        {
            int uniformLocation = GL.GetUniformLocation(shaderProgramHandle, "pvm");
            Matrix4 mat;
            GL.GetFloat(GetPName.ProjectionMatrix, out mat);
            GL.UniformMatrix4(uniformLocation, false, ref mat);
    
            GL.UseProgram(shaderProgramHandle);
        }
    

    I changed from GL.UniformMatrix4(uniformLocation, true, ref mat); to GL.UniformMatrix4(uniformLocation, false, ref mat); and in the shader itself the order of gl_Position was changed from Position * pvm; to pvm * Position;

    public static string vertexShaderSource = "#version 330\n" +
        "uniform mat4 pvm;\n" +
        "in vec4 Position;\n" +
        "in vec2 texCoord;\n" +
        "out vec2 texCoordV;\n" +
        "void main()\n" +
        "{\n" +
            "texCoordV = texCoord;\n" +
            "gl_Position = pvm * Position;\n" +
        "}\n";
    
        public static string fragmentShaderSource = "#version 330\n" +
        "in vec2 texCoordV;\n" +
        "out vec4 colorOut;" +
        "void main()\n" +
        "{\n" +
            "colorOut = vec4(texCoordV, 0.0, 0.0);\n" +
        "}\n" ;
    

    After this was fixed I was getting an error where the rendering surface went white. The error was located within the Draw() function. Basically I wasn't assigning the array locations properly.

    public void Draw()
        {
            GL.EnableVertexAttribArray(0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vertHandle);
            GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, Vector3.SizeInBytes, 0);
    
            GL.EnableVertexAttribArray(1);
            GL.BindBuffer(BufferTarget.ArrayBuffer, texHandle);
            GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, Vector2.SizeInBytes, 0);
    
            GL.DrawArrays(PrimitiveType.Triangles, 0, 6);
    
            GL.DisableVertexAttribArray(0);
            GL.DisableVertexAttribArray(1);
        }