vbo显示相同的对象
vbo displays same object
在我的项目中,我想使用vbo显示许多对象(球体)。我设法毫无问题地显示了1个对象,但当涉及到2个或更多时,所有对象(vbo)都会被最后定义的对象(vbo)替换。
CosmicBody(int x)
{
this->verticesSize=0;
//this->VaoId=x;
//this->VaoId=1;
this->VboId=x;
};
void CosmicBody::InitShape(unsigned int uiStacks, unsigned int uiSlices, float fA, float fB, float fC)
{
float tStep = (Pi) / (float)uiSlices;
float sStep = (Pi) / (float)uiStacks;
float SlicesCount=(Pi+0.0001)/tStep;
float StackCount=(2*Pi+0.0001)/sStep;
this->verticesSize=((int) (SlicesCount+1) * (int) (StackCount+1))*2;
glm::vec4 *vertices=NULL;
vertices=new glm::vec4[verticesSize];
int count=0;
for(float t = -Pi/2; t <= (Pi/2)+.0001; t += tStep)
{
for(float s = -Pi; s <= Pi+.0001; s += sStep)
{
vertices[count++]=glm::vec4(fA * cos(t) * cos(s),fB * cos(t) * sin(s),fC * sin(t),1.0f);
vertices[count++]=glm::vec4(fA * cos(t+tStep) * cos(s),fB * cos(t+tStep) * sin(s),fC * sin(t+tStep),1.0f);
}
}
glGenBuffers(1, &VboId);
glBindBuffer(GL_ARRAY_BUFFER, VboId);
glBufferData(GL_ARRAY_BUFFER, 16*verticesSize, vertices, GL_STATIC_DRAW);
glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);
glBindBuffer(GL_ARRAY_BUFFER, VboId);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
delete[] vertices;
}
void CosmicBody::Draw()
{
glBindBuffer(GL_ARRAY_BUFFER, this->VboId);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLE_STRIP, 0,this->verticesSize); //when I replace this->verticesSize with number of vertices of last object ,instead of getting x different objects I get same instances of the last one.
glDisableVertexAttribArray(0);
}
当您使用VAO时,您应该为绘图绑定VAO,而不是VBO缓冲区:
void CosmicBody::Draw()
{
glBindVertexArray( this->VaoId ); // <-- this is the difference
glDrawArrays(GL_TRIANGLE_STRIP, 0,this->verticesSize);
glBindVertexArray(0); // unbind the VAO
}
并在绑定VAO后移动CosmicBody::InitShape
中的glEnableVertexAttribArray(0);
,无需每次绘制时启用/禁用:
...
glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);
glEnableVertexAttribArray(0); // <-- here
glBindBuffer(GL_ARRAY_BUFFER, VboId);
...
相关文章:
- 对象初始化后在C++中显示 char 数组时的异常行为
- 为什么静态数组成员变量在调用对象的实例后不显示任何内容?
- 使用 CIN COUT 显示垃圾值的文件对象?
- IM 试图显示对象数组,但它给出了错误的输出
- 对象错误地显示为引用?
- C++ 矢量不显示包含对象的数据
- C++ csv 文件无法正确加载到矢量,它似乎知道对象在那里但不显示它们
- 如何在屏幕上显示对象列表?
- 为什么谷歌测试/模拟显示 std::unique_ptr 泄露的模拟对象错误?
- 是否有一个函数可以显示我创建的类中的对象到 QTextBrowser 中?
- 在同一屏幕上显示两个不同的对象
- 为什么析构函数和复制构造函数使用对象数组显示此行为
- 使用OpenGL显示两个对象.纹理不像预期的那样行为
- 如何以某种格式为用户显示对象向量
- 使用 main() 以外的其他函数显示类的对象,但在 main() 中调用;
- 如何使 UI 对象仅在单击按钮后显示在对话框中
- 当从此到子类中的新对象时,将显示警告"destination for this 'memcpy' call is a pointer to dynamic class..."
- 在 opengl 中应用转换函数后未显示对象
- Mac上的选定对象显示背景高亮显示,而不是边框高亮显示
- 如何在c++中使用单链表对象显示和添加数据