• Create Account

# Terrain 'holes'

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

10 replies to this topic

### #1RobMaddison  Members   -  Reputation: 543

Like
0Likes
Like

Posted 09 October 2013 - 07:55 AM

Afternoon all, hope you're all having a great day.

I'm at work but my mind is wandering onto my game engine.

I was wondering about the Z buffer. Is it possible to reset a pixel's depth in the depth buffer mid-scene? So if I wanted to draw arbitrary holes in my terrain, could I draw my terrain with z buffer enabled, then switch it off, draw something that is 'under' the terrain and draw perhaps the walls of the hole with the z buffer back on?

Think of a hole on a golf putting green. I draw the green (minus hole), switch off the z buffer, draw an ellipse under the terrain, switch z buffer back on and then draw the sides of the hole.

Is this possible? Or is there a better way? The shapes I want to cut out of the terrain are going to be arbitrary so I can't really substitute a terrain triangle for one with a hole model in it.

Thanks

### #2Orangeatang  Members   -  Reputation: 1101

Like
0Likes
Like

Posted 09 October 2013 - 09:34 AM

Sounds like you need a stencil buffer http://en.wikipedia.org/wiki/Stencil_buffer

### #3RobMaddison  Members   -  Reputation: 543

Like
1Likes
Like

Posted 09 October 2013 - 12:54 PM

Sounds like you need a stencil buffer http://en.wikipedia.org/wiki/Stencil_buffer

That looks like just what I'm looking for, thanks

### #4Krohm  Crossbones+   -  Reputation: 2418

Like
0Likes
Like

Posted 10 October 2013 - 01:15 AM

Think of a hole on a golf putting green. I draw the green (minus hole), switch off the z buffer, draw an ellipse under the terrain, switch z buffer back on and then draw the sides of the hole.

Is this possible?
In theory: yes.

In practice: no.

I spent quite some effort on that. I used to go with a "hole map" which would discard terrain fragments on alpha test. Nowadays, it would probably be a distance map. Problem is it is very hard to get the intersections right and unless the geometry is completely "embeddeding" the boundary there will be gaps. Most of the time, they're not very noticeable but I grew confident the only way to deal with them was a proper geometry manipulation.

### #5C0lumbo  Crossbones+   -  Reputation: 1389

Like
0Likes
Like

Posted 10 October 2013 - 01:23 AM

You can do a similar thing without a stencil buffer too. Get your artists to make two parts to the golf hole, the hole geometry itself, and a cap (geometry that would represent the lid of the hole). Then when it's time to render you do:

- Render the normal golf hole geometry.

- Render the golf hole 'cap' geometry with z-write on and a little z-bias. Use a frame buffer mask or blend mode or something to avoid writing anything to the frame buffer.

- Render the terrain normally, it will now leave a hole

### #6tonemgub  Members   -  Reputation: 435

Like
0Likes
Like

Posted 10 October 2013 - 01:45 AM

You can do a similar thing without a stencil buffer too. Get your artists to make two parts to the golf hole, the hole geometry itself, and a cap (geometry that would represent the lid of the hole). Then when it's time to render you do:

- Render the normal golf hole geometry.

- Render the golf hole 'cap' geometry with z-write on and a little z-bias. Use a frame buffer mask or blend mode or something to avoid writing anything to the frame buffer.

- Render the terrain normally, it will now leave a hole

This sounds like a good idea at first, but you'd have to make a cap that stands out of the terrain, to avoid z-fighting between the cap and terrain, but if the cap is above the terrain, that will cause the hole to be shaped like the cap along the view direction, which would cause problems if the camera is (extremely?) close to the ground - getting a cap shape that wouldn't cause z-fighting and wouldn't be noticeable sounds tricky. If the hole is on top of a high, narrow hill, and the camera is looking at it from below, the cap would cut a whole into the sky, or whatever else is behind there.

Edited by tonemgub, 10 October 2013 - 01:46 AM.

### #7RobMaddison  Members   -  Reputation: 543

Like
0Likes
Like

Posted 10 October 2013 - 06:44 AM

You can do a similar thing without a stencil buffer too. Get your artists to make two parts to the golf hole, the hole geometry itself, and a cap (geometry that would represent the lid of the hole). Then when it's time to render you do: - Render the normal golf hole geometry.- Render the golf hole 'cap' geometry with z-write on and a little z-bias. Use a frame buffer mask or blend mode or something to avoid writing anything to the frame buffer.- Render the terrain normally, it will now leave a hole

This sounds like a good idea at first, but you'd have to make a cap that stands out of the terrain, to avoid z-fighting between the cap and terrain, but if the cap is above the terrain, that will cause the hole to be shaped like the cap along the view direction, which would cause problems if the camera is (extremely?) close to the ground - getting a cap shape that wouldn't cause z-fighting and wouldn't be noticeable sounds tricky. If the hole is on top of a high, narrow hill, and the camera is looking at it from below, the cap would cut a whole into the sky, or whatever else is behind there.

C0lumbo's idea was pretty much was I was thinking you could do with the zenable on/off thing, but it would still work I think. He mentioned adding a little zbias (which I assume means initially drawing the hole 'cap' with zenable on and colour off but very slightly closer to the camera in screen space z (meaning the hole's silhouette would be identical wherever along the screen space z axis you draw it). That would place the cap in the zbuffer slightly closer and when you draw the terrain, it should leave a hole.

So the order would be, sides of hole first (zenable on and colour on), cap of hole (zenable on, colour off, adjust final z coord), terrain with normal state

### #8RobMaddison  Members   -  Reputation: 543

Like
0Likes
Like

Posted 10 October 2013 - 06:47 AM

If the hole is on top of a high, narrow hill, and the camera is looking at it from below, the cap would cut a whole into the sky, or whatever else is behind there.

If the hole is on top of a high, narrow hill, and the camera is looking at it from below, the cap would cut a whole into the sky, or whatever else is behind there.

The cap in this example would be facing away from the viewer wouldn't it? Meaning it'll get discarded anyway

### #9tonemgub  Members   -  Reputation: 435

Like
0Likes
Like

Posted 11 October 2013 - 02:15 AM

The cap in this example would be facing away from the viewer wouldn't it? Meaning it'll get discarded anyway

I guess you're imagining the cap to be a disc parallel to the ground, while I'm imagining it to be a speherical or cylindrical shape (or whatever sahpe he wants), with only a small part protruding above the ground? The part of the sphere that protrudes from the top of the hill would still be visible. But It think even with a disc shape and back-face clipping, you'd have problems placing it on the ground so as to avoid z-fighting - if you place it too far up from the ground, the hole will appear displaced, and the fact that it would disappear when looking at it from below wouldn't be a very nice-looking effect either. Maybe it can be fixed with depth-bias. Of course, this all depends on what requirements the op has related to the possible angle the hole can be seen at.

I'm also not sure how using the cap-model geometry for the hole fits in with the op's idea of "arbitrary".

So the order would be, sides of hole first (zenable on and colour on), cap of hole (zenable on, colour off, adjust final z coord), terrain with normal state

Yes, that would work. I thought you wanted to avoid using geometry... If you already have the sides of the hole as geometry, then it would be pretty hard to match the stencil to the shape of your geometry or vice-versa, so the cap ideea is better. You still have to align the sides of the hole geometry to the terrain - you'll get artifacts if they don't align properly?

Edited by tonemgub, 11 October 2013 - 02:34 AM.

### #10RobMaddison  Members   -  Reputation: 543

Like
0Likes
Like

Posted 11 October 2013 - 04:37 PM

The cap in this example would be facing away from the viewer wouldn't it? Meaning it'll get discarded anyway

I guess you're imagining the cap to be a disc parallel to the ground, while I'm imagining it to be a speherical or cylindrical shape (or whatever sahpe he wants), with only a small part protruding above the ground? The part of the sphere that protrudes from the top of the hill would still be visible. But It think even with a disc shape and back-face clipping, you'd have problems placing it on the ground so as to avoid z-fighting - if you place it too far up from the ground, the hole will appear displaced, and the fact that it would disappear when looking at it from below wouldn't be a very nice-looking effect either. Maybe it can be fixed with depth-bias. Of course, this all depends on what requirements the op has related to the possible angle the hole can be seen at.

I'm also not sure how using the cap-model geometry for the hole fits in with the op's idea of "arbitrary".

So the order would be, sides of hole first (zenable on and colour on), cap of hole (zenable on, colour off, adjust final z coord), terrain with normal state

Yes, that would work. I thought you wanted to avoid using geometry... If you already have the sides of the hole as geometry, then it would be pretty hard to match the stencil to the shape of your geometry or vice-versa, so the cap ideea is better. You still have to align the sides of the hole geometry to the terrain - you'll get artifacts if they don't align properly?

No, I don't need to avoid using geometry, in fact that's the way I had it planned. I was wondering about the alignments, I'll just have to be careful with it I guess. The player won't be able to see the holes too closely so it should be fine and the 'holes' will actually be a thick trench cut out of the terrain. Thought a golf hole might have been easier to explain!

### #11RobMaddison  Members   -  Reputation: 543

Like
0Likes
Like

Posted 12 October 2013 - 08:34 AM

Guys, it works perfectly!  Check out the image...

#### Attached Thumbnails

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS