Results 1 to 3 of 3

Thread: Strangeness in drawing lines and polygons

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2008
    Posts
    3

    Strangeness in drawing lines and polygons

    Hi,

    I'm quite new to opengl so this may be a FAQ but I didn't find the answer while searching the forum archives (searched for grid and GL_LINES) or the wiki (though the problem might be related to the Polygon Offset thingy).

    I want to draw a simple filled polygon (as GL_POLYGON) with the x, y and z axis drawn as lines (as GL_LINES). The polygon lies across the three axis (I mean it is located so that the axis "goes throuh it" (see below)).

    The problem I meet is that if I draw the polygon first it looks like it is "behind" the axis lines, and if I draw the axis first, then the polygon sort of completely "masks" them while there should be only parts of the axis lines that should be masked.

    Here is the code (a slight extrapolation from one of the first example from the Red Book):

    Code :
    void display(void)
    {
      glClear(GL_COLOR_BUFFER_BIT);
      glColor3f(1.0, 1.0, 1.0);
      glLoadIdentity();
      gluLookAt(7.0, -10.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
     
      glColor3f(0.0,0.0,1.0);
      glBegin(GL_LINES);
        glVertex3f(-50.0, 0.0, 0.0);
        glVertex3f(50.0, 0.0, 0.0);
      glEnd();
     
      glColor3f(1.0,0.0,0.0);
      glBegin(GL_LINES);
        glVertex3f(0.0, -50.0, 0.0);
        glVertex3f(0.0, 50.0, 0.0);
      glEnd();
     
      glColor3f(0.0,1.0,0.0);
      glBegin(GL_LINES);
        glVertex3f(0.0, 0.0, -50.0);
        glVertex3f(0.0, 0.0, 50.0);
      glEnd();
     
      glColor3f(1.0, 1.0, 1.0);
      glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
      glBegin(GL_POLYGON);
        glVertex3i(1, 1, 1);
        glVertex3i(-1, -1, 1);
        glVertex3i(-1, -1, -1);
        glVertex3i(1, 1, -1);
      glEnd();
     
      glFlush();
    }
     
    void reshape(int w, int h)
    {
      glViewport(0, 0, (GLsizei) w, (GLsizei) h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      glFrustum(-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
      glMatrixMode(GL_MODELVIEW);
    }

    I'd be really thankful if someone could give me an hint.

    TIA

  2. #2
    Super Moderator OpenGL Lord
    Join Date
    Dec 2003
    Location
    Grenoble - France
    Posts
    5,574

    Re: Strangeness in drawing lines and polygons

    Apparently you don't have depth testing.
    To make it work, you have to request a depth buffer. Example if you use glut :
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    // to do once, after you get the GL window :
    glEnable (GL_DEPTH_TEST);

    Then you have to clean the depth buffer at the same time as the color buffer :
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    If all this does not solve your problem, then you have to fix your near and far projection planes.

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2008
    Posts
    3

    Re: Strangeness in drawing lines and polygons SOLV

    You're right, that's it !

    I eventually came across it jumping ahead in the Red Book (Chapter 5 - A hidden-surface removal survival kit) but I was extrapolating on example 3.1.

    And you guessed right, I then hit the frustum depth problem too. Mine was so deep the quantization was too big for such a small object.

    Thank you ZbuffeR !

Similar Threads

  1. Replies: 3
    Last Post: 06-15-2015, 03:48 AM
  2. Blending Lines and Polygons
    By san3Designer in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 09-06-2010, 06:17 AM
  3. Faint lines on polygons
    By jerryyyyy in forum OpenGL: Basic Coding
    Replies: 12
    Last Post: 03-15-2003, 08:47 PM
  4. polygons formed by lines
    By jxu in forum OpenGL: Advanced Coding
    Replies: 10
    Last Post: 03-23-2001, 10:53 AM
  5. lines lying on polygons
    By Velayudham in forum OpenGL: Advanced Coding
    Replies: 4
    Last Post: 06-23-2000, 05:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean