Jump to content
  • Advertisement
Sign in to follow this  
Plerion

Problems rendering to texture

This topic is 3118 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 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->BeginScene();

	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->EndScene();
	sD3D->Present(0, 0, 0, 0);

	::D3DXSaveSurfaceToFile("Target.jpg", D3DXIFF_JPG, pSurface, 0, 0);
	pSurface->Release();
}

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
Advertisement
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
Hey!

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.

Greetings
Plerion

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
Yes, it only was debugging code. I have a texture that get created once and i take its surface as render target.

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!