Render to texture water reflection

You are an experienced programmer and have a problem with the engine, shaders, or advanced effects? Here you'll get answers.
No questions about C++ programming or topics which are answered in the tutorials!
esaptonor
Posts: 145
Joined: Sat May 06, 2006 11:59 pm

Render to texture water reflection

Post by esaptonor »

I am trying to make my own water scene node. I have a big plane, and i flip the screen upside down and render that to the texture for the reflection. But the problem is it doesn't line up on my plane to where it is suppose to.
How on earth am i ment to line up the RTT'ed texture so that it looks like its in the correct place?
Has somebody perhaps got an example of this already? I have seen shader ones which didn't look right, and i have tried a mirror scene node i found in the code snippets but it did not have the desired effect.

thanks for your help,
esaptonor
hey_i_am_real
Posts: 44
Joined: Thu Sep 28, 2006 2:27 pm
Location: Europe

Post by hey_i_am_real »

do you have some screen-capture to show ?
esaptonor
Posts: 145
Joined: Sat May 06, 2006 11:59 pm

Post by esaptonor »

Image

Its hard to convey the problem accuratly...but what i am after is the terrain to be reflected in the water...and parts of it are, but i have no idea how to line it up and make everything reflect properly.
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

Use texture projection, the viewer camera being the projector, with the "capture camera" being equally below the water as the viewer cam is above, the pitch of the capture cam is inversed while the other rotations are equal to the viewer cam.
ProSoft
Posts: 59
Joined: Thu Sep 08, 2005 10:55 am
Location: Brasil

Post by ProSoft »

This topic is of my interest to. Can you be more spcific? First, i dont know how to use texture projections in irrlicht.
esaptonor
Posts: 145
Joined: Sat May 06, 2006 11:59 pm

Post by esaptonor »

is it as simple as using texture projection? thats good...but i also don't know anything about it :/
Can you explain texture projections and how to use them please?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Check out the Render to Texture tutorial, it's basically just doing that.
Image Image Image
esaptonor
Posts: 145
Joined: Sat May 06, 2006 11:59 pm

Post by esaptonor »

what? that is just standard render to texture stuff, which is what i am doing to get the reflection in the first place. If that is texture projection then it doesn't fix the problem. I hope that is not texture projection though.
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Do what Omar said, have a second camera (or move your current camera) and have it below the water line the same amount you current camera is above the water line and then use that second camera as the render to texture camera.
Image Image Image
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

RTT is not texture projection, but to get a texture onto a quad (what your water more or less is) you won't need texture projection. Simply get your render image from the right angle.
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

The right angle wont rotate the image on the Quad, it will simply obey the laws of reflection but the texture rotation wont be mapped. And thus the bug seen in the image.
esaptonor
Posts: 145
Joined: Sat May 06, 2006 11:59 pm

Post by esaptonor »

I already tried exactly what omar said before i got to what i am doing now, which both produce a very similar image.
Omaremad, you did not explain anything in that post other than why my problem occurs...does this mean its a lost cause? does nobody know how to fix this problem? :(
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

did you attempt texture projection?, in the shader?
esaptonor
Posts: 145
Joined: Sat May 06, 2006 11:59 pm

Post by esaptonor »

If you mean like your water shader, i tried to avoid that as it doesn't work very well when the water is large.
I am trying to replicate what was done here: http://www.gametutorials.com/gtstore/pc ... part6.aspx

doesn't seem like you can download that anymore, ill post some code. Its all in opengl.
this is just a snippet from where they get the reflection. I don't know opengl, but to me it seems as though all they are doing is rendering to a texture like i am trying to do...but their one works!

Code: Select all

// Change the view port to be the size of the texture we will render to
    glViewport(0,0, textureSize, textureSize);

	// Clear the color and depth bits, reset the matrix and position our camera.
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    g_Camera.Look();

	// So we don't affect any other objects in the world we push on a new matrix
    glPushMatrix();

		// If our camera is above the water we will render the scene flipped upside down.
		// In order to line up the reflection nicely with the world we have to translate
		// the world to the position of our reflected surface, multiplied by two.
		if(g_Camera.Position().y > waterHeight)
		{
			// Translate the world, then flip it upside down
			glTranslatef(0.0f, waterHeight*2.0f, 0.0f);
			glScalef(1.0, -1.0, 1.0);

			// Since the world is updside down we need to change the culling to FRONT
			glCullFace(GL_FRONT);
			
			// Set our plane equation and turn clipping on
			double plane[4] = {0.0, 1.0, 0.0, -waterHeight};
			glEnable(GL_CLIP_PLANE0);
			glClipPlane(GL_CLIP_PLANE0, plane);

			// Render the world upside down and clipped (only render the top flipped).
			// If we don't turn OFF caustics for the reflection texture we get horrible
			// artifacts in the water.  That is why we set bRenderCaustics to FALSE.
			RenderWorld(false);

			// Turn clipping off
			glDisable(GL_CLIP_PLANE0);

			// Restore back-face culling
			glCullFace(GL_BACK);
		}
		else
		{
			// If the camera is below the water we don't want to flip the world,
			// but just render it clipped so only the top is drawn.
			double plane[4] = {0.0, 1.0, 0.0, waterHeight};
			glEnable(GL_CLIP_PLANE0);
			glClipPlane(GL_CLIP_PLANE0, plane);
			RenderWorld(true);
			glDisable(GL_CLIP_PLANE0);
		}

	// Restore the previous matrix
    glPopMatrix();

    // Bind the current scene to our reflection texture
	glBindTexture(GL_TEXTURE_2D, g_Texture[REFLECTION_ID]);
    glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, textureSize, textureSize);
somehow their reflection lines up perfectly...
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

... beacuse they send the correct matrices of the camera in the shader which does the projection, its in the shader code. Howver we can use fixed function stuff and texture matrices but thats harder to do projection with.
Post Reply