【问题标题】:error c0000: syntax error, unexpected '?' at token '?'错误 c0000:语法错误,意外的“?”在令牌'?
【发布时间】:2014-10-26 21:57:08
【问题描述】:

好的,我搜索了其他人的问题,但找不到解决问题的方法。我在 C# 和 GLSL 330 中使用 OpenTK。它正在产生错误消息 错误 c0000:语法错误,意外的“?”在令牌'?'

由于某种原因,它不喜欢我正在做的事情。所以,这是我的代码,希望有人能告诉我 我做错了什么。

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

【问题讨论】:

  • 通常有一些线路/位置信息有错误
  • 编译顶点或片段着色器时是否出现此错误消息?
  • 对我来说听起来像是字符编码的问题。 GLSL 需要 UTF-8 的一个小子集,如果遇到在 cmets 之外不希望出现的字符,则会生成诸如此类的错误。如果您删除 @ 并在每一行的末尾使用 \n 而不是一个长的逐字字符串文字,这会改变什么吗?
  • 棘轮怪胎它说 0(13) 表示行号||||||| BDL 是的,我可能应该提到它的编译错误|||||||||| Andon M. Coleman 似乎消除了错误,但现在它只是一个白屏。你在哪里是正确的:) 我之前在绑定顶点数据时遇到了一个类似的问题。可能与此有关。
  • @LeviGraham:嗯,我可以看到另一个问题。 gl_Position = Position * pvm; 对我来说看起来很不寻常。通常在 OpenGL 中,你会有一些效果:gl_Position = pvm * Position。除非您的矩阵以行为主(这是可能的,因为我记得至少有一个 C# 框架可以做到这一点,但我不记得是哪个),您应该考虑交换操作数。

标签: c# opengl shader vbo opentk


【解决方案1】:

好的,问题已解决。感谢上面有用的 cmets。

让我们从着色器开始。必须删除字符串声明之前的 @ 符号,并且必须插入每一行 \n 之后。另外,当我使用着色器绘制时,我正在调用转置。这可以通过改变矩阵的顺序来解决。

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

我从 GL.UniformMatrix4(uniformLocation, true, ref mat); 更改为 GL.UniformMatrix4(uniformLocation, false, ref mat); 并且在着色器本身中 gl_Position 的顺序从 Position * pvm; 更改为 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" ;

修复此问题后,出现渲染表面变白的错误。错误位于Draw() 函数中。基本上我没有正确分配数组位置。

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

【讨论】:

  • 如果您在其他人之前正确回答了自己的问题,请接受您的正确答案。
  • 我真的不知道你在说什么,但我猜它是复选标记。
  • 看起来你找到了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-31
  • 1970-01-01
  • 2014-09-23
  • 1970-01-01
  • 2019-10-28
相关资源
最近更新 更多