Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


How to apply vertex normals in VBO, Vertex array working perfect but VBO?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 luckyyyyyy   Members   -  Reputation: 144

Like
0Likes
Like

Posted 11 March 2012 - 09:01 AM

I don't know what's wrong here, everything is working fine except normals. When I use vertex array, model looks perfect but when I switch to VBO, model looks worse because of vertex normals. I spent a lot of time to fix it but do not know what's wrong. VBO generation seems perfect. but still do not know. any idea?


#define BUFFER_OFFSET(i) ((char *)NULL + (i))

void InitVBO()
{

glGenBuffers(1, &vboNormID);
glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, VertNormals);
glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));

glGenBuffers(1, &vboVertID);
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, p_VERTICES);
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));


glGenBuffers(1, &indexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLFace)*nb_Faces, p_indices);

//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, p_indices, GL_DYNAMIC_DRAW);}


Rendering code is followed. (VBO + Vertex Array). Vertex array is working perfect. I can see the perfect shape of model with vertex normals, but with VBO there is some issue with vertex normals. I think I am doing something wrong with BUFFER_OFFSET(12).


void RenderTringularModel(GLvoid)
{
if(VertNormals && !MESH_SMOOTH)
{
	glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
	glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);

	glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_VERTEX_ARRAY);

	glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));
	glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));

	glDrawElements(GL_TRIANGLES, nb_Faces*3, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glPopClientAttrib();

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

}
else
{
	//glShadeModel (GL_FLAT);
	glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), p_VERTICES);
	glNormalPointer(GL_FLOAT,  sizeof(GLpoint), VertNormals);
	glDrawElements(GL_TRIANGLES, 3*nb_Faces, GL_UNSIGNED_INT, p_indices);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glPopClientAttrib();
}

Posted Image

Sponsor:

#2 shacktar   Members   -  Reputation: 809

Like
0Likes
Like

Posted 11 March 2012 - 10:47 AM

I think I am doing something wrong with BUFFER_OFFSET(12).


Yes, you would only do that if you wanted to skip the first normal for some reason. It looks like you would also want it to be BUFFER_OFFSET(0), here. BUFFER_OFFSET(12) is also probably causing the normal pointer to go out of bounds.

glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);

glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
glEnableClientState(GL_NORMAL_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);

glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12)); // should be BUFFER_OFFSET(0)
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));
...


You're calling glNormalPointer and glVertexPointer both with vboVertID bound. This means that the normals are coming from vboVertID as well. Try this instead:

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);

glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT);

glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));

glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));

...





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS