Rendering to a texture HELP!

Started by
2 comments, last by shaqdx 18 years, 8 months ago
Hi, I am having major problems with finding examples or tutorials on how to render to a texture. I want to render a basic quad (which has its own texture) onto a larger quad (which has its own texure aswell). I have the basic idea of using createTexture and setRenderTarget but have no idea how to use them. Can anyone please help? Thanks
Advertisement
http://www.codesampler.com/source/dx9_offscreen_rendering.zip
First off, why do you want to render one quad onto another? I don't quite see the point? Are you just trying to increase the size of the texture by rendering to a larger quad? Nonetheless, I'll place a quick sample of what you need to do to make the textures.

        // directx texture and surface (both required since we write to the surface not the texture)        LPDIRECT3DTEXTURE9 m_pTexture;	LPDIRECT3DSURFACE9 m_pSurface;	// create texture	if( FAILED( g_pD3DDevice->CreateTexture( WIDTH, HEIGHT, 1,                                      D3DUSAGE_RENDERTARGET, D3DFMT_A8R8G8B8,		// D3DFMT_ is the format, we are just using regular 8-bit RGBA                                     D3DPOOL_DEFAULT, &m_pTexture, NULL)))			 return FALSE;	// grab the surface -- we dont write directly to the texture	if(FAILED(m_pTexture->GetSurfaceLevel(0, &m_pSurface)))		return FALSE;


okay, so this is the initialization. We create both a texture and surface. They both return HRESULTs and it's usually best to error check here, which is why I have the FAILED bits, but it's not absolutely necessary.
Then to write to the texture you just need to use

D3DDevice->setRenderTarget( m_pSurface )

(or it might ask for &m_pSurface -- I can't be bothered to check about that at the moment).

Finally, and this is very important, make SURE you save the address of the screen backbuffer BEFORE you set it to anything else since otherwise you will LOSE it and you wont be able to render to the screen. Do this with

LPDIRECT3DTEXTURE9 g_pBackBuffer;g_pD3DDevice->GetRenderTarget(0, &g_pBackBuffer);


now you must setRenderTarget( &g_pBackBuffer ) whenever you want to render to the screen again.
I hope this is a concise crash-course. Read the tutorial above for more detail, since this really is barebones, but it gives you what you need.
Thanks FancyZero and rjackets. That's helped alot.

The reason for me texturing a smaller quad to a larger quad is because I am trying to perform effects on the smaller quad using shaders and I needed the smaller qaud rendered ON the larger one.

Also, there is another tutorial I found that does not use "off-screen rendering" like the one above.

http://www.two-kings.de/tutorials/d3d16/d3d16.html

Thanks

This topic is closed to new replies.

Advertisement