此处的标题问题类似:GLSL:shader linking fail (but no log),但就我而言,顶点着色器和片段着色器都非常简单,并且输入/输出变量匹配,如下所示。
[EDIT]加载着色器的代码在下方列出。
由于VS2010不支持基于范围的for循环,因此将部分代码#ifdef
添加了。
但是无论如何...我已经在MinGW 32位环境下尝试了该代码,并且它链接确定。
使用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;
}
最佳答案
我认为您在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);
顺便说一句,如果您想支持较旧的编译器,则最好只使用旧版本的循环。
关于c++ - GLSL链接失败,没有有用的信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24321873/