Sign in to follow this  
Sparhawk42

MeshRendering to Rendertarget works only in DebugMode

Recommended Posts

Sparhawk42    138
I've a small problem with rendering a Mesh to a surface. My Codes works just fine, when I activate the Direct3D_Debug-Mode: But when I activate the retail mode something doesn't work as my RenderTarget now fills the whole window: Here you can download my whole Code And here the essence of my onFrameRender-Method (not yet really tidy...):
void CALLBACK OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
    HRESULT hr;

    // Clear the render target and the zbuffer 
    	V( pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0) );

    // Render the scene
    if( SUCCEEDED( pd3dDevice->BeginScene() ) )
    {
				IDirect3DSurface9* surface_ = NULL;
				IDirect3DSurface9* origTarget_ = NULL;
	

	  pd3dDevice->GetRenderTarget( 0, &origTarget_ );
	  pd3dDevice->CreateRenderTarget( 320, 240, D3DFMT_X8R8G8B8, origTarget_->MultiSampleType,
																			 origTarget_->MultiSampleQuality,false, &surface_, NULL );

							RECT DstRect;
		// scale pciture for actual resolution in Position and size
		DstRect.left = 0;
		DstRect.top = 0;
		DstRect.right = 320;
		DstRect.bottom = 240;
		
		pd3dDevice->SetRenderTarget( 0, surface_ );
	
		
		pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,30), 1, 0  );
		D3DXMATRIXA16 trans;
		D3DXMatrixRotationY( &trans, timeGetTime()/5000.0f );
		
		D3DXMATRIXA16 trans2;
		D3DXMatrixRotationZ( &trans2, timeGetTime()/5000.0f );
	
		trans *= trans2;

		pd3dDevice->SetTransform( D3DTS_WORLD, &trans );
    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMATRIXA16 matView;
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
    pd3dDevice->SetTransform( D3DTS_VIEW, &matView );

		D3DXMATRIXA16 matProj;
		D3DXMatrixPerspectiveFovLH( &matProj, 
			                          D3DX_PI/4, 1.0f, 1.0f, 100.0f  );

    pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
		pd3dDevice->SetRenderState( D3DRS_AMBIENT, D3DXCOLOR( 1.0, 1.0, 1.0, 1.0 ) );
					

		static D3DMesh* mesh = new D3DMesh( "tiger.x" );
		static bool loaded = false;
		if (!loaded ) 
		{
			mesh->loadFromFile( pd3dDevice );
			loaded = true;
		}
		mesh->draw( pd3dDevice );
		
		DstRect.left = 40;
		DstRect.top = 30;
		DstRect.right = 360;
		DstRect.bottom = 270;				
	  pd3dDevice->SetRenderTarget( 0, origTarget_ );	

	  IDirect3DSurface9* backBuffer = NULL;

	  if( !FAILED( pd3dDevice->GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer ) ) )
	  {

		  pd3dDevice->StretchRect( surface_, NULL, backBuffer, &DstRect, D3DTEXF_NONE );
	  }

		if( backBuffer ) backBuffer->Release();
		if( surface_ ) surface_->Release();
		if( origTarget_ ) origTarget_->Release();

        V( pd3dDevice->EndScene() );
    }
}

Thanks for any help!!! I don't really know what goes wrong...especially as does code works fine in Debug-modus... and yes I know Rendering via a texture would be much faster, but this just my first try and I want to use this code mainly for something like a civiliopedia, where only one element is shown at a given time.

Share this post


Link to post
Share on other sites
S1CA    1418
Lesson for today: using undocumented data members of Direct3D interfaces directly in your code is a really bad idea (compatibility, readability, etc) and is also the cause of your troubles...

Those members are only present when D3D_DEBUG_INFO is defined and are only meant to be used in MSVC Watch windows for debugging use rather than as handy shortcuts for accessing surface descriptions.


Change this:

pd3dDevice->CreateRenderTarget( 320, 240, D3DFMT_X8R8G8B8, origTarget_->MultiSampleType, origTarget_->MultiSampleQuality,false, &surface_, NULL );



To this:

D3DSURFACE_DESC desc;
origTarget_->GetDesc( &desc );
pd3dDevice->CreateRenderTarget( 320, 240, D3DFMT_X8R8G8B8, desc.MultiSampleType, desc.MultiSampleQuality,false, &surface_, NULL );


And you'll get consistent behaviour between the Debug and Retail Direct3D runtimes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this