# Clip plane + reflection problem

## Recommended Posts

I'm trying to implement water with reflections. When rendering the reflected geometry, I know I'm supposed to use clip planes to clip all geometry that is, in unreflected form, below the water plane (i.e. all geometry that is above the water plane in reflected form) in order to avoid artifacts. I think I've written correct code for this, but I still get artifacts that suggest I haven't: Here's the code. The water is located at height 0:
	double plane[] = {0.0f, -1.0f, 0.0f, 0.0f};
glClipPlane(GL_CLIP_PLANE0, plane);
glEnable(GL_CLIP_PLANE0);

//draw reflected stuff here...

glDisable(GL_CLIP_PLANE0);


I'm using a GeForce 7600 GS. [Edited by - all_names_taken on June 11, 2007 7:34:26 AM]

##### Share on other sites
Hm, some further reading suggests the clip planes may not be defined in world space... That may be the problem. But which space am I supposed to express the clip plane coordinates in? And when, in relation to the glMatrix-operations, should I set the clip plane?

This code, as well as around 10 other configurations, won't work. Some sources claim DirectX defines clip planes in clip space when using shaders, and world space otherwise. I can't find any sources on how OpenGL does. In my case, I'm using GLSL shaders.
	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	gluPerspective(45.0f, 1024.0f / 768.0f, 0.1f, 10000.0f);	glMatrixMode(GL_MODELVIEW);	glLoadIdentity ();	glMultMatrixf((float *) &view); //set view matrix	glMultMatrixf((float *) &refl); //set reflection matrix	//activate clip plane	double eqr[] = {0.0f, 1.0f, 0.0f, 0.5f};	glClipPlane(GL_CLIP_PLANE0, eqr);	glEnable(GL_CLIP_PLANE0);	//RENDER HERE...	//deactivate clip plane	glDisable(GL_CLIP_PLANE0);

Edit: This seems to be exactly my problem:
http://www.ks.uiuc.edu/Research/vmd/mailing_list/vmd-l/4424.html
but that was posted in 2005. Surely it must have been fixed now?

##### Share on other sites
According to this:
http://arkiv.netbsd.se/?ml=openscenegraph-submissions&a=2006-05&t=2041664
clip planes are defined in model space, and I have to set gl_ClipVertex in the shader.

EDIT: hm, this didn't solve the problem either :(

[Edited by - all_names_taken on June 11, 2007 10:02:04 AM]

##### Share on other sites
What OS and driver version are you using? I ask this because some of nVidia's drivers (i.e. between 89.xx and up to and including 97.46) the clip planes together with shaders did not work, but without shaders was ok. On Linux driver 97.55 clip planes (atleast for me) work fine, and I use 87.xx under WinXP to get the clip planes to be good... as for what co-ordinate system to use, since you are specifying gl_ClipVertex, you can use any system you want (as long as you are consistent)... in my system, I actually use eye-coordiantes, and before I specify the clip plane I first do glMatrixMode(GL_MODELVIEW) and glLoadIdentity() and caclulate what the clip plane is in eye-coordiantes (which is just a matrix*vector multiply) and in my shaders I (essentially) do gl_ClipVertex=gl_ModelView*gl_Vertex.

##### Share on other sites
Set up your Camera and then call glClipPlane, worked fine on my app.
Are you going to use GLSL Shaders for the water?
If so you will be in a nvidia/ati incompatibility hell.

On nvidia cards you have to write gl_ClipVertex in the Shader. If you write glClipVertex on a ati card, the driver will jump to software rasterizer. To get it work on ati to, you have to comment out the glClipVertex line. I searched a few days for a solution of this problem and came over the following preprocessor command:

  #ifdef __GLSL_CG_DATA_TYPES  gl_ClipVertex = position;  #endif

##### Share on other sites
Thanks! I haven't got any ATI card to test on, unfortunately. I got the stuff to work on Nvidia now, at least.

##### Share on other sites
Thanks for help, I also faced the same problem this forum helped me a lot.
Sometimes it's a real hell to write some GLSL code running at Nvidia and ATi. Good luck.

##### Share on other sites
One more note: this code works for both Nvidia and ATi:

    gl_Position = ftransform();    // fix of the clipping bug for both Nvidia and ATi    #ifdef __GLSL_CG_DATA_TYPES    gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;    #endif

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627719
• Total Posts
2978796

• 10
• 9
• 21
• 14
• 12