Advertisement Jump to content
Sign in to follow this  

projective texture in a vertex shader

This topic is 4989 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

I'm trying to emulate the fixed function pipline like m_pd3dDevice->SetTextureStageState(TexStage, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_COUNT4 | D3DTTFF_PROJECTED); in a vertex shader (for a depth shadow map) I use this but it don't work: ;c12 contain matProjLight*matViewLight*matModel*matBias ... m4x4 r0,v0,c12 rcp r0.w, r0.w mul,, r0 mov oT1,r0 ... My code work fine with fixed pipeline.

Share this post

Link to post
Share on other sites
Firstly, apologies for not even attempting to answer your question :)

Secondly, I see you're doing shadow mapping in DX9... I'm also trying to do this, but failing *miserably*. I just can't seem to get anything to render to a depth buffer. It'd be awesome if you could take a quick look at what I'm doing, cos I feel like I'm getting nowhere with it.

I'm following this technique ( but I can't get it to work. I'm creating and setting the depth buffers like this:

eng->device->CreateTexture(shadowMapSize, shadowMapSize, 1, D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &shadowMapTargetTexture, NULL);
shadowMapTargetTexture->GetSurfaceLevel(0, &shadowMapTargetSurface);
eng->device->SetRenderTarget(0, shadowMapTargetSurface);

eng->device->CreateTexture(shadowMapSize, shadowMapSize, 1, D3DUSAGE_DEPTHSTENCIL, D3DFMT_D24S8, D3DPOOL_DEFAULT, &shadowMapDepthTexture, NULL);
shadowMapDepthTexture->GetSurfaceLevel(0, &shadowMapDepthSurface);

I then render the scene from the light (I've outputted a screen from the target surface of this rendering pass and everything seems ok).

I then reset everything back so it's rendering to the window, and the right kinds of shading are enabled etc.

I then set a dim light up, and set projection and view matrices for the camera up and render the scene in dim light (Screen: - the light is the small ball in the top-left).

I then get ready to projectively texture the shadow map onto the scene this this :

eng->device->SetRenderState(D3DRS_ALPHABLENDENABLE, 1);
eng->device->SetTexture(1, shadowMapDepthTexture);
eng->device->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
eng->device->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
eng->device->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
eng->device->SetSamplerState(1, D3DSAMP_ADDRESSU, D3DTADDRESS_CLAMP);
eng->device->SetSamplerState(1, D3DSAMP_ADDRESSV, D3DTADDRESS_CLAMP);
eng->device->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
eng->device->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

(I have regular textures in texture unit 0, so this has to be done in texture unit 1 - are my multitexturing stages ok for this?). I then set up the texture matrix in exactly the same way as in the paulsprojects tutorial, and set it to be done in texture unit 1.

I then enable a bright light and render the scene again. But no shadows appear, instead it's as if everything is shadowed (Screen:

Have you got any idea what I could be doing wrong? It's got to be something obvious! When I projectively render the *target* texture rather than the depth texture I do get messy shadow maps, but obviously I need the depth test to make them clean and tidy so this isn't a solution to the problem.

Any ideas? I'm totally stuck.

Thanks for any help, Duncan

Share this post

Link to post
Share on other sites
try this:

int TexStage=1;
m_pd3dDevice->SetTexture(TexStage, m_pShadowBufferDepthTexture);

//use linear filtering
m_pd3dDevice->SetTextureStageState(TexStage, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
m_pd3dDevice->SetTextureStageState(TexStage, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
//Set up texture coordinate generation

m_pd3dDevice->SetTextureStageState( TexStage, D3DTSS_ADDRESSU, D3DTADDRESS_BORDER );
m_pd3dDevice->SetTextureStageState(TexStage, D3DTSS_ADDRESSV, D3DTADDRESS_BORDER );
m_pd3dDevice->SetTextureStageState( TexStage, D3DTSS_BORDERCOLOR, 0xffffffff );
m_pd3dDevice->SetTextureStageState( TexStage, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
m_pd3dDevice->SetTextureStageState( TexStage, D3DTSS_COLOROP, D3DTOP_MODULATE );
m_pd3dDevice->SetTextureStageState( TexStage, D3DTSS_COLORARG1, D3DTA_TEXTURE );

//Set up texture matrix
D3DXMATRIX inverseView;
D3DXMatrixInverse(&inverseView, NULL, &m_matView);

D3DXMATRIX textureMatrix=inverseView * m_matViewLightDX*m_matProjLightDX*biasMat;

m_pd3dDevice->SetTransform(D3DTS_TEXTURE1, &textureMatrix);

with pixel shader:
def c0, 0.5f, 0.5f, 0.5f, 1.0f

tex t0 //your base texture
tex t1 //depth texture

add r1, t1, c0
mul r0, v0, r1
mul r0, t0, r0

pay attention, texture usage of type "D3DUSAGE_DEPTHSTENCIL" are not recognized by ATI hardware, on NVIDIA no problem.

sorry for my bad english.

Share this post

Link to post
Share on other sites
Your english is fine, I understood what you were saying :)

Thanks for the reply, but it doesn't seem to have done anything. Everything is still rendered dimly, as if everything were in shadow. Are you using DirectX 9.0? If so, how are you rendering to your depth buffer when generating the depth texture?

Cheers, Duncan

Share this post

Link to post
Share on other sites
//my code
//"m_pDC" is derived from the Direct3D device (var from my engine).

m_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT);
static D3DVIEWPORT8 viewport = { 0, 0, SHADOWBUFFERWIDTH, SHADOWBUFFERHEIGHT, 0.0f, 1.0f };
if (
!SUCCEEDED(m_pd3dDevice->SetRenderTarget( m_DummyRenderTargetSurface, m_pShadowBufferDepthSurface ))

m_pd3dDevice->SetViewport( &viewport );

m_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, 0x000000000, 1.0f, 0L );

m_pd3dDevice->SetTexture(0, NULL);
m_pd3dDevice->SetTexture(1, NULL);

//Deactivate lighting

m_VC->setMatrix(DC_MATRIX_STACK_PROJECTION, m_matProjLight);
m_VC->setMatrix(DC_MATRIX_STACK_MODELVIEW, m_matViewLight);

//Render your primitive here

//Restore lighting

// Restore your target surface

m_pd3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);

//try to save your texture to view from the light point of view
D3DXSaveTextureToFile("c://light.bmp",D3DXIFF_BMP, m_DummyRenderTargetTexture, NULL);
If the image is good the stencil surface must be good too.

Share this post

Link to post
Share on other sites
The image is good. But still the depth texture does not work - everything is rendered shadowed. Is there anyway I can check the depth texture manually? It does not work when I use D3DXSaveTextureToFile. Thanks for your help.

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!