Sign in to follow this  

Problems rendering to texture

Recommended Posts

Hey there! Im creating a level-editor for a game. As usual you can add objects to this level. In order to facilate the process of choosing the right model i decided to make a preview within a little frame of the scene. I googled that and found rendering to a texture as a good solution. Now thats my code i use:
void BrowsingUI::Render3D()
	IDirect3DSurface9* pSurface, *pOldSurface;
	sD3D->CreateRenderTarget(150, 250, D3DFMT_A8R8G8B8, D3DMULTISAMPLE_NONE, 0, TRUE, &pSurface, 0);

	sD3D->GetRenderTarget(0, &pOldSurface);
	sD3D->SetRenderTarget(0, pSurface);

	D3DXMATRIX matViewOld, matViewNew, matProjOld, matProjNew;
	D3DXVECTOR3 eye(10, 0, 0), at(0, 0, 0), up(0, 1, 0);
	D3DXMatrixLookAtLH(&matViewNew, &eye, &at, &up);
	D3DXMatrixPerspectiveFovLH(&matProjNew, D3DXToRadian(45.0f), 150.0f / 250.0f, 0.0f, 1000.0f);

	sD3D->GetTransform(D3DTS_VIEW, &matViewOld);
	sD3D->GetTransform(D3DTS_PROJECTION, &matProjOld);
	sD3D->SetTransform(D3DTS_VIEW, &matViewNew);
	sD3D->SetTransform(D3DTS_PROJECTION, &matProjNew);


	sD3D->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0, 1.0f, 0);

	m_pModel->Draw(Vertex3F(10, 0, 0), Vertex3F(0, 0, 0));

	sD3D->SetRenderTarget(0, pOldSurface);
	sD3D->SetTransform(D3DTS_VIEW, &matViewOld);
	sD3D->SetTransform(D3DTS_PROJECTION, &matProjOld);

	sD3D->Present(0, 0, 0, 0);

	::D3DXSaveSurfaceToFile("Target.jpg", D3DXIFF_JPG, pSurface, 0, 0);

Now im facing a problem: All that is rendered in 2D works perfectly. Text and UI get saved as i whish but the m_pModel->Draw has no effect! With this code i only get a black image. I checked that everything works fine within my Draw-Code of the model. Is there anything special i have to concern if i render to a texture? Greetings Plerion

Share this post

Link to post
Share on other sites
Well, I think I had something like this, too, but with two swapchains, which should be the same.

I could render the UI-stuff to the Viewport, but my meshes weren't shown.
After some time I found out, that the Mesh is drawn, but outside the screen!

To fix this problem, just call Device->SetViewport() before you draw to the texture with the settings of your texture, and after it call it again with the default settings.

Hope that helps :)

Share this post

Link to post
Share on other sites
Off the top of my head I would guess that there is some problem either with the z-buffer configuration, or that one of the view/projection matrices aren't set up properly (both of which would cause a black screen - i.e. nothing rendered). One easy way to check these things is to run your application in PIX, and then inpect the results of your draw calls before and after vertex transformation.

There are many possible uses for PIX, so it is a good time investment to learn how to use it!

Share this post

Link to post
Share on other sites

Thanks for the tip, ill keep that in mind. But the main problem was that i messed up with my renderstates so now everything gets displayed as i like to.


Share this post

Link to post
Share on other sites
Glad you sorted it out, but just wanted to comment that creating the surface every draw is not good practice. I figure it's just debugging code, but still...

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