2

In opengl we have glClipPlane() that takes a plane equation in the form Ax+by+Cz+D = 0. I have 6 such planes that forms a cube. But instead of showing the world inside the cube, I want to show the outside world. For example, if a sphere goes inside the cube it should clipped but when it comes out it shouldnt be. Not sure but has this something to do with drawing cube faces in clockwise direction so normal will be away from view?

5 Answers 5

3

Unfortunately this is not possible the way OpenGL clip planes work. Since all clip planes are applied to vertices in your inverted cube case there'll always be at least 3 planes that clip.

This special behaviour of OpenGL clip planes thus gives you one important constraint: You can clip only into a convex region. A "inverted" cube however is not convex: In mathematical terms a convex set is a set where for any two given points of the set the (shortest) straight line between those points does not lie outside the set.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks wolf. I asked the same question in my other comments -- is there a way to limit the damn plane? Negating the plane equation works but only with one plane equation. I have a cube with 6 equations, one for each side. The problem is left side (of cube) will cut entire "right" world and vice versa. Same thing for top-bottom sides.
No, there is no way to do this just using clip planes, since planes strech, by definition into the infinite. You have to implement the clipping yourself, or use a geometry shader to modify the geometry.
1

While I'm not familiar with the precise workings of opengl's clip planes, I assume it has to do with defining "clip" and "don't clip" half spaces - whether it is with the normal vector (A, B, C) or some similar fashion shouldn't be an issue.

Try flipping the equation on each of the planes ( -Ax -By -Cz -D = 0) . That should flip the orientation of the plane (unless opengl uses a completely different method for determining in-space and out-space).

1 Comment

Unfortunately all clip planes are applied on a vertex. In case of clip planes arranged in a cube and clipping what's "inside" there's always one plane that clips.
0

IvoDankolov does mention that flipping the coefficients changes the side of the clipping, but... Each additional clip plane clips more geometry, it never will allow you to limit the extent of a previous clip plane.

So in short, you can't use clip-planes to do what you're after (unless you start drawing your geometry 6 times, and use 1 clip-plane at a time, and deal with overdraw. That's darn expensive).

You can however, compute whether each fragment is inside or outside your cube directly in the fragment shader, and discard the inside fragments accordingly. That essentially means implementing the clipping in the fragment shader.

1 Comment

"it never will allow you to limit the extent of a previous clip plane." Thats my main problem. Negating the plane equation works as suggested by Ivo but since every side of cube cuts the inside world my entire screen is blank. :( - left side will cut entire "right" world and vice versa. Same thing for top-bottom sides.
0

This is kind of like what Bahbar suggested, but in only 3 passes. It'd still be kind of expensive, but you could:

  1. Write the cube into the stencil buffer
  2. Render everything that doesn't hit the stencil (draws things to the sides of the cube)
  3. Apply the forward facing clip planes (the ones in the back of the cube)
  4. Render things over the stencil buffer (only draws stuff completely behind the clip-cube)
  5. Apply the rear facing clip planes (the front of the cube)
  6. Render with the stencil buffer again (things completely in-front-of the clip-cube)

If there were a second depth buffer, you could write the back of the cube into the second depth buffer and only render if the fragment is less than that, but greater than the first depth buffer. Then you could render the front of the cube into the depth buffer and then make a second pass over the scene to render things in front of the cube.

2 Comments

THanks J. Your sol seems to be CPU hogging. But I'll definitely give it a shot. And do you know if there's a way to limit the plane equation? WOnder why they didnt implement it in opengl.
@josh It shouldn't need to be a CPU hog, I think. You can put the scene into a call-list and then everything sits on the graphics card (even if the content is fully dynamic, you can render/create the list on the first pass and then just call it on the next two passes). So the CPU just has to send a dozen calls or so. A GPU hog, quite probably.
0

I have used for clipping on original sample:GLdouble eqn1[4] = { 0.7, 0.7, 0.7, 0.0 }; and the result is: enter image description here

I have used for clipping on original sample:GLdouble eqn1[4] = { -0.7,- 0.7, -0.7, 0.0 }; and the result is:

enter image description here

so you can use negative a,b,c,d for show each side of cube

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.