Skip to main content
GameDev.net gamedev.net
🔒 Locked

Render to Texture

Started by Sagito Jul 20, 2008 at 4:36 PM 1 replies 1.1k views
Original Post
Sagito
Sagito
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...
http://sagito.wordpress.com
ET3D
ET3D
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.
Sagito
Sagito
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
http://sagito.wordpress.com

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.