Adding depth to render targets?

Started by
3 comments, last by SolDirix 11 years, 4 months ago
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.

View my game dev blog here!

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


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!

View my game dev blog here!

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

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid


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.

View my game dev blog here!

This topic is closed to new replies.

Advertisement