Normal Woes
I've written a function that randomly generates a terrain map and calculates all the vertex normals so it can be rendered wiht Gouraud shading. However, when I pass the vertices to DirectX it doesn't seem to be able to calculate the lighting in any sensible way. All normals are in three-space and normalized to (0,1), relative to thier vertices.
I suspect my problem comes from the fact that this is not the way DirectX wants its normals. I have also attempted setting the normals relative to world space, but that didn't help either (although it gave me some seriously interesting lighting ~_^). How exactly is a normal defined in DirectX? Coordinates? Angles? A quick explanation of what it's looking for would be great!
Also, do I have to have a material to use the D3D lighting routines? I set the color of each of my vertices manually, so I'm not sure if I need to set a material as well.
*edit* I'm building against DX8.1, btw */edit*
It should simply be a normalised vector for D3D. To calc a normal for a triangle take the cross product of the two sides and normalise. For Gouraud you then combine all the face normals connecting to a vertex (e.g. average).
Hmmm, that's exactly what I'm doing. I'll keep hashing away at it and hopefully figure it out.
Normals are defined in model space. If your World matrix is identity ( as is usual for terrain ), then they are in world space. You said your normals are normalized to [0..1]. They should be in the range [-1,1].
Try forcing all vertex normals to be < 0, 1, 0 > ( straight up ) and see how that looks.
Try forcing all vertex normals to be < 0, 1, 0 > ( straight up ) and see how that looks.
Heh, sorry about that SimmerD ^_^. By "normalized to (0,1)", I meant that the lenght of the vertices was normalized to (0,1). Actualy values did go from (-1,1).
Anyway, I discovered the problem last night. I had made a typo when I wrote the code to calulate the cross-product for the facet normals, and so all of the z components were just plain wrong. I works great now, I had a cute little demo of it running where you could watch the sun rise and set over the terrain. Thanks guys.
Anyway, I discovered the problem last night. I had made a typo when I wrote the code to calulate the cross-product for the facet normals, and so all of the z components were just plain wrong. I works great now, I had a cute little demo of it running where you could watch the sun rise and set over the terrain. Thanks guys.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement