View Full Version : Masking a portion of a model

04-08-2004, 07:48 AM
I am trying to implement a masking tool that masks out a portion of the mesh. I tried the stencil buffer approach, but the solution was too slow, and I cannot be sure that all my models are wound consistently(data issue).

I do not want to modify the mesh data itself(ie intersection testing and partitioning). I just want to mask an area on a mesh defined by something like a polygon lasso. Obviously the mask will rotate along with the model. Ie I am not trying to mask the screen area, but the model itself.

Anyone have any suggestions, how I can accomplish this with decent performance. A 4 pass algorithm is not a solution.

04-08-2004, 02:28 PM
If I understand you, you want to be able to draw a lasso in screen space and have it extruded through the model, masking everything inside the extruded volume, even when you rotate the model.

There are a bunch of ways this could be done, including the stencle buffer, as you said. (That would be roughly O(number of selections * their size)). You could also do tricks with vertex or fragment shaders as well as textures.

What are you trying to do overall? Also, do you want new surfaces added where you are cutting the object? That would reduce your options.

04-08-2004, 03:26 PM
I would keep a base copy of the full triangle list, and a copy that is the list of triangles to render. Then I would run intersection between triangles and screen-space polygon, and remove the intersecting triangles from my list to render. This takes a little bit of processing when you actually modify the polygon, but does not slow down anything at render time (in fact, makes it faster, because you render less).

04-08-2004, 04:54 PM
Actually, the only algorithm that seems to work at all is the csg algorithm like the example from
glut source. It takes 4 passes for both each a,b. In this case b is only a few dozen polygons. But a, the model is much larger, 10,000 polys or many more.

Also, there seems to be visual problems, when I use it + depth offset. Althought that could be my mistake.

This solution is easiest to implement, but not flexible. My data is rendered via a vertex array, composed from the list of polygons, glDrawArrays.
Unfortunantly, too much code would break if I implemented this solution, or I would need to disable virtually all functionality in the app do it.

I was hoping the stencil approach would work, but as I mentioned in my first post, I cannot always be sure that the model uses CCW winding. This is a problem with the file data. I have user controlled routines to do this, but nothing automatic, as far as I know, unless the model is mostly flat, there is no means to determine if it is correctly(CCW) wound.

If anyone has an idea with stencil, that eliminates the problem, and is a bit faster than csg, I would greatly welcome it. Thank you very much in advance.

04-08-2004, 08:40 PM
Although I think I would have used stecil buffers, I have a few other suggestions as well.

You could add a texture to the mesh where you include the masking data in the alpha value and use GL_ALPHA_TEST. You would recalculate the texture coordinates each time the view changes and edit the texture each time the mask was changed. This may be something similiar to what endash hinted.

You could also write the mask (I can think of several ways to do this efficiently) to the Z-buffer before you render the mesh to make sure that the depth test will eliminate the unwanted geometry.

04-08-2004, 08:45 PM
"Obviously the mask will rotate along with the model. Ie I am not trying to mask the screen area, but the model itself."

This is obviously too early in the morning for me since I missed this part. I think the obvious solution is to use textures and the alpha test.

04-09-2004, 07:35 AM
It seems like the problmes with textures and alpha test would be computing the texture, however if you were to use texture coordinate magic to project a texture into the scene, that aught to be easy, althouh you'd be limited by the number of textures.

04-09-2004, 09:28 AM

Why would that solution break anything? If you're currently using DrawArrays, you're golden.

Just keep piecing together the array just like you're doing now. Then, if something is masked, generate a triangle list which contains the triangles you DO want to draw, and call DrawElements(). Conceptually, if nothing was masked, this triangle list would just contain the integers: 0, 1, 2, 3, 4, 5, ... which is how DrawArrays issues vertices. Dropping things out of that list seems easy, assuming you know what it is you want to drop. If you don't, it seems that you should figure that out first (else you can't fulfill the solution to "rotating the selection with the object").

04-09-2004, 11:11 AM
It seems like the problmes with textures and alpha test would be computing the texture I don't really see the problem. Render the polygon defined by the lasso to a pbuffer. Calculate the texture cordinates (easiest way - by using the projection matrix). Although there is some work involved in doing this, it is only done when the mask is redefined. The number of textures used may be a problem though, as you said.

04-09-2004, 11:32 AM
neomind: Right, the thing I'm thinking would be the hard way would be to (manually, I guess) map the selection onto a texture to be mapped on to the object. That's the only way (I think) to have this selection be per-fragment and be done in model space .