Rendering to texture problems (OpenGL)

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
andyZER0
Posts: 5
Joined: Thu Jan 05, 2006 7:37 am
Location: Irvine, CA, USA
Contact:

Rendering to texture problems (OpenGL)

Post by andyZER0 »

What I'm trying to accomplish is rendering a scene to a texture and displaying it on the screen as if it was the scene. (I'm actually just experimenting in hopes of using HDR in Irrlicht :)

This is the screen resolution

Code: Select all

#define DIMENSION_X 640
#define DIMENSION_Y 480
So I have my ITexture pointer...

Code: Select all

ITexture *rTexture = driver->createRenderTargetTexture(dimension2d<s32>(DIMENSION_X, DIMENSION_Y));
And basically straight out of the render to texture tutorial

Code: Select all

driver->beginScene(true, true, SColor(255,100,101,140));

		if (rTexture)
		{
			// set render target texture
			driver->setRenderTarget(rTexture,true,true);

			// set cube invisible then set active camera to the fixed one
			cube0->setVisible(false);
			lightBox->setVisible(false);
			smgr->setActiveCamera(fixedCamera);

			// now render
			smgr->drawAll();

			driver->setRenderTarget(0,true,true); // get back to old target
			
			driver->draw2DImage(rTexture, position2d<s32>(0,0), rect<s32>(0,0,DIMENSION_X,DIMENSION_Y));

			cube0->setVisible(true);
			lightBox->setVisible(true);
			smgr->setActiveCamera(camera);
		}
		//smgr->drawAll();

		driver->endScene();
I receive the results I want using the Direct3D9 renderer, but when I use OpenGL, I get...
Image

Anyone have this problem or know how to fix this? Thanks in advance.
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

OpenGL texture coordinates have the origin at the bottom left. DirectX texture coordinates are at the top left.

To work around this I think you need to either render the texture in such a way that it is mirrored automatically or flip the texture coordinates of the mesh that you are mapping the texture onto.

If you are doing a post processing effect, the solution is going to be pretty simple. You will probably map the rtt to a full screen quad and render that to the viewport. If you do that, all you need to do is flip the Y coordinates of that full screen quad when you're using an OpenGL driver. If you are using a shader, you can invert the texture coordinates in the vertex shader.

Travis
mandrav
Posts: 117
Joined: Sat Aug 27, 2005 8:29 pm
Contact:

Re: Rendering to texture problems (OpenGL)

Post by mandrav »

andyZER0 wrote:What I'm trying to accomplish is rendering a scene to a texture and displaying it on the screen as if it was the scene. (I'm actually just experimenting in hopes of using HDR in Irrlicht :)

This is the screen resolution

Code: Select all

#define DIMENSION_X 640
#define DIMENSION_Y 480
Regarding the image flipping, read vitek's post.
You should be using power-of-2 dimensions for your rendertarget texture (like 256x256, 512x512, etc).

Note though that Irrlicht uses the framebuffer when rendering to rendertargets and then copies the framebuffer to the texture (using glCopySubTexImage2D).
This means that you cannot render to a rendertarget whose dimensions are bigger than the framebuffer (actually you can, but it will be clipped).
So, for your example, the biggest rendertarget you could use would be 256x256 (256 is less than 480 - the smaller dimension).

Who knows, maybe I will contribute a real off-screen rendertarget (OpenGL only, sorry) so that this won't hinder anyone else ;)

Yiannis.
Post Reply