2014-10-26 101 views
0

好吧我搜索了其他人的問題,並找不到解決我的問題。我在C#和GLSL 330中使用OpenTK。它生成錯誤消息 錯誤c0000:語法錯誤,意外的'?'在令牌'?'錯誤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); 
    } 
+0

通常有一些與編譯頂點或片段着色器時 – 2014-10-26 22:10:13

+0

此錯誤訊息apear錯誤線/位置信息? – BDL 2014-10-26 22:37:28

+0

聽起來像是一個字符編碼問題。 GLSL需要UTF-8的一小部分,並且如果遇到不希望評論之外的字符,就會產生這樣的錯誤。如果刪除'@'並在每行的末尾使用'\ n'而不是一個長的逐字字符串文字,那麼這會改變什麼嗎? – 2014-10-26 23:45:02

回答

0

好的,所以問題已修復。感謝上面的有用評論。

讓我們從着色器開始。必須刪除字符串聲明前的@符號,並且必須插入每行\ 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); 
    } 
+1

如果您在其他人之前正確回答了您自己的問題,請接受您的答案爲正確答案。 – STLDeveloper 2014-10-28 23:45:23

+0

我真的不知道你在說什麼,但我猜測它的複選標記。 – LeviGraham 2014-10-30 01:01:05

+0

看起來像你找到它。 – STLDeveloper 2014-10-30 19:57:40