Sign in to follow this  
Darkbouncer4689

Calculating outward pointing normals

Recommended Posts

Just a quick question here.

If I calculate my normals with something like

Vector3 p1 = vertices[indices[i+0]];
Vector3 p2 = vertices[indices[i+1]];
Vector3 p3 = vertices[indices[i+2]];

Vector3 v1 = p2 - p1;
Vector3 v2 = p3 - p1;
Vector3 normal = v1.Cross( v2 );

Will the normal vector be guaranteed to be pointing outward and not inward?

I must admit I normally just plug and chug the normals from 3d files, but for terrain I need to calculate my own normals =)

Thanks for the help!

Share this post


Link to post
Share on other sites
japro    887
[quote name='Darkbouncer4689' timestamp='1315039046' post='4857065']
Will the normal vector be guaranteed to be pointing outward and not inward?

I must admit I normally just plug and chug the normals from 3d files, but for terrain I need to calculate my own normals =)
[/quote]

There isn't really a "outward" direction for a single triangle... if you swap any two vertices the the direction of what is the cross product changes its sign. You have to ensure that all the triangles are "wound" the same way when you pass them to the normal calculation.

Share this post


Link to post
Share on other sites
I think I understand how it's done now. If all of the triangles are CW or CCW the outward pointing normals can be calculated correctly.

My terrain is rendered using triangle_strips, which changes the ordering of the triangles for each row. So I think in order to calculate my normals I need to swap the way I calculate the normals each time I change rows (which changes CW/CCW ordering).

I intended to optimize my index buffer for gpu cache coherency in the future. I suppose I should look into that before I hard code how my normals are calculated. Any tips on that are appreciated =)

Share this post


Link to post
Share on other sites
ProfL    701
[quote name='Darkbouncer4689' timestamp='1315205437' post='4857731']
I think I understand how it's done now. If all of the triangles are CW or CCW the outward pointing normals can be calculated correctly.

My terrain is rendered using triangle_strips, which changes the ordering of the triangles for each row. So I think in order to calculate my normals I need to swap the way I calculate the normals each time I change rows (which changes CW/CCW ordering).

I intended to optimize my index buffer for gpu cache coherency in the future. I suppose I should look into that before I hard code how my normals are calculated. Any tips on that are appreciated =)
[/quote]
keeping the order is especially important to have backface culling running, this saves you 50% of the rasterized pixel, low hanging fruit everyone should use (except on PS2 :D)

Share this post


Link to post
Share on other sites
ProfL    701
[quote name='Darkbouncer4689' timestamp='1315254649' post='4857950']
Thanks for the tip. I know about backface culling but I have never designed a mesh to take advantage of it. Should I not be using triangle_Strips for terrain?
[/quote]
backface culling works with triangle strips without a problem, for your normal calculation you just need to flip it every second triangles, if you use the raw strip indices.

enable backface culling, you'll see if something has a wrong orientation. if everything looks fine, then yournormal calculation code shall work.


Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this