# User Defined Clip Planes

## Recommended Posts

xycsoscyx    1149
Back to mirrors, oy! XD First off, thanks to all the help from everyone, I'm gonna post some screen shots and full source code sooner or later, once I finish up a few things. Now onto the question. I want to use user defined clip planes to clip rendering at the mirror plane. I want to use them over the "clip" instruction because the user defined clip planes happen before the pixel program process, so it can save a bit of time (especially for expensive rendering). As it is, I know I have to Invert then Transpose my transformation matrix, then transform the plane by it, all because I am using vertex and pixel programs (is it actually only one or the other?).
		GEKPLANE kPlane(m_kClipPlane);

GEKMATRIX4 kTransform(m_pkCurrentViewer->eye.transform);
kTransform.Inverse();
kTransform.Transpose();
kPlane = kTransform.Transform(kPlane);

m_pkEngine->m_pkVideo->SetClipPlane(0, kPlane);
m_pkEngine->m_pkVideo->SetRenderState(GEKRS_CLIPPLANES, GEKCP_ENABLE(0));

The Transform function is copied right from D3DX, it's the formula for transforming a plane by a matrix. When I do this for the regular camera (I use the cameras X axis as a normal, and origin as a point, and construct a plane from that), it works correctly. The scene is only rendered on one half of the screen, sweeeeet. As a note, the eye.transform matrix is just the standard (View * Project) total. So for rendering the regular world, this all works fine. Now for the kink in the chain, when I try to do this when I'm rendering a mirror, it goes fubar (so far, I can't really tell what's wrong with it). My transform matrix for mirrors is (Reflect * View * Project), where View and Project are same as before, but Reflect is a reflection matrix generated from the mirror plane. Soooo, am I missing something here? I've tried not having the Reflect matrix in the total, doing it a matrix at a time (this works fine, too, for the regular camera), etcetc. So I need to modify the plane or something before hand, THEN transform it to clip space? Am I just way off? Thanks

##### Share on other sites
xycsoscyx    1149
Whoops, I just noticed that I posted this in the general graphics forum, I should have posted it in the DirectX forum instead. Anyways, someones got to know what's going on, I'm sure many people have gotten user defined clip planes to work when rendering mirrors.

##### Share on other sites
jamesw    400
This thread might have something for you.

##### Share on other sites
xycsoscyx    1149
Like I said, it works for regular rendering of the scene, but not when rendering the reflection (when the view matrix is the combined (Reflect * OriginalView). For the regular scene, I can clip anywhere correctly, and it will be transformed to clip space properly. When rendering the reflection scene, it goes fubar, the thread you linked to says about the regular "transforming a plane into clip space", but doesn't mention specifically about the reflected scene (unless I missed something).

##### Share on other sites
xycsoscyx    1149
Hmm, now that I have power again (I live in Washington, power was out for three days, PLUS I had to replace my power supply 'cause it seems to have blown when power was restored). Anyone have any thoughts on this? It's really bugging me 'cause I can't seem to wrap my head about what's going on (math-wise) even. :( Sad panda am I!

##### Share on other sites
jbarcz1    265

Could you be more specific about what 'Goes fubar' means? Does it not clip or does it clip the wrong way? Pics might help.

##### Share on other sites
xycsoscyx    1149
That's the thing, I can't quite tell what it's doing, to be honest. It's not backwards rotation or anything simple, I'll have to redesign a scene to make it visible (the current scene has the back of the mirror in the way when things don't clip properly), then I can take some pics. Basically, if I transform the plane normally (transform by the transpose of the inverse of the matrix), then nothing is rendered in my reflection.

It SEEEEEMS that it's just the wrong direction, that it's cliping at the right distance, but wrong direction. If I try to reverse my normal, then I see that the direction becomes wrong (reversed, like it should since I just reversed the direction), but the distance (which I didn't touch) is wrong, it's the other side of the room (reverse distance) that the plane is located at.

Like I said, I'll try to redesign the room a bit to show (hopefully) what I am seeing.

##### Share on other sites
jamesw    400
I remember having problems setting up clip planes once because I assumed it used the a*N.x + b*N.y + c*N.z = d plane equation, but dx really uses a*N.x + b*N.y + c*N.z + d = 0, so try keeping your normal the same and just flip d. Hooking up a slider to tweak the d can help you get a better idea of what's going on quickly.

##### Share on other sites
xycsoscyx    1149
Wow, I figured out what's going on, and I have no idea how to fix it (and it kinda means I can't actually show what's going on). Basically, if the camera is behind the clip plane, nothing is rendering (at all). I need to see if I'm intentionally doing that (and just completely didn't notice in my engine), but has anyone else encounterd that issue before? It's clipping properly aside from that (when I reverse the plane normal, it clips it properly the other way, but when it's facing the way it should be, the camera should always be behind it, so for some reason nothing renders).

James, thanks for the suggestion to add a slider, that helped me figure out what was going on, now if I can just solve it. :D

##### Share on other sites
xycsoscyx    1149
Okay, here are some screenshots, fuuuubar. At extreme angles, it all goes funky and things are way off, from straight on, the clipping plane seems correct (the normal, but not the distance). I really have no idea what's going on in the shots here, where the clipping plane seems to affect the transformation itself for a small slice on the mirror! XD

Other then that is the issue that it still seems to overclip. If the camera is behind the clipping plane, then everything is clipped, not just the pixels behind the plane?

edit: Whoops, wrong HTML for images. :D

[Edited by - xycsoscyx on December 19, 2006 12:44:15 PM]

##### Share on other sites
jbarcz1    265

I can't any screenshots.

One thing that might be worth checking on is this: Are you remembering to flip the culling mode when you render the reflected scene? If you reflect across a plane, the winding order of the triangles changes, and you need to reverse your culling mode to compensate for it.

I can't say with any certainty that this is whats causing your current problems but if you dont know about it it could cause problems later.

JB

##### Share on other sites
xycsoscyx    1149
Got the images to work now. :D

Yeah, I reverse the culling mode when rendering the reflected scene.

##### Share on other sites
Nedelman    166
After you transform the plane you should check the sign of the D coefficient. In my own program, D > 0 means the view is behind the plane. If you don't plan on having two sided mirrors you can skip rendering the reflection at this point. Otherwise if you want to render the reflection you need to reinitialize kPlane with m_kClipPlane, negate kPlane (negate all the coefficients, not just the normal) and then transform it again with the inverse transpose matrix.

##### Share on other sites
xycsoscyx    1149
So the problem seems to be that if the viewer is behind the clip plane, then Direct3D itself clips everything, not just pixels behind the plane.

       /|Plane      / |Viewer  |->      \ |       \|

In the above picture, the viewer is looking at the plane, and the plane faces the other direction (so the viewer is behind the plane), this clips everything! I want it to clip the pixels behind the plane, but still render what's in front of it properly. I actually tested the reverse of this, revered the plane normal and clipped, it renders what's in front of it, so everything after it is clipped properly. Sadly in the case of the mirror, I need the first case scenario.

Has anyone else encountered this? I'm using Direct3D, if that makes a difference. Are there any render states that control clipping (other than enable/disable user clip planes)?

##### Share on other sites
Nedelman    166
The only things that control user clipping are the plane coefficents and the "enabled" state.

I mentioned my 3DS Viewer project before and it's worth mentioning again. In the second screenshot you can see two upright mirrors in the scene. They are two sided so you can walk around them in the demo and the user clipping is performed correctly by doing the negation as I mentioned in a previous post. Also, the floor is a single sided mirror. If you move the view below the floor the program doesn't draw the reflection and of course the floor polygons are culled by the hardware.

Both cases are handled in the DrawMirrors() method in the source code in the Scene.cs file. Since I managed to get an effect similar to what you're trying to achieve using that code it stands to reason that the idea and implementation are reasonably sound. You might want to try adapting it to fix your problem.

##### Share on other sites
xycsoscyx    1149
Here's clipping of the main scene just to show that it's working correctly, it's just adding in the reflection matrix to the whole thing that throws it off. I have the configurable distance with a plane direction equal to the negative camera Z vector. It clips this correctly, as long as the camera is in front of the plane. When it's behind the clipping plane, then it doesn't render anything at all (I tested this by point the plane forwards and moving the distance, at <= 0 it's fine, but > 0 nothing renders since the camera is now behind the plane).

Thanks for the demo, Nedelman (I had meant to look at that when you mentioned it earlier, but I forgot after I got home from work), it looks like I'm doing things the same way, but I'll hafta go over it with a fine tooth comb to find out for sure, I'm sure it's something really small that I'm missing. XD

##### Share on other sites
xycsoscyx    1149

Here is another screenshot to show that the clipping is working, as long as the camera is in front of the plane.