   (D3D10) My Terrain won't Render
  Hey guys, I'm new here but I've used GD alot due to Google search results.I'm having trouble rendering my terrain in Direct3D. I don't know what it could be. But here's my HLSL code:-----matrix g_mtxWorld;matrix g_mtxView;matrix g_mtxProj;Texture2D g_Textures[8];struct VS_INPUT{float3 vPosition : POSITION;float3 vNormal : NORMAL;float4 vColor : COLOR;float2 vTexCoords : TEXCOORD;};struct VS_OUTPUT{float4 vPosition : SV_POSITION;float3 vNormal : NORMAL;float4 vColor : COLOR;float2 vTexCoords : TEXCOORD;};VS_OUTPUT DefaultVS(VS_INPUT In){VS_OUTPUT result;float4 vPos = float4(In.vPosition, 1.0f);float4 vFinalPos = mul(vPos, g_mtxWorld);vFinalPos = mul(vPos, g_mtxView);vFinalPos = mul(vPos, g_mtxProj);result.vPosition = vFinalPos;result.vNormal = float3(0,1,0);result.vColor = In.vColor;result.vTexCoords = In.vTexCoords;return result;}SamplerState SamplerStateWrap{Filter = MIN_MAG_MIP_LINEAR;AddressU = Wrap;AddressV = Wrap;};float4 DefaultPS(VS_OUTPUT In) : SV_Target{float4 finalColor = In.vColor;finalColor *= g_Textures[0].Sample(SamplerStateWrap, In.vTexCoords);return finalColor;}technique10 DefaultTechnique{pass Pass0{SetGeometryShader(NULL);SetVertexShader(CompileShader(vs_4_0, DefaultVS()));SetPixelShader(CompileShader(ps_4_0, DefaultPS()));}}-------Then here's my camera/matrix code:-------/*****************************************  SetCameraPosition()*************************************/void SetCameraPosition(){D3DXMatrixIdentity(&g_World);D3DXVECTOR3 Up(0.0f, 1.0f, 0.0f);D3DXMatrixLookAtLH(&g_View, &g_CameraPosition, &g_LookAtPosition, &Up);D3DXMatrixPerspectiveFovLH(&g_Projection, 3.1415f / 4, 1.333f, 0.1f, 1000.0f);g_pTerrainWorldVariable-&gt;SetMatrix((float *) &g_World);g_pTerrainViewVariable-&gt;SetMatrix((float *) &g_View);g_pTerrainProjectionVariable-&gt;SetMatrix((float *) &g_Projection);}-------If you see anything wrong with the above or that I need to post more code, please reply. I've been working on this for 3 days now, trying to get it to render and no luck.Thanks!!![Edited by - eyeshotchris on May 24, 2010 7:01:13 PM]
  I think in your vertex shader, you're multiplying your world view and projection matrices incorrectly.  I think you want vFinalPos to be world * view * projection.  The way you have it there, only the last assignment to vFinalPos 'sticks'.  I think you want something like: vFinalPos = mul(vPos, mtxWorld);vFinalPos = mul(vFinalPos, mtxView);vFinalPos = mul(vFinalPos, mtxWorld); Also, its been awhile, I can't remember, but the order of the multiplications might matter.Edit: Using the code tags described here will make code you post easier to read.   ;  Thanks! I see what you mean. Although even after changing my HLSL, the terrain is still not showing, so I must have another problem.Oh, I also have used PIX (DX debugger) app and that's not showing anything out of the ordinary, but I don't know if theres more to PIX than what I know about the program that could possibly help. When I debug a pixel, it just says that the original color is black, then "ClearRenderTargetView" is setting the pixel to blue, and that the final color is blue.Lastly, I'll post the algorithm on how I'm calculating the vertices and indices...for(int i = 0; i < 16; i++){for(int z = 0; z < 16; z++){VertexData[(i * 16) + z].m_Color.a = 1.0f;VertexData[(i * 16) + z].m_Color.r = 0.0f;VertexData[(i * 16) + z].m_Color.g = 1.0f;VertexData[(i * 16) + z].m_Color.b = 0.0f;VertexData[(i * 16) + z].m_vNormal = D3DXVECTOR3(0.0f, 1.0f, 0.0f);VertexData[(i * 16) + z].m_vPosition.x = z;VertexData[(i * 16) + z].m_vPosition.y = 0.0f;VertexData[(i * 16) + z].m_vPosition.z = i;VertexData[(i * 16) + z].m_vTexCoords.x = (1.0f / 16) * z;VertexData[(i * 16) + z].m_vTexCoords.y = (1.0f / 16) * i;}}for(int y = 0; y < 15; y++){for(int x = 0; x < 15; x++){indices[((y * 90) + (x * 6)) + 0] = (y * 16) + (x * 1) + 0;indices[((y * 90) + (x * 6)) + 1] = (y * 16) + (x * 1) + 1;indices[((y * 90) + (x * 6)) + 2] = (y * 16) + (x * 1) + 16;indices[((y * 90) + (x * 6)) + 3] = (y * 16) + (x * 1) + 1;indices[((y * 90) + (x * 6)) + 4] = (y * 16) + (x * 1) + 17;indices[((y * 90) + (x * 6)) + 5] = (y * 16) + (x * 1) + 16;}}That's what I'm using to calculate a 16x16 grid terrain that's flat. Which the output is correct AFAIK from looking at the values in VS's debugger.   ;  I'm afraid I might need to see more code, what you have there looks like it could be ok, depending on some other things.  Your terrain has a corner at (0,0,0) and extends along the positive x and positive z axes for 16 units.  So long as your camera's position has a positive Y value, between say 8 and 16,  and your 'look at' vector is somewhere in the range of (0, 0, 0) to say (16, 0, 16), it should be at least looking in the right place.Another thing is your indices look like they're in clockwise order, are you sure that is the same as your rasterizer state is set up for?I've been in a similar position, thinking I've gotten all this set up correctly only to curse at a blue screen for 3 days because its not drawing my damn terrain.  Eventually I set up a quick and dirty mouse look camera, and drew a simple cube right in front of it to make sure that was at least working.  I've had just about everything go wrong that could, the indices going the wrong way so the terrain wasn't visible until I was underneath it looking up, normals going the wrong way so it was all black, and at one point it was being drawn totally behind the camera, and flipped upside down, rotated every which way, etc.[Edited by - Matt328 on May 24, 2010 9:04:03 PM];  Hi there.Check your D3D10_INPUT_ELEMENT_DESC size may be wrong.   ;  I just now tried putting the camera position underneath the terrain, still nothing.Here's my D3D10_INPUT_ELEMENT_DESC code:D3D10_INPUT_ELEMENT_DESC defaultLayout[] = {{"POSITION", 0, ::DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},{"NORMAL", 0, ::DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0},{"COLOR", 0, ::DXGI_FORMAT_R32G32B32A32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0},{"TEXCOORD", 0, ::DXGI_FORMAT_R32G32_FLOAT, 0, D3D10_APPEND_ALIGNED_ELEMENT, D3D10_INPUT_PER_VERTEX_DATA, 0},};Here's my RenderTerrain() code:/*****************************************  RenderTerrain()*************************************/void RenderTerrain(){UINT size = sizeof(TerrainVertex);UINT Offset = 0;D3DDevice->IASetVertexBuffers(0, 1, &g_pTerrainVertexBuffer, &size, &Offset);D3DDevice->IASetIndexBuffer(g_pTerrainIndexBuffer, ::DXGI_FORMAT_R16_UINT, 0);D3DDevice->IASetPrimitiveTopology(::D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST);D3DDevice->IASetInputLayout(g_pTerrainVertexLayout);if(FAILED(g_pTerrainDiffuseVariable->SetResource(g_pTerrainTextureRV))){MessageBox(NULL, L"Error", NULL, MB_OK);}g_pTerrainTechnique->GetPassByIndex(0)->Apply(0);D3DDevice->DrawIndexed(1350, 0, 0);}Once again, thanks for your help!   ;  I tryed using D3D10_APPEND_ALIGNED_ELEMENT once and it neaver workedso I now just do thisD3D10_INPUT_ELEMENT_DESC defaultLayout[] = {{"POSITION", 0, ::DXGI_FORMAT_R32G32B32_FLOAT,0, 0, D3D10_INPUT_PER_VERTEX_DATA, 0},{"NORMAL", 0, ::DXGI_FORMAT_R32G32B32_FLOAT,0,12,D3D10_INPUT_PER_VERTEX_DATA, 0},{"COLOR", 0, ::DXGI_FORMAT_R32G32B32A32_FLOAT,0, 24,D3D10_INPUT_PER_VERTEX_DATA, 0},{"TEXCOORD", 0, ::DXGI_FORMAT_R32G32_FLOAT,0,36,D3D10_INPUT_PER_VERTEX_DATA, 0},};   ;  Sorry to have left you hanging, but I still can't see anything wrong with the code you've posted.  I was going to suggest explicitly passing the offset to each vertex 'element' in the layout description, but the docs seem to indicate what you have there should work.Are you sure the data type you're using for your indices array is actually only 16 bits?  I suspect you'd see more things blowing up though if that were the problem.   ;  Why not use PIX to see if the mesh is being drawn and transformed correctly?That will help you narrow down the problem!I'm quite new to game programming, although not programming an the best pieces od advice I got here were to check error codes and use PIX.   ;  OK, I tried removing "D3D10_APPEND_ALIGNED_ELEMENT" from my input element code and then adding numbers (0, 12, 24, 36) for that variable but still nothing.Matt: I'm using the WORD type for my indices and inside the description struct, I'm specifying "sizeof(WORD) * 1350;" for the size of the data since I have 1350 indices. ( 15 * 15 * 2 * 3 )I'm also using PIX and from what I can tell, the terrain is not rendering what so ever. I got the camera position at (0, 10, 0) and the lookAt point at (0,0,0) and since the corner of the terrain is at (0,0,0), I'm expecting a quarter of the screen be something besides the background color (blue) but no luck.I debugged 4 pixels (one from each corner of the render) in PIX and they all said that the final color was last affected by the ClearRenderTargetView() method which is blue.I don't know what's wrong with this. Anymore questions is great to have at this point, from anyone! Otherwise, this might just have to go down as a mystery bug in the books. Oh well.   