Clamp a part of the scene

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!
Post Reply
DeusXL
Posts: 114
Joined: Sun Mar 14, 2004 9:37 am
Contact:

Clamp a part of the scene

Post by DeusXL »

Is there a way to clamp a part of the scene ?
For instance, remove all the vertex that are below (0, 0, 0).
I have this problem while I'm making a WaterSurfaceSceneNode with reflection and GLSL.
Everything works fine except that too many polygons are drawn and it looks weird sometimes.

Take a look at the file if you want to see what I mean (require .NET framework 2.0) :
http://deusxl.free.fr/WaterSceneNode.zip

And if you're interested, here's my C# code of the scene node (it looks a lot like the C++ code) :
EDIT by DeusXL : Code removed because it was too heavy. You can take a look at it here :
http://svn.sourceforge.net/viewvc/irrli ... iew=markup
Last edited by DeusXL on Thu Oct 05, 2006 3:39 pm, edited 1 time in total.
Irrlicht .NET complete and Cross Platform Wrapper
The kid on my avatar wrote:A painless lesson is one without any meaning
esaptonor
Posts: 145
Joined: Sat May 06, 2006 11:59 pm

Post by esaptonor »

i asked this the other day and aparently irrlicht doesn't support clip planes.
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

nice projection method, my matrix based method doesnt seem to scale the texture properly maybe ill try trig. Any way this bug seems to only happen when the camera is above water, some underwater parts of the scene are still rendered and thus them appearing on you reflection texture.

To fix this bug you would need a "clamping shader" to be applied on the terrain during the scene -> RTT render pass. This shader is applied terrrainand would look something like this:

in the vertex shader you would add the following:

before the actual main function you would add:

Code: Select all

varying float Cutoff;
then in the main function of the vertex shader:

Code: Select all

Cutoff=terrainpos.y;
terrainpos being a vec3 or a vec4 containing the transformed position of the terrain vertex.

in the pixel shader of the terrain:
you would render the terrain with a normal texture + the detail map since the shader would remove the fixed function detail map stuff

to do so you would:

before the actual main function you would add:

Code: Select all

varying float Cutoff;
declare two samplers for the each texture:

Code: Select all

uniform sampler2D Diffuse;
uniform sampler2D Detail;
then in the main function you would do this to render the terrain with a detail map

Code: Select all

vec4 color=texture2D(Diffuse,gl_TexCoord[0])*texture2D(Detail,vec2(gl_TexCoord[0].x*100,gl_TexCoord[0].y*100));
if(Cutoff<0)
color.a=0;
else
color.a=1;
  gl_FragColor =color;

see what this part does? First it proceses the diffuse pixels then it takes the detailmap and multiplies it with the diffuse color, the texcoord are multiplied by 100 to make the diffuse map repeat by 100 times.

the cutoff variable is the y-pos of the vertex (interpolated to that pixel being processed) if its less than 0 the alpha value is zeroed. the zero alpha value with the material compiled with EMT_TRANSPARENT_ALPHA_CHANNEL

makes the underwater pixels invisible!!!!!

so in summary we extract the vertex y-pos of the current vertex in the vertex shader, we pass it to the pixel shader via a varying float, we use that to zero the alpha value if needed and thus a transparent terrain under water during the reflection pass.

ask more if some parts werent explained in detail!

dont forget to set the sampler values in the pixel shader constants, ask about that if you need to.
DeusXL
Posts: 114
Joined: Sun Mar 14, 2004 9:37 am
Contact:

Post by DeusXL »

Thanks omaremad. I used your solution, it's quite easy to understand (but a bit heavy I found because it only works with terrains OR meshes OR ...).

I finished my scene node, here are the screenshot and the demo :
http://irrlichtnetcp.sourceforge.net/ph ... c.php?t=54

And here's the code :
http://svn.sourceforge.net/viewvc/irrli ... iew=markup

Again, thanks a lot !
Irrlicht .NET complete and Cross Platform Wrapper
The kid on my avatar wrote:A painless lesson is one without any meaning
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

Nice demo, and whats nicer is that you understood my explaination lol. Maybe we can optimpise the clip shader. CPU clipping was an option that came to mind but that is even slower for terrains.

You deform the reflection using sin and cosine waves(it can be replaced by using a texture that acts as a shifter for the reflection texcoords, this is slow however you can keep these sine's and cosines and use them to their maximum potenial and add them to the y-value of water vertices

you would use a plane with lots of vertices which would give your water a 3d feeling. ie using the same method as the irrlicht water node except you only take the plane and you do all the calculations on the gpu using the same texture distort values.

anyway your RTT value seems high maybe thats a reason for the slowness.

one question, can i steal your projection code :) i have been doing some tests with parallax mapped water but my projection method doesnt scale the RTT properly.
DeusXL
Posts: 114
Joined: Sun Mar 14, 2004 9:37 am
Contact:

Post by DeusXL »

The thing that slow down the most the code is the number of tiles in the water.

I can stick at 100+ fps with 10 tiles whereas the demo has 70 tiles (to make it look better).
But I'm sure that if you play with all the values with a small number of tiles you can get quite a better FPS.
you would use a plane with lots of vertices which would give your water a 3d feeling. ie using the same method as the irrlicht water node except you only take the plane and you do all the calculations on the gpu using the same texture distort values.
Unless I'm wrong this is what I did on the vertex shader.
First it was only a rewrite of Irrlicht's water scene node and it worked quite faster with shaders but then I added reflection and all that stuff.
anyway your RTT value seems high maybe thats a reason for the slowness.
That's also what I thought but the difference on my Linux between 512 and 128 was really small so I took the best size. But everything can be modified when you use the scene node (could be converted in native code by the way).
one question, can i steal your projection code
Of course you can because 1] the scene node is under zlib/libpng license and 2] I even stole it :lol:
Irrlicht .NET complete and Cross Platform Wrapper
The kid on my avatar wrote:A painless lesson is one without any meaning
omaremad
Competition winner
Posts: 1027
Joined: Fri Jul 15, 2005 11:30 pm
Location: Cairo,Egypt

Post by omaremad »

Unless I'm wrong this is what I did on the vertex shader.
First it was only a rewrite of Irrlicht's water scene node and it worked quite faster with shaders but then I added reflection and all that stuff.
Wow i must be blind not to see the code lol, but the water seemed really flat in the demo thats why i made that comment.

Thanks for the code
Post Reply