8
\$\begingroup\$

I need to draw contour around 2d objects in 3d space. I tried drawing lines around object(+points to fill the gap), but due to line width, some part of it(~50%) was covering object. I tried to use stencil buffer, to eliminate this problem, but I got sth like this(contour is green):

http://goo.gl/OI5uc (sorry I can't post images, due to my reputation)

You can see(where arrow points), that some parts of line are behind object, and some are above. This changes when I move camera, but always there is some part, that is covering it. Here is code, that I use for drawing object:

    glColorMask(1,1,1,1);
    std::list<CObjectOnScene*>::iterator objIter=ptr->objects.begin(),objEnd=ptr->objects.end();
    int countStencilBit=1;
    while(objIter!=objEnd)
    {
        glColorMask(1,1,1,1);
        glStencilFunc(GL_ALWAYS,countStencilBit,countStencilBit);
        glStencilOp(GL_REPLACE,GL_KEEP,GL_REPLACE );
        (*objIter)->DrawYourVertices();

        glStencilFunc(GL_NOTEQUAL,countStencilBit,countStencilBit);
        glStencilOp(GL_KEEP,GL_KEEP,GL_REPLACE);
        (*objIter)->DrawYourBorder();

        ++objIter;
        ++countStencilBit;
    }

I've tried different settings of stencil buffer, but always I was getting sth like that. Here is question: 1.Am I setting stencil buffer wrong?

2. Are there any other simple ways to create contour on such objects?

Thanks in advance.

EDIT:

1. I don't have normals of objects.

2. Object can be concave.

3. I can't use shaders(see below why).

\$\endgroup\$
6
  • \$\begingroup\$ You can check out 'toon' or cell shading, where there is a contour drawn around an object. lighthouse3d.com/tutorials/glsl-tutorial/?toon. \$\endgroup\$ Jun 27, 2011 at 10:54
  • \$\begingroup\$ The case is, I can't use shaders in this project... \$\endgroup\$ Jun 27, 2011 at 12:18
  • \$\begingroup\$ @Maciekp Why can't you use shaders? Is this homework? \$\endgroup\$ Jun 27, 2011 at 12:22
  • \$\begingroup\$ This app will have to work on many computers, not always the newest one(that's 1 point).2. It is not a game, this is app with graphics as a part of it+ it will have to work on mac,win,linux. \$\endgroup\$ Jun 27, 2011 at 12:27
  • 1
    \$\begingroup\$ It should maybe have been posted on SO if it is not for GameDev and you should give the most information possible if you want your question answered. \$\endgroup\$ Jun 27, 2011 at 13:01

2 Answers 2

12
\$\begingroup\$
  • Draw your entire scene, except highlighted objects.
  • Draw the objects you want to highlight, in pure color, sorted from back to front, and with a small scale applied (1.05f - 1.1f).
  • Draw the final objects, again, from back to front.

For extra eye-candy, try drawing the contour pass to a separate render target and apply a small blur, then blend this texture with your final scene using the appropiate Z values.

\$\endgroup\$
13
  • \$\begingroup\$ This is a great Barba-trick! \$\endgroup\$
    – FxIII
    Jun 27, 2011 at 11:14
  • \$\begingroup\$ This is the old trick they used for outlines when the gfx pipeline was still fixed. \$\endgroup\$ Jun 27, 2011 at 12:22
  • 2
    \$\begingroup\$ That won't work for concave objects \$\endgroup\$ Jun 27, 2011 at 12:22
  • \$\begingroup\$ @Maciekp Your question was about 2D objects (planes) in 3D space, where does concave come into it? Just scale along the two axis != to the face direction. \$\endgroup\$ Jun 27, 2011 at 13:00
  • \$\begingroup\$ It should work for concave objects if you invert the normals (you must have face culling enabled) while drawing the contours. \$\endgroup\$
    – r2d2rigo
    Jun 27, 2011 at 13:26
0
\$\begingroup\$

I'll do this like that:

  • Sort objects in front-back(distance from camera) order

  • Draw object's border(as lines and points) with disabled GL_DEPTH_TEST

  • Draw object with enabled GL_DEPTH_TEST

  • Do last two steps for every object

I'll try to do this like that, but if anyone have ideas of better solution to this problem(MIND CONCAVE OBJECTS, PLEASE)- feel free to share with us.

PS. I'm not fully satisfied with my solution-changing OpenGL states so often can be harmful. But if no one can help me with stencil buffer settings, I guess that's the only way for me.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .