Jump to content

  • Log In with Google      Sign In   
  • Create Account


Adding depth to render targets?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Solid_Spy   Members   -  Reputation: 401

Like
0Likes
Like

Posted 29 November 2012 - 06:34 PM

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[i].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.

Sponsor:

#2 L. Spiro   Crossbones+   -  Reputation: 13293

Like
2Likes
Like

Posted 29 November 2012 - 09:44 PM

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, 30 November 2012 - 02:47 AM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#3 Solid_Spy   Members   -  Reputation: 401

Like
0Likes
Like

Posted 30 November 2012 - 01:03 PM

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!

#4 L. Spiro   Crossbones+   -  Reputation: 13293

Like
0Likes
Like

Posted 30 November 2012 - 05:50 PM

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, 30 November 2012 - 05:51 PM.

It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
I went to my local Subway once to find some guy yelling at the staff. When someone finally came to take my order and asked, “May I help you?”, I replied, “Yeah, I’ll have one asshole to go.”
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#5 Solid_Spy   Members   -  Reputation: 401

Like
0Likes
Like

Posted 03 December 2012 - 09:28 AM

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 [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, 03 December 2012 - 10:20 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS