Subtractive CSG on irregular surfaces using the stencil buffer

Started by
6 comments, last by irreversible 18 years ago
My problem is this: I have a heightmapped terrain that I want to partition into colored segments using customized region information (doesn't make much sense, does it? :) ). Regions are defined as a series of 2D coordinates that map onto the terrain and can be any shape (currently I'm only going for convex polygons). I'm a little puzzled on how to go about this. I tried using a capped volumetric polygon (such as a cylinder in the case of a circular region) to carve a shape out of the terrain, but that places the cap above the ground and that's not really what I want (I really don't know if this makes sense). Basically I'm using the same z-pass algorithm used for shadow volumes - I think it has all the potential I need. To visualize what I'm trying to do, take a political map of, say, Europe with all the fancy colors on it (each coutry has its own color). Now, place the map on a table and add height values to it, making mountains stick out of the map. But also retain the fancy colors on the map (in a perspectively correct way). The following is just an account of what I'm doing and should only serve to show that I have actually done my homework :). I'm rendering the colored segments on textured terrain (much like areas affected by cities in Civilization), which entails the following: 1) render the terrain normally 2) start stenciling, disable color masking, and set glStencilFunc(GL_ALWAYS, 0x0, 0x0) 3) next render two passes (one increments on zpass, the other decrements) - this is what I'm not at all sure about because I can't figure out how to composit the CSG (thus it doesn't make much sense in posting any code because I could post any possible variant that I've tried, none of which works) 4) enable color masking and draw a quad across the entire screen with glStencilFunc(GL_NOTEQUAL, 0, ~0) 5) end stenciling edit: fixed typos [Edited by - irreversible on March 27, 2006 11:50:40 AM]
Advertisement
bump...
perhaps a diagram of what u want will help.
u mention CSG but from your discription it sounds like u dont need it.
if u wanna use stencil to draw only part of the screen a certain color.

clear stencil to 0
stencil op KEEP KEEP REPLACE
stencil func ALWAYS 1 0xffffffff

draw fullscreen quad with
stencil op KEEP KEEP KEEP
stencil func EQUAL 1 0xffffffff

only parts with stencil of 1 will get drawn
A diagram - sure. Here's the political map of Europe:



I want to achieve the same coloring effect on a 3D terrain except that the colored areas are defined by a series of points not known in advance (much like country borders).

I will reiterate what I think I need to do (this sort of invalidates what you said, zedzeek - sorry - feel free to correct me if I'm wrong).

First of all, I want to blend these colored regions on texture-mapped terrain, resulting in a tinted areas (a colored section simply alpha blended on textured terrain). The problem is that the regions may contain any number of points and these points may be laid out in just about any (convex) shape - much like the borders of, say Germany (okay, so Germany's not convex...) and they are never guaranteed to lay on polygon intersecion points of the original terrain. That is - these points are guaranteed to be placed "randomly" on the terrain with no quantization in mind.

What I figure I have to do is use CSG to first separate the region on the XZ-plane by using a volumetric mesh in the shape of the region that extends up. Then, I need to (invert the subtracted region and) paint over only the pixels that were subtracted from the rest of the terrain.

Example:


I feel bad that I cannot upload a diagram or an image of what I'm trying to do, but the problem comes with the subtraction itself: imagine a cylinder (such as a jar or a test tube) half-filled with dirt. The cylinder is a circular region extended into 3D on the Y-axis and the dirt (which isn't even) is the surface of the heightmap. Now, it doesn't matter how much dirt exists outside of the cylinder - I only want to add a yellow (for example) tint to the dirt that is physically inside it. For full comparison, dig the cylinder half into the ground, fill it with dirt until it matches with the ground outside it and now use the stencil buffer to tint the dirt inside it yellow.

In a nutshell: what are the operations I need to do to achieve that using the stencil buffer?


I guess that's as clear as I can get :).
standard csg then. perhaps have a look here
http://www.delphi3d.net/download/stencilcsg.zip
Quote:Original post by zedzeek
standard csg then. perhaps have a look here
http://www.delphi3d.net/download/stencilcsg.zip


I'll go over this again in my head - the problem I having so far is coming up with a way that retains the cap of the subtracted region in the shape of the terrain (the subtraction itself isn't the problem)... Could be I'm just not getting this (possibly because of overcomplicating it in my head), though. Bah.
soory im slow, i cant still picture what u want, im thinking this is one of those cases where a picture is worth a thousnad words.
i get the impression u wanna do a bollean AND csg
The link you sent me got me off in the right direction - thanks; I'm a little busy right now to actually spend a lot of time on it (I'm hoping to over the weekend), but after some thought I came to the conclusion that CSG intersection is pretty much what I need (it's weird how something so simple took me that long to figure out). Yes - essentially that's a boolean AND :). However, I gave it a shot last night and couldn't really get it to do exactly what I need - I got some mildly weird results (that, regardless, were a step in the right direction) - I'm just not sure if it was my rendering code or the fact that one of the operands is essentially a 2D surface "with wrinkles"... I'll keep you posted.

This topic is closed to new replies.

Advertisement