   [SOLVED] HLSL + Lambert problem
  Hey there!I'm trying to render an axis aligned heightmap with Lambertian lightning, but I've got some problems with the lightning. It seems that the shadows\highlights are perpendicular to each other, even though I'm only using 1 directional light.After hours of fiddling around I don't know what to try anymore..To show you my problem (a hill as shown from above):What bothers me is:1. The shadow suggests the light is pointing towards +z (red arrow)2. The highlight suggests the light is pointing towards -x & +z (blue arrow)3. The green arrow shows where the light SHOULD be pointing: (float3( 0.25f, -1.0f, 0.0f))...My HLSL code:struct VertexToPixel{    float4 Position   : POSITION;   float2 TextureCoords: TEXCOORD;};struct PixelToFrame{    float4 Color : COLOR0;};float4x4 xView;float4x4 xProjection;float4x4 xWorld;float4 xCamPosition;// Position of the camera in world space (camX, camY, camZ, 0)Texture xNormalmap;sampler TNormalSampler = sampler_state { texture = <xNormalmap>; magfilter = ANISOTROPIC; minfilter = LINEAR; mipfilter=LINEAR; AddressU = mirror; AddressV = mirror;};VertexToPixel TexturedVS( float4 inPos : POSITION, float2 inTexCoords: TEXCOORD){VertexToPixel Output = (VertexToPixel)0;float4x4 preViewProjection = mul (xView, xProjection);float4x4 preWorldViewProjection = mul (xWorld, preViewProjection);Output.TextureCoords = inTexCoords;Output.Position = mul(inPos, preWorldViewProjection);    return Output;    }PixelToFrame TexturedPS(VertexToPixel PSIn) {PixelToFrame Output = (PixelToFrame)0;float3 terrainNormal = (tex2D(TNormalSampler, PSIn.TextureCoords / 32.0f) - 0.5f)*2.0f;//// Lambert://float3 lightDir =  normalize(float3( 0.25f, -1.0f, 0.0f));// Light directionfloat4 lightLambert = saturate(dot(terrainNormal,lightDir));// Calculate Lambert termOutput.Color.rgb = lightLambert;return Output;}technique Textured_3_0{pass Pass0{   VertexShader = compile vs_3_0 TexturedVS();PixelShader  = compile ps_3_0 TexturedPS();}}As you can see I pass the normals as a texture. My code for generating the normals:  int[] normals = new int[heightmapWidth * heightmapDepth];  Vector3 nml = new Vector3(0.0f, 0.0f, 0.0f);  for (int x = 0; x < heightmapWidth; x++)  { for (int z = 0; z < heightmapDepth; z++) {int xmin = Math.Max(x - 1, 0);int xmax = Math.Min(x + 1, heightmapWidth);int zmin = Math.Max(z - 1, 0);int zmax = Math.Min(z + 1, heightmapDepth);nml.X = heightMap[xmin + z * (heightmapWidth + 1)] - heightMap[xmax + z * (heightmapWidth + 1)];nml.Y = heightMap[x + zmin * (heightmapWidth + 1)] - heightMap[x + zmax * (heightmapWidth + 1)];nml.Z = 4.0f;nml.Normalize();int r = (int)MathHelper.Clamp(nml.X * 255.0f, 0.0f, 255.0f);int g = (int)MathHelper.Clamp(nml.Y * 255.0f, 0.0f, 255.0f);int b = (int)MathHelper.Clamp(nml.Z * 255.0f, 0.0f, 255.0f);normals[x + z * heightmapWidth] = b + g * 256 + r * 65536; }  }  return normals;Not that elegant, but I only have to to this once. Furthermore, when I save the heightmap as a bitmap and load it in photoshop it *looks* good.A few more points that might be of value:- When I set the light direction to float3(0.0f, -1.0f, 0.5f), everything turns practically to white.- When I set the light direction to float3(0.5f, -1.0f, 0.5f), I get the same result as in the picture posted above.I really hope someone might be able to help, as I'm starting to show a strange resemblance to the blue smiley below my user name...EDIT:Added the following to the PS after reading the normals from the texture:float3 normals = normalize(mul(terrainNormal, xWorld));But didn't change anything...[Edited by - Pauluss86 on September 30, 2009 12:24:45 PM]
  SOLVED!Forgot to scale the normals from [-1, 1] to [0, 1] before saving them to a texture :|   