Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2009
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. HRESULT DeferredPointLight(D3DXVECTOR3 vLightPosition, D3DXVECTOR3 vColour, float fLightRadius, float fLightIntensity, LPDIRECT3DSURFACE9 pBackBuffer)
  2. {
  3.     UINT uiPass, uiPassCount;
  4.     //HRESULT hr;
  5.  
  6.     V_RETURN(g_pd3dDevice->SetRenderTarget(0, g_pSurfaceLightMap));
  7.  
  8.     V_RETURN(g_pPointLight->SetTechnique("PointLight"));
  9.     V_RETURN(g_pPointLight->SetTexture("colorMap", g_pColourRT));
  10.     V_RETURN(g_pPointLight->SetTexture("normalMap", g_pNormalRT));
  11.     V_RETURN(g_pPointLight->SetTexture("depthMap", g_pDepthRT));
  12.  
  13.     // compute the light world matrix
  14.         // scale according to light radius, and translate it to light position
  15.     D3DXMATRIX mSphereWorldMatrix;
  16.     D3DXMATRIX mScaleMatrix;
  17.     D3DXMATRIX mTranslationMatrix;
  18.    
  19.     D3DXMatrixScaling(&mScaleMatrix, fLightRadius, fLightRadius, fLightRadius);
  20.     D3DXMatrixTranslation(&mTranslationMatrix, vLightPosition.x, vLightPosition.y, vLightPosition.z);
  21.     mSphereWorldMatrix = mScaleMatrix * mTranslationMatrix;
  22.  
  23.     D3DXMATRIX mViewProj = D3DXMATRIX(g_Camera.GetViewMatrix() * g_Camera.GetProjectionMatrix());
  24.     D3DXMATRIX mInverseViewProj;
  25.     D3DXMatrixInverse(&mInverseViewProj, NULL, &mViewProj);
  26.  
  27.     D3DXVECTOR3 vViewPosition = g_Camera.GetEyeVector();
  28.  
  29.     V_RETURN(g_pPointLight->SetValue("World", mSphereWorldMatrix, sizeof(mSphereWorldMatrix)));
  30.     V_RETURN(g_pPointLight->SetValue("View", g_Camera.GetViewMatrix(), sizeof(g_Camera.GetViewMatrix())));
  31.     V_RETURN(g_pPointLight->SetValue("Projection", g_Camera.GetProjectionMatrix(), sizeof(g_Camera.GetProjectionMatrix())));
  32.     V_RETURN(g_pPointLight->SetValue("lightPosition", vLightPosition, sizeof(vLightPosition)));
  33.     V_RETURN(g_pPointLight->SetValue("Color", vColour, sizeof(vColour)));
  34.     V_RETURN(g_pPointLight->SetFloat("lightRadius", fLightRadius));
  35.     V_RETURN(g_pPointLight->SetFloat("lightIntensity", fLightIntensity));
  36.     V_RETURN(g_pPointLight->SetValue("cameraPosition", vViewPosition, sizeof(vViewPosition)));
  37.     V_RETURN(g_pPointLight->SetValue("InvertViewProjection", mInverseViewProj, sizeof(mInverseViewProj)));
  38.  
  39.     float fCameraToCentre;
  40.     float xDistance;
  41.     float yDistance;
  42.     float zDistance;
  43.  
  44.     xDistance = pow(vViewPosition.x - vLightPosition.x, 2.0f);
  45.     yDistance = pow(vViewPosition.y - vLightPosition.y, 2.0f);
  46.     zDistance = pow(vViewPosition.z - vLightPosition.z, 2.0f);
  47.  
  48.     fCameraToCentre = pow(xDistance + yDistance + zDistance, 0.5f);
  49.  
  50.     if(fCameraToCentre < fLightRadius)
  51.     {
  52.         V_RETURN(g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW));
  53.     }
  54.  
  55.     else
  56.     {
  57.         V_RETURN(g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW));
  58.     }
  59.  
  60.     V_RETURN(g_pd3dDevice->SetVertexDeclaration(g_pVertexDecl));
  61.  
  62.     V_RETURN(g_pPointLight->Begin(&uiPassCount, 0));
  63.     for(uiPass = 0; uiPass < uiPassCount; uiPass++)
  64.     {
  65.         V_RETURN(g_pPointLight->BeginPass(uiPass));
  66.  
  67.         // Draw a fullscreen quad to sample the RT
  68.         DrawFullScreenQuad(0.0f, 0.0f, 1.0f, 1.0f);
  69.  
  70.         V_RETURN(g_pPointLight->EndPass());
  71.     }
  72.     V_RETURN(g_pPointLight->End());
  73.  
  74.     V_RETURN(g_pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW));
  75.  
  76.     return S_OK;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement