[英]Vertex Shader and Fragment Shader(s) failed to compile before gllinkProgram() was called,
頂點着色器和片段着色器都是小蒼蠅(文件),用於出現在藍色背景上的小紅色框。
他們來了:
第一個是 colorShading.vert
#version 130
in vec2 vertexPosition;
void main(){
gl_Positiion.xy = vertexPosition;
gl_Positiion.z = 0;
gl_Positiion.w = 1;
}
第二個是 colorShading.frag
#version 130
out vec3 color;
void main(){
color vec3(1.0, 0.0, 0.0);
}
這是錯誤:
Vertex and Fragment shader(s) were not successfully compiled
before glLinkProgram() was called. Link failed.
Shaders failed to link!
Enter any key to quit...
我使用控制台輸出它使用
std::vector<GLchar> errorLog(maxLength);
glGetProgramInfoLog(_programID, maxLength, &maxLength, &errorLog[0]);
std::printf("%s\n", &(errorLog[0]));
fatalError("Shaders failed to link!");`
我用來編譯着色器的函數如下
而且我把filepath和shaderID傳給函數,而不是復制粘貼,方便吧?
void GLSLProgram::compileShader(const std::string& filePath, GLuint& id)
{
std::ifstream shaderFile(filePath);
if (shaderFile.fail()) {
perror(filePath.c_str());
fatalError("Failed to Open " + filePath);
}
std::string fileContents = "";
std::string line;
while (std::getline(shaderFile, line)) {
fileContents += line + "\n";
}
shaderFile.close();
const char* contentsPtr = fileContents.c_str();
glShaderSource(id, 1, &contentsPtr, nullptr);
glCompileShader(id);
GLint success = 0;
glGetShaderiv(id, GL_COMPILE_STATUS, &success);
if (success = GL_FALSE) {
GLint maxLength = 0;
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &maxLength);
//The maxLength include the NULL character
std::vector<char> errorLog(maxLength);
glGetShaderInfoLog(id, maxLength, &maxLength, &errorLog[0]);
//Exit with failure
glDeleteShader(id);
std::printf("%s\n", &(errorLog[0]));
fatalError(" Shader " +filePath +" failed to compile!");
return;
}
}
我該如何解決我遇到的這個錯誤?
您的代碼中至少有兩個問題:
編譯碼
檢查編譯成功的if
語句包含一個賦值,而不是一個比較:
if (success = GL_FALSE) {
你真正需要的是(不是兩個等號):
if (success == GL_FALSE) {
我認識的每個編譯器至少會對此發出警告。
着色器
在片段着色器中,還缺少一個 = 符號:
color vec3(1.0, 0.0, 0.0);
這不是有效的 glsl 代碼。 很可能你想分配這種顏色,因此代碼應該是
color = vec3(1.0, 0.0, 0.0);
您的第一個代碼示例將gl_Position
拼錯為gl_Positiion
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.