GLSL语法错误:“在”语法错误

问题描述:

我想在我的程序中使用着色器,但我发现很奇怪的错误...GLSL语法错误:“在”语法错误

Vertex shader failed to compile with the following error 
ERROR: 0:6: error(#132) Syntax error: "in" parse error 
ERROR: error(#273) 1 compilation errors. No code generated 

我认为问题是与文件读取,但尝试了很多方法后,仍然无法正常工作。

因此,这里是我的代码:

bool ShaderProgram::LoadShaderFile(const char* shader_path, GLuint& shader_id) 
{ 
    ifstream oFileStream(shader_path); 
    if(oFileStream) 
    { 
     // Load shader code 
     string sShaderSource; 
     sShaderSource.assign((istreambuf_iterator<char> (oFileStream)), istreambuf_iterator<char>()); 

     // Forward shader code to OGL 
     const GLchar* chShaderSource = sShaderSource.c_str() + '\0'; 
     printf("%s", chShaderSource); 
     glShaderSource(shader_id, 1, (const GLchar**) &chShaderSource, NULL); 


     return true; 
    } 
    else 
     return false; 

} 

我的着色器:

// shader.vs 
// Vertex Shader 
#version 330 

in vec3 vVertex 
in vec3 vColor 

smooth out vec4 vVaryingColor; 

void main() 
{ 
    vVaryingColor = vec4(vColor, 1.0); 
    gl_Position = vec4(vVertex, 1.0); 
} 


// shader.fs 
// Fragment Shader 
#version 330 

smooth in vec4 vVeryingColor; 
out vec4 vVaryingColor; 

void main() 
{ 
    vFragColor = vVaryingColor; 
} 

你缺少在in线末端的分号。

您有:

in vec3 vVertex 
in vec3 vColor 

你应该有:

in vec3 vVertex; 
in vec3 vColor; 
+0

你是对的,谢谢! 我也在frag着色器中错过了输出向量。 那就是当你从许多来源复制粘贴代码时会发生什么...... – ddl

+1

@ user2843974另外,请注意,某些GPU不喜欢在'#version'指令之前进行注释。所以这在某些情况下也可能是Shader无法编译的原因。 – Vallentin

+0

我会记住,谢谢! – ddl