我试图弄清楚为什么使用的偏移值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)
};
属性绑定是这样发生的:
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 驱动程序)并呈现以下内容:
如果我将属性绑定更改为:
program.bindVertexAttribs<float>(vbo1,0, "in_Position", 3, 6, 12);
program.bindVertexAttribs<float>(vbo1,1, "in_Color", 3, 6, 0);
我得到这样的正确渲染:
这对我来说没有意义,因为位置是数据数组中的前 3 个浮点数,颜色是接下来的 3 个浮点数,然后再次定位,依此类推。我错过了一些明显的东西吗?