2

I am rendering complex 3d objects. Here is a simple example with a sphere-like object:

enter image description here

Next I am applying a clipping plane to these objects and rendering a texture on this plane, giving the impression you are looking at the inside of the object, as if it was sliced. For example:

enter image description here

The problem is the jagged edge of the texture. It will stick out passed the boundary of the surface. Here's another angle where you can see it sticking out. The surface and the texture both derive from the same source data, but the surface is smoothed and has a higher resolution than the texture.

enter image description here

What I want is to be able to somehow clip the texture, so that it never sticks out past the boundary of the surface. Also, I don't want to simply scale down the texture, since although this might prevent it from sticking outside, it would create interior gaps between the texture edge and the surface edge. I would rather the texture be a little too big and have it clipped so that it sits flush against the edge of the surface.

Here's where I am:

I figured the first step would be to define the intersection of the plane and the surface. So now I have that, as an ordered list of line segments. However, I'm not sure how to proceed with this info (or if this is even the best approach).

I've been reading up on stencil buffers. One approach might be to turn the intersection line into a 2d shape and draw this into a stencil buffer. Then apply this when drawing the texture. (Although I think it's a lot of work since the shapes can be complicated.)

I am wondering if I can somehow use the already drawn surface (in conjunction with a stencil buffer or some other technique) to somehow clip the texture -- without having to go through the extra trouble of deriving the intersection line, etc.

What's the best approach here? (Any online examples you can point me to would also be really helpful.)

martinez314
  • 12,162
  • 5
  • 36
  • 63

1 Answers1

3

If you're clipping convex objects and know coordinates of clipped points, you can create polygonal "cap" yourself - just draw clipped points in proper order using GL_TRIANGLE_FAN, and that's it. Won't work with non-convex object - that would require triangulation algorithm. You could use glu tesselators to triangulate polygons, but that can be tricky/difficult.

If clipped area can be defined by formula, you can write a shader that'll precisely clip pixels over certain distance (i.e. if x^2+x^2+z^2 > r^2 do not draw pixel).

You could also draw back-facing faces with a shader that would draw every back facing pixel as if it were on on clip-plane using simple raytracing. That's complicated, and might be overkill in your case. Dead Rising used similar technique in their game engine.

Also you can use stencil buffer.

Draw back-facing faces first with GL_INCR (glStencilOp(GL_KEEP, GL_INCR, GL_INCR)), then draw front-facing surfaces with GL_DECR (glStencilOp(GL_KEEP, GL_DECR, GL_DECR)). Then draw texture only where stencil is non-zero. (glStencilFunc(GL_GREATER, 0, 0xff); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);). If you have many overlapping shapes, however, you'll need to take special care of them.

--edit--

However, I'm not sure how to proceed with this info (or if this is even the best approach).

Draw it as a triangle fan. For convex objects, that's all you need. For non-convex objects that won't work.

ve been reading up on stencil buffers. One approach might be to turn the intersection line into a 2d shape

No, it won't work like that. Region you want to fill with texture should hold certain stencil value. That's how stencil clipping works.

to somehow clip the texture

In OpenGL you have 6(?) clip planes. If you need more than that, you'll need advanced techniques - stencil, deriving intersection line, shaders, or triangulation.

Any online examples you can point me to would also be really helpful

Drawing Filled, Concave Polygons Using the Stencil Buffer

SigTerm
  • 26,089
  • 6
  • 66
  • 115
  • Thanks @SigTerm. I was able to get it to work with a stencil buffer using the method you described. This [page](http://www.opengl.org/archives/resources/code/samples/advanced/advanced97/notes/node10.html) also has more info about this technique. – martinez314 Sep 27 '13 at 15:18