类似的标题问题:GLSL:shader linking fail (but no log)但在我的情况下,顶点和片段着色器都非常简单,输入/输出变量匹配如下所示。
[编辑]进一步列出用于加载着色器的代码。
由于VS2010不支持基于范围的for循环,因此代码的某些部分为#ifdef
'd。
但无论如何......我已经尝试使用MinGW 32bit环境的代码并且链接正常。
使用VS2010构建时,着色器链接成功并运行正常,但使用NetBeans + MinGW-w64失败并显示此日志消息:
Link info
---------
No shader objects attached.
这可能与MinGW-w64 OpenGL图书馆有关吗?
这是我的顶点着色器,
#version 330
in vec4 vPosition;
in vec4 vColor;
out vec4 color;
void main()
{
color = vColor;
gl_Position = vPosition;
}
这是我的片段着色器。
#version 330
in vec4 color;
out vec4 fColor;
void main()
{
fColor = color;
}
LoadShader.h:
typedef struct {
GLenum type;
const char* filename;
GLuint shader;
} ShaderInfo;
main.cpp中:
vector<ShaderInfo> shaders;
ShaderInfo vert = {GL_VERTEX_SHADER, "SimpleVertexShader.vert"};
ShaderInfo frag = {GL_FRAGMENT_SHADER, "SimpleFragmentShader.frag"};
shaders.push_back(vert);
shaders.push_back(frag);
program = LoadShaders(shaders);
LoadShader.cpp - LoadShaders()
GLuint LoadShaders(vector<ShaderInfo> shaders)
{
if (shaders.empty()) return 0;
#if !defined(_MSC_VER) || 1600 < _MSC_VER
for (auto entry : shaders)
entry.shader = CreateShader(entry.type, entry.filename);
#else
for (vector<ShaderInfo>::iterator entry = shaders.begin(); entry != shaders.end(); ++entry)
entry->shader = CreateShader(entry->type, entry->filename);
#endif
// Create the program
return CreateProgram(shaders);
}
LoadShader.cpp - CreateShader()
GLuint CreateShader(GLenum shaderType, const char* shader_file_path)
{
// Create the shader
GLuint shaderID = glCreateShader(shaderType);
if (!shaderID)
return 0;
// Read the shader code from the file
std::string shaderCode;
std::ifstream shaderStream(shader_file_path, std::ios::in);
if(shaderStream.is_open())
{
std::string Line = "";
while(getline(shaderStream, Line))
shaderCode += "\n" + Line;
shaderStream.close();
}
// Compile the shader
printf("Compiling shader : %s\n", shader_file_path);
char const* sourcePointer = shaderCode.c_str();
glShaderSource(shaderID, 1, &sourcePointer , NULL);
glCompileShader(shaderID);
// Check the shader
GLint compiled;
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &compiled);
if (!compiled) {
GLsizei len;
glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &len);
GLchar* log = new GLchar[len+1];
glGetShaderInfoLog(shaderID, len, &len, log);
std::cerr << "Shader compilation failed: " << log << std::endl;
delete [] log;
return 0;
}
return shaderID;
}
LoadShader.cpp - CreateProgram()
GLuint CreateProgram(vector<ShaderInfo> shaders)
{
// Create and link the program
fprintf(stdout, "Linking program\n");
GLuint programID = glCreateProgram();
if (!programID)
return 0;
// attach shaders and link the program
#if !defined(_MSC_VER) || 1600 < _MSC_VER
for (auto iter : shaders)
glAttachShader(programID, iter.shader);
#else
for (vector<ShaderInfo>::iterator iter = shaders.begin(); iter != shaders.end(); ++iter)
glAttachShader(programID, iter->shader);
#endif
glLinkProgram(programID);
// Check the program
GLint linked;
glGetProgramiv(programID, GL_LINK_STATUS, &linked);
if (!linked) {
GLsizei len;
glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &len);
GLchar* log = new GLchar[len+1];
glGetProgramInfoLog(programID, len, &len, log);
std::cerr << "Shader linking failed: " << log << std::endl;
delete [] log;
#if !defined(_MSC_VER) || 1600 < _MSC_VER
for (auto iter : shaders) {
glDeleteShader(iter.shader);
iter.shader = 0;
}
#else
for (vector<ShaderInfo>::iterator iter = shaders.begin(); iter != shaders.end(); ++iter) {
glDeleteShader(iter->shader);
iter->shader = 0;
}
#endif
return 0;
}
return programID;
}
答案 0 :(得分:3)
我认为您在LoadShaders
中有错误。循环的C ++ 11版本:
for (auto entry : shaders)
entry.shader = CreateShader(entry.type, entry.filename);
获取shaders
中每个条目的副本,因此不会更新向量中的元素。请尝试改为:
for (auto &entry : shaders)
entry.shader = CreateShader(entry.type, entry.filename);
顺便说一句,如果你想支持较旧的编译器,你可能只是拥有旧版本的循环。