   Water lighting and refraction
  I have a fluid simulator and currently water is rendered as refracting a cubemap with fresnel refraction shaders I found, so it's unaffected by local lighting or visible geometry in the scene.I need to achieve two tings.1) Since I have animated ground geometry and the water is placed on various locations, I need to be able to see the ground through the water, so I can't just refract the cubemap.  I figure the ground could be rendered in a first pass to the cubemap texture, but how to get the effect that the ground is directly under the water, and so only a little distorted?2) The ground geometry is lit by small spotlights, each covering a limited area.  How would I add make the water be affected by the lighting so it's not visible outside the spotlight areas?[Edited by - Prune on September 13, 2007 8:07:42 PM]
  Usual way is rendering everything below the water surface to a texture and just shift the texture coords to simulate refraction. Downside (especially with reflection) are artifacts when moving the coords too much or the "wrong" way. What I'd try is storing the distance from the water surface for each fragment (if 8bit are enough in the alpha channel, else a second texture). Calculating the distance of the vertex to the camera and subtracting the distance of the water plane to the camera should work just fine. When rendering the water, scale the refraction/reflection by the distance. It's pretty much what I was intending to try today. Still not physically correct, but it should prevent more obvious errors.   ;  But the water is free-surface and doesn't cover all of the ground.  It flows around and is displaced by impacting objects etc.  So parts of the ground are being covered/uncovered by the water.Edit: also, a cubemap is normally centered at the origin.  How would I go about making the liquid be very close to a particular face, the bottom of the cubemap, and then I would only make that face dynamic, or is there no way to do that?One thing is that I don't know shader writing and was hoping to use existing shaders I had found that refract by reading from a cubemap, and they happened to be of the GL_FRAGMENT_PROGRAM_ARB/GL_VERTEX_PROGRAM_ARB type which is like assembly so it's not clear to me how to modify it.[Edited by - Prune on September 14, 2007 3:48:54 PM];http://www.bonzaisoftware.comClick on Water tutorial on the left side.Basically, you render everything above the water surface to render_to_texture (RTT)Then render everything below the water to another RTTCopy the depth of the previous pass to another texture (a regular one will do. Use glCopyTexSubImage2D)Now you can render the water surface, use the shader, bind the 3 texture above, plus some other textures for displacement.GLSL code is available as well on that site.Quote:But the water is free-surface and doesn't cover all of the ground. It flows around and is displaced by impacting objects etc. So parts of the ground are being covered/uncovered by the water.You will have to modify the shader since I guess you will not need to use a displacement texture to fake water waves.Second issue : your water surface needs to be more or less flat. This rendering technique is just an approximation. It's not a raytracer!   ;  If I have a spotlight, how do I make the liquid not render outside the spotlight cone area?  I have a dark environment, only lit where the spotlight is.  By refracting the cubemap, the liquid ends up visible anywhere, whereas I want it to be dark when outside the lit-by-spotlight area.  How can I best accomplish that?Thanks.   ;  The refraction part is just taking the cubemap and outputing. You could apply the lighting to it as well so that it ends up black when outside the light cone.   ;  Well, how do I apply the lighting?   ;  When you do the lighting calculation for the spotlight, you have something like result = clamp(dot(normal, light_vector), 0.0, 1.0), right?Of course, for a spotlight there is more code.For the reflection, just multiply it by that result. Everywhere outside the cone of light will be black.