#### Archived

This topic is now archived and is closed to further replies.

# Computing normals for terrain

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

## Recommended Posts

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

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

##### Share on other sites
Can you point me at a tutorial? I almost now where I need to "move", along the lines of Cross vector and Dot product.

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

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

##### Share on other sites
Try setting a lower ambient value

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

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

##### Share on other sites
Cool, do you think I have them reversed? I''ll try to see. Thank you very much!

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

	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]

1. 1
2. 2
Rutin
19
3. 3
4. 4
khawk
14
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633743
• Total Posts
3013643
×