#### Archived

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

# Normal Calculation Fails in some cases...

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

## Recommended Posts

hi, my normal calculation works only fine if i define my vertex data as follows: v[4] = {-1, -1, -1}, {1, -1, -1}, {1, 1, -1}, {-1, 1, -1} but it does not work properly for cases like this one: v[4] = {0, 0, -1}, {2, 0, -1}, {2, 2, -1}, {0, 2, -1} the normal is still perpendicular, but in the opposite direction what the hell should i do? thankful christian

##### Share on other sites
here is the source code for calculatiing the normals:

 void ReduceToUnit(GLfloat vector[3]){ float length; length = (float)sqrt((vector[0]*vector[0]) + (vector[1]*vector[1]) + (vector[2]*vector[2])); if(length == 0.0f) length = 1.0f; vector[0] /= length; vector[1] /= length; vector[2] /= length;}void calcNormal(float v[3][3], float out[3], bool toUnit){ float v1[3],v2[3]; static const int x = 0; static const int y = 1; static const int z = 2; v1[x] = v[0][x] - v[1][x]; v1[y] = v[0][y] - v[1][y]; v1[z] = v[0][z] - v[1][z]; v2[x] = v[1][x] - v[2][x]; v2[y] = v[1][y] - v[2][y]; v2[z] = v[1][z] - v[2][z]; out[x] = v1[y]*v2[z] - v1[z]*v2[y]; out[y] = v1[z]*v2[x] - v1[x]*v2[z]; out[z] = v1[x]*v2[y] - v1[y]*v2[x]; if (toUnit) ReduceToUnit(out);}

another question: how can i avoid using the sqrt??
thanks!!

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002131
×