# Clipping away the 'correct' part of a scene?!

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

## Recommended Posts

I´ve got a question. I am creating a clipplane class, that gives me easy access to clipplanes and even 'seals' the cuts it made in a scene with planar faces and stuff. Anyway, now I wanted to make the clipplane automatically clip away the side of the scene from which I am looking. E.g. if my point of view is somewhere on the positive z-axis and the clipplane lies in the x-y-plane, then everything with a positive z-coordinate has to be clipped away. If point of view would be on the negative z-axis, everything with negative z value has to be clipped. Of course, the clipplane can be arbitrary and does not have to be aligned to any axis. Now, what I got is my view matrix (4x4) and the clipplane (Ax + By + Cz = D). Can anybody tell me how to do that? That would be great. If it´s a lot of math, look at me like I was a dummy ... which I am. Thanks a lot. Twist

##### Share on other sites
That should be easy.

First, plug the viewer's position, say px,py,pz, into the equation of the clipping plane, i.e. compute

A*px + B*py + C*pz - D

The sign of this value indicates on which side of the plane you are. OpenGL clips everything which is on the negative side (as far as I remember), so if the sign of the value you've computed above is negative, then everything is fine - everything on your side of the plane will be clipped.

If the sign is positive, then you're on the side of the plane OpenGL would not clip. In this case, you just have to replace (A,B,C,D) by (-A,-B,-C,-D) - that's all. If you plug the viewer's position into this new plane equation, then the value you get is negative again and everything is fine.

*side note*
As far as I remember, plane equations are given in the form Ax+By+Cz+D=0, not Ax+By+Cz-D=0. I don't know if this might lead to confusions with OpenGL's clip plane representations. Anyway, it doesn't affect what I said above about clipping the right side of the plane.

Apropos right side of the plane: (Warning, joke!)
Why does American Airlines always put people from Poland on the left side of their airplanes? Because Poles on the right side of the plane cause instabilities. (You need some stability theory for this...)

##### Share on other sites
Hi,

the camera frustum can generally be represented by 6 planes : near plane, far plane, and 4 for the sides.

So the first step is to extract this planes from the camera. You could find the math part anywhere (http://www.magic-software.com/Graphics.html)

Then you loop through the 3d objects in your scene, and for each of them, compute on which side the planes its bounding box is. In the case it is completly on the extern side of one plane (without any intersection) you can safetely cull your object. if completly inside the 6 planes the object is completly visible. If an intersection occures with one plane you can either render the object, or recurse the procedure if it's composed by any child object.

##### Share on other sites
Hey thanks,

but Lutz, that may sound strange now, but is it also possible to ONLY use the view matrix and NOT the position of the viewer.

Twist

##### Share on other sites
You mean you only want to work with matrices, not with positions, right?

In which coordinate system do you define the clip planes?
If it's in eye space, then it's easy. In eye space, the viewer is always at (0,0,0), so you just have to check whether D is positive or negative.
If you define your clip planes in another coordinate system and let OpenGL transform them to eye space (with the modelview matrix), then you could transform the planes "by hand" and again check whether the transformed D is positive or negative.

But be careful! Clipping planes do not transform like usual points. If M is the modelview matrix and v is a vertex, then M*v transforms v from object coordinates (the usual coordinate system where models are defined) to eye coordinates. If P=(A,B,C,-D) is the plane, however, M * P does NOT transform the plane to eye coordinates, but (M^-1)^T * P does! (M^-1)^T is the inverse transpose modelview matrix. First invert M, then transpose it. This is - by the way - also the reason why normals are transformed by the inverse transpose modelview matrix in vertex programs.

So what you need to do is multiply P by the inverse transpose modelview matrix and then check if the 4th component (the transformed D) is positive or negative. Depending on this, negate the plane equation (A,B,C,-D) -> (-A,-B,-C,D) or not.

##### Share on other sites
Ah, well, that´s more what I was looking for. Nice.

The clip plane isn´t defined in eye space, by the way. But you have addressed that already. Thanks :).

Only a minor wondering left now. When you speak of a plane P=(A,B,C,-D), where is the xyz in there ? Is that how it is written down generally ?

Twist

##### Share on other sites
Yes, in OpenGL, both 3D-points and planes can be written as a 4-vector. A 3D point (x,y,z) corresponds to the 4-vector (x,y,z,1). The reason for the 4th coordinate is that you can express translations and even projections with a matrix-vector-multiplication that way. Planes are also written like P=(A,B,C,D) (I wrote -D in my last post to comply with your way of defining a plane). This is just a short way of writing the plane as

plane = set of all points (x,y,z) such that Ax+By+Cz+D=0.

Note that this is the same as the dot-product of P=(A,B,C,D) and the 4-vector (x,y,z,1).

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 18
• 13
• 14
• 44
• 63