Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HRESULT DeferredPointLight(D3DXVECTOR3 vLightPosition, D3DXVECTOR3 vColour, float fLightRadius, float fLightIntensity, LPDIRECT3DSURFACE9 pBackBuffer)
- {
- UINT uiPass, uiPassCount;
- //HRESULT hr;
- V_RETURN(g_pd3dDevice->SetRenderTarget(0, g_pSurfaceLightMap));
- V_RETURN(g_pPointLight->SetTechnique("PointLight"));
- V_RETURN(g_pPointLight->SetTexture("colorMap", g_pColourRT));
- V_RETURN(g_pPointLight->SetTexture("normalMap", g_pNormalRT));
- V_RETURN(g_pPointLight->SetTexture("depthMap", g_pDepthRT));
- // compute the light world matrix
- // scale according to light radius, and translate it to light position
- D3DXMATRIX mSphereWorldMatrix;
- D3DXMATRIX mScaleMatrix;
- D3DXMATRIX mTranslationMatrix;
- D3DXMatrixScaling(&mScaleMatrix, fLightRadius, fLightRadius, fLightRadius);
- D3DXMatrixTranslation(&mTranslationMatrix, vLightPosition.x, vLightPosition.y, vLightPosition.z);
- mSphereWorldMatrix = mScaleMatrix * mTranslationMatrix;
- D3DXMATRIX mViewProj = D3DXMATRIX(g_Camera.GetViewMatrix() * g_Camera.GetProjectionMatrix());
- D3DXMATRIX mInverseViewProj;
- D3DXMatrixInverse(&mInverseViewProj, NULL, &mViewProj);
- D3DXVECTOR3 vViewPosition = g_Camera.GetEyeVector();
- V_RETURN(g_pPointLight->SetValue("World", mSphereWorldMatrix, sizeof(mSphereWorldMatrix)));
- V_RETURN(g_pPointLight->SetValue("View", g_Camera.GetViewMatrix(), sizeof(g_Camera.GetViewMatrix())));
- V_RETURN(g_pPointLight->SetValue("Projection", g_Camera.GetProjectionMatrix(), sizeof(g_Camera.GetProjectionMatrix())));
- V_RETURN(g_pPointLight->SetValue("lightPosition", vLightPosition, sizeof(vLightPosition)));
- V_RETURN(g_pPointLight->SetValue("Color", vColour, sizeof(vColour)));
- V_RETURN(g_pPointLight->SetFloat("lightRadius", fLightRadius));
- V_RETURN(g_pPointLight->SetFloat("lightIntensity", fLightIntensity));
- V_RETURN(g_pPointLight->SetValue("cameraPosition", vViewPosition, sizeof(vViewPosition)));
- V_RETURN(g_pPointLight->SetValue("InvertViewProjection", mInverseViewProj, sizeof(mInverseViewProj)));
- float fCameraToCentre;
- float xDistance;
- float yDistance;
- float zDistance;
- xDistance = pow(vViewPosition.x - vLightPosition.x, 2.0f);
- yDistance = pow(vViewPosition.y - vLightPosition.y, 2.0f);
- zDistance = pow(vViewPosition.z - vLightPosition.z, 2.0f);
- fCameraToCentre = pow(xDistance + yDistance + zDistance, 0.5f);
- if(fCameraToCentre < fLightRadius)
- {
- V_RETURN(g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW));
- }
- else
- {
- V_RETURN(g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW));
- }
- V_RETURN(g_pd3dDevice->SetVertexDeclaration(g_pVertexDecl));
- V_RETURN(g_pPointLight->Begin(&uiPassCount, 0));
- for(uiPass = 0; uiPass < uiPassCount; uiPass++)
- {
- V_RETURN(g_pPointLight->BeginPass(uiPass));
- // Draw a fullscreen quad to sample the RT
- DrawFullScreenQuad(0.0f, 0.0f, 1.0f, 1.0f);
- V_RETURN(g_pPointLight->EndPass());
- }
- V_RETURN(g_pPointLight->End());
- V_RETURN(g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW));
- return S_OK;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement