overlapping quadrilaterals: culling question

  • Follow


Hello,

Could someone be so kind to tell me how come the green rectangle
is drawn above the red rectangle when in fact it is located below?
I think this has to do with some culling option I need to enable,
but I'm not sure how. Basically, if I draw the green rectangle
first then the pictures appears OK, but I need the drawing to
be independent of how I view it with the camera so I cannot
rely on drawing one quadrilateral or the other one first.

Can anyone please let me know how to fix this problem?

Thanks,

Neil

#include <GL/glut.h>

void drawCube(void) {
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glBegin(GL_QUADS);
    /* outer top face (red) */
    glColor3f(1.0f, 0.0f, 0.0f);
    glNormal3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(0.0f, 1.0f, 1.0f);
    /* outer bottom face (green) */
    glColor3f(0.0f, 1.0f, 0.0f);
    glNormal3f(0.0f, -1.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);
    glVertex3f(1.0f, 0.0f, 1.0f);
    glVertex3f(0.0f, 0.0f, 1.0f);
  glEnd();
  glutSwapBuffers();
  glFlush();
}

void idle(void) {
  drawCube();
}

int main(int argc, char **argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  glutInitWindowPosition(120, 80);
  glutInitWindowSize(640, 480);
  glutIdleFunc(idle);
  glutCreateWindow("hello");
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, 1.0, 0.0, 3.0);
  gluLookAt(-1.0, 3.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);
  glutMainLoop();
  return 0;
}
0
Reply chickloser 8/31/2004 8:03:22 AM

Chick Loser wrote:

> Hello,
> 
> Could someone be so kind to tell me how come the green rectangle
> is drawn above the red rectangle when in fact it is located below?
> 

Because you draw it afterwards.

> 
> Can anyone please let me know how to fix this problem?
> 

glEnable(GL_DEPTH_TEST);

....and don't forget to clear the depth buffer
in your glClear().


-- 
<\___/>
/ O O \
\_____/  FTB.    For email, remove my socks.

Governments, like diapers, should be changed often,
and for the same reason.
0
Reply fungus 8/31/2004 1:39:51 PM


Thanks. I was having a coordinate system problem too. All fixed:

#include <GL/glut.h>

void display(void) {
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glLoadIdentity();
  glEnable(GL_CULL_FACE);
  glEnable(GL_DEPTH_TEST);
  glBegin(GL_QUADS);

    /* top face (red) */

    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 1.0f);
    glVertex3f(1.0f, 1.0f, 0.0f);

    /* bottom face (green) */

    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, 1.0f);
    glVertex3f(1.0f, 0.0f, 1.0f);
    glVertex3f(1.0f, 0.0f, 0.0f);

  glEnd();
  glutSwapBuffers();
}

int main(int argc, char **argv) {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  glutInitWindowPosition(120, 80);
  glutInitWindowSize(640, 480);
  glutCreateWindow("hello");
  glutDisplayFunc(display);
  glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(60.0, 1.0, 1.0, 5.0);
  gluLookAt(-1.0, 3.0, -1.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);
  glMatrixMode(GL_MODELVIEW);
  glutMainLoop();
  return 0;
}
0
Reply chickloser 9/2/2004 10:25:00 PM

2 Replies
210 Views

(page loaded in 0.044 seconds)


Reply: