Sign in to follow this  

VBOs/vertex normal calculation help!

This topic is 3311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

EDIT: scroll way down please! (post #7) _______________________________________ This is a continuation of the following thread: http://www.gamedev.net/community/forums/topic.asp?topic_id=511520 Sorry for bringing this one up so late but I was in no shape to answer any sooner :( So here's what I've done to calculate, store and 'feed' my vertex normals into the VBO: [From the previous thread: the idea is to add up the non-normalized normals for the corresponding vertices and then normalize them.]
for(k=0;k<numVerts;k++)
	{
		normals.push_back( 0.0 );
		normals.push_back( 0.0 );
		normals.push_back( 0.0 );
	}

	for(k=0;!prim.eof();k++)
	{
		GLuint x, a, b, c;
		prim>>x>>a>>b>>c; //just ignore the x :P

		float* f = getNormal3fv ( &verts[a], &verts[b], &verts[c] );

		normals[3*a]  +=f[0];
		normals[3*a+1]+=f[1];
		normals[3*a+2]+=f[2];
		normals[3*b]  +=f[0];
		normals[3*b+1]+=f[1];
		normals[3*b+2]+=f[2];
		normals[3*c]  +=f[0];
		normals[3*c+1]+=f[1];
		normals[3*c+2]+=f[2];

		indices.push_back( a );
		indices.push_back( b );
		indices.push_back( c );
	}

	for(k=0;k<normals.size();k+=3)
	{
		norm( &normals[k] );
	}

The function getNormal3fv just calculates the cross product from two sides of the triangle. Maybe something wrong here?
	  float* getNormal3fv ( float* v1, float* v2, float* v3 )
	  {
		float* v = new float[3];
		float* u = new float[3];
		float* f = new float[3];

		u[0] = v1[0] - v2[0];
		u[1] = v1[1] - v2[1];
		u[2] = v1[2] - v2[2];

		v[0] = v1[0] - v3[0];
		v[1] = v1[1] - v3[1];
		v[2] = v1[2] - v3[2];

		crossP( u, v, f );

		delete u, v;
		return f;
	  }

The results are pretty odd: I decided I'd like to see the normal vertices, hence the wireframe. I'm pretty lost here. There's obviously something wrong here, I just can't seem to find it. Thanks for the replies everyone! [Edited by - glopen on November 19, 2008 1:44:11 AM]

Share this post


Link to post
Share on other sites
I think you should probably change the way you're computing your vectors:

u[0] = v2[0] - v1[0];
u[1] = v2[1] - v1[1];
u[2] = v2[2] - v1[2];

v[0] = v3[0] - v1[0];
v[1] = v3[1] - v1[1];
v[2] = v3[2] - v1[2];

so both u,v have the same common vertex

Share this post


Link to post
Share on other sites
Hey, thanks.

Well, I tried your version and it's pretty much the same. That's odd because it looks like the normals should've been inverted because your version of u,v is the opposite vectors of my u,v?

I'm wondering if there's something wrong with the storage. Is the data in an STL vector contiguous? Thanks.

Share this post


Link to post
Share on other sites
I noticed you also should delete your u,v arrays as:

delete []u;
delete []v;
u = NULL;
v = NULL;

I don't know, could it be because of the storage? (verts array) or because of the CrossP() function? double check that. And try dumping the verts array to be sure it has what you expect after you call getNormal3fv.

Share this post


Link to post
Share on other sites
Nope. Dumped all the data stored and they look alright. However:
	glBufferData(GL_ARRAY_BUFFER, normals.size()*sizeof(float), &normals[0], GL_STATIC_DRAW);


I'd imagine using &normals[0] would mean the data should continuous in the memory. It works for the vertex data so I don't know why it shouldn't work now.

Here's my crossP function, just in case:
	  void crossP(float* v1, float* v2, float v[3]) {
v[0] = v1[2]*v2[1]-v1[1]*v2[2];
v[1] = v1[2]*v2[0]-v1[0]*v2[2];
v[2] = v1[0]*v2[1]-v1[1]*v2[0];
}


I've double checked everything :( Thanks.

Share this post


Link to post
Share on other sites
Quote:
It should be v1[1] * v2[2] - v1[2] * v2[1];

Hey, I remember that! I was messing around with the code a lot, that one must've been overlooked :P Fixed it.

But that's not the problem. There must be something wrong with the algorithm itself. I think it's something like what's shown in this image:

From: This thread

My models are all triangles and the indices to the faces. I don't think the vertices are shared. I really think the problem is with how the vertex 'P' here is getting two face normals from the top and right faces (faces E,D,C&B) while only one from the front face (face A). Maybe that's the problem? If that is so, the idea is to add only one face normal for two coplanar faces that share the vertex, right? Any standard way of going about doing that?

Thanks.

Share this post


Link to post
Share on other sites

This topic is 3311 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this