我目前正试图编译GLSL着色器时遇到的错误让我头发失去理智,我已成功设法编译两个着色器,但是当涉及链接它们时,我得到一个错误。没有输出,但链接失败。
这是编译+链接代码:
GLuint program = NULL;
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertCont, NULL);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
char logBuf[1024];
int len;
if (0 == compile_ok)
{
glGetShaderInfoLog(vs, sizeof(logBuf), &len, logBuf);
LogMgr::logMessage(LogMgr::CRITICAL, "Error in vertex shader\n", logBuf);
return false;
}
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragCont, NULL);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if (!compile_ok)
{
glGetShaderInfoLog(vs, sizeof(logBuf), &len, logBuf);
LogMgr::logMessage(LogMgr::CRITICAL, "Error in fragment shader", logBuf);
return false;
}
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok)
{
glGetShaderInfoLog(vs, sizeof(logBuf), &len, logBuf);
LogMgr::logMessage(LogMgr::CRITICAL, "Could not link basic shaders!", logBuf);
return false;
}
return program;
这是顶点着色器:
#version 330
layout(location = 0) in vec4 position;
void main()
{
gl_Position = position;
}
片段着色器:
#version 330
out vec4 outputColor;
void main()
{
outputColor = vec4(1.0f, 0.0f, 1.0f, 1.0f);
}
任何线索?
答案 0 :(得分:7)
您对getShaderInfoLog的调用是错误的。您总是检查顶点着色器的编译日志。这解释了为什么您没有看到任何错误消息。这就是你复制和粘贴代码所得到的;)
对于片段着色器,它应该是:
glGetShaderInfoLog(fs, sizeof(logBuf), &len, logBuf);
对于链接状态:
glGetProgramInfoLog(program, sizeof(logBuf), &len, logBuf);
答案 1 :(得分:0)
问题是我没有创建程序对象。我添加了这行代码来进行链接,并且工作正常:
program = glCreateProgram();