Draw contour around object in Opengl

therealszaka
  • Draw contour around object in Opengl therealszaka

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

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

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

Tags
opengl c++ graphics directx graphics-programming
Related questions and answers
  • ); // Set The Color Of The Model ( NEW ) // ORIGINAL DRAWING CODE //Draw the model as an interpolation between the two frames glBegin(GL_TRIANGLES); for(int i = 0; i...].MatIndex].tex.Use(); // AFTER THE TEXTURE IS APPLIED I INSERT THE TOON FUNCTIONS HERE (FIRST PASS) glHint (GL_LINE_SMOOTH_HINT, GL_NICEST); // Use The Good..., In); // Get The Current Line ( NEW ) shaderData[i][0] = shaderData[i][1] = shaderData[i][2] = float(atof (Line)); // Copy Over The Value ( NEW ) } fclose

  • ; case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: cout<<"GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT\n"<<endl; break; case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER...); glDrawArrays(GL_TRIANGLES, 0, 6); glBindTexture(GL_TEXTURE_2D, 0); } Tried everything I can think of or find in a FBO tutorial or have read about. I don't get any errors and it returns as complete. (Also I can't seem to get it glTexImage2d a image of size width and height. says invalid values, and if I try to use GL_TEXTURE_RECTANGLE it says invalid enum :-/ but that's for a different question

  • onscreen, in the shape of the terrain rectangle, but there are no regular lines etc. Here's the code I use for rendering: void ShaderProgram::Draw() { using namespace AntiMatter; if( ! m... object from an array of CustomVertex objects. This array is said to be interleaved. It renders successfully with the following code: void VertexBufferObject::Draw() { if( ! m_bInitialized... ); glBindVertexArray( m_nVaoHandle ); glDrawArrays( GL_TRIANGLES, 0, m_nNumTris ); glBindVertexArray(0); glUseProgram(0); } Can anyone see errors or omissions in either the VAO creation code

  • wrong in my frame buffer set up code, or elsewhere. But I can't see what. The FBO is set up through the following function: unsigned int fbo_id; unsigned int depth_buffer; int m_FBOWidth, m_FBOHeight...); glBindFramebuffer(GL_FRAMEBUFFER, 0); } Here is my drawing box code, which just takes a transformation matrix and calls the appropriate functions. The current values of P is a projection matrix... transformation has occurred somewhere. Which doesn't make sense as the object renders fine when not using my frame buffer (see bottom of post). The current result is such: Current result http://k.minus.com

  • I have a MD2 model loader, I am trying to substitute its immediate drawing function with a Vertex Buffer Object one.... I am getting a really annoying access violation reading error and I can't...-&gt;texCoordY); glVertex3f(pos[0], pos[1], pos[2]); } } glEnd(); What I'd like to do is to calculate all positions before hand, store them in a Vertex array and then draw...; Vec3f normal = v1-&gt;normal * (1 - frac) + v2-&gt;normal * frac; if (normal[0] == 0 && normal[1] == 0 && normal[2] == 0) { normal = Vec3f(0, 0, 1

  • = SDL_DisplayFormat(zoomedImage); SDL_FreeSurface(zoomedImage); } return optimizedImage; } ...here is the code I actually use to create the GL texture... GLuint thisIsMyTexture; glGenTextures(1, &thisIsMyTexture); temporarySurface = loadImage("theimagetomytexture.png"); glBindTexture(GL_TEXTURE_2D,thisIsMyTexture); glTexImage2D(GL_TEXTURE_2D, 0, 4, temporarySurface-&gt;w...; outside that space, white is drawn. Here is my OpenGL init code... SDL_SetVideoMode(windowWidth, windowHeight, 16, SDL_HWSURFACE|SDL_GL_DOUBLEBUFFER|SDL_OPENGL); glClearColor(0, 0, 0, 0

  • I'm writing a simple class to draw all the debugging lines I have in my scene at once. The code in the draw loop is this so far: (If I put for example, 2 instead of temporary2DVerts.size() where I have marked with //THIS LINE, the code works fine.) when I run the code below the line breaks //HERE. Access violation reading location 0x00000000. seems like the create buffer line is not working, but why? what's the solution? D3D10_BUFFER_DESC bd; bd.Usage = D3D10_USAGE_DYNAMIC; bd.ByteWidth = sizeof(VERTEX) * temporary2DVerts.size();// THIS LINE bd.BindFlags = D3D10_BIND_VERTEX_BUFFER

  • I want to include the drawing of other vectors of vertices in the following code, in a number of places it seems easy enough to simply add or multiply size()s of the two vectors, for instance... it all separately? Basically how would I extend this code to include more vectors of vertices? if (temporary2DVerts.size() &gt; 0) { // create the vertex buffer and store the pointer into pBuffer... version of both. This is so the permanent ones can just be loaded in and are always drawn, and the temporary ones only get drawn for a frame. This way I can create moving lines for debugging, like

  • to collide with.. as follows. I expect there is issues with this as I get objects colliding with the ground but half way through it and they glitch around like crazy on collision. void GameState... code shows the changes I made to get accurate physics. void GameState::createScene() { m_pSceneMgr-&gt;createLight("Light")-&gt;setPosition(75,75,75); // Physics // As a test we want a floor plane...::DEFAULT_RESOURCE_GROUP_NAME, p, 200000, 200000, 20, 20, true, 1, 9000,9000,Ogre::Vector3::UNIT_Z); ent = m_pSceneMgr-&gt;createEntity("floor", "FloorPlane"); ent-&gt;setMaterialName("Test/Floor

Data information