# OpenGL Polygon Filling

This topic is 3643 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm currently working on a project where the first step is to be able to 'slice' the scene's geometry. The image below should help me describe what I mean: Top left - The scene, which features three dolphins. Eventually, these would be replaced with a human organ system. Top right - A 'slice' of the scene, taken by orthographic projection, the thickness given by zNear and zFar. Bottom - What I essentially want to do is 'fill' the polygon outlines of the slice, as if the dolphin objects are solid. The problem I have is that I'm unsure how to do this. My first attempt was to program an advanced clipping plane , that essentially does the job for me. The problem with this method is that I eventually plan to colour code the filling, so that one object is filled in a different colour to another, which would be very difficult to do. I'm currently looking at Polygon Filling, which will obviously work but I'm unsure about a few things. For example, the website above performs polygon filling in 2D. How easy would it be to extend this to 3D? Would it simply be a case of transforming the vertices so that they lie on the image plane, and treat as 2D? Can anyone offer me any advice on how I could approach this problem? Are there other techniques you can recommend? I'd be very grateful for any help!

##### Share on other sites
You could do this using the stencil buffer. First, set up a clipping volume that has the desired slicing plane as the near plane, and infinity-ish as the far plane. Render the object, with color and depth writes disabled, with backface culling enabled and set in reverse, and an "increment" stencil op. Then render the object again with color and depth writes disabled, with backface culling enabled and set normally, and a "decrement" stencil op. Then just set the clipping volume back to something sane, turn color writes back on, and draw over the screen, with the stencil test set to >0. This is a variant of the winding-number-based rasterization algorithm.

NOTE: This algorithm requires that your mesh be a closed manifold.

##### Share on other sites

That certainly seems like the most likely approach to take. I've started adapting some code I found on ATI's site (here) that seems to do the same thing.

Will this technique extend to a scene consisting of multiple meshes (as long as they aren't intersecting/enclosing each other?). The reason I ask is that I intend to use this algorithm to slice a plane through an internal organ system, where each organ is a different colour (for tagging purposes).

When using the ATI example as is, I get some artifacts with multiple (coloured) meshes:

Would this be an inherent flaw of the technique, or do I just need to extend the algorithm? For example, the 'advanced clipping planes' link I referenced above seems to require that the (overlapping) spheres be drawn in order (from small to large).

##### Share on other sites
Quote:
 Original post by SneftelYou could do this using the stencil buffer. First, set up a clipping volume that has the desired slicing plane as the near plane, and infinity-ish as the far plane. Render the object, with color and depth writes disabled, with backface culling enabled and set in reverse, and an "increment" stencil op. Then render the object again with color and depth writes disabled, with backface culling enabled and set normally, and a "decrement" stencil op. Then just set the clipping volume back to something sane, turn color writes back on, and draw over the screen, with the stencil test set to >0. This is a variant of the winding-number-based rasterization algorithm.NOTE: This algorithm requires that your mesh be a closed manifold.
I'm pretty certain this is the exact same algorithm I came up with to solve a very similar problem just last week on an slicing (fake CSG) an arbitrarily complex 3D model. We haven't coded it yet but I'm confident in the theory, its not even that complicated an algorithm.

##### Share on other sites
Is there any way around this problem?

The slices work fine, but the colours from foreground objects project onto more distal ones. :(

[Edited by - SFA on December 18, 2008 10:07:30 AM]

##### Share on other sites
I've been trying to get around this color problem for the last few days, but with little success. I've managed to get it working temporarily by simply not drawing objects behind the near plane. An alternative to this would be to use a clip plane when drawing the objects, eliminating anything behind znear.

I'm posting this code on the off chance someone can spot something - I am still unsure whether it is even possible to retain the colour information. Anyone?

// *********** Setup *************	glOrtho(ortho[0].value, ortho[1].value, ortho[2].value, ortho[3].value, ortho[4].value, 10);	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);	glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);	glDisable(GL_DEPTH_TEST);	glEnable(GL_STENCIL_TEST);	glEnable(GL_CULL_FACE);// *********** First Pass *************	glCullFace(GL_BACK);	glFrontFace(GL_CW);	glStencilFunc(GL_ALWAYS, 0, 0);	glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);	drawObjects();// *********** Second Pass *************	glCullFace(GL_BACK);	glFrontFace(GL_CCW);	glStencilFunc(GL_ALWAYS, 0, 0);	glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);	drawObjects();// *********** And Finally *************	glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);	glEnable(GL_DEPTH_TEST);	glOrtho(ortho[0].value, ortho[1].value, ortho[2].value, ortho[3].value, 1, 10000);		glStencilFunc( GL_NOTEQUAL, 0, ~0); //???	glStencilOp( GL_KEEP, GL_KEEP, GL_INVERT); //???	glEnable(GL_LIGHTING);	drawObjects();        glutSwapBuffers();

EDIT : I have a feeling the last StencilFunc and StencilOp are incorrect, although I'm not sure how much impact it would have on the colour projection.

1. 1
2. 2
Rutin
29
3. 3
4. 4
5. 5
khawk
14

• 11
• 11
• 23
• 10
• 9
• ### Forum Statistics

• Total Topics
633648
• Total Posts
3013111
×