Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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).

share|improve this question
    
You can check out 'toon' or cell shading, where there is a contour drawn around an object. lighthouse3d.com/tutorials/glsl-tutorial/?toon. –  Jonathan Connell Jun 27 '11 at 10:54
    
The case is, I can't use shaders in this project... –  jeicam Jun 27 '11 at 12:18
    
@Maciekp Why can't you use shaders? Is this homework? –  Jonathan Connell Jun 27 '11 at 12:22
    
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. –  jeicam Jun 27 '11 at 12:27
1  
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. –  Jonathan Connell Jun 27 '11 at 13:01
show 1 more comment

3 Answers

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.

share|improve this answer
add comment

Here's an old tutorial (for OpenGL 1.1 so you should have no trouble getting it to run on all the systems you mention) showing this effect.

enter image description here

share|improve this answer
    
Nice find! Love this type of hack ^^ –  Jonathan Connell Jul 1 '11 at 10:47
    
That's exactly the same way I was trying to do this(but got this: goo.gl/OI5uc ). I ended up first drawing all borders in x-axis ascending order with lines(incrementing stencil bit- with glStencilFunc(GL_ALWAYS,countStencilBit,0xFFFFFFF); and glStencilOp(GL_REPLACE,GL_KEEP,GL_KEEP);), and then drawing objects(with disabled depth test) in descending order with glStencilFunc(GL_GEQUAL,countStencilBit,0xFFFFFFFFFFF); and glStencilOp(GL_REPLACE,GL_KEEP,GL_KEEP);. That works fine, but I've got situation, where it works good,and suddenly overlapped object's border is drawn improperly. –  jeicam Jul 3 '11 at 16:37
add comment
  • 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.

share|improve this answer
    
This is a great Barba-trick! –  FxIII Jun 27 '11 at 11:14
    
This is the old trick they used for outlines when the gfx pipeline was still fixed. –  Jonathan Connell Jun 27 '11 at 12:22
    
That won't work for concave objects –  jeicam Jun 27 '11 at 12:22
    
@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. –  Jonathan Connell Jun 27 '11 at 13:00
    
It should work for concave objects if you invert the normals (you must have face culling enabled) while drawing the contours. –  r2d2rigo Jun 27 '11 at 13:26
show 8 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.