# Find vertex normal of unordered triangles on a mesh

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

## Recommended Posts

I have a mesh without any adjacent data (Index buffer), Can i find all its vertex normals (with Index buffer)? Also , can i have some examples or websites for this kind of thing? thx

##### Share on other sites
Hi,

the algorithm for calculating smooth vertex normals for a given triangle list looks like this (Pseudocode!):

for each triangle (v1,v2,v3) in mesh   face_normal = (v2-v1) cross (v3-v1)   normalize face_normal   for all points in triangle      store face_normal    endendfor each vertex v1 in mesh   vertex_normal = 0;   counter = 0;   for each vertex v2 in mesh      if position of v1 equals position of v2         vertex_normal += normal of v2         counter += 1      end if   end   vertex_normal /= counter;   normalize vertex_normal   store vertex_normal in v1end

However this can be optimized heavily. Don't forget to use a reliable epsilon to check vertex position differences against. If you have an Indexbuffer, simply access the Vertexbuffer indirectly by iterating through the index data.
Hope this helps you!

Best regards,
Porthos

##### Share on other sites
The method i tend to use is this:
Essentialy, you have all vertex normals initialised to 0, you then loop through all triangles calculating the triangle normal, and add this to each of the vertice's normals for that triangle.

You then loop through all vertices normalising their normals.

vertex normals initialised to 0

for each triangle
calculate triangle normal
add triangle normal to each vertice's normal in triangle

for each vertex
normalise normal

##### Share on other sites
wt is the difference between Porthos's method and luca-deltodesco's method?
which one is faster?

##### Share on other sites
i just reread the topic starters post and realised that ofcourse, mine is not suitable for his needs as it assumes that vertices are shared between triangles properly.

##### Share on other sites
Quote:
 Original post by PorthosHi,the algorithm for calculating smooth vertex normals for a given triangle list looks like this (Pseudocode!):for each triangle (v1,v2,v3) in mesh face_normal = (v2-v1) cross (v3-v1) normalize face_normal for all points in triangle store face_normal endendfor each vertex v1 in mesh vertex_normal = 0; counter = 0; for each vertex v2 in mesh if position of v1 equals position of v2 vertex_normal += normal of v2 counter += 1 end if end vertex_normal /= counter; normalize vertex_normal store vertex_normal in v1endHowever this can be optimized heavily. Don't forget to use a reliable epsilon to check vertex position differences against. If you have an Indexbuffer, simply access the Vertexbuffer indirectly by iterating through the index data.Hope this helps you!Best regards,Porthos

I also have this kind of problem.
how to optimized it?

The second part need to process each point ,and each loop search from the begin to the end , it will be quite slow.

will it be faster when i create a index buffer for it?
But...How?

##### Share on other sites

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