triangle normal vector

Started by
10 comments, last by lakibuk 23 years, 6 months ago
i build a small landscape with direct3d triangles. now i need the normal vectors for the triangles d3dvertex-objects. how can i calculate them ? sorry if this is in the tutorials,but i didnt find it.
Karl - Blueskied Games | my german site: Gratis Spiele
Advertisement
I don''t really know how D3D defines vectors and such, but assuming they look something like:

struct vector3 {
float x;
float y;
float z;
}

And assuming a triangle is defined by 3 vector3''s (and texture-coords and stuff, of course) named v1, v2, and v3 you can do this:
    vector3 normal;normal = crossProduct (v2 - v1, v2 - v3);    


In effect you take the cross product of the vector (v2-v1) and the vector (v2-v3).

-Neophyte


- Death awaits you all with nasty, big, pointy teeth. -
thx,that formula was what i was looking for
Karl - Blueskied Games | my german site: Gratis Spiele
Remember, if you multiply them backwards then your normal''s facing the opposite way.

------------------------------
#pragma twice


sharewaregames.20m.com

another question: which normal vector do i get ? one pointing up or down ? does it make any difference ? i need the normal vectors for lighting.
Karl - Blueskied Games | my german site: Gratis Spiele
YOu get the right one by multiplying anticlockwise, ie:
(vertex 2 - vertex 1) x (vertex 2 - vertex 3)

I think that is correct. I could be wrong about that though, and it might be the other way round. If it goes wrong try it the other way.

------------------------------
#pragma twice


sharewaregames.20m.com

double post, hmmm....

-------------------------------
I'll screw up whoever screws around with the gamedev forum!

..-=gLaDiAtOr=-..

Edited by - furby100 on October 10, 2000 3:11:24 AM
I''m glad you''re the new moderator but I hope that your ego doesn''t grow and do stupid things like you just did a minute ago. Thanks. You can''t just edit my post and say whatever you feel like in there because I don''t want you to. Why did you have to offend me because I pointed out that you double posted? You''re the moderator, you should watch what you say if you want to stay there to be one.

-------------------------------
I'll screw up whoever screws around with the gamedev forum!

..-=gLaDiAtOr=-..
I misread you. If you were just trying to help, thankyou. I think you misread me as well, as just someone messing around. Look, you won't find anyone's posts edited. I was quite upset at the time anyway. I thought you were finding fault. Shall I delete these posts? They don't really help the thread much. I apologise Gladiator.

------------------------------
#pragma twice


sharewaregames.20m.com

Edited by - furby100 on October 10, 2000 3:34:54 AM

The usual way of computing the triangle normal is like this:

n = (v1 - v2) x (v1 - v3)

This is why counter-clockwise triangles are culled in D3D and clockwise triangles are culled in OGL. This plus the fact that D3D uses a lefthanded system whereas OGL uses a righthanded one.



- WitchLord

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement