• Advertisement
Sign in to follow this  

Yet another How to find Normal of a triangle thread!

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

My mind over heating... since days... I've read numerous articles Is there a readily available function that returns a normal vertex taking in 3 vertices? void vertnormal(float v1x,float v1y,float v1z,float v2x,float v2y,float v2z,float v3x,float v3y,float v3z,float *nx,float *ny,float *nz) { //Here goes the code that does all calculations which I donot know! :< *nx = valuex; *ny = valuey; *nz = valuez; } Could someone post such a similar function? Thankxs

Share this post


Link to post
Share on other sites
Advertisement
Three vertices form two vectors, right?
The cross product of two vectors forms the normal.

So, vector 1 = <v2x-v1x, v2y-v1y, v2z-v1z>
And, vector 2 = <v2x-v3x, v2y-v3y, v2z-v3z>

Then, take the cross product


[ i j k ]
[v2x-v1x v2y-v1y v2z-v1z]
[v2x-v3x v2y-v3y v2z-v3z]


= ((v2y-v1y)*(v2z-v3z)-(v2z-v1z)*(v2y-v3y))i - ((v2x-v1x)*(v2z-v3z)-(v2z-v1z)*(v2x-v3x))j + ((v2x-v1x)*(v2y-v3y)-(v2y-v1y)*(v2x-v3x))k

So the x portion of the vector is: ((v2y-v1y)*(v2z-v3z)-(v2z-v1z)*(v2y-v3y))
The y portion is: -((v2x-v1x)*(v2z-v3z)-(v2z-v1z)*(v2x-v3x))
The z portion is: ((v2x-v1x)*(v2y-v3y)-(v2y-v1y)*(v2x-v3x))

Of course, the issue is that the vector can go two ways (positive and negative direction), so make sure you know which way your triangle is facing (as in, which way is 'out')

Also, if you want to 'normalize' (make of size 1) the vector, divide each part of the vector by the size of the vector, which is:

sqrt(x*x + y*y + z*z)

Share this post


Link to post
Share on other sites
I donot do OPENGL or direct x programming...

I just need a plain C function.. More over I am not so goood in vectors.. I donot know what cross product is.

Hope someone helps.

Share this post


Link to post
Share on other sites
Quote:
Paraphrased from visage
x = (v2y-v1y)*(v2z-v3z)-(v2z-v1z)*(v2y-v3y);
y = -(v2x-v1x)*(v2z-v3z)+(v2z-v1z)*(v2x-v3x);
z = (v2x-v1x)*(v2y-v3y)-(v2y-v1y)*(v2x-v3x);

r = sqrt(x*x + y*y + z*z);

x /= r;
y /= r;
z /= r;

Quote:
Original post by OmniscientN00b
Hope someone helps.


Somebody did.

Share this post


Link to post
Share on other sites
Quote:
Original post by erissian
x = (v2y-v1y)*(v2z-v3z)-(v2z-v1z)*(v2y-v3y);
y = -(v2x-v1x)*(v2z-v3z)+(v2z-v1z)*(v2x-v3x);
z = (v2x-v1x)*(v2y-v3y)-(v2y-v1y)*(v2x-v3x);

r = sqrt(x*x + y*y + z*z);

x /= r;
y /= r;
z /= r;

Quote:
Original post by OmniscientN00b
Somebody did.


So those are the normal vertices right?
Excellent I will try those.



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement