PDA

View Full Version : VBO with multiple objects



Cucus
05-13-2011, 12:27 PM
Hi,

I need to use VBO method in a program where I need to draw 7 joints of a robot. Those joints are, each one, an object of "Joint" class.

This class has a function to initialize the arrays:


void Joint::initArray()
{
vertexArray = new GLfloat[sizeVertex];
normalArray = new GLfloat[sizeNormal];
Vertex v;
Vertex n;
int i = 0;
int j;
for(j=0;j<vertexVector.size();j++)
{
v = vertexVector[j];
vertexArray[i] = v.x;
vertexArray[i+1] = v.y;
vertexArray[i+2] = v.z;
i+=3;
}

i = 0;
for(j=0;j<normalVector.size();j++)
{
n = normalVector[j];
normalArray[i] = n.x;
normalArray[i+1] = n.y;
normalArray[i+2] = n.z;
i+=3;
}

GLuint vertexs,normals;


glGenBuffers(1,&amp;vertexs);
glBindBuffer(GL_ARRAY_BUFFER,vertexs);
glBufferData(GL_ARRAY_BUFFER,sizeVertex*sizeof(GLf loat),vertexArray,GL_DYNAMIC_DRAW);
glVertexPointer(3,GL_FLOAT,3*sizeof(GLfloat),0);

glGenBuffers(1,&amp;normals);
glBindBuffer(GL_ARRAY_BUFFER,normals);
glBufferData(GL_ARRAY_BUFFER,sizeNormal*sizeof(GLf loat),normalArray,GL_DYNAMIC_DRAW);
glNormalPointer(GL_FLOAT,3*sizeof(GLfloat),0);
}


After that, I have a render function:


void Joint::render()
{
glEnableClientState(GL_VERTEX_ARRAY);
glDrawArrays(GL_TRIANGLES,0,sizeVertex);
glDisableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_NORMAL_ARRAY);
glDrawArrays(GL_TRIANGLES,0,sizeNormal);
glDisableClientState(GL_NORMAL_ARRAY);

}


I think that everything is ok but the program just draw the first join. Do you know where's the problem?
I thinnk it's in the buffer's name but in this case, I don't know how to solve it. Can someone help me plz?

Thanks

Aleksandar
05-13-2011, 02:17 PM
Please go to wiki or elsewhere where you can learn how VBO works.
Whenever you issue glDrawArray() you are actually drawing vertices. All enabled attributes are used.

By the way, you haven't set attribute pointers (with glVertexPointer and glNormalPointer)! You have to bind appropriate VBO (since you are using two separate for vertices and normals), and while it is active call appropriate gl*Pointer function. You can enable attributes before of after those calls, but certainly before glDrawArray.

Everything mentioned above considers using fixed functionality. It is a little bit different with shaders. (I assume you are using fixed functionality since you are calling glEnableClientState).