Sign in to follow this  
crshinjin

Messed up lighting

Recommended Posts

Hi! I'm writing a sort of cloth simulation program, but I have run into some problems while trying to "turn the lights on". It's okay, as long as the surface is flat, as you can see below: Free Image Hosting at www.ImageShack.us But when the cloth starts to fall, sharp edges appear in the shading: Free Image Hosting at www.ImageShack.us The little grey lines are the vertex normals. I'm drawing the cloth using GL_TRIANGLES, but using the same normal for a single vertex every time, so I don't get it how is it possible to get different lit colour when a vertex used in different triangle, as it apparently happens. Any ideas would be appreciated. shinjin

Share this post


Link to post
Share on other sites
Are you defining your normals on a per-vertex of per-triangle basis? The pertinent code would help, too.

Also, just to make sure I see it right, is the lower right corner the one falling in the second shot?

Egészségedre!
-jouley

Share this post


Link to post
Share on other sites
You are right, the lower right corner is falling.

At each timestep I recalculate the face normals and accumulate them for each vertex in each face. Then I normalize the normals, and finally output the normals along with the vertices.


glBegin(GL_TRIANGLES);
vector<face>::iterator it, endit = faces.end();
vector<vector3>::iterator nit, nendit = normals.end();
for (nit = normals.begin(); nit!=nendit; ++nit)
*nit = vector3::ZERO;
for (it = faces.begin(); it!=endit; ++it)
{
vector3 n = it->calcNUNormal(); // calculate non-unit normal of the triangle
normals[it->a_idx] += n;
normals[it->b_idx] += n;
normals[it->c_idx] += n;
}
for (nit = normals.begin(); nit!=nendit; ++nit)
nit->normalise();
for (it = faces.begin(); it!=endit; ++it)
{
glNormal3fv(normals[it->a_idx].val);
glVertex3fv(it->a->val);
glNormal3fv(normals[it->b_idx].val);
glVertex3fv(it->b->val);
glNormal3fv(normals[it->b_idx].val);
glVertex3fv(it->c->val);
}
glEnd();



Köszi, :D
shinjin

Share this post


Link to post
Share on other sites
Sorry it's taken so long to get back to you, I got slammed with a bunch of work to get done. I haven't looked into it with much detail, but if you haven't solved this one yet, you may look into this region, particularly the normal you assign for the last vertex:
for (it = faces.begin(); it!=endit; ++it)
{
glNormal3fv(normals[it->a_idx].val);
glVertex3fv(it->a->val);
glNormal3fv(normals[it->b_idx].val);
glVertex3fv(it->b->val);
glNormal3fv(normals[it->b_idx].val);
glVertex3fv(it->c->val);
}
I imagine it worked when the cloth was flat because all the normals were the same, then.

-jouley

Share this post


Link to post
Share on other sites

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