#### Archived

This topic is now archived and is closed to further replies.

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

## Recommended Posts

hi ppl i got a new problem with my app i have a 3d model and im tryin to light it nicely but i dont know how to calculate normals for each vertex i only calculate them for an entire face so it looks quite bad. if u got any ideeas pls reply /*ilici*/

##### Share on other sites
I think for each vertex you can average the normals of all the faces it is a part of and normalize the result.

##### Share on other sites
Here is the trick:

void Normalize(float *coords_src,float *coords_dest)
{
float m = (float)sqrt(coords_src[0] * coords_src[0] +
coords_src[1] * coords_src[1] +
coords_src[2] * coords_src[2]);

//Avoid dividing by 0
if(!m)
{
memset(coords_dest,0,sizeof(float) * 3);
return;
}

coords_dest[0] = coords_src[0] / m;
coords_dest[1] = coords_src[1] / m;
coords_dest[2] = coords_src[2] / m;
}

and now coords_dest will hold the normals for the vertex pointed by coords_src.

Hope this helps!

The PAIN is coming...this summer!!!In cinemas everywhere.

##### Share on other sites
quote:
Original post by Mihail121
Here is the trick:

void Normalize(float *coords_src,float *coords_dest)
{
float m = (float)sqrt(coords_src[0] * coords_src[0] +
coords_src[1] * coords_src[1] +
coords_src[2] * coords_src[2]);

//Avoid dividing by 0
if(!m)
{
memset(coords_dest,0,sizeof(float) * 3);
return;
}

coords_dest[0] = coords_src[0] / m;
coords_dest[1] = coords_src[1] / m;
coords_dest[2] = coords_src[2] / m;
}

and now coords_dest will hold the normals for the vertex pointed by coords_src.

Hope this helps!

The PAIN is coming...this summer!!!In cinemas everywhere.

Normalizing a vector, is not calculating the normal of a vertex !

##### Share on other sites
The best way todo it is to go through every vertex for every face in the model and check if its a neighbour by these conditions.

- The triangle contains a vertex that has the same location
- The angle between the normals of the triangle that the vertex belongs and the triangle that you are calculating is more than 90
(*or what ever angle you want)

This will make sharp edges (eg side of a cube) remain sharp and curved objects remain curved...

You then add the Normal of the Vertex''s triangle that you are comparing to the Vertex''s normal...

once you have gone through the whole model you normalize the vector

##### Share on other sites
ok thnx everybody i solved it by adding the normals of every face the vertex is on and normalizing the result

/*ilici*/

1. 1
2. 2
3. 3
4. 4
Rutin
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013550
×