Computing normals for terrain
How can I compute vertex normals for a terrain chunk? Suppose the chunk is 32x32, what should I do?
So far, I am setting the ny to 1 and all other to 0. Any other ways?
Thanks
my 3d math is beyond rusty but look up the cross product that should give you the info you need to calculate the normal for a surface/triangle, dot product can also come in handy when culling geometry. just look up any vector math tutorial and it will probably show you how to do this and a lot more...
- Damage Inc.
- Damage Inc.
Can you point me at a tutorial? I almost now where I need to "move", along the lines of Cross vector and Dot product.
if you have your terrain data in one of the D3DX mesh object classes then you can just use D3DXComputeNormals.
otherwise you can use D3DXVec3Cross to get the cross product for each triangle. you would then want to calculate the vertex normals by averaging all of the face normals together for all triangles that made use of each vertex.
otherwise you can use D3DXVec3Cross to get the cross product for each triangle. you would then want to calculate the vertex normals by averaging all of the face normals together for all triangles that made use of each vertex.
Thank you, I fixed them now. But I have a problem. I have a material for my terrain, with diffuse (1,1,1,1) and ambient (0,0,0).
For my light, I use directional, with direction = 0,-1,0 (downwards), ambient (1,1,1) and diffuse(1,1,1).
Now, something weird happens. If I disable lighting, the terrain is lit, where it should be black, right? When I enable lighting, the terrain''s color is dependant of the material, not the light. Also, the diffuse (material or light color) isnt changing anything, only the ambient is. For my scene ambient color, I use full white.
Any ideas? I believe that the diffuse light sets the color for a mesh after it is lit, not the ambient.
For my light, I use directional, with direction = 0,-1,0 (downwards), ambient (1,1,1) and diffuse(1,1,1).
Now, something weird happens. If I disable lighting, the terrain is lit, where it should be black, right? When I enable lighting, the terrain''s color is dependant of the material, not the light. Also, the diffuse (material or light color) isnt changing anything, only the ambient is. For my scene ambient color, I use full white.
Any ideas? I believe that the diffuse light sets the color for a mesh after it is lit, not the ambient.
I just did. Well, sorry to ask so many questions, but it was not directly my fault. I fixed it, but for the light, the Y is reversed! I was using 0,-1,0 as I told you for my directional light direction. I wasnt getting results whatsoever, and I wondered. So I switched to 0,1,0 and voila.. But why, its weird. Shouldnt it be the reverse way?
Anyway it works now, but if you could tell me why this happens, it would be excellent.
Anyway it works now, but if you could tell me why this happens, it would be excellent.
Could it be that your normals are pointing the wrong way? If you switch places of the two source vectors in the D3DXVec3Cross function the direction of the normal will be reversed.
You can compute cross products for each vertex, but if your terrain is a grid, this is a faster method (and gives you the same answer):
[edited by - Jambolo on May 4, 2003 4:04:48 AM]
The 4 adjacent points in a uniform grid: A, B, C, D B | C--0--A | D The ratio of XY-scale to Z-scale: s = Sxy / Sz The desired normal: N = cross(A,B) + cross(B,C) + cross(C,D) + cross(D,A), (then normalize) A faster way to get the same answer: Nx = 2 * s * (Cz - Az) Ny = 2 * s * (Dz - Bz) Nz = 4 * s^2 N = normalize( N ) And since N is normalized in the end, it can be divided by 2 * s: Nx = Cz - Az Ny = Dz - Bz Nz = 2 * s N = normalize( N )
[edited by - Jambolo on May 4, 2003 4:04:48 AM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement