Removing a rectangular section from a surface

I’ve been working on a 3D game written in OpenGL for a while, and I’m nearly finished with it, though there is one major issue I need to resolve. Firstly, the game involves a stupid looking guy on a Unicycle riding around on randomly generated terrain. This is all fine and good, but I also need to include randomly generated endless pits in the terrain.

The terrain is simply an N x N grid of points spanning the XZ plane, with each point having its own Y-coordinate (simple enough). The question is, how do I get OpenGL to quickly and easily render endless pits in this? An endless pit (in the context of the game) is defined by a point anywhere on the grid (X,Z), an orientation (rotation around the Y-axis), and a size Dx,Dz. The pit sort of “lies” across the terrain as though it were a rectangular blanket, except make that blanket an ACME-style rectangular portable hole. Is there any convenient way to get the terrain in the pit not to be drawn?

What I’ve tried/considered trying:

  • Using the stencil buffer, draw the pit on the screen only in the part where it meets the surface of the terrain. This more or less failed miserably, because it’s way too processor intensive (especially when there are lots of pits), and all sorts of other depth-sorting errors present themselves as well.

  • Actually splicing the polygons over the terrain so that the part covering the pit isn’t there. This is monumentally difficult to do, since it gets really nasty and complicated when you have overlapping pits and whatnot, involving oodles of non-convex polygons and clipping.

What would be ideal is if there was some way to define a volume in space and just tell OpenGL not to draw anything in there, though I have a feeling that such a thing wouldn’t be too easy on the processor either. Can anyone suggest something that might do the trick?

Is there any convenient way to get the terrain in the pit not to be drawn?

Don’t you just want to draw a pit on top of the terrain, so that where the terrain used to be, it’s black now? No need to modify geometry or use the stencil buffer. Just draw the pit as a step after drawing the terrain.

Originally posted by Korval:
Don’t you just want to draw a pit on top of the terrain, so that where the terrain used to be, it’s black now? No need to modify geometry or use the stencil buffer. Just draw the pit as a step after drawing the terrain.

Actually, there’s more to the pits than just a black surface. There actually has to be a hole in the terrain so that the insides of the pit are viewable.

Originally posted by Korval:
Don’t you just want to draw a pit on top of the terrain, so that where the terrain used to be, it’s black now? No need to modify geometry or use the stencil buffer. Just draw the pit as a step after drawing the terrain.

If the “portable hole” is just a black rectangle painted on the terrain, then that’s the right approach. But ithe hole polygon may need to be “fit” to the terrain surface to look right. That operation can in the worst case be almost as complex as cutting the hole into the terrain geometry, or perhaps can be approximated more easily with a small regular mesh and some crude height-of-terrain calculations at the mesh’s vertices.

Or one could render the black rectangles as a 2nd (or nth) texture using the texcoords to control its placement. That gives a great fit, height-wise. But for many dynamic holes, it gets more complex, requiring either multi-pass or render-to-texture to build a composite texture.

But I’m inferring the poster may want something where the pit seems to go down into the terrain, hence the stencil experimentation.

Here’s an old trick I used to draw dynamic craters and trenches in a vis-sim application:

  1. draw the “inside” walls of the hole, trench, pit: a cylinder, box, etc, with normals facing in so the polys are visible from the inside. The top verts will need to be fit to the height of terrain.

  2. Draw your hole “cover” using alpha = 0 or color-masked (i.e., invisible, but with z-writes) instead of stencil. This lays down the right z-values to prevent the terrain from filling your hole. This is also roughly fit to the height of terrain, such that the cover is always slightly higher than the terrain but meets the walls at the edges to avoid unwanted holes.

  3. draw the terrain normally. The cap will prevent the terrain from overwriting your hole, but a hill that’s visually in front of the hole will still occlude it.

Avi

Originally posted by Cyranose:
[b]1. draw the “inside” walls of the hole, trench, pit: a cylinder, box, etc, with normals facing in so the polys are visible from the inside. The top verts will need to be fit to the height of terrain.

  1. Draw your hole “cover” using alpha = 0 or color-masked (i.e., invisible, but with z-writes) instead of stencil. This lays down the right z-values to prevent the terrain from filling your hole. This is also roughly fit to the height of terrain, such that the cover is always slightly higher than the terrain but meets the walls at the edges to avoid unwanted holes.
  1. draw the terrain normally. The cap will prevent the terrain from overwriting your hole, but a hill that’s visually in front of the hole will still occlude it.

Avi[/b]

This will almost certainly work, and I’m surprised I didn’t think of it earlier. I’ll give it a try and will report the results soon enough.