Jump to content
  • Advertisement
Sign in to follow this  
Solid_Spy

Adding depth to render targets?

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

Hey, I'm trying to render a 3D scene in a 2D surface, however, for some reason, the zbuffer doesn't work when i use it!

Here's my code:


void render_frame(void)
{
shaderbuff->GetSurfaceLevel(0, &blur_surface1);
d3ddev->SetRenderTarget(0, blur_surface1);
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(1, 0, 0, 0), 1.0f, 0);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();
d3ddev->SetFVF(CUSTOMFVF);

if(playing == 1)
{
D3DXMATRIX matView;
D3DXMatrixLookAtLH(&matView,
&D3DXVECTOR3 (x, y + 1, z + 10),
&D3DXVECTOR3 (cos(yaw) + x, y + 1, sin(yaw) + z + 10), // the look-at position
&D3DXVECTOR3 (0.0f, 1.0f, 0.0f));
d3ddev->SetTransform(D3DTS_VIEW, &matView);
D3DXMATRIX matProjection;
D3DXMatrixPerspectiveFovLH(&matProjection,
D3DXToRadian(45),
(FLOAT)SCREEN_WIDTH / (FLOAT)SCREEN_HEIGHT,
0.0f,
100.0f);
d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection); // set the projection
D3DXMATRIX mattranslate;
for(int i = 0; i < 16; i++)
{
for(int j = 0; j < 16; j++)
{
D3DXMatrixTranslation(&mattranslate, i * 3.0f, 0.0f, j * 3.0f);
d3ddev->SetTransform(D3DTS_WORLD, &mattranslate);
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));
d3ddev->SetIndices(i_buffer);
d3ddev->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 4, 0, 2);
}
}
}
d3ddev->EndScene();
d3ddev->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer);
d3ddev->SetRenderTarget(0, backbuffer);
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, 0, NULL, NULL);
d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
d3ddev->BeginScene();

d3ddev->StretchRect(blur_surface1, &rect, backbuffer, &rect2, D3DTEXF_NONE);
if(playing == 0)
{
for(UINT i = 0; i < objectlist.size(); i++)
{
objectlist.Render();
}
}
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
d3ddev->SetTexture(0, NULL);
}


I give credit to DirectXTutorial.com for helping me write the code.

Please, I don't know what i'm doing wrong here, can someone please help me out? thanks.

Share this post


Link to post
Share on other sites
Advertisement
Where do you believe you are “using” your depth buffer?
There is no sign of such use in the code you posted. Are we to assume you have actually created it and it is set?


There are several other problems with your code.
Firstly you should only BeginScene() and EndScene() once per frame.
Secondly don’t make multiple calls to Clear(). Do this:
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0);
Notice also that the D3DCOLOR_ARGB() is not used correctly in your code. Full alpha = 255, not 1.
If you do actually have a depth buffer and it is bound, there is no code shown that actually “uses” it. You never enabled depth testing via a call to SetRenderState( D3DRS_ZENABLE, TRUE ).
Even though this should be done automatically if you created a depth buffer on the creation of your present parameters, you should call it manually to be sure.


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites

Where do you believe you are “using” your depth buffer?
There is no sign of such use in the code you posted. Are we to assume you have actually created it and it is set?


There are several other problems with your code.
Firstly you should only BeginScene() and EndScene() once per frame.
Secondly don’t make multiple calls to Clear(). Do this:
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB(255, 0, 0, 0), 1.0f, 0);
Notice also that the D3DCOLOR_ARGB() is not used correctly in your code. Full alpha = 255, not 1.
If you do actually have a depth buffer and it is bound, there is no code shown that actually “uses” it. You never enabled depth testing via a call to SetRenderState( D3DRS_ZENABLE, TRUE ).
Even though this should be done automatically if you created a depth buffer on the creation of your present parameters, you should call it manually to be sure.


L. Spiro


Ok, however i did set the zbuffer to true, and sorry for all the mistakes, i've been learning from different sources.

I just don't know if surfaces should have depth or not. It seems like only the pixels get drawn, but aren't stenciled.

It works whenever i have the rendering to back buffer without using any of the render targets, but not when i render to a surface.

Please help!

Share this post


Link to post
Share on other sites
Why don’t you post a screenshot or 2 to show what you mean?


Additionally, GetBackBuffer() and GetSurfaceLevel() increase reference counts and Release() must be called afterwards on the respective objects.
Comments in your code would go a long way towards helping us help you. I really can’t understand the point of the code (why StretchRect()??) and there are functions such as objectlist[i].Render() that can only leave us guessing. I assume this call is not related to your problem, but comments would help to know for sure.


L. Spiro Edited by L. Spiro

Share this post


Link to post
Share on other sites

Why don’t you post a screenshot or 2 to show what you mean?


Additionally, GetBackBuffer() and GetSurfaceLevel() increase reference counts and Release() must be called afterwards on the respective objects.
Comments in your code would go a long way towards helping us help you. I really can’t understand the point of the code (why StretchRect()??) and there are functions such as objectlist.Render() that can only leave us guessing. I assume this call is not related to your problem, but comments would help to know for sure.


L. Spiro


nvm i found out the problem :]

I had the near view plane set to 0.0f. I set it there because i though it would be better, but apparently it broke the depth buffer. Edited by Solid_Spy

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!