GL Stenciling Algorithms for Rendering Object Silhouettes, Exams of Computer Science

Two algorithms for rendering object silhouettes using opengl stenciling. The first algorithm involves translating the image in four directions and counting pixels that pass the depth test. The second algorithm renders the offset depth image and toggles the stencil bit at each pixel for the remaining facets. Both algorithms are explained in detail.

Typology: Exams

Pre 2010

Uploaded on 08/31/2009

koofers-user-7bt
koofers-user-7bt 🇺🇸

10 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
/*
** save the depth buffer and turn off the color buffer
*/
glReadPixels(0, 0, winWidth, winHeight, GL_DEPTH_COMPONENT,
GL_FLOAT, depthSave);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
/*
** clear the stencil buffer for this object
*/
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 0, 0); /* clear stencil for this object */
glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
/*
** set the polygon offset a tiny bit
*/
glEnable(GL_POLYGON_OFFSET_FILL);
glPolygonOffset(0.5, 1.0);
drawMyObject();
glDisable(GL_POLYGON_OFFSET_FILL);
/*
** set up for stenciling, write a ’1’ whereever there is
** a line
*/
glStencilFunc(GL_ALWAYS, 1, 1);
glStencilOp(GL_ZERO, GL_ZERO, GL_REPLACE);
/*
** disable writes to the Z−buffer test
** test for less than or equal to
*/
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_FALSE);
/*
** render all polygons in outline mode without updating the
** color buffer or the depth buffer,
** this sets the stencil bit where for all the non−hidden
** lines in the object
*/
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
drawMyObject();
/*
** now the stencil only has the bit set for the non−hidden lines
** Turn on the color buffer
** restore the depth buffer
** write only were the stencil bit is set
** now the depth buffer and color buffer are current
**/
glDepthMask(GL_TRUE);
glDepthFunc(GL_ALWAYS);
glDisable(GL_STENCIL_TEST);
pf3
pf4
pf5

Partial preview of the text

Download GL Stenciling Algorithms for Rendering Object Silhouettes and more Exams Computer Science in PDF only on Docsity!

** save the depth buffer and turn off the color buffer */ glReadPixels(0, 0, winWidth, winHeight, GL_DEPTH_COMPONENT, GL_FLOAT, depthSave); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);

/* ** clear the stencil buffer for this object / glEnable(GL_STENCIL_TEST); glStencilFunc(GL_ALWAYS, 0, 0); / clear stencil for this object */ glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);

/* ** set the polygon offset a tiny bit */ glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(0.5, 1.0);

drawMyObject();

glDisable(GL_POLYGON_OFFSET_FILL);

/* ** set up for stenciling, write a ’1’ whereever there is ** a line */ glStencilFunc(GL_ALWAYS, 1, 1); glStencilOp(GL_ZERO, GL_ZERO, GL_REPLACE);

/* ** disable writes to the Z−buffer test ** test for less than or equal to */ glDepthFunc(GL_LEQUAL); glDepthMask(GL_FALSE);

** render all polygons in outline mode without updating the ** color buffer or the depth buffer, ** this sets the stencil bit where for all the non−hidden ** lines in the object */ glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); drawMyObject();

/* ** now the stencil only has the bit set for the non−hidden lines ** Turn on the color buffer ** restore the depth buffer ** write only were the stencil bit is set ** now the depth buffer and color buffer are current **/

glDepthMask(GL_TRUE); glDepthFunc(GL_ALWAYS); glDisable(GL_STENCIL_TEST);

** now the stencil only has the bit set for the non−hidden lines ** Turn on the color buffer ** restore the depth buffer ** write only were the stencil bit is set ** now the depth buffer and color buffer are current **/

glDepthMask(GL_TRUE); glDepthFunc(GL_ALWAYS); glDisable(GL_STENCIL_TEST); pushOrthoView(0, 1, 0, 1, 0, 1); glRasterPos2f(0, 0); glDrawPixels(winWidth, winHeight, GL_DEPTH_COMPONENT, GL_FLOAT, depthSave); popView(); glEnable(GL_STENCIL_TEST);

glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); glStencilFunc(GL_EQUAL, 1, 1); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); glColor3f(1,1,1); drawMyObject();

/* ** Turn off stenciling, enable writes to the color buffer, then ** render all polygons again in outline mode using foreground color */ glDisable(GL_STENCIL_TEST); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glDepthFunc(GL_LESS);

Stenciling

Alg 1:

  1. Draw the object 4 times, offsetting in X & Y (screen−space) each time
  2. increment the stencil buffer each time

stencil values meaning 0 no object 1 outline of object 2 silhouette 3 silhouette 4 interior

  1. test for cases 2 & 3, draw only these in line mode

Alg 2.

  1. offset the polygon
  2. disable color buffer
  3. draw the objects (fills the depth buffer)
  4. disable offset
  5. make no further changes to the depth buffer (mask out)
  6. toggle stencil bits
  7. cull the back faces
  8. draw objects in line mode
  9. set stencil test = 1, zeroing out the stencil buffer
  10. enable color buffers
  11. disable the Z buffer
  12. draw objects in line mode with stencil test
  13. reset state

** render silhouette edges using the second algorithm

else if (mode == SILHOUETTE_ALG2) {

** render the offset depth image

glEnable(GL_POLYGON_OFFSET_FILL);

glPolygonOffset(1,1);

glColorMask(0,0,0,0);

DrawObjects();

glDisable(GL_POLYGON_OFFSET_FILL);

** make no further changes to the depth image

glDepthMask(0);

** cull all facets of one (arbitrary) orientation. render the

** remaining facets in outline mode, toggling the stencil bit

** at each pixel.

glEnable(GL_STENCIL_TEST);

glStencilFunc(GL_ALWAYS, 0, 1);

glStencilOp(GL_KEEP, GL_KEEP, GL_INVERT);

glEnable(GL_CULL_FACE);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

DrawObjects();

** color all pixels in the framebuffer with stencil value 1

glStencilFunc(GL_EQUAL, 1, 1);

glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);

glColorMask(1,1,1,1);

glColor3f(1,1,1);

glDisable(GL_DEPTH_TEST);

DrawObjects();

** return state to default values

glDisable(GL_STENCIL_TEST);

glDisable(GL_CULL_FACE);

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glDepthMask(1);

glEnable(GL_DEPTH_TEST); /* XXX */