出于某种原因,OpenGL VAO指向0
OpenGL VAO is pointing to address 0 for some reason
我的vao遇到了一些麻烦(至少这是我认为正在发生的事情)。
所以,我正在做的是,我有一个正在从某些原始数据中创建VBO和VAO的类
RawModel* Loader::loadToVao(float* positions, int sizeOfPositions) {
unsigned int vaoID = this->createVao();
this->storeDataInAttributeList(vaoID, positions, sizeOfPositions);
this->unbindVao();
return new RawModel(vaoID, sizeOfPositions / 3);
}
unsigned int Loader::createVao() {
unsigned int vaoID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);
unsigned int copyOfVaoID = vaoID;
vaos.push_back(copyOfVaoID);
return vaoID;
}
void Loader::storeDataInAttributeList(unsigned int attributeNumber, float* data, int dataSize) {
unsigned int vboID;
glGenBuffers(1, &vboID);
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, dataSize * sizeof(float), data, GL_STATIC_DRAW);
glVertexAttribPointer(attributeNumber, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
unsigned int copyOfVboID = vboID;
vbos.push_back(copyOfVboID);
}
void Loader::unbindVao() {
glBindVertexArray(0);
}
Rawmodel只是一个应该吸入浮子并创建VBO和VAO的类。我正在使用的向量vbos
和vaos
只是为了跟踪所有ID,因此使用所有数据完成后,我可以将它们删除。
我90%的人相信这都应该正常工作。但是,当我尝试运行一些绘制它的代码时,OpenGL即将退出,因为它正在尝试从地址0x0000000读取,并且不喜欢那样。我将我从代码创建的原始模型传递到看起来像这样的渲染器中的函数中:
void Renderer::render(RawModel* model) {
glBindVertexArray(model->getVaoID());
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, model->getVertexCount());
glDisableVertexAttribArray(0);
glBindVertexArray(0);
}
我已经检查以确保当我创建VAO时,以及试图检索VAO时的vaoid相同。实际上是一样的。
我不知道如何读取当前在OpenGL当前以Vertex Attrab阵列绑定的任何地址存储的地址,因此我无法测试它是否指向顶点数据。我很确定它是由于某种原因指向0的指向。
编辑:
事实证明,不是硬编码0是一个问题。它删除了Visual Studio和OpenGL给我的错误,但实际错误在其他地方。我意识到我在上面的某些代码中将vaoID
作为attributeNumber
传递,当时我应该通过硬编码0。我在此处编辑了我的代码:
RawModel* Loader::loadToVao(float* positions, int sizeOfPositions) {
unsigned int vaoID = this->createVao();
this->storeDataInAttributeList(0, positions, sizeOfPositions);
this->unbindVao();
return new RawModel(vaoID, sizeOfPositions / 3);
}
我将行this->storeDataInAttributeList(vaoID, positions, sizeOfPositions);
更改为您在上面看到的。但是,在更改后效果很好。
您应该使用glVertexAttribPointer
,glEnableVertexAttribArray
和glDisableVertexAttribArray
使用顶点属性索引,但是您所拥有的是:
-
glVertexAttribPointer
使用的VAO ID - 用
glEnableVertexAttribArray
和glDisableVertexAttribArray
使用的硬编码0
(如果您确定该值,这不一定是错误)
如果您不确定索引值(例如,如果您不指定着色器中的布局),则可以使用glGetAttribLocation
调用:
// The code assumes `program` is created with glCreateProgram
// and `position` is the attribute name in your vertex shader
const auto index = glGetAttribLocation(program, "position");
然后,您可以将index
与上述呼叫一起使用。
- 助记符和指向成员语法的指针
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 调整大小后指向元素值的指针unordered_map有效?
- 使用指向成员的指针将成员函数作为参数传递
- 指向指向字符数组的指针数组的指针
- 指向多维数组C++的指针
- 删除指向指针的指针是运行时错误吗
- 指向设备对象成员的指针
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 通过指向指针数组的指针访问子类的属性
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- C++,指针数组,指向双链表中的条目
- 返回的指向C++对象的链接是什么样的
- 指针没有更新它在void函数内部指向的值
- 初始化或分配空字符串文字到指向 C 中的 char 的指针或指向 C++ 中 const char 的指针的原因是什么
- 如果C++对象的类在另一个boost模块中声明,如何使用boost将指向该对象的指针返回到python
- 在C++中,如何初始化指向wchar_t*的指针数组(生成wchar_t**)
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- 出于某种原因,OpenGL VAO指向0