PDA

View Full Version : Describing Indice Pattern



DanielSaucen
02-23-2012, 06:49 PM
Hello all,

I've been having some problems with the COLLADA format and describing index info from buffers and file.
I have been messing with attrib pointers for a while to no avail, I'm hoping someone can shed some light on this.

One Triangle:
V1 N1 T1 V2 N2 T2 [B]V3 N3 T3
(Same as)
[Vertex,Normal,Texcoord] * 3
(and)
<input offset="0" semantic="VERTEX"...
<input offset="1" semantic="NORMAL"...
<input offset="2" semantic="TEXCOORD" ... set="0"...

How exactly to I specify the above pattern to glDrawElements?
My draw code follows:




glEnableClientState(GL_VERTEX_ARRAY);

// Vertex data
Buffer.Bind(VERTEX);
glVertexPointer(3, GL_FLOAT,0, 0);


// Normal data
Buffer.Bind(NORMAL);
glNormalPointer(GL_FLOAT, 0, 0);


// Texture coordinates
Buffer.Bind(TEXCOORD);
glTexCoordPointer(2, GL_FLOAT, 0, 0);

// Index Data
Buffer.Bind(INDICE);

glDrawElements(GL_TRIANGLES, Buffer.Get_BufferCouple_ByType(INDICE).Count, GL_UNSIGNED_INT, 0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);



code for uploading:



void CVertexBuffer::Buffer_From_Mesh_V(CMesh* Mesh)
{
// for forum reference (not in block)
void Upload(Bind_Type Type,int count,float* Data)
{
GLenum EN = Get_BindENUM(Type);
int i = Get_Buffer_Index_ByType(Type);

Buffers[i].Count = count;
glBindBuffer(EN, Buffers[i].Buffer);
glBufferData(EN, count*sizeof(float), Data,GL_STATIC_DRAW);

glBindBuffer(EN, 0);
}



// gen 4 buffers (can now reference them by type)
Buffer.Generate(new BindCouple(VERTEX,NORMAL,TEXCOORD,INDICE));



Buffer.Upload(VERTEX,Mesh->VERTEX.mCount,Mesh->VERTEX.mData);
Buffer.Upload(NORMAL,Mesh->NORMAL.mCount,Mesh->NORMAL.mData);
Buffer.Upload(TEXCOORD,Mesh->TEXCOORD.mCount,Mesh->TEXCOORD.mData);

Buffer.Upload(INDICE,Mesh->Indicies_count,Mesh->Indicies);


Any help would be much appreciated.

BionicBytes
02-24-2012, 07:22 AM
Oh boy. There are various thing wrong with your code.
You have mixed up legacy vertex arrays with vertex buffer objects and not enabled the correct client state.
You are better off ditching the legacy vertex arrays in favor of the GL 2.0+ generic vertex arrays which are generally more flexible. You WILL need shaders to use these generic vertex attibutes however.


Let's start with this part:


glEnableClientState(GL_VERTEX_ARRAY);

// Vertex data
Buffer.Bind(VERTEX);
glVertexPointer(3, GL_FLOAT,0, 0);


// Normal data
Buffer.Bind(NORMAL);
glNormalPointer(GL_FLOAT, 0, 0);


// Texture coordinates
Buffer.Bind(TEXCOORD);
glTexCoordPointer(2, GL_FLOAT, 0, 0);



I'm going to assume you want each attribute in a separate buffer object - as that's what your code kinda looks like.


The above code becomes...


glBindBuffer(GL_ARRAY_BUFFER, VBO_vertex); //VBO is the buffer object for Vertex data
glenableVertexAttribArray (0); //generic attribute index #0
glVertexAttribPointer (0, 3,GL_FLOAT, GL_FALSE, 0, pointer(0));


glBindBuffer(GL_ARRAY_BUFFER,VBO_Normal);
glenableVertexAttribArray ( 1 ); //generic attribute index #1
glVertexAttribPointer (1, 3,GL_FLOAT, GL_FALSE, 0, pointer(0))

glBindBuffer(GL_ARRAY_BUFFER,VBO_Texcoord);
glenableVertexAttribArray ( 2 ); //generic attribute index #2
glVertexAttribPointer (1, 2,GL_FLOAT, GL_FALSE, 0, pointer(0))



Next you could upload your element indicies into a buffer object too by using
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, Indices.iARBBuffer);
with the draw call becoming:
glDrawElements (GL_TRIANGLES, numTrianglesToDo, GL_UNSIGNED_INTEGER, pointer(0) );

Disabling the state is as easy as:


glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

gldisableVertexAttribArray (0); //vertex
gldisableVertexAttribArray (1); //normal
gldisableVertexAttribArray (2); //texcoord0

DanielSaucen
02-24-2012, 07:20 PM
Many thanks for the help, I knew that I'd eventually have to use "glenableVertexAttribArray", but wasn't entirely sure on the use.
You've also covered disabling states, another question I was going to ask.

It's great to finally have a tailored example.

BionicBytes
02-25-2012, 09:06 AM
no problem.
Just noticed a slight cut/paste error on my previous post.
It should read:

glBindBuffer(GL_ARRAY_BUFFER,VBO_Texcoord);
glenableVertexAttribArray ( 2 ); //generic attribute index #2
glVertexAttribPointer (2, 2,GL_FLOAT, GL_FALSE, 0, pointer(0))

as the first parameter to glVertexAttribPointer is the generic index used - which should match the enable state.

V-man
02-26-2012, 05:14 AM
You aren't forced to use the generic vertex attrib calls.
One example is here
http://www.opengl.org/wiki/Vertex_Buffer_Object

However, using generic vertex attrib and shaders is a good idea.

http://www.opengl.org/wiki/VBO_-_just_examples