gpt4 book ai didi

c++ - OpenGL 属性偏移量未按预期工作

转载 作者:行者123 更新时间:2023-11-30 04:22:59 24 4
gpt4 key购买 nike

我想弄清楚为什么 glVertexAttribPointer 中使用的偏移值没有像我预期的那样工作。

代码可在 http://glbase.codeplex.com/ 获得

我的顶点数据是这样的:

glBase::Vector3 data1[]={
glBase::Vector3(-0.4f,0.8f,-0.0f), //Position
glBase::Vector3(1.0f,0.0f,0.0f), //Color
glBase::Vector3(-.8f,-0.8f,-0.0f), //Position etc
glBase::Vector3(0.0f,1.0f,0.0f),
glBase::Vector3(0.0f,-0.8f,-0.0f),
glBase::Vector3(0.0f,0.0f,1.0f)
};

属性绑定(bind)是这样发生的:

program.bindVertexAttribs<float>(vbo1,0, "in_Position", 3, 6, 0);
program.bindVertexAttribs<float>(vbo1,1, "in_Color", 3, 6, 12);

bindVertexAttribs 函数定义如下:

template<typename T>
void bindVertexAttribs(const VertexBufferObject& object, GLint location,const std::string& name, unsigned int width, unsigned int stride, unsigned int offset)
{
object.bind();

glBindAttribLocation(m_ID, location, name.c_str());

glVertexAttribPointer(location, width, GL_FLOAT, GL_FALSE, stride*sizeof(T),(GLvoid *)offset);
glEnableVertexAttribArray(location);
}

此代码无法在我的机器(ATI 5850,12.10 驱动程序)上正确呈现并呈现以下内容:

enter image description here

如果我将属性绑定(bind)更改为:

program.bindVertexAttribs<float>(vbo1,0, "in_Position", 3, 6, 12);
program.bindVertexAttribs<float>(vbo1,1, "in_Color", 3, 6, 0);

我得到了这样的正确渲染:

enter image description here

这对我来说没有意义,因为位置是数据数组中的前 3 个 float ,颜色是接下来的 3 个 float ,然后再次定位,依此类推。我错过了一些明显的东西吗?

最佳答案

glBindAttribLocation(m_ID, location, name.c_str());

这什么都不做;随意删除此行,看看它有多少作用。

如果 m_ID 是一个链接的程序对象(如果不是,那么你的代码就更没有意义了。除非你使用 VAO 来捕获你的属性数组状态),那么这个函数不会有实际效果。 glBindAttribLocation 仅在链接之前 有效。链接后无法更改属性绑定(bind)。所以每个使用程序的对象都需要使用相同的属性。

你应该establish a proper convention for your vertex attribute names.当您创建任何程序时,您应该在链接之前将您的约定应用于该程序。这样,您就知道 in_position 始终是属性 0,例如。您不必通过毫无意义的 glGetAttribLocation 调用来查找它。而且您不会被诱惑去做您在这里所做的事情,那是行不通的。

关于c++ - OpenGL 属性偏移量未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13557708/

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