【问题标题】:vbo displays same objectvbo 显示相同的对象
【发布时间】:2012-09-12 11:34:18
【问题描述】:

在我的项目中,我想使用 vbo 显示许多对象(球体)。 我设法毫无问题地显示 1 个对象,但是当涉及 2 个或更多时,所有对象(vbos)都被最后定义的对象(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);
}

【问题讨论】:

  • 我觉得显示列表会更好。您可以在显示列表中创建一个球体,然后轻松地在世界任何地方多次显示它。
  • @Rookie,你可以用 VBO 做同样的事情
  • @Rookie 是的,我曾经使用显示列表,但它们已被弃用。

标签: c++ opengl vbo


【解决方案1】:

当您使用 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);
...

【讨论】:

  • 好的,不过你也可以把EnableVertexAttribArray移到设置VAO的地方,画完后绑定0可能是个好主意
  • @harold 我想把它包括在答案中......也可以:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-10
  • 1970-01-01
相关资源
最近更新 更多