# 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: But when the cloth starts to fall, sharp edges appear in the shading: 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 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 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 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 on other sites
Have you tried this:

It's a longshot, but if your normals are ok, which they appear to be, maybe you're using flat shading instead.

##### Share on other sites
Aaah! Darned copy-paste!

Thanks Jouley! I've been stuck with this project for ages because of this.

shinjin

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628336
• Total Posts
2982157

• 9
• 24
• 9
• 9
• 13