在 c++ 和 openGL4 中我可以做这样的事情
std::vector<Vertex> vertices;
其中 Vertex 是一个包含相关的每个顶点数据的类。
this->vertices.pushback(Vertex())
....define the rest of the vertices and set position and color data etc
//Opengl code
glBindBuffer(GL_ARRAY_BUFFER, this->vboID[0]);
glBufferData(GL_ARRAY_BUFFER, ( this->vertices.size() * sizeof(Vertex) ) , this->vertices.data(), GL_STATIC_DRAW);
glVertexAttribPointer((GLuint)0, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid *)0); // Set up our vertex attributes pointer
glEnableVertexAttribArray(0);
这工作正常并显示我正在渲染的内容。现在,如果我尝试制作 vector
std::vector<Vertex*> vertices;
this->vertices.push_back(new Vertex());
....
然后我想显示的形状永远不会显示。
我的问题是,因为我使用了数据不再连续的指针,所以 opengl 无法读取顶点数据,或者是否可以更改 openGL 代码以接受指针 vector ?
最佳答案
当然可以。在第一个版本中,您将实际的 Vertex 实例作为字节缓冲区传递给 OpenGL。在第二个版本中,您将指向 Vertex 实例的指针传递给 OpenGL,而 OpenGL 不会为您取消引用这些指针。
您需要使用第一个版本,无法将指向您的顶点的指针传递给 OpenGL。
关于c++ - 带有指针 vector 的 glbufferdata C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21611866/