Jump to content
  • Advertisement
Sign in to follow this  

Water projected reflections

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

Hi, I´ve been trying to render my water using projected texture for reflections. I have already readed Yann's article, and lots of topics discussing water reflections. But my problem is: is there a way to do it without any pixel shaders? By the way, I´m using DirectX and a HLSL Vertex Shader. Thanks!

Share this post

Link to post
Share on other sites
Basic projected water doesn't require anything per pixel at all, except for a simple decal texturing. Everything is done in the vertex shader.

However, if you want to increase realism, it will be difficult to do it without at least some basic per pixel shading. Fresnel and refractions will still look OK without a complex pixel shader (just a simple blend), most operations can be kept in the per vertex. But as soon as bumpmaps or perpixel reflections come into the equation, there's no way around a pixel shader.

Share this post

Link to post
Share on other sites
I understand the necessity of the pixel shader. But I have to make it work in older machines, and the pixel shader cannot be emulated by software as the vertex shader. I have do deal with it because the game prototype will be in a (sort of) contest in the college I study.

I didn't understand what you mean with "except for a simple decal texturing".
In your article you do use a pixel shader for projection (text2Dproj).

How can I actually do the reflection without pixel shaders?
(some code sample will help)

Thanks for replying!

Share this post

Link to post
Share on other sites
You mean the refleciton itself? Just refleciting doesn't require pixel shaders at all. All that shaders will do for you, in a rough explanation, is allow you to perturb the texture UV coords at fragment level. It won't do reflection for you.

First, you need to get the reflected image of your scene into a texture. Virtually any videocard in use today can do that, even old stuff like the TNT. Calculate the reflected camera matrix, clip your world at the water plane, and render it to a texture (google for "render to texture" and directX, you'll gett awesome resources on how to do it).

Second, you need to project the said texture on the water. Again, google for "projective mapping" to get good resources on how to do it. This can be done without problems at vertex level.

By now you'll have a nice flat reflective water mirror. No waves. There are various ways to simulate waves, and most of them can be done at vertex level. When you get your waves running, you just perturb the UV coords at vertex-level based on the vertex normal.

Share this post

Link to post
Share on other sites
I already have my waves simulation. I'm using the FFT approach.

And already rendered the reflection to my texture too.
My problem is just projecting it.

If you read Yann's article you will se that he uses vertex and pixel shaders for projecting the texture. How can I do it without the pixel shaders?

Share this post

Link to post
Share on other sites
Original post by lgcosta
I didn't understand what you mean with "except for a simple decal texturing".
In your article you do use a pixel shader for projection (text2Dproj).

The pixel "shaders" in this article can't really be called shaders, since they are extremely basic. Everything important is really done in the vertex shader. tex2Dproj is just like a normal texture lookup, but with projection (ie. with enabled homogeneous divide). Every standard fixed function pipeline can do that, however old it might be. You just have to load the projective matrix into the texture matrix of the appropriate texture stage, and enable the D3DTTFF_PROJECTED setting, so that D3D does the homogeneous divide on the texture coordinates.

That's all, no real pixel shader needed for such basic water.

Share this post

Link to post
Share on other sites
Sorry for so many questions, but I can´t make it work. I´ve already readed all the topics about this, where Yann talk to Mephs and Paul7.
I don´t know where I´m wrong.

What I´m doing is:

// Gets the projection and view matrix.
device->GetTransform(D3DTS_PROJECTION, &matProj);
device->GetTransform(D3DTS_VIEW, &matView);

// Creates the reflection plane.
D3DXVECTOR3 vPoint(0,0,0);
D3DXVECTOR3 vNormal(0,1,0);
D3DXPlaneFromPointNormal( &plane, &vPoint, &vNormal );

// Reflect the plane and saves the new view matrix.
D3DXMatrixReflect(&matReflect, &plane);
D3DXMatrixMultiply(&matView, &matReflect, &matView);
device->SetTransform(D3DTS_VIEW, &matView);

// Calculates the projective texture mapping matrix.
D3DXMatrixMultiply(&m_pReflectTexMatrix, &matView, &matProj);
D3DXMatrixMultiply(&m_pReflectTexMatrix, &remap, &m_pReflectTexMatrix);


I'm sure that the rendered reflection is ok, but I'm not 100% sure about the projective texture matrix.

So.. when I´m rendering the water I use:

// Sets the tranformation matrix.
device->SetTransform(D3DTS_TEXTURE0, &m_pReflectTexMatrix);
device->SetTextureStageState(0, D3DTSS_TEXTURETRANSFORMFLAGS,

// Set texture.
device->SetTexture(0, this->m_pReflectTexture);

// Set FVF


By the way, my FVF has 2 coordinates for the texture. Is that right?

And my VS does:

// sets the output vertex position
Out.vPos = mul(matViewProj, In.vPos);

// map the texture
// mapReflect is the m_pReflectTexture in VS.
Out.vTexture = mul(matReflect, In.vPos);

return Out;

As I´m using the Directx D3DTTFF_PROJECTED I suppose I don´t need pixel shaders for doing the homogeneous divide, right?

There is a point in other topics wich I didn´t understand. It´s about the remapping matrix. How do I know if my remapping matrix has to be the same matrix of Yann's article (transposed for dx)?
I didn´t understand how the range of the coordinates will be from -1 to 1.

Any obvious mistake?

Share this post

Link to post
Share on other sites
Think I can see one thing your doing wrong.

What you need to do it render the scene to the texture with the reflected view matrix, then render the scene again with an unreflected view matrix and calculate the projective texture mapping matrix here, you appear to be calculating this using the reflected view matrix.

Also I think you may need more than 2 texture coordinates, not 100% sure though dont know how it works out of shaders. I just use the whole vertex for my texture coordinates (X Y Z W).

Dont really get what your using your vertex shaders for.

As for the remap matrix this is what I use in DirectX:


Share this post

Link to post
Share on other sites
I tried changing the reflection matrix to be calculated with the unreflected view matrix, but nothing changed. I found in other posts Yann saying:


The projective texturing is not very hard either, now that you got this thing working. It's basically the exact same matrix you used for rendering the RTT, only multiplied with your current projection matrix and an additional remapping matrix. Load that into the texture matrix of the texture unit you want to project from (is it also called texture matrix in D3D ? I think so), and feed the unit the same 3D vertex positions you used for rendering, only as texcoords this time.

I understood I need to use the reflected view matrix.
Anyway, my reflection now is the same when I use the reflected view matrix or the unreflected view matrix to get the transformation matrix.
I´ll put an screenshot as soon as my public domain server goes up.

I'm using the vertex shader because I do some water color calculation at vertex level too. But It´s disabled for now.

Share this post

Link to post
Share on other sites
Yeah thats what it sounds like from Yanns description, maybe thats why I have to negate one of the coordinates in the remapping matrix.

Screenshots will be helpful to see what results your actually getting.

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!