   Terrain - Calculating Normals Problem
  Well, I've got my Terrain fully working.But now I want to calculate Normal for lighting but the whole terrain is black.My Function to calculate them looks like this:HRESULT CTerrain::CalculateNormals(){// Initialize Normalsfor(DWORD i = 0; i < (sizeof(m_stTerrainVertices) / sizeof(TERRAINVERTEX)); i++){m_stTerrainVertices.normal = D3DXVECTOR3(0.0f, 0.0f, 0.0f);}// Calculate and Set Normals for each Triangle Vertexfor(int i = 0; i < (sizeof(m_sTerrainIndices) / sizeof(short) / 3); i++){int TriangleEdge1 = m_sTerrainIndices;<span class="cpp-keyword">int</span> TriangleEdge2 = m_sTerrainIndices;<span class="cpp-keyword">int</span> TriangleEdge3 = m_sTerrainIndices;D3DXVECTOR3 side1;D3DXVec3Subtract(&amp;side1, &amp;m_stTerrainVertices[TriangleEdge1].position, &amp;m_stTerrainVertices[TriangleEdge3].position);D3DXVECTOR3 side2;D3DXVec3Subtract(&amp;side2, &amp;m_stTerrainVertices[TriangleEdge1].position, &amp;m_stTerrainVertices[TriangleEdge2].position);D3DXVECTOR3 normal;D3DXVec3Cross(&amp;normal, &amp;side2, &amp;side1);D3DXVec3Add(&amp;m_stTerrainVertices[TriangleEdge1].normal, &amp;m_stTerrainVertices[TriangleEdge1].normal, &amp;normal);D3DXVec3Add(&amp;m_stTerrainVertices[TriangleEdge2].normal, &amp;m_stTerrainVertices[TriangleEdge2].normal, &amp;normal);D3DXVec3Add(&amp;m_stTerrainVertices[TriangleEdge3].normal, &amp;m_stTerrainVertices[TriangleEdge3].normal, &amp;normal);}<span class="cpp-comment">// Normalize Normals</span><span class="cpp-keyword">for</span>(DWORD i = <span class="cpp-number">0</span>; i &lt; (<span class="cpp-keyword">sizeof</span>(m_stTerrainVertices) / <span class="cpp-keyword">sizeof</span>(TERRAINVERTEX)); i++){D3DXVec3Normalize(&amp;m_stTerrainVertices<span style="font-weight:bold;">.normal, &amp;m_stTerrainVertices<span style="font-weight:bold;">.normal);}<span class="cpp-keyword">return</span> S_OK;}</pre></div><!–ENDSCRIPT–>Does anyone know what's wrong with this function ? Why is the terrain black, while the Airplane above it works perfectly ?Screenshot: http://yfrog.com/78bildschirmfoto20100409up
  Try reversing the vectors in the cross productD3DXVec3Cross(&normal, &side2, &side1);to:D3DXVec3Cross(&normal, &side1, &side2);;  under your cross product line, add:normal = -normal;or normal.X = -normal.X;normal.Y = -normal.Y;normal.Z = -normal.Z;i usually have the same problem.  i assume it has to do with vertex order.   ;  I tried both of your suggestions but nothing changed. I have the feeling something might be wrong in the shader maybe ?struct PositionTextureLightVSOutput{float4 Position: POSITION;float2 TexCoord: TEXCOORD0;float3 Normal: TEXCOORD1;float3 LightDirection: TEXCOORD2;};struct PositionTextureLightPSOutput{float4 Color: COLOR0;};PositionTextureLightVSOutput PositionTextureLightVS(float4 inPos : POSITION, float2 inTexCoord : TEXCOORD0, float3 inNormal : NORMAL){PositionTextureLightVSOutput output = (PositionTextureLightVSOutput)0;float4x4 ViewProjection = mul(xView, xProj);float4x4 WorldViewProjection = mul(xWorld, ViewProjection);output.Position = mul(inPos, WorldViewProjection);output.TexCoord = inTexCoord;output.LightDirection = normalize(lightDirection);output.Normal = normalize(mul(xWorldInverseTranspose, inNormal));return output;}PositionTextureLightPSOutput PositionTextureLightPS(float2 inTexCoord : TEXCOORD0, float3 inLightDirection : TEXCOORD1,float3 inNormal : TEXCOORD2){PositionTextureLightPSOutput output = (PositionTextureLightPSOutput)0;output.Color = ambientColor * ambientIntensity;output.Color += tex2D(TextureSampler, inTexCoord);output.Color *= saturate(dot(inLightDirection, inNormal));return output;}technique PositionTextureLight{pass Pass0{VertexShader = compile vs_2_0 PositionTextureLightVS();PixelShader = compile ps_2_0 PositionTextureLightPS();// Render StatesFillMode = Solid;}}Anything wrong here ?VertexDeclaration seems to work, too.// VertexElement Structure for VertexDeclarationconst D3DVERTEXELEMENT9 declarationTerrain[] ={{0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0},{0, 12, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0},{0, 20, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0},D3DDECL_END()};// Position (xyz) and TexCoord(uv)struct TERRAINVERTEX{D3DXVECTOR3 position;float u,v;D3DXVECTOR3 normal;};;  Try debugging the shader in PIX. If you haven't used it before: start PIX, select your file and a single frame capture. When you have that capture, click on the draw call on the left and select the 'mesh' tab on the right. You'll see the before and after of the vertices, and can choose to debug them. (Okay, this is from memory, and I think I'm missing something, but it's close enough that you should be able to manage).   ;  I'm not using XNA, would it still work ?I don't get this program at all, there's no mesh option ??And I'm not sure how this would help me here?[Edited by - lipsryme on April 10, 2010 10:09:59 AM];  ok sorry for double post but I just found out why it didn't work.And I have no idea why that is.For my airplane mesh I use light direction (0, 1, 1).But for the terrain I have to use (0, -1, -1) to get the light working right.Why the hell is that ?why do these values have to be negative ???   ;Quote:Original post by lipsrymeok sorry for double post but I just found out why it didn't work.And I have no idea why that is.For my airplane mesh I use light direction (0, 1, 1).But for the terrain I have to use (0, -1, -1) to get the light working right.Why the hell is that ?why do these values have to be negative ???That sounds very odd, the "solution" you got does sound like your normals are just the wrong way around, which both Nibbles and my own solution should have fixed. So I'm not quite sure whats going on. Try to eliminate the shader from the equation, use a basic shader.Looking at the shader it appears quite odd, the normal comes in in model space, gets multiplied by some world inverse transpose matrix which I'm not sure exactly what that is going to do but it does sound not too good.You need both your light and normal in the same space, the light I guess is in world space? (weird way to send light direction in btw), your normal is in model space so if anything you need to multiply the normal by the world matrix.not entirly sure but I beleive thats your problem, confusion as to what space the light and the normal are in, ensure they are in the same space.   ;  I've done the shader anew but still the same.The airplane has the right lighting with direction(0, 1, 1) but the terrain is black.So the problem is not the shader I think.But the thing is even if I invert the normal in the CalculateNormal function it's still the same.I'll try to do the whole function all over maybe it'll miraculously work...[Edited by - lipsryme on April 11, 2010 11:08:10 AM];  Oh my god. I've solved it.Finally....I'm not entirely sure that doing the function anew solved it but there were 2 things entirely wrong.1. The Normals were calculated AFTER the vertices were copied to the Vertex Buffer.2. The FVF on the VertexBuffer didn't include Normal DataI feel so stupid now :DWell thanks anyway people, this can be closed now.   