   Rendering Depth to a Texture (HLSL)
  So I'm attempting to get to Shadow Mapping, and looking over http://www.gamedev.net/reference/articles/article2193.asp for a few hints.  I'm starting simple right now and just going for the depth map rendered to a texture and displaying it.  Right now though my texture is blank and I'm not exactly sure why.  One thing I do notice right off the bat is I'm not using a IDirect3DSurface9 for my attempt.  Maybe I'm a little over my head here, but it didn't seem necessary to me.  Anywhere, this is what I have so far:Initializing my depth mapIDirect3DTexture9* m_DepthMap;//texture size is temporary, just to display over the entire screenm_pDevice->CreateTexture( 800,   600,  1, D3DUSAGE_RENDERTARGET, D3DFMT_R32F, D3DPOOL_DEFAULT, &m_DepthMap, NULL );the shader:float4x4 WorldViewProjection;struct VS_OUT{float4 Pos : POSITION0;float  Depth    : TEXCOORD0;};//Vertex shaderVS_OUT vsDepthMap( float4 inPosition : POSITION ){VS_OUT output;output.Pos = mul( inPosition, WorldViewProjection);output.Depth = output.Pos.z;return output;}float4 psDepthMap( VS_OUT input ) : COLOR0{return float4( input.Depth, input.Depth, input.Depth, 1.0f );}technique BuildDepth{pass p0{Lighting = False;CullMode = CCW;VertexShader = compile vs_2_0 vsDepthMap();PixelShader  = compile ps_2_0 psDepthMap();}}Now for rendering:ClearTexture( m_DepthMap,  0xFFFFFFFF );SetRenderTarget( 0, m_DepthMap );if ( scene->m_RenderableThreeDList.size() > 0 ){Matrix view = m_Camera.LookAtMatrix();ID3DXEffect* shader = COMPONENTS.GetShader("../Assets/Shaders/DepthMap.fx");shader->SetTechnique("BuildDepth");for ( unsigned i = 0; i < scene->m_RenderableThreeDList.size() ; i++ ){ThreeDRenderable *obj = scene->m_RenderableThreeDList;Matrix scale = D3DMatrix::CreateScale( obj->m_Scale ),   translation = D3DMatrix::CreateTranslation( obj->m_Position ),   rotation = D3DMatrix::CreateRotationFromQuaternion( obj->m_Rotation ),   world = scale * rotation * translation,   worldviewprojection = world * view * m_Projection;//Load the dynamic constantsshader->SetMatrix("WorldViewProjection", &worldviewprojection);//Draw the objectID3DXMesh* mesh    = COMPONENTS.GetMesh( obj->m_MeshFile.c_str() );Material* material = COMPONENTS.GetMaterial( obj->m_MaterialFile.c_str() );// Get the number of subsetsunsigned long subsets = 0;mesh->GetAttributeTable(NULL, &subsets);//Draw each subsetfor(unsigned i = 0; i < subsets; i++) {//Begin the shaderunsigned int passes = 0;shader->Begin(&passes, 0);//Draw each passfor (unsigned int pass = 0 ; pass < passes ; pass++) {shader->BeginPass(pass);mesh->DrawSubset(i);shader->EndPass();}shader->End();}}//Restore the backbufferIDirect3DSurface9* BackBufferSurface;m_pDevice->GetBackBuffer(0,0,D3DBACKBUFFER_TYPE_MONO,&BackBufferSurface);m_pDevice->SetRenderTarget(0,BackBufferSurface);BackBufferSurface->Release();}Yeaaaah, ignore the strings and component system.  Basically just thrown together for lazy device recovery.  Anyways, I'm not exactly sure what is wrong.  Any help would be greatly appreciated.**EDIT**http://img.photobucket.com/albums/v238/billerwjustin/Graphics/ScreenShot0.pngThat is what my "depth map" currently looks like.  Not even close to what the example image looks like.[Edited by - eDuDe on May 26, 2009 10:10:45 PM]
You seem to be using your camara's view matrix for rendering the depth map, while this typically should be the 'view' from the light source. That's not a problem of itself, but with this setup the light would always seem to come from the camera when doing shadowing. Since the statue does look a bit cut off, you also might be using a projection matrix with a far clipping plane that is too near to properly render everything.I hope that 'view' from the lightsource isn't too confusing. In short the basic idea is that your shadow map contains the lit pixels closest to the light. To obtain these, you treat the light as a camera, rendering the scene with a view matrix based on the light's position and target.Hope this helps :);Quote:Original post by remigiusYou seem to be using your camara's view matrix for rendering the depth map, while this typically should be the 'view' from the light source. That's not a problem of itself, but with this setup the light would always seem to come from the camera when doing shadowing. Since the statue does look a bit cut off, you also might be using a projection matrix with a far clipping plane that is too near to properly render everything.I hope that 'view' from the lightsource isn't too confusing. In short the basic idea is that your shadow map contains the lit pixels closest to the light. To obtain these, you treat the light as a camera, rendering the scene with a view matrix based on the light's position and target.Hope this helps :)I understand this part, but right now I'm just trying to do plan old regular depth mapping.  I can adjust what matrices I pass in (change from camera to light) later.  One thing I noticed from the soft-edged shadows example, is that the webpage has:// Projection matrix for the lightD3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian(30.0f), 1.0f, 1.0f, 1024.0f );But in the actual code of the sample project he switched to:// Compute the projection matrixD3DXMatrixOrthoLH( &matProj, 45.0f, 45.0f, 1.0f, 1024.0f );I'm messing around with it now, but if I switch over to the Orthographic projection it renders out extremely tiny (and as a solid color).**EDIT**Also, this is what the scene rendered normally looks like with simple diffuse lighting and a solid color texture:http://img.photobucket.com/albums/v238/billerwjustin/Graphics/ScreenShot0-1.pngEDITx2:Here is the scene with the orthographic projection:http://img.photobucket.com/albums/v238/billerwjustin/Graphics/ScreenShot0-2.pngWhen attempting to zoom the camera it behaves oddly, by causing portions to no longer render and it seems reversed.EDIT x 3 (these edits looking to be a good log my stupidity):I was simply passing into too large values for the height and width when creating the Orthographic projection matrix.  My map is larger, but still a single color.[Edited by - eDuDe on May 27, 2009 4:56:48 PM];  I figure this is worthy of its own post, but I think I've narrowed the problem down to my render target initialization.  In my pixel shader, return float4( input.Depth, 0, 0, 1.f ) ends up rendering the same exact color as return float4( input.Depth, input.Depth, input.Depth, 1.f )And if I try to return float4( 1, 0, 0, 1.f ) nothing gets rendered at all.  *head desk*   ;  Hi, its me again.  I think I have it working now, but it doesn't really look that close to the sample image so I'm not exactly sure if its 100% correct.  I think my problem was my far plane was too far away so I couldn't see any noticeable difference between the pixel colors.  Also things change up drastically when I screw with the texture format.  Results using D3DFMT_R32F:http://img.photobucket.com/albums/v238/billerwjustin/Graphics/ScreenShot0-5.pngResults using D3DFMT_G16R16F:http://img.photobucket.com/albums/v238/billerwjustin/Graphics/ScreenShot0-4.pngThanks for letting me use you like a corner to talk to myself and work things out!   ;Quote:One thing I noticed from the soft-edged shadows example, is that the webpage has:// Projection matrix for the lightD3DXMatrixPerspectiveFovLH( &matProj, D3DXToRadian(30.0f), 1.0f, 1.0f, 1024.0f );But in the actual code of the sample project he switched to:// Compute the projection matrixD3DXMatrixOrthoLH( &matProj, 45.0f, 45.0f, 1.0f, 1024.0f );I knew I should have left this in my previous post [smile] The type of projection matrix effectively determines the type of light for the shadowing, where PerpectiveFov gives you a spotlight (with tapering shadows) and Ortho gives you a directional light.Quote:I think my problem was my far plane was too far away so I couldn't see any noticeable difference between the pixel colors.Yeah, getting the near and far planes right can be a bit tricky indeed. With my own shadow mapping adventures though I noticed that rendering an R32F surface as color can be a bit deceiving, as there is more 'precission' than your monitor can show. At least I've had pretty bland looking R32F shadow maps producing perfectly good shadows.Quote:Thanks for letting me use you like a corner to talk to myself and work things out!Hey, you're welcome [grin]