VBO отображает тот же объект

В моем проекте я хочу отобразить много объектов (сфер), используя 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);
}

1

Решение

Когда вы используете VAO, вы должны связать ВАО для рисования, а не буфера VBO:

void CosmicBody::Draw()
{
glBindVertexArray( this->VaoId );  // <-- this is the difference
glDrawArrays(GL_TRIANGLE_STRIP, 0,this->verticesSize);
glBindVertexArray(0);  // unbind the VAO
}

И переместить glEnableVertexAttribArray(0); в CosmicBody::InitShape после привязки VAO нет необходимости включать / отключать его каждый раз, когда вы рисуете:

...
glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);
glEnableVertexAttribArray(0);  // <-- here
glBindBuffer(GL_ARRAY_BUFFER, VboId);
...
5

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector