Jump to content
  • Advertisement
Sign in to follow this  
Phynix

Full screen quad projection

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

My ultimate goal is water refraction. But to achieve this, I've found that it would help to render a quad to a texture. Consequently, it implies I must transform the quad so that it covers the area of the screen.

So my question is..

How do I transform a 3d rectangle so that it fills the screen? I'd think it has something to do with finding the normal of the rectangle and supplying the rotation opposite to that, but I don't know how much to translate it...

Share this post


Link to post
Share on other sites
Advertisement
For water refraction you need to render to a texture and then project the texture onto the water surface. You do this with projective texturing. The setup is like this:

1.0 Render from the eye's point of view into a texture


// pseudocode
camera.look();
renderScene();
copySceneToTexture();

or

myFbo.bind();
camera.look();
renderScene();
myFbo.unbind();





2.0 Projective texture the surface of the rendering from the eye's point of view.


myProjectiveShader.bind();
bindTexture(TEXTURE_2D, projTextureID);
myProjectiveShader->setUniform("projTexture", 0);

renderWaterSurface();

myProjectiveShader.unBind();



The projective shaders might look like this (GLSL):


// VERTEX SHADER
varying vec2 texCoord1;
varying vec4 viewCoords;
void main()
{
texCoord1 = (gl_TextureMatrix[1] * gl_MultiTexCoord1).xy;
viewCoords = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_Position = viewCoords ;
}

// FRAGMENT SHADER
uniform sampler2D projTexture;
varying vec2 texCoord1;
varying vec4 viewCoords;

void main()
{
vec4 projCoord = viewCoords / viewCoords.q;
projCoord = (projCoord + 1.0) * 0.5;
projCoord = clamp(projCoord, 0.0001, 0.9999);

gl_FragColor = texture2D(projTexture, projCoord.xy);
}



Share this post


Link to post
Share on other sites
(that's odd, my previous post was deleted...)

Anyway, thanks Deliverance, but my main question is the camera.look() part. I'd like to find out how to do this--how to calculate the desired transformation--because once this happens everything else is fairly straightforward.

And I'm pretty sure finding the desired translation/rotation of the camera will involve trigonometry, dot products, and some circular functions, but how I can do this escapes me...

Share this post


Link to post
Share on other sites
Okay, so the camera.look() can be calculated in any way you like, it is the position of the camera you define in your application. I assume you positioned a camera before, if that is so, i'm talking exactly about that camera in my camera.look() not about another camera reflected or good God "refracted" in any way :D. An example would be this(i assume you use opengl from the fact you're using GLSL):

glClear(GL_DEPTH_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// setup camera, translate world 6 units away from the origin
glTranslatef(0.0f, 0.0f, -6.0f);

// draw something
myDrawFunc();

// render scene to texture
myFBO.bind();
glLoadIdentity();
// setup camera the same way
glTranslatef(0.0f, 0.0f, -6.0f);

myDrawFunc();

myFBO.unbind();

// now projective texture the texture onto the screen
etc

To achieve refraction you must use this texture and distort it in a pixel shader, based on the normal, refraction index and eye position.

Share this post


Link to post
Share on other sites
Sorry, but I'm not sure if we're on the same page.

I'm trying to render a body of water. So I'm planning on doing this by rendering everything below the water to a texture, and then refracting that. I don't want the whole screen to be distorted--just the underwater geometry.

Share this post


Link to post
Share on other sites
Quote:
Original post by Phynix
Sorry, but I'm not sure if we're on the same page.

I'm trying to render a body of water. So I'm planning on doing this by rendering everything below the water to a texture, and then refracting that. I don't want the whole screen to be distorted--just the underwater geometry.


You are right you don't have to render all the scene :D, only what's under the water for refraction. As searching through the internet i found this illustrative paper for water reflection and refraction.

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!