**1**

# normal and wind order

###
#1
Members - Reputation: **211**

Posted 17 September 2011 - 10:03 AM

I have a triangle and his normal.

The triangle stores the wind order of vertexes for create it A,B,C.

Is possible to calculate if the triangle is backface triangle or a frontface triangle respect to a default conterclowise triangle with and A,B,C indexes?

thanks.

###
#2
Members - Reputation: **841**

Posted 17 September 2011 - 11:52 AM

Now, if you want to know the winding order of triangle ABC, you have to calculate the cross product AB X AC. If this is oriented towards you (can be checked with dot product), triangle if CCW.

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya

Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

###
#3
Members - Reputation: **211**

Posted 22 September 2011 - 05:48 AM

Normally if you already have triangle normal you do not need to do any other back-face/front face testing - if normal is towards you, triangle is front-faceing and vice versa.

Now, if you want to know the winding order of triangle ABC, you have to calculate the cross product AB X AC. If this is oriented towards you (can be checked with dot product), triangle if CCW.

thanks Lauris, but my project is an importer and i can't have my position. but

I have the normal of the plane of the triangle.

I have also the cartesian axes of the model.

Is possible do a dot product of the normal vector with one of the axis that compose the matrix of cartesian axis?

the problem is that there are 3 axes in the cartesian axis , with what axis i must do the dot product of the normal?

if the dot product of normal with the correct axes is < 0 i think that is a backface.

have you any advice ?

thanks.

by

###
#4
Members - Reputation: **211**

Posted 23 September 2011 - 03:48 AM

Thanks Lauris , but whats mean "towards you" if i don't have a camera position?if normal is towards you, triangle is front-faceing and vice versa.

Now, if you want to know the winding order of triangle ABC, you have to calculate the cross product AB X AC. If this is oriented towards you (can be checked with dot product), triangle if CCW.

my problem is that if i try to calculate back/front face in the shader with this simple formula:

float4 Dir = dot(Normal,CameraModelPosition);

if(Dir >0.0)

and works fine .

The problem is that this is a dinamic calculation relative to the position of the camera, i can do this in a "static calculation" without the camera position?

thanks.

ps.

i see the gl_frontFacing formula at:

http://www.opengl.org/sdk/docs/manglsl/xhtml/gl_FrontFacing.xml

but i'm not understand how i can coding it in c++.

###
#5
Members - Reputation: **841**

Posted 23 September 2011 - 04:38 AM

I am not sure I understand your problem completely.

I.e. what does front-facing mean for you? The order of triangle is not a property of triangle itself (i.e. a function of its vertices), but property of triangle and some direction (i.e. a function of the vertices of triangle AND the direction vector). Thus it is always relative to that direction. Usually you have some reason to test the order of triangle and and thus the direction is determined by the semantic meaning of test.

Commonly we want to test the order (CW or CCW) of triangles relative to camera direction, because we want to optimize and not render backwards facing triangles. As the order is relative to direction, using any other axis will not give you the same results as using camera direction.

Testing triangle order against arbitrary axis usually does not have any real meaning.

If you have triangle normal, you can also test the order of triangle against that normal. If you know, that normal is always pointing outside from your mesh, it detects whether triangle is CW or CCW relative to the mesh surface. Although I think that all normal modeling programs keep triangles always in fixed order relative to their normals.

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya

Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/

###
#6
Members - Reputation: **211**

Posted 23 September 2011 - 04:59 AM

Hello!

If you have triangle normal, you can also test the order of triangle against that normal. If you know, that normal is always pointing outside from your mesh, it detects whether triangle is CW or CCW relative to the mesh surface. Although I think that all normal modeling programs keep triangles always in fixed order relative to their normals.

very thanks Lauris, yes , the normal must always pointing outside my mesh, if not it's a backface, what that i'm searching .

but how i can test if the normal point outside the mesh or in the mesh?

thanks

###
#7
Members - Reputation: **841**

Posted 23 September 2011 - 05:25 AM

So you are not sure, whether it is the case with your model? I reiterate, that most well-formed models exported from 3D programs always have normals pointing outside.very thanks Lauris, yes , the normal must always pointing outside my mesh, if not it's a backface, what that i'm searching .

but how i can test if the normal point outside the mesh or in the mesh?

As of determining the normal direction, I think the only foolproof way is to "trace" the normal (as ray directed to your normal). I.e starting from the plane of your triangle (excluding this triangle itself) count all triangles that intersect with ray. If the number is odd, normal is pointing inside mesh (as it has to "escape" from the mesh through at least one triangle. if the number is even (or 0) it is pointing outwards.

It expects, that mesh is closed and well-formed (i.e. all edges are shared by exactly 2 triangles and there are no coincident triangles).

This has a still potential of nasty numerical errors unfortunately. To be sure, you may want to repeat tracings few times.

Once you have determined the proper direction of normal for at least one triangle, you can "walk" over surface to determine the directions of other normals - i.e.

- Say you have triangle ABC with normal N and you determine, that this normal is pointing outwards
- Determine the order of given triangle relative to normal - let's say it is CCW
- Thus (AB)X(AC) dot N > 0
- Now find another triangle sharing angle with ABC, say it is DAC
- Individual edges shared by adjacent triangles have to be in opposite winding order, so
- The edge "AC" would be "wrong" order on ABC - thus it is in "right" order on adjacent DAC
- Thus DAC is with the same order as ABS - i.e. also CCW
- Thus the outwards pointing normal of DAC is DAxDC
- Say you have another triangle EBC
- As the edge "BC" would be in right order on ABC, it is in "wrong" order on EBC
- Thus EBC is CW
- Thus the outwards pointing normal of EBC would be -1 * EBXEC

I.e. if you now that all normals are pointing outwards, it is easy to determine the winding order of all triangles (relative to mesh surface). And vice versa - if you know the winding order of traingles, it is easy to calculate outwards pointing normals. But if you do not know either, you have to start from somewhere - i.e. determine the direction of at least one normal.

Also if the mesh is not well-formed there probably is not generic algorithm, as the outside and inside of mesh are not properly defined.

First technology demo of my game Shinya is out: http://lauris.kaplinski.com/shinya

Khayyam 3D - a freeware poser and scene builder application: http://khayyam.kaplinski.com/