Any OpenGL programmers around? Easy question.

Discussion in 'OT Technology' started by Joe_Cool, Feb 21, 2005.

  1. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,487
    Likes Received:
    614
    I'm having a problem with an assignment for class. The thing I draw last is always on top of everything else I've drawn, regardless of position. How do I make things that are positioned behind other things show up behind them?

    For example, I drew 3 rectangles: one in the XY plane, one in the YZ plane, and one in the XZ plane. They should all intersect at the origin and along each axis. But when they draw, they don't intersect at all. The first one I draw is on the bottom, and the last one is on top of all the others.

    How do I get things in the foreground to draw on top of things in the background?
     
  2. kingtoad

    kingtoad OT Supporter

    Joined:
    Sep 2, 2003
    Messages:
    55,924
    Likes Received:
    11
    Location:
    Los Angeles
    I'm not an openGL programmer... but, did you make a grid for the application you are making, like rows and columns? If so, you could specificy where you want the rectangle by assigning the x and y, and z position on the gridmap.
     
  3. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,487
    Likes Received:
    614
    Yeah. Everything is drawn in XYZ space, specified by coordinate. But things that should be closer to the camera (based on z-coordinate) are being overdrawn by things that should be farther away, but are drawn later.

    I'm sure there's a GL function I should be calling or matrix I should be loading to do what I need, but I can't find what it is.
     
  4. Jkuao

    Jkuao New Member

    Joined:
    Jan 10, 2004
    Messages:
    513
    Likes Received:
    0
    Think you have to clear the matrix stack doing glPushMatrix() glPopMatrix. Push at the start of the shape and pop when you finish. OpenGL is stack based so you probably want to reset your stack between them. If the identity matrix has been modified then it would cause the later drawn items to be translated closer.

    I only did a couple OpenGL projects but that's all that came to mind.
     
  5. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,487
    Likes Received:
    614
    Good thought, but it didn't work. :o

    Here's what I'm getting:
    [​IMG]

    The three planes should intersect and overlap in different places, but as you can see, they're just drawn on top of each other.
     
  6. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    post ur openGL code plz ;)
     
  7. Jkuao

    Jkuao New Member

    Joined:
    Jan 10, 2004
    Messages:
    513
    Likes Received:
    0
    Are you doing the drawing in a loop? It looks like it's not refreshing during the draw. A lot of the stuff I had to do was rewriting some of OpenGL's functionality but I thought OpenGL had its own Z-Buffer algorithm so it can figure out what to draw on top of each other.

    My main project doing OpenGL just involved a whole lot of part replication and animation but everything came up where it was supposed to on the first try.

    It's been a while so I get the OpenGL stuff confused w/ what we had to code at a lower level so ignore my ideas if they aren't correct.
     
  8. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,487
    Likes Received:
    614
    Here's the relevant stuff. I'm almost positive that there's a function call to fix this, but I can't remember what it is or where I saw it.

    void ChangeSize(GLsizei w, GLsizei h)
    {
    windowWidth=w;
    windowHeight=h;
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-150.0f,150.0f, -150.0f,150.0f, 150.0f, -150.0f);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    }

    void RenderScene(void)
    {
    glClear(GL_COLOR_BUFFER_BIT);

    // Draw X-Y rect
    glBegin(GL_QUADS);
    glColor3f(1.0f,1.0f,0.0f);
    glVertex3f(-100.0f,-100.0f,0.0f);
    glVertex3f(-100.0f,100.0f,0.0f);
    glVertex3f(100.0f,100.0f,0.0f);
    glVertex3f(100.0f,-100.0f,0.0f);
    // Draw X-Z rect
    glColor3f(1.0f,0.0f,1.0f);
    glVertex3f(-100.0f,0.0f,-100.0f);
    glVertex3f(-100.0f,0.0f,100.0f);
    glVertex3f(100.0f,0.0f,100.0f);
    glVertex3f(100.0f,0.0f,-100.0f);
    // Draw Y-Z rect
    glColor3f(0.0f,1.0f,1.0f);
    glVertex3f(0.0f,-100.0f,-100.0f);
    glVertex3f(0.0f,-100.0f,100.0f);
    glVertex3f(0.0f,100.0f,100.0f);
    glVertex3f(0.0f,100.0f,-100.0f);
    glEnd();

    // Draw Axes
    glBegin(GL_LINES);
    // X - Green
    glColor3f(0.0f,1.0f,0.0f);
    glVertex3f(-100.0f, 0.0f, 0.0f);
    glVertex3f(100.0f, 0.0f, 0.0f);
    // Y - Yellow
    glColor3f(1.0f,1.0f,0.0f);
    glVertex3f(0.0f, -100.0f, 0.0f);
    glVertex3f(0.0f, 100.0f, 0.0f);
    // Z - Blue
    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(0.0f, 0.0f, -100.0f);
    glVertex3f(0.0f, 0.0f, 100.0f);
    glEnd();

    glColor3f(RedVal, GrnVal, BluVal);
    glRectf(xpos, ypos, xpos+rwidth, ypos+rheight);

    glutSwapBuffers();
    }
     
  9. Bono

    Bono Guest

    glEnable(GL_DEPTH_TEST); I think is what you're talking about.

    You'd also have to change your call to
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    before each frame.
     
  10. Joe_Cool

    Joe_Cool Never trust a woman or a government. Moderator

    Joined:
    Jun 30, 2003
    Messages:
    299,487
    Likes Received:
    614
    Awesome, GL_DEPTH_TEST was it. Thanks! :bigthumb:
     

Share This Page