DXF normals

I’m doing a program to display and manipulate 3D files. I chose to use dxf files… now i can read and display 3Dface objects properly in wireframe, but when i render them i have some problems.
I calculated the normals for each triangle (V0-V1)x(V1-V2):some trangles are rendered well,but others have the normals inverted.
What am i doing wrong?
Bye

If your normals get inverted it is most probably a problem of your crossproduct.

If (V0-V1)x(V1-V2) calculates the correct normal then (V1-V2)x(V0-V1) calculates the inverted one.
So you have to make sure, that the ‘winding for your crossproduct’ stays the same for all normal calculations.
Also keep in mind that OpenGL uses vertex and not face normals.

hih

[This message has been edited by satan (edited 07-14-2002).]

The problem is that i calculate the normal for each vertex of each triangle in the same way, and some triangles are rendered well while other triangles not. (

Then you don’t have any specific order in which you store your vertices.

You have to store your vertices in either clockwise or counter clockwise order. If you mix them you will have a hard time to figure out the correct normal.

I read the vertexes in the order they are in the dxf file. I thought they were already in the right order… am I doing something wrong?
Anyway, thanks so much for the help you’re giving me.

Reading them from the file in the correct order is not enough. The vertices also have to be stored in the correct order.

The source of the problem is probably that the mode itself is modelled without respect to the winding order of the vertices. If you want it to be correct in the first place, you have to fix the model itself.

I store them in the order i read them

I’m not sure if this is relavant.
In triangle strips, the winding order alternates. Given the vertices in a strip …,vi,vi+1,vi+2,vi+3,…, the winding order of the triangle (vi,vi+1,vi+2) is opposite of the next triangle (vi+1,vi+2,vi+3).

You must negate every other face normal in the strip.

Try glEnable( GL_CULL_FACE ); to see if you can still see the object properly… If you cant see it properly is because the winding of the triangles are alternating somehow…
If they look good, then you are fine…

I would say that you compute the average normals… When I load my 3ds models and light them up without averaging the normals, the lighing looks really weird… I fix it with average normals…

Jambolo, a trick for avoiding the whole negating normals with triangle strips is that you read

vi, vi+1, vi+2
and then
vi+3, vi+2, vi+1

So, in a loop you read pairs of triangles with the order I mentioned rather than reading one triangle at a time and negating every other normal.

mmm… I was using GL_TRIANGLES, non GL_TRIANGLE_STRIP.
With glEnable(GL_CULL_FACE) and GL_TRIANGLES the sphere looks strange… I try with GL_TRIANGLE_STRIP and let you know.
Thanks

GL_TRIANGLE_STRIP doesn’t work: I have to use GL_TRIANGLES.

Here’s the link to the picture of what i’m getting (with GL_TRIANGLES): http://spazioinwind.libero.it/galilee/sphere.jpg

(to see the above link, copy and paste it in the browser’s address bar, clicking on it doesn’t seem to work).
I loaded a model of a teapot in DXF with faces made up of 4 vertexes: the front part renders well, but the back part has the normals inverted (with light in front of it is black, with light behind it is white).

I still think that it is a problem with the order of your crossproduct. For some vertices you seem to get the correct order for others not.
Try drawing your normals (unlit) just to see what they actually look like.
Since you use sphere and teapot as examples i think you average your normals, right?
Sure your averaging code is correct?
Do you use any edge preservation technique?
What about trying a simple cube ?
You could calculate your normals by hand and see what they should be and what your code calculates.

p.s.:I just saw that you said your sphere looks strange when you enable GL_CULL_FACE, that means that the winding of your triangles is wrong for some of them. You should fix this and then your problem with the normals should disappear. Keep backfaceculling enabled, lighting disabled and make sure that the winding is the same for all tris.

[This message has been edited by satan (edited 07-15-2002).]

I don’t average normals: before going into that I wanted to make this work.
I tried to paint the normals with a DXF cube: normals come out from only 3 faces, probably the others point downside the cube and are overlapped with the edges and the other normals.
Now i try with a hand-made cube and see what happens.

3 faces?
That should be 12 normals.

Put this three lines of code in your glsetup routine and render your dxf cuber again:

glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);

Does it show up right?

My god.
I create a DXF of a cube manually, and i added a function to draw the normals.
Now, i have normals coming out from each point of the cube (now i do (v0-v1)x(v2-v1)).
BUT:
-with lighting enabled but no light in the scene, i can see the faces far from the camera and INSIDE the cube
-with a light in the scene, if i place the light near the camera, everytithing’s black; if i move it behind the object, the inside part of the far faces becomes visible

To have an idea of what i’m talkin about, copy and paste: http://spazioinwind.libero.it/galilee/cube.jpg

Your winding is wrong.

Make sure that all your triangles are oriented the same way, default is counter clockwise.
That seems to have been your problem all the time.Like Bob said.

Ahhhhhhhhh…
I checked http://spazioinwind.libero.it/galilee/sphere.jpg

Thet normals are fine… Try
glShadeModel( GL_SMOOTH );

It should help a bit… And with average normals you are set to go…

But, if the normals point outside, why aren’t the face lighted???
I inserted the triangles of the cube manually, and they are in counter clockwise order.
I really don’t know what else to try