PDA

View Full Version : Base vertex and VertexAttribBinding



ajith.mk
01-03-2016, 08:23 AM
Hi everyone,

I have a struct Vertex{glm::vec4 t,n,v;}. I have written a obj loader that takes two parameters, obj file path as string and reference to a vector of 'Vertex'es. This function populates my vector and returns the number of indices(In my case indices are just sequential numbers, anyway).

As I have 6 objects to render, after using that function 6 times I have the following


vector<Vertex> objects[6];
GLint SIZES[6],OFFSETS[6],SIZES_I[6],OFFSETS_I[6];

filled. SIZES are number of 'Vertex'es(object[i].size()) and SIZES_I are number of indices. The offsets are calculated as below

for(int i=0;i<6;i++)
{
if(i==0)
OFFSETS[0]=0;OFFSETS_I[0]=0;
OFFSETS[i]=OFFSETS[i-1]+SIZES[i-1];
OFFSETS_I[i]=OFFSETS_I[i-1]+SIZES_S[i-1];
}

I transferred vectors of Vertex into a single VBO all back to back. Similarly for indices, transferred into buffer bound to element array buffer. That part is shown below.

glBufferData(GL_ARRAY_BUFFER,(OFFSETS[5]+SIZES[5])*sizeof(Vertex),data,GL_STATIC_DRAW);
for(int i=0;i<6;i++)
glBindVertexBuffer(i,buffer[0],OFFSETS[i]*sizeof(Vertex),sizeof(Vertex));
glBufferData(GL_ELEMENT_ARRAY_BUFFER,(OFFSETS_I[5]+SIZES_I[5])*sizeof(GLuint),indices,GL_STATIC_DRAW);
glVertexAttribFormat(0,4,GL_FLOAT,GL_FALSE,offseto f(Vertex,v));

Now comes my problem. Of the two rendering codes shown below the first one doesn't work but the second works perfectly.

for(int i=0;i<6;i++)
{
glVertexAttribBinding(0,i);
glDrawElements(GL_TRIANGLES,SIZES_I[i],GL_UNSIGNED_INT,reinterpret_cast<void*>(OFFSETS_I[i]*sizeof(GLuint));
}

//second

glVertexAttribBinding(0,0);
for(int i=0;i<6;i++)
glDrawElementsBaseVertex(GL_TRIANGLES,SIZES_I[i],GL_UNSIGNED_INT,reinterpret_cast<void*>(OFFSETS_I[i]*sizeof(GLuint)),OFFSETS[i]);

The second code is more elegant (may be, you guys have much more elegant solution?), but I am concerned as to why my first code is not working. May be I dint understand bindings. They are so useful that eventually I may have to work with them. It's drawing partially finished models and some models not at all. To summarily say what I have done so u guys can understand whats going on here, in the first case i created 6 buffer bindings on the same buffer with 6 offsets. In the second case there is only one binding, but I used base vertex to offset 6 times. Btw both are compiling, so ignore any typos as I typed all this in my tab.

GClements
01-03-2016, 11:13 AM
Are you using any attributes other than zero? If you are, you need to set the binding for each one. If you're using glVertexPointer(), that will change the binding point whose index matches the attribute index.

ajith.mk
01-03-2016, 09:23 PM
Hmm... I would be happy if the above code is conceptually correct. I always thought that base vertex is designed exactly to take away those binding calls unless the binding call changes the buffer itself.

GClements
01-04-2016, 04:16 AM
Hmm... I would be happy if the above code is conceptually correct.
By itself, it appears to be; but that's not the entire program. There are things which might occur in the rest of the program which would cause the first approach not to work while the second approach works.