works i-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:13:17 25 4
gpt4 key购买 nike

我有一个顶点着色器:

#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/

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