Advertisement Jump to content
Sign in to follow this  

Projected textures + view space

This topic is 3107 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I have recently added projected texturing to my directx 10 game to allow for the use of textures as spotlights. The problem I have is that I have a model which is in view space (it has an identity matrix for the view matrix) so that the model is always in view (it's a hand and gun).

I need my new projected spotlights to cast light on the model when the player is in range, previously I would just translate the spotlights position and direction into view space and perform the spotlight calculation as normal.

With projected texturing this method no longer seems to work, i'm having some trouble visualizing excatly what I need to do for the lighting to be correct again. The projected texture has a view and projection matrix which defines the frustum that it casts light on.

If anyone could give any nudges in the right direction I would be very grateful.


Share this post

Link to post
Share on other sites
Okay so you have a view and projection matrix that you use for generating the projective texture coordinate. This lets you go from world-space to texture-space. In your shader you'll have positions in view space, which means you need to go from view-space to world-space to texture-space. Making a matrix that does this is easy: just take the inverse of the view matrix (AKA the camera's world matrix) and transform it with the view and projection matrices for your projection. So InvCamView * LightView * LightProj.

Share this post

Link to post
Share on other sites
Thanks, I have tried your above suggestion and I'm getting somewhere.
Currently I have your suggested matrix CameraInverse * LightView * LightProj, I have added this into my existing projected spotlight calculation, but now with the new HUD model matrix.

pSpotLight.mViewProjMatrixHUD in the code below is the above matrix calculation for this particular spotlight. The problem i'm having now is that when I perform the calculation with the models world co-ordinates light shines on the model when you are close to it's world position (which is obviously not desired because it's a view space object).

So I thought to correct that I would just the final position of the model i.e. it's position when multiplied by world * identity * proj. But this provides no lighting on the model at all - i'm probably being dense here, but I seem to get lost in what space i'm in at each point!

This piece of code is called from within the pixel shader for each spotlight shining on this given world object. I have tried it with pSurface.mPosition as the world position and the homogenous clip space position (world * identity * proj) to no avail.

float4 CalcSpotLightHUD(gSurfaceInfo pSurface, gSpotLight pSpotLight, uint pTexIndex)
float4 wLitColour = float4(0.0f, 0.0f, 0.0f, 0.0f);
float3 wLightVecCurr;
float wDiffuseFactor;
float wConeFade;

wLightVecCurr = (pSpotLight.mPosition - pSurface.mPosition) / length(pSpotLight.mPosition - pSurface.mPosition);
wDiffuseFactor = dot(wLightVecCurr, pSurface.mNormal);
wConeFade = dot(-wLightVecCurr, pSpotLight.mDirection);

if (wDiffuseFactor > 0.0f && wConeFade > 0.0f)
float4 wSpotTexColour;
float4 wFinalSpotTexColour;
float4 wProjTexCoords;
float3 wLightVecMax;
float wAtt;

wLightVecMax = (pSpotLight.mPosition - pSurface.mPosition) / pSpotLight.mRange;
wAtt = max(1.0f - dot(wLightVecMax, wLightVecMax), 0.0f);
wProjTexCoords = mul(float4(pSurface.mPosition, 1.0f), pSpotLight.mViewProjMatrixHUD); /= wProjTexCoords.w;
wProjTexCoords.x = 0.5f * wProjTexCoords.x + 0.5f;
wProjTexCoords.y = -0.5f * wProjTexCoords.y + 0.5f;
wSpotTexColour = gSpotLightTextures[pTexIndex].Sample(gSpotLightFilter, wProjTexCoords.xy);
wFinalSpotTexColour = float4( * wSpotTexColour.a, wSpotTexColour.a) * pSpotLight.mSpotPower;
wLitColour = (wDiffuseFactor * pSurface.mDiffuse * wFinalSpotTexColour) * pSpotLight.mDiffuse;

return wLitColour * wAtt;

return wLitColour;

Here is the inverse calculation code:

D3DXMatrixInverse(&nCameraInverseViewMatrix, NULL, &nCameraViewMatrix);

Thanks again.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!