PDA

View Full Version : Clipping despite translation



question2
06-05-2015, 11:58 AM
Hi all,

I've got some rectangles that appear just in front of a bigger rectangle, so their Z values are very slightly more negative.
When I positioning each rectangle that is in front of the bigger rectangle, I use a translation. I may also do a rotation in future.
What I'm trying to figure out how to do is how can I clip in 3-D the smaller rectangles so that no part of than ever appears outside of the bigger rectangle that is behind them?


This is how things are now:
1858

This is how I want to clip:
1859

I've tried using glClipPlane but so far, no luck. What I'm doing:
* Draw larger rectangle.
* Set 4 clipping planes based on corners of larger rectangle for top/bottom/left/right.
* Draw each smaller rectangle using
- glPushMatrix
- glTranslate
- glRotate optionally
- glBegin quads
- glBindTexture
- glTexCoord2d & glVertex3d
- glEnd
- glPopMatrix
* Clear the 4 clipping planes.

Thanks.

GClements
06-05-2015, 03:18 PM
Depending upon what you want to do, scissoring (glScissor) may be preferable to geometric clipping. Or stencilling.

Regarding geometric clipping:

* The clip planes are specified in object coordinates but transformed using the current model-view matrix (current at the time of the glClipPlane() call) and stored in eye coordinates.

* As well as specifying the plane coefficients, each plane must be enabled with glEnable(GL_CLIP_PLANE0) etc.

Points inside the clipping region are those for which


p1*x + p2*y + p3*z + p4*w >= 0


Thus, the planes should probably be:


GLdouble planes[4][4] = {
{ 1, 0, 0, -x_left},
{-1, 0, 0, x_right},
{ 0, 1, 0, -y_bottom},
{ 0, -1, 0, y_top}
};


where x_left, x_right, y_bottom and y_top are the actual values used for the rectangle's vertices, and the model-view matrix is unchanged between the drawing of the rectangle and the glClipPlane() calls.

question2
06-05-2015, 07:50 PM
....
where x_left, x_right, y_bottom and y_top are the actual values used for the rectangle's vertices, and the model-view matrix is unchanged between the drawing of the rectangle and the glClipPlane() calls.

Thanks, that works now for x and y directions.

However there is a glitch. When I rotate the containing (clipping) rectangle, the clipping planes don't also rotate.
This is not a big deal since I don't plan to rotate them but I wonder how that could be accomplished...

GClements
06-06-2015, 07:27 AM
When I rotate the containing (clipping) rectangle, the clipping planes don't also rotate.
Assuming that you're rotating the rectangle via the model-view matrix (e.g. glRotate), you should just need to call glClipPlane() while that transformation is still in effect (e.g. before calling glPopMatrix).