# 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.

## 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.

Thanks.

##### 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 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.xyz	/= 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.xyz * 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.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015655
×