c++ - QGLShaderProgram::setAttributeArray(0, ...) 与 QGLShaderProgram::setAttributeArray ("position", ...)

标签 c++ qt opengl

我有一个顶点着色器:

#version 430
in vec4 position;

void main(void)
{
    //gl_Position = position; => works in ALL cases
    gl_Position = vec4(0,0,0,1);
}

如果我这样做:

m_program.setAttributeArray(0, m_vertices.constData());
m_program.enableAttributeArray(0);

一切正常。但是,如果我这样做:

m_program.setAttributeArray("position", m_vertices.constData());
m_program.enableAttributeArray("position");

注意:m_program.attributeLocation("position"); 返回 -1。

然后,我得到一个空窗口。

Qt 帮助页面状态:

void QGLShaderProgram::setAttributeArray(int location, const QVector3D * values, int stride = 0)

Sets an array of 3D vertex values on the attribute at location in this shader program. The stride indicates the number of bytes between vertices. A default stride value of zero indicates that the vertices are densely packed in values.

The array will become active when enableAttributeArray() is called on the location. Otherwise the value specified with setAttributeValue() for location will be used.

void QGLShaderProgram::setAttributeArray(const char * name, const QVector3D * values, int stride = 0)

This is an overloaded function.

Sets an array of 3D vertex values on the attribute called name in this shader program. The stride indicates the number of bytes between vertices. A default stride value of zero indicates that the vertices are densely packed in values.

The array will become active when enableAttributeArray() is called on name. Otherwise the value specified with setAttributeValue() for name will be used.

那么,为什么它在使用“int 版本”时有效,而在使用“const char * 版本”时却无效?

最佳答案

它返回 -1,因为您注释掉了着色器中实际使用 position 的唯一一行。

这不是错误,这是由于误解了属性位置的分配方式而导致的。制服和属性仅在编译和链接所有着色器阶段后才分配位置。如果统一或属性未在事件代码路径中使用,则不会为其分配位置。即使你使用变量来做这样的事情:

#version 130

in vec4 dead_pos; // Location: N/A
in vec4 live_pos; // Location: Probably 0

void main (void)
{
  vec4 not_used = dead_pos; // Not used for vertex shader output, so this is dead.
  gl_Position   = live_pos;
}

它实际上比这更进一步。如果某些内容从顶点着色器输出但未在几何、曲面 segmentation 或片段着色器中使用,则其代码路径被视为不活动。

顺便说一下,顶点属性位置 0 是隐式顶点位置。它是 GLSL 规范中唯一的顶点属性。允许别名到固定函数指针函数(例如 glVertexPointer (...) == glVertexAttribPointer (0, ...))

关于c++ - QGLShaderProgram::setAttributeArray(0, ...) 与 QGLShaderProgram::setAttributeArray ("position", ...),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20636665/

相关文章:

c++ - 如何确保以正确的顺序初始化全局变量?

c++ - QT Creator 无法识别新项目的 STL 类型

c++ - 长时间例程后的 Qt 事件卡住了我的应用程序一段时间

c++ - 将外部库添加到 Qt Creator 项目中

c++ - 如何从 Qt Creator 中的继承类自动创建虚方法?

c - 如何修复Glad.c中对符号 'dlclose@@GLIBC_2.2.5'的 undefined reference

c++ - OpenGL 2d 纹理不工作

c++ - 检测何时单击按钮? [C++, WinAPI]

c++ - 如何将 npapi 插件编译到 mac

c++ - 将像素数据渲染到 OpenGL 纹理四边形中