Skip to main content
GameDev.net gamedev.net
🔒 Locked

Normals problem

Started by xSergio Apr 21, 2005 at 10:18 AM 2 replies 950+ views
Original Post
xSergio
xSergio
Hi, i'm a little newbie to DirectX programming and have a problem with normals: I define the vertex in my scene getting them from a serie of text files containing vertex positions for all the triangle. The triangles all togheter generate complex forms including convex and non-convex figures. the real problem is that the vertex order in the triangle is not always CW or CCW and i don't know how to calculate the correct orientation for normals. A way to work around this problem is to light both faces of the triangle, but i don't know how to do since it seems it is possible only using PixelShader, but I don't know absolutely nothing about this. can any one help me?
jimmynelson
jimmynelson
This something you can set in DirectX whether is uses CCW or CW. I think CCW is the common one. So if you set of points use both, you need to fix that. Also once you have then all corrected the normal (or simple triangle normal) can be calculated using just 2 of the vectors the make up the triangles edges

normal = (v1 - v2) cross (v2 - v3);

I believe that will do it for CCW, you can see how this would be reversed for CW, but rather than thinking if I'm rock there do it the other way, in which you (v2 - v3) corss (v1 - v2). Crossing creates a vector perpendicular to the plain the triangle creates.

Also if you want you set Directx to not backface cull. Which is what you are looking for. If you set to no lighting or full ambient lighting and set to no backface culling both sides of the triangle will be rendered.
gufino2
gufino2
Well, I think you should just fix your file to get every triangle defined in a fixed way, be it CW or CCW. If you don't take this step you'll find it almost impossible to do a correct culling, and believe me, you'll need it sooner or later... When you have an "ordered" file, than you can calculate the normal for the triangle with the forumlas posted by jimmynelson...
Always remember, a good data structure is the best way to optimize your code...

One step further: if you have to use Gourad Shading, you'll need a normal for each vertex. To get the normal for a vertex, add up the normal of EVERY triangle sharing that vertex, and then normalize the resulting vector. Simple, hu?

Greetz
xSergio
xSergio
I know that if i can fix the vertex source i solve my problems, but I can't do that since I'm importing data from another program and the amount of vertex to check and fix is really huge and can't be done by hand.

I need an algorithm!!, but even if i tought on it for a long, i can't find one.
That's why i was thinking to solve using pixelshader.

using full ambient light instead, give bad detailed scene...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.