Jump to content
  • Advertisement
Sign in to follow this  
Sagito

Render to Texture

This topic is 3742 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

Hi, I've been trying to create a render-to-texture effect within my game engine, but I can't seem to get it working. I've been following this tutorial from two kings: http://www.two-kings.de/tutorials/dxgraphics/dxgraphics16.html However, the whole code stops working when I chand the pyramids by a sphere object (although it is the same size and in the same position as the pyramid). The code I'm using is the following: at main.cpp:
scene->beginRenderToTexture(renderTex, EColor(0, 255, 255, 0));
skull->draw();
scene->endRenderToTexture();

scene->begin(EColor(50, 50, 200, 0));
renderTex->applyTexture();
cube->draw();
skull->draw();
scene->end();

at EScene.cpp:
bool EScene::beginRenderToTexture(ETexture* tex, EColor backgroundColor)
{
	IDirect3DDevice9* device = this->window->getVideoDevice()->getVideoDriver();	

	if (!tex->isRenderTarget())
		return false;

	LPDIRECT3DSURFACE9 pRenderSurface = NULL;
	tex->getLowLevelTexture()->GetSurfaceLevel(0, &pRenderSurface);
	device->GetRenderTarget(0, &this->pBackBuffer);
	device->SetRenderTarget(0, pRenderSurface); 
	this->begin(backgroundColor);

	return true;
}

//End render to texture using active camera
void EScene::endRenderToTexture()
{
IDirect3DDevice9* device = this->window->getVideoDevice()->getVideoDriver();
device->EndScene();
device->SetRenderTarget(0, this->pBackBuffer);
device->SetTransform(D3DTS_PROJECTION, &this->cameras[this->activeCamera]->getProjectionMatrix());
}

Texture is being created with:
if (renderTarget)
	Usage |= D3DUSAGE_RENDERTARGET;

this->device->CreateTexture((UINT)width, (UINT)height, 1, Usage, D3DFMT_R5G6B5, D3DPOOL_DEFAULT, &this->texture, NULL);

The render texture is being filled with the clear color I specify, but the objects in the scene do not appear to be rendered to it. Any idea why? What am I doing wrong in here? =X Thanks...

Share this post


Link to post
Share on other sites
Advertisement
Things you might try:

Render the object to the back buffer instead of the render target, just to simplify the problem. I assume that the problem has nothing to do with the render target.

Use PIX to see the device state and the how the sphere is transformed.

Share this post


Link to post
Share on other sites
Thanks for your reply,

Everything in the code was correct and you were completely right! After your suggestion, I tried PIX and saw the render texture after rendering to it and before being applied and it was correct. The only thing that was missing were the texture coords of the cube mesh. For some reason the importer forgot that they existed and clamped the texture... Never thought of that...

Thanks again! :D

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!