Yet another How to find Normal of a triangle thread!

Started by
5 comments, last by OmniscientN00b 16 years, 10 months ago
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
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

<br>[v2x-v1x  v2y-v1y    v2z-v1z]<br>[v2x-v3x  v2y-v3y    v2z-v3z]<br></pre><br><br>= ((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<br><br>So the x portion of the vector is: ((v2y-v1y)*(v2z-v3z)-(v2z-v1z)*(v2y-v3y))<br>The y portion is: -((v2x-v1x)*(v2z-v3z)-(v2z-v1z)*(v2x-v3x))<br>The z portion is: ((v2x-v1x)*(v2y-v3y)-(v2y-v1y)*(v2x-v3x))<br><br>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')<br><br>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:<br><br>sqrt(x*x + y*y + z*z)<br><br>
[google] "normal to triangle"

http://www.gamedev.net/community/forums/topic.asp?topic_id=125426

-me
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.
Cross Product
<span class="smallfont">That is not dead which can eternal lieAnd with strange aeons even death may die.   -- "The Nameless City" - H. P. Lovecraft</span>
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.
We''re sorry, but you don''t have the clearance to read this post. Please exit your browser at this time. (Code 23)
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.



This topic is closed to new replies.

Advertisement