gpt4 book ai didi

c++ - GLSL 4.5 : Why is my "in" variable for Color Not Being Found by glGetAttribLocation

转载 作者:行者123 更新时间:2023-11-28 00:10:32 25 4
gpt4 key购买 nike

这是顶点着色器

#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. 1) 每次调用 Open GL 函数调用后,我都会遇到 glGetError。在对顶点着色器中的 iColor 变量调用 glGetAttribLocation 之前,我没有收到任何错误。

  2. 在分别调用 glCompileShaderglLinkProgram 之后,我有以下代码:


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);
}
  1. 我已尝试将版本改回 150,但出现同样的错误。我知道有些实现会优化变量。但是我明明是用这个来判断颜色的。

  2. 我打算返回版本控制历史,看看是什么变化导致了这个错误,但这可能需要我一些时间。

如果有帮助,这里是绘制函数的简化版本:

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;
}

关于c++ - GLSL 4.5 : Why is my "in" variable for Color Not Being Found by glGetAttribLocation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33327303/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com