# normal and wind order

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

## Recommended Posts

Hello.
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.

##### Share on other sites
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.

##### Share on other sites

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.

thanks.

by

##### Share on other sites

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 whats mean "towards you" if i don't have a camera position?

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++.

##### Share on other sites
Hello!

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.

##### Share on other sites

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

##### Share on other sites

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?

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.

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.

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

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.

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 16
• 9
• 15
• 9
• 11