# Clipping for water-plane

This topic is 4661 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If now made a terrain-implementation that satifies me, so I've decided to move on a little ;) It's now time for me to make some water, but already at this early state I've run into problems. What I want is to clip everything beneath y = 0.0f, so I've defined my plane using simple DirectX measures (I'm to lazy to do the plane-math myself ;)
D3DXPLANE ClippingPlane;
D3DXVECTOR3 Point(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 Normal(0.0f, 1.0f, 0.0f);

D3DXPlaneFromPointNormal(&ClippingPlane, &Point, &Normal);
Now, this seems to be fine, if you ask me. So, to get to the point, when I apply this to the screen like this:
Engine.DX.D3DDevice->SetClipPlane(0, ClippingPlane);
Engine.DX.D3DDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0);
Engine.DX.D3DDevice->SetRenderState(D3DRS_CLIPPING, TRUE);
... it seems to be clipping in eye space (half of the screen simply gets clipped). I've tried putting the Point-coordinates into View-, WorldView-, World-, Projection-, WorldViewProjection, ViewProjection - and so on -space, but without any luck (or I should say so -- if anyone knows which one of these I should use please inform me ;) My vertices are processed in a vertex-shader multiplying position with WorldViewProjection, and as the user-defined clipping happens after the vertex shader, I guessed that it had to be in this space, but now I'm completely lost. What am I doing wrong? :) Thanks in advance Nizro

##### Share on other sites
I've never had to deal with this problem, but I do know that the clipping plane is defined in the local space of the shader, so you need to find the right matrices to transform it back. This would probably involve the view/world matrix.

##### Share on other sites
Believe me, I have tried that - any other sugestions?

##### Share on other sites
In the fixed function, the plane equations are assumed to be in world space, but in the programmable pipeline, they are assumed to be in clip space (the same space as the output vertices).

Edit:

Erm, so what you really need to do is to specify your plane in clip space - which would mean transforming it by the WorldViewProjection matrix before even setting the clip plane. That's about the only suggestion I can think of, if you've tried that and it doesn't work perhaps there is a bigger problem elsewhere, or maybe you've just made a little error somewhere which you haven't spotted and will kick yourself when you do ;)

-Mezz

##### Share on other sites
Hmm... yeah, that rings a bell, actually :)

Currently I do like this: http://extraball.sunsite.dk/notepad.php?ID=7435

Is that something like you were thinking of, or am I way of line? :|

##### Share on other sites
Striking line 37 is possible, if you want to play around with matrices, yet none of the combinations that I've tried actually worked.

Can anyone see what I'm doing wrong here? It's starting to annoy me :D

##### Share on other sites
I had exactly the same problem with the clipping plane, and couldn´t solve it. So I decided to use the clip() function of HLSL.
Finally I managed to find room for one more float in my struct that is passed from the vertex shader to the pixel shader, so I could use the clip()-function in HLSL properly.
To use clip() you have to pass the distance of the current point from your plane from VS to PS. In your case it should suffice to simply to store the height of the point in world space in another float and pass it to the PS. Then, you call clip(worldHeight) and every point with a height less than 0 is clipped.

##### Share on other sites
I don't think that's a possibility, as I would like to be able to clip no matter what shader is applied. Any other suggestions?

##### Share on other sites
Like it was mentioned by Mezz, to use a clip plane in a shader
it should be in clip-space.

[source=cpp]// 'plane setting' function{	D3DXPLANE tPlaneWS; // the 'normal' D3DXPLANE, in world space.	//..	D3DXMATRIXA16 matTemp;	D3DXMatrixMultiply(&matTemp, cpmatView, cpmatProj);	D3DXMatrixInverse(&matTemp, NULL, &matTemp);	D3DXMatrixTranspose(&matTemp, &matTemp);	D3DXPLANE tPlaneCS; // our final 'clip space plane'	D3DXPlaneTransform(&tPlaneCS, &tPlaneWS, &matTemp);	pd3dDevice->SetClipPlane(0, (float *)&tPlaneCS);}// real time, function{	pd3dDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, D3DCLIPPLANE0);	// set shader, render.	pd3dDevice->SetRenderState(D3DRS_CLIPPLANEENABLE, FALSE);}

##### Share on other sites
Thanks man :D You're a genius :)

I guess my approach to transforming from world to clip space wasn't really that good, but at the moment I didn't really think of the D3DX-functions. Will remember that in the future :)