错误c0000:语法错误,意外'';代币'';
本文关键字:错误 代币 c0000 语法 意外 | 更新日期: 2023-09-27 18:27:58
好吧,我搜索了其他人的问题,但找不到解决我问题的方法。我在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);
}
好吧,问题已经解决了。感谢以上有益的评论。
让我们从着色器开始。字符串声明之前的@符号必须删除,每行之后必须插入。此外,当我使用着色器绘制时,我调用了transpose。可以通过改变矩阵的顺序来固定。
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);
}