I am having some trouble getting stencil shadows to work. Instead of creating a shadow it creates a distortion effect over the entire window where there is drawn.I haven't included any pictures because I don't think they're necessary, if you think they might help just tell me and I'll post em.
The Scene is drawn as follows
float clear[] = {0.0f, 0.0f, 0.0f, 1.0f }; //rgba pContext->ClearRenderTargetView(pRenderTargetView, clear); pContext->ClearDepthStencilView(pDepthStencilView,1,1,0); pContext->ClearDepthStencilView(pLight->pDepthStencil,1,1,0); RenderShadowMap(pLight); pContext->OMSetRenderTargets(1,&pRenderTargetView,pDepthStencilView); D3D11_MAPPED_SUBRESOURCE mappedResource; pContext->Map(pConstantBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); ConstantBufferType* constantBuffer; constantBuffer = (ConstantBufferType*)mappedResource.pData; //Set Matrices D3DXMatrixTranspose(&WorldMatrix, &WorldMatrix); D3DXMatrixTranspose(&ViewMatrix , &ViewMatrix); constantBuffer->World = WorldMatrix; constantBuffer->View = ViewMatrix; constantBuffer->Projection = ProjectionMatrix; constantBuffer->CamPos = D3DXVECTOR4(pCamera->getPosition(),1.0f); constantBuffer->LightView = pLights[0].LightView; pContext->Unmap(pConstantBuffer, 0); pContext->VSSetConstantBuffers(0 ,1 ,&pConstantBuffer); pContext->PSSetConstantBuffers(0 ,1 ,&pConstantBuffer); //Init Shaders UINT stride = sizeof(SimpleVert); UINT offset = 0; pContext->IASetVertexBuffers(0,1,&pVertexBuffer,&stride,&offset); pContext->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0); pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); pContext->IASetInputLayout(pInputLayout); //Set Shaders pContext->VSSetShader(pVertexShader,NULL,0); pContext->PSSetShader(pPixelShader,NULL,0); pContext->PSSetShaderResources(0,1,&pTexture); pContext->PSSetShaderResources(1,1,&pLights[0].pDepthMap); //Draw pContext->DrawIndexed(numIndices,0,0);
The pLight is a Light struct some of the relevant methods is as follows
void Light::RenderShaderResourceView(){ //Initialize everything needed// if I don't use a null tex it gives an error Still bound as Input ID3D11ShaderResourceView* pNULLTex = NULL; pContext->PSSetShaderResources(2,1,&pNULLTex); ID3D11RenderTargetView *pNullRTView = NULL; pContext->OMSetRenderTargets(1, &pNullRTView, pDepthStencil);}//The render shadowMap functionvoid Scene::RenderShadowMap(Light light){ D3D11_MAPPED_SUBRESOURCE mappedResource; pContext->Map(pDepthShaderBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource); ShadowMapBuffer* constantBuffer; constantBuffer = (ShadowMapBuffer*)mappedResource.pData; //Create View Matrix from light Point of view D3DXMatrixLookAtLH(&light.LightView,&D3DXVECTOR3(5.0f,15.0f,5.0f),&D3DXVECTOR3(0.0f,0.0f,0.0f),&D3DXVECTOR3(0.0f,1.0f,0.0f)); D3DXMatrixTranspose(&light.LightView,&light.LightView); D3DXMatrixTranspose(&ProjectionMatrix,&ProjectionMatrix); constantBuffer->Projection = ProjectionMatrix; constantBuffer->LightView = light.LightView; pContext->Unmap(pDepthShaderBuffer, 0); pContext->IASetInputLayout(pInputLayout); UINT stride = sizeof(SimpleVert); UINT offset = 0; pContext->IASetVertexBuffers(0,1,&pVertexBuffer,&stride,&offset); pContext->IASetIndexBuffer(pIndexBuffer, DXGI_FORMAT_R32_UINT, 0); pContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST); //Set Shaders pContext->VSSetShader(pDepthVertexShader,NULL,0); pContext->PSSetShader(NULL,NULL,0); light.RenderShaderResourceView(); pContext->DrawIndexed(numIndices,0,0);}
I know the code isn't very well organized but I am still learning DX
the shader code is as follows
PS_INPUT VS( VS_INPUT input ){ PS_INPUT output; input.pos.w = 1.0f; //Position output.pos = mul(input.pos, View); output.pos = mul(output.pos, Projection); //Lighting //Pass Along Values; output.norm = input.norm; output.tex = input.tex; output.lightTex = mul(input.pos,LightView); output.lightTex = mul(output.lightTex,Projection); return output;}float4 PS( PS_INPUT input ) : SV_TARGET{ float4 color = Texture.Sample(TextureSample, input.tex); //Shadow DEBUG //float2 shadowCol = NormalShader(input.pos, input.norm, CamPos, 20.0f, LightPos); float4 finalColor = color + 0.1f; //Simple is or isn't lit if I change biger than to smaller than the //distortion stays mostly the same, I added the float value because I //thought it might simply see everything in the shadow if more than //that no shadows if less everywhere shadows if(DepthMap.Sample(DepthSample,input.lightTex.xy) > input.lightTex.z/input.lightTex.w + 0.0028723f) finalColor = 0.1f; return finalColor;};PS_INPUT_SHADOWPASS ShadowMapVS( VS_INPUT_SHADOWPASS input ){ PS_INPUT_SHADOWPASS output; input.pos.w = 1.0f; output.pos = mul(input.pos,LightView); output.pos = mul(output.pos,Projection); return output;};//dont use the pixel shader but I did write one anywayfloat ShadowMapPS( PS_INPUT_SHADOWPASS input ) : SV_DEPTH{ return input.pos.z; }
The data structures in the shader
cbuffer PerFrameBuffer{ matrix World; matrix View; matrix Projection; matrix LightView; float4 CamPos;};struct PS_INPUT{ float4 pos : SV_POSITION; float2 tex : TEXCOORD0; float3 norm : NORMAL0; float4 lightTex : TEXCOORD1;};struct VS_INPUT{ float4 pos : POSITION; float2 tex : TEXCOORD0; float3 norm : NORMAL;};struct PS_INPUT_SHADOWPASS{ float4 pos : SV_POSITION;};struct VS_INPUT_SHADOWPASS{ float4 pos : POSITION;};Texture2D Texture;Texture2D<float> DepthMap;
the rastersate and depthstencil state
depthStencilDesc.DepthEnable = true; depthStencilDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL; depthStencilDesc.DepthFunc = D3D11_COMPARISON_LESS; depthStencilDesc.StencilEnable = true; depthStencilDesc.StencilReadMask = 0xFF; depthStencilDesc.StencilWriteMask = 0xFF; // Stencil operations if pixel is front-facing. depthStencilDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; depthStencilDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS; depthStencilDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR; depthStencilDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP; depthStencilDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS; rasterDesc.FillMode = D3D11_FILL_SOLID; rasterDesc.CullMode = D3D11_CULL_FRONT; rasterDesc.FrontCounterClockwise = true; rasterDesc.DepthBias = true; rasterDesc.DepthBiasClamp = 1.e5; rasterDesc.SlopeScaledDepthBias = 8.0f; rasterDesc.DepthClipEnable = true; rasterDesc.ScissorEnable = false; rasterDesc.MultisampleEnable = false; rasterDesc.AntialiasedLineEnable = false;
The dephtStencil is gotten from the depth render and share a texture.
Thanks in advance, any advice is welcome.
[Edited by - bushmanpaul on October 20, 2010 9:32:11 AM]