这是顶点着色器
#version 450
in vec3 iPosition;
in vec4 iColor;
out vec4 oColor;
uniform mat4 uMVP;
void main()
{
oColor = iColor;
gl_Position = uMVP * vec4(iPosition, 1.0);
}
这是片段着色器
#version 450
in vec4 iColor;
out vec4 oColor;
void main()
{
oColor = iColor;
}
没有粘贴我所有的代码,下面是我所做的:
1)每次调用Open GL函数调用后,我都有
glGetError
。我没有得到任何错误,直到我调用glGetAttribLocation
在顶点着色器的iccolor变量。我在分别调用
glCompileShader
和glLinkProgram
后右边有如下代码:
GLint isCompiled = 0;
glGetShaderiv(m_fragmentShader, GL_COMPILE_STATUS, &isCompiled); GL_CALL
if(isCompiled == GL_FALSE) {
GLint maxLength = 0;
glGetShaderiv(m_fragmentShader, GL_INFO_LOG_LENGTH, &maxLength); GL_CALL
std::vector<GLchar> errorLog(maxLength);
glGetShaderInfoLog(m_fragmentShader, maxLength, &maxLength, &errorLog[0]);
GL_CALL
std::stringstream out;
std::copy(errorLog.begin(), errorLog.end(), std::ostream_iterator<char>(
out, ""));
qDebug() << out.str().c_str();
glDeleteShader(m_fragmentShader); GL_CALL
QCoreApplication::exit(-1);
}
GLint isLinked = 0;
glGetProgramiv(m_shaderProgram, GL_LINK_STATUS, (int *)&isLinked);
if(isLinked == GL_FALSE) {
GLint maxLength = 0;
glGetProgramiv(m_shaderProgram, GL_INFO_LOG_LENGTH, &maxLength); GL_CALL
std::vector<GLchar> infoLog(maxLength);
glGetProgramInfoLog(m_shaderProgram, maxLength, &maxLength, &infoLog[0]);
GL_CALL
std::stringstream out;
std::copy(infoLog.begin(), infoLog.end(), std::ostream_iterator<char>(
out, ""));
qDebug() << out.str().c_str();
glDeleteProgram(m_shaderProgram); GL_CALL
glDeleteShader(m_vertexShader); GL_CALL
glDeleteShader(m_fragmentShader); GL_CALL
QCoreApplication::exit(-1);
}
我试图将版本更改回150,但这给出了相同的错误。我知道有些实现会优化变量。但我显然是用这个来决定颜色的。
我打算回到版本控制历史,看看是什么变化带来了这个错误,但这可能需要我一点。
如果有帮助的话,下面是draw函数的简化版本:
void Spline::draw()
{
glUseProgram(m_shaderProgram); GL_CALL
GLint posAttrib = glGetAttribLocation(m_shaderProgram, "iPosition"); GL_CALL
GLint colAttrib = glGetAttribLocation(m_shaderProgram, "iColor"); GL_CALL
glBindBuffer(GL_ARRAY_BUFFER, m_vbo); GL_CALL
std::size_t stride = verts::traits::stride;
std::size_t dimension1 = verts::datum_type::internal_type1::dimension;
std::size_t byte_offset1 = verts::traits::type1_byte_offset;
glEnableVertexAttribArray(posAttrib); GL_CALL
glVertexAttribPointer(posAttrib, dimension1, GL_FLOAT, GL_FALSE,
stride, (void*)byte_offset1); GL_CALL
std::size_t dimension2 = verts::datum_type::internal_type2::dimension;
std::size_t byte_offset2 = verts::traits::type2_byte_offset;
glEnableVertexAttribArray(colAttrib); GL_CALL
glVertexAttribPointer(colAttrib, dimension2, GL_FLOAT, GL_FALSE,
stride, (void*)byte_offset2); GL_CALL
opengl_math::matrix_4X4<float, opengl_math::column> model(
opengl_math::identity);
opengl_math::matrix_4X4<float, opengl_math::column> view =
opengl_math::look_at<float, opengl_math::column>(
opengl_math::point_3d<float>(0.0f, 0.0f, 20.0f),
opengl_math::point_3d<float>(0.0f, 0.0f, 0.0f),
opengl_math::vector_3d<float>(0.0f, 1.0f, 0.0f));
GLint uniMVP = glGetUniformLocation(m_shaderProgram, "uMVP"); GL_CALL
glUniformMatrix4fv(uniMVP, 1, GL_FALSE,
(m_projection * view * model).to_gl_matrix()); GL_CALL
glDrawArrays(GL_LINE_STRIP, 0, m_vertexAttrib.get_attribute_count());
GL_CALL
}
您使用iColor
作为两个着色器阶段的输入名称。由于片段着色器属性不能被查询,你必须确保名称在着色器中是唯一的。可以使用以下代码:
顶点:
#version 450
in vec3 iPosition;
in vec4 iColor;
out vec4 xColor;
uniform mat4 uMVP;
void main()
{
xColor = iColor;
gl_Position = uMVP * vec4(iPosition, 1.0);
}
片段:
#version 450
in vec4 xColor;
out vec4 oColor;
void main()
{
oColor = xColor;
}