DEPTH BUFFER QUESTION

I AM TRYING TO MANIPULATE THE DEPTH BUFFER TO ALLOW FOR AN OBJECT TO HIDE BEHIND CLOSER POLYGONS. CAN SOMEONE PLEASE TEACH ME HOW TO DO THIS PROPERLY. THE FOLLOWING DOES NOT WORK RIGHT. I DON’T KNOW IF I AM SUPPOSED TO USE glClear(GL_DEPTH_BUFFER) ONCE OR MULTIPLE TIMES. AND IN WHAT ORDER SHOULD I BE RENDERING?? FRONT TO BACK, OR BACK TO FRONT?

#include <OpenGLSB.h>
#include <GLTools.h>
#include “glbmp.h”

GLdouble a = 0.0;
GLdouble s = 0.0;
int stance = 0;
int cycleone = 1;
int direction = 1;
int time = 0;
int switchstance = 1;
GLfloat posx = 0.450;
GLfloat posy = 0.0;

void init(void)
{

glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
//glEnable(GL_SCISSOR_TEST);
glViewport(0, 0, 1024, 1024);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

//glClear(GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(85.0, 1.0, -5.0, 0.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(2.0 , 4.00 , -5.0, 2.0 , 4.00 , 0.0, 0.0, 10.0, 0.0);

}

void RenderScene(void)
{

if (cycleone == 1)

{

GLuint texture1 = 0;

glbmp_t bitmap1;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glbmp_LoadBitmap(“grass.bmp”, 1, &bitmap1);

//generate and bind the OpenGL texture
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);

//copy data from bitmap into texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmap1.width, bitmap1.height,
0, GL_RGB, GL_UNSIGNED_BYTE, bitmap1.rgb_data);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glShadeModel(GL_SMOOTH);

glFrontFace(GL_CCW);

glBegin(GL_QUAD_STRIP);

glTexCoord2f(30.0, -10.0);
glVertex3f(30.0f, -10.0f, 0.0f);

glTexCoord2f(30.0, 30.0);
glVertex3f(30.0f, 30.0f, 0.0f);

glTexCoord2f(-20.0, -10.0);
glVertex3f(-20.0f, -10.0f, 0.0f);

glTexCoord2f(-20.0, 30.0);
glVertex3f(-20.0f, 30.0f, 0.0f);

glEnd();

GLuint texture = 0;

glbmp_t bitmap;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glbmp_LoadBitmap(“wood.bmp”, 1, &bitmap);

//generate and bind the OpenGL texture
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);

//copy data from bitmap into texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmap.width, bitmap.height,
0, GL_RGB, GL_UNSIGNED_BYTE, bitmap.rgb_data);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glShadeModel(GL_SMOOTH);

glFrontFace(GL_CCW);

//glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUAD_STRIP);

glTexCoord2f(6.0, 2.0);
glVertex3f(6.0f, 2.0f, 0.1f);

glTexCoord2f(6.0, 4.0);
glVertex3f(6.0f, 4.0f, 0.1f);

glTexCoord2f(2.0, 2.0);
glVertex3f(2.0f, 2.0f, 0.1f);

glTexCoord2f(2.0, 4.0);
glVertex3f(2.0f, 4.0f, 0.1f);

glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

glBegin(GL_QUAD_STRIP);

glTexCoord2f(6.0, 6.0);
glVertex3f(6.0f, 6.0f, 0.1f);

glTexCoord2f(6.0, 8.0);
glVertex3f(6.0f, 8.0f, 0.1f);

glTexCoord2f(2.0, 6.0);
glVertex3f(2.0f, 6.0f, 0.1f);

glTexCoord2f(2.0, 8.0);
glVertex3f(2.0f, 8.0f, 0.1f);

glEnd();

GLuint texture2 = 0;

glbmp_t bitmap2;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glbmp_LoadBitmap(“siding.bmp”, 1, &bitmap2);

//generate and bind the OpenGL texture
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);

//copy data from bitmap into texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmap2.width, bitmap2.height,
0, GL_RGB, GL_UNSIGNED_BYTE, bitmap2.rgb_data);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

glShadeModel(GL_SMOOTH);

glFrontFace(GL_CCW);

glBegin(GL_QUAD_STRIP);

glTexCoord2f(6.0, 4.0);
glVertex3f(6.0f, 4.0f, 0.1f);

glTexCoord2f(6.0, 8.0);
glVertex3f(6.0f, 8.0f, 0.1f);

glTexCoord2f(5.9, 4.0);
glVertex3f(5.9f, 4.0f, 0.1f);

glTexCoord2f(5.9, 8.0);
glVertex3f(5.9f, 8.0f, 0.1f);

glEnd();

glBegin(GL_QUAD_STRIP);

glTexCoord2f(2.1, 4.0);
glVertex3f(2.1f, 4.0f, 0.1f);

glTexCoord2f(2.1, 8.0);
glVertex3f(2.1f, 8.0f, 0.1f);

glTexCoord2f(2.0, 4.0);
glVertex3f(2.0f, 4.0f, 0.1f);

glTexCoord2f(2.0, 8.0);
glVertex3f(2.0f, 8.0f, 0.1f);

glEnd();

glBegin(GL_QUAD_STRIP);

glTexCoord2f(2.0, 7.9);
glVertex3f(2.0f, 7.9f, 0.1f);

glTexCoord2f(2.0, 8.0);
glVertex3f(2.0f, 8.0f, 0.1f);

glTexCoord2f(6.0, 7.9);
glVertex3f(6.0f, 7.9f, 0.1f);

glTexCoord2f(6.0, 8.0);
glVertex3f(6.0f, 8.0f, 0.1f);

glEnd();

glBegin(GL_QUAD_STRIP);

glTexCoord2f(2.0, 3.9);
glVertex3f(2.0f, 3.9f, 0.1f);

glTexCoord2f(2.0, 4.0);
glVertex3f(2.0f, 4.0f, 0.1f);

glTexCoord2f(6.0, 3.9);
glVertex3f(6.0f, 3.9f, 0.1f);

glTexCoord2f(6.0, 4.0);
glVertex3f(6.0f, 4.0f, 0.1f);

glEnd();

glDeleteTextures(1, &texture);
glbmp_FreeBitmap(&bitmap);

glDeleteTextures(1, &texture1);
glbmp_FreeBitmap(&bitmap1);

glDeleteTextures(1, &texture2);
glbmp_FreeBitmap(&bitmap2);

}

//CHARACHTER MOVING LEFT ***********************************

if (direction == 1)
{

glPushMatrix();

;

if (switchstance == 0)
{
glTranslatef(s , a , 0);
posx = posx + s;
s = s + .0360;
}

if (switchstance == 1)
{
glTranslatef(s - .001, a, 0);
}

glPushAttrib(GL_CURRENT_BIT);
glPolygonMode(GL_FRONT, GL_FILL);

glScalef(10.0f, 10.0f, 0.0f);

//HEAD OF MAIN CHARACHTER
glColor3f(1.0, .92, .92);

glBegin(GL_POLYGON);
glVertex3f(.47f, .48f , 0.000f);
glVertex3f(.47f, .50f , 0.000f);
glVertex3f(.485f, .50f , 0.000f);
glVertex3f(.485f, .49f , 0.000f);
glVertex3f(.489f, .49f , 0.000f);
glVertex3f(.489f, .488f , 0.0000f);
glVertex3f(.485f, .488f , 0.0000f);
glVertex3f(.485f, .478f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//EYE OF MAIN CHARACHTER
glColor3f(0.0, 0.0, 1.0);

glBegin(GL_POLYGON);
glVertex3f(.480f , .490f , 0.0000f);
glVertex3f(.485f , .499f , 0.0000f);
glVertex3f(.485f , .490f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//MOUTH OF MAIN CHARACHTER
glColor3f(0.65, 0.0, 0.0);

glBegin(GL_POLYGON);
glVertex3f(.482f , .480f , 0.0000f);
glVertex3f(.482f , .482f , 0.0000f);
glVertex3f(.485f , .482f , 0.0000f);
glVertex3f(.485f , .480f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//HAIR OF MAIN CHARACHTER
glColor3f(0.65, 0.50, 0.0);

glBegin(GL_POLYGON);
glVertex3f(.469f , .497f , 0.0000f);
glVertex3f(.472f , .501f , 0.0000f);
glVertex3f(.485f , .501f , 0.0000f);
glVertex3f(.485f , .497f , 0.0000f);
glVertex3f(.47f , .492f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//EAR OF MAIN CHARACHTER
glColor3f(1.00, 0.75, 0.75);

glBegin(GL_POLYGON);
glVertex3f(.473f , .487f , 0.0000f);
glVertex3f(.471f , .492f , 0.0000f);
glVertex3f(.478f , .487f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//BODY OF MAIN CHARACHTER
glColor3f(.40, 0.40, 0.90);

glBegin(GL_POLYGON);
glVertex3f(.470f , .463f , 0.0000f);
glVertex3f(.470f , .479f , 0.0000f);
glVertex3f(.485f , .479f , 0.0000f);
glVertex3f(.485f , .463f , 0.0000f);
glEnd();

//LEGS OF MAIN CHARACHTER

if (switchstance == 1 || time == 3 || time == 4 || time == 5 || time == 6 || time == 7)
{

glClear(GL_DEPTH_BUFFER_BIT);

glColor3f(.50, 0.50, 0.50);

glBegin(GL_POLYGON);
glVertex3f(.472f , .456f , 0.0000f);
glVertex3f(.472f , .463f , 0.0000f);
glVertex3f(.482f , .463f , 0.0000f);
glVertex3f(.482f , .456f , 0.0000f);
glEnd();
}

if(switchstance != 1 && time != 3 && time != 4 && time != 5 && time != 6 && time != 7)
{

glClear(GL_DEPTH_BUFFER_BIT);

glColor3f(.50, 0.50, 0.50);
glBegin(GL_POLYGON);
glVertex3f(.465f , .459f , 0.0000f);
glVertex3f(.470f , .463f , 0.0000f);
glVertex3f(.477f , .463f , 0.0000f);
glVertex3f(.470f , .459f, 0.0000f);
glEnd();

glBegin(GL_POLYGON);
glVertex3f(.490f, .459f, 0.000f);
glVertex3f(.485f, .463f, 0.000f);
glVertex3f(.477f, .463f, 0.000f);
glVertex3f(.485f, .459f, 0.000f);
glEnd();
}

//ARMS OF MAN CHARACHTER

if(switchstance != 1 && time != 4 && time != 5 && time != 6 && time != 7 && time != 8)
{

glClear(GL_DEPTH_BUFFER_BIT);

glColor3f(1.00, 0.92, 0.92);
glBegin(GL_POLYGON);
glVertex3f(.480f , .466f , 0.0000f);
glVertex3f(.480f , .470f , 0.0000f);
glVertex3f(.486f , .470f , 0.0000f);
glVertex3f(.486f , .466f , 0.0000f);
glEnd();
}

if (switchstance == 1 || time == 4 || time == 5 || time == 6 || time == 7 || time == 8)
{

glColor3f(1.00, 0.92, 0.92);
glBegin(GL_POLYGON);
glVertex3f(.475f , .464f , 0.0000f);
glVertex3f(.475f , .468f , 0.0000f);
glVertex3f(.480f , .468f , 0.0000f);
glVertex3f(.480f , .464f , 0.0000f);
glEnd();

}

glPopAttrib();

glPopMatrix();

}

//CHARACHTER MOVING RIGHT ***********************************

if (direction == 0)

{

glPushAttrib(GL_CURRENT_BIT);

glPolygonMode(GL_FRONT, GL_FILL);

glPushMatrix();

if (switchstance == 0)
{
glTranslatef(s, a , 0);
posx = posx - s;
s = s - .0360;
}

if (switchstance == 1)
{
glTranslatef(s - .001, a , 0);

}

glScalef(10.0f, 10.0f, 0.0f);

glClear(GL_DEPTH_BUFFER_BIT);

//HEAD OF MAIN CHARACHTER
glColor3f(1.0, .92, .92);
glBegin(GL_POLYGON);
glVertex3f(.485f, .48f , 0.000f);
glVertex3f(.485f, .50f , 0.000f);
glVertex3f(.470f, .50f , 0.000f);
glVertex3f(.470f, .49f , 0.000f);
glVertex3f(.466f, .49f , 0.000f);
glVertex3f(.466f, .488f , 0.0000f);
glVertex3f(.470f, .488f , 0.0000f);
glVertex3f(.470f, .478f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//EYE OF MAIN CHARACHTER
glColor3f(0.0, 0.0, 1.0);

glBegin(GL_POLYGON);
glVertex3f(.475f , .490f , 0.0000f);
glVertex3f(.470f , .499f , 0.0000f);
glVertex3f(.470f , .490f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//MOUTH OF MAIN CHARACHTER
glColor3f(0.65, 0.0, 0.0);

glBegin(GL_POLYGON);
glVertex3f(.473f , .480f , 0.0000f);
glVertex3f(.473f , .482f , 0.0000f);
glVertex3f(.470f , .482f , 0.0000f);
glVertex3f(.470f , .480f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//HAIR OF MAIN CHARACHTER
glColor3f(0.65, 0.50, 0.0);

glBegin(GL_POLYGON);
glVertex3f(.486f , .497f , 0.0000f);
glVertex3f(.483f , .501f , 0.0000f);
glVertex3f(.470f , .501f , 0.0000f);
glVertex3f(.470f , .497f , 0.0000f);
glVertex3f(.485f , .492f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//EAR OF MAIN CHARACHTER
glColor3f(1.00, 0.75, 0.75);

glBegin(GL_POLYGON);
glVertex3f(.482f , .487f , 0.0000f);
glVertex3f(.484f , .492f , 0.0000f);
glVertex3f(.477f , .487f , 0.0000f);
glEnd();

glClear(GL_DEPTH_BUFFER_BIT);

//BODY OF MAIN CHARACHTER

glColor3f(.40, 0.40, 0.90);

glBegin(GL_POLYGON);
glVertex3f(.485f , .463f , 0.0000f);
glVertex3f(.485f , .479f , 0.0000f);
glVertex3f(.470f , .479f , 0.0000f);
glVertex3f(.470f , .463f , 0.0000f);
glEnd();

//LEGS OF MAIN CHARACHTER

if (switchstance == 1 || time == 3 || time == 4 || time == 5 || time == 6 || time == 7)
{

glClear(GL_DEPTH_BUFFER_BIT);

glColor3f(.50, 0.50, 0.50);

glBegin(GL_POLYGON);
glVertex3f(.484f , .456f , 0.0000f);
glVertex3f(.484f , .463f , 0.0000f);
glVertex3f(.471f , .463f , 0.0000f);
glVertex3f(.471f , .456f , 0.0000f);
glEnd();
}

if(switchstance != 1 && time != 3 && time != 4 && time != 5 && time != 6 && time != 7)
{

glClear(GL_DEPTH_BUFFER_BIT);

glColor3f(.50, 0.50, 0.50);
glBegin(GL_POLYGON);
glVertex3f(.490f , .459f , 0.0000f);
glVertex3f(.485f , .463f , 0.0000f);
glVertex3f(.477f , .463f , 0.0000f);
glVertex3f(.485f , .459f, 0.0000f);
glEnd();

glColor3f(.50, 0.50, 0.50);
glBegin(GL_POLYGON);
glVertex3f(.465f , .459f , 0.0000f);
glVertex3f(.470f , .463f , 0.0000f);
glVertex3f(.477f , .463f , 0.0000f);
glVertex3f(.470f , .459f, 0.0000f);
glEnd();

}

//ARMS OF MAN CHARACHTER

if(switchstance != 1 && time != 4 && time != 5 && time != 6 && time != 7 && time != 8)
{

glClear(GL_DEPTH_BUFFER_BIT);

glColor3f(1.00, 0.92, 0.92);
glBegin(GL_POLYGON);
glVertex3f(.467f , .466f , 0.0000f);
glVertex3f(.467f , .470f , 0.0000f);
glVertex3f(.471f , .470f , 0.0000f);
glVertex3f(.471f , .466f , 0.0000f);
glEnd();
}

if (switchstance == 1 || time == 4 || time == 5 || time == 6 || time == 7 || time == 8)
{

glClear(GL_DEPTH_BUFFER_BIT);

glColor3f(1.00, 0.92, 0.92);
glBegin(GL_POLYGON);
glVertex3f(.475f , .464f , 0.0000f);
glVertex3f(.475f , .468f , 0.0000f);
glVertex3f(.480f , .468f , 0.0000f);
glVertex3f(.480f , .464f , 0.0000f);
glEnd();

}

glPopAttrib();
glPopMatrix();

}

glutSwapBuffers();
}

void TimerFunction(int value)
{

glutTimerFunc(1, TimerFunction, 1);

time = time + 1;

if (time > 10)
{
time = 0;
switchstance = 1;
}

glutPostRedisplay();
}

void SpecialKeys(int key, int x, int y)

{

if (key == GLUT_KEY_RIGHT)
{
//s = 0;
s = s - .0045;
direction = 0;
switchstance = 0;
time = 0;
posx = posx - .045;
}

if (key == GLUT_KEY_LEFT)
{
//s = 0;
s = s + .0045;
direction = 1;
switchstance = 0;
time = 0;
posx = posx + .0045;
}

if (key == GLUT_KEY_UP)
{
a = a + .045;
posy = posy + .045;
}

if (key == GLUT_KEY_DOWN)
{
a = a - .045;
posy = posy - .045;
}

glutPostRedisplay();

}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1024, 1024);
glutCreateWindow(“ARENIUS: THE SACRED STONE”);
glutDisplayFunc(RenderScene);
glutSpecialFunc(SpecialKeys);
glutTimerFunc(1, TimerFunction, 1);
init();

glutMainLoop();

return 0;
}

  1. No all caps please. It is considered as if you were shouting.
    http://www.catb.org/~esr/faqs/smart-questions.html#writewell

  2. Use the [ code ] tags correctly around you code, so that code is easier to read. Please edit your existing post.

  3. from you description you seem to need the normal depth buffer behaviour. No need to ‘manipulate’ it.
    Something like this should work :

// render scene :
  // clear both depth buffer and color buffer only once per frame
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  // here draw all your 3D stuff, in any order.
  // near objects will end up in front of background objects
  ...
  // now show finished frame on the front buffer
  glutSwapBuffers();

The documentation says znear can’t be 0.0 or negative

“gluPerspective(85.0, 1.0, -5.0, 0.0);”

so that’s likely your problem. Try
gluPerspective(85.0, 1.0, 0.1, 1000.0);

and yes, you just call
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
or
glClear(GL_DEPTH_BUFFER_BIT);

before rendering your scene everytime