Sandundhammika's Blog











{April 12, 2010}   OpenGL depth buffer

welll what is the opengl depth buffer

Opengl is supporting the drawing 3D objects. So there is something called depth in the 3rd dimension.

Therefore OpenGL uses this depth buffer to represent the depth information about the each pixel.For

a example consider the bellow code and it’s drawing two triangles with different depths. One is at depth

1.0(red one)  and one is at depth 0.0(green one).So the blue one is on the red one.


glBegin(GL_TRIANGLES);
// set hte color
glColor3f(1.0,0.0,0.0);

// the triangle with the depth 1
glVertex3f(0.6,0.6,1.0);
glVertex3f(0.2,-0.2,1.0);
glVertex3f(-0.8,0.2,1);

// set the second color
glColor3f(0.0,1.0,0.0);
// traingle with the depth 0
glVertex3f(0.2,1.0,0.0);
glVertex3f(0.8,0.0,0.0);
glVertex3f(-0.8,0.0,0.0);
glEnd();

So when OpenGL is drawing each triangle it will compare the current depth buffer value and update if it is occlude

with the new value.So simply it contains the depth value of the pixel from the triangle that is visible.Simply that’s

how it’s working.

To enable this depth buffer you should enable two properties  form the windowing system, and the OpenGL.

If you are using GLUT(GL utility toolkit) then use this function to enable that.


glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA);

and to enable the property on OpenGL you should invoke the bellow line.


glEnable(GL_DEPTH_TEST);

so after that you can use the depth buffer.

here is a simple program that just draw two triangles and demonstrates the depth buffer.


#include <GL/glut.h>
#include <GL/gl.h>
#include <stdio.h>
#include <stdlib.h>


void init()
{
/* performs the init routines for the
* GLUT window
*/
// set the clear color
glClearColor(0.0,0.0,0.0,1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
}

void draw_loop()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
// set hte color
glColor3f(1.0,0.0,0.0);

// the triangle with the depth 1
glVertex3f(0.6,0.6,1.0);
glVertex3f(0.2,-0.2,1.0);
glVertex3f(-0.8,0.2,1);

// set the second color
glColor3f(0.0,1.0,0.0);
// traingle with the depth 0
glVertex3f(0.2,1.0,0.0);
glVertex3f(0.8,0.0,0.0);
glVertex3f(-0.8,0.0,0.0);
glEnd();
}

void keyboard_loop(unsigned char key, int x,int y)
{
if(key=='q')
{
printf("Exiting.......\n");
exit(0);
}
}

int main( int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGBA);
glutInitWindowSize(300,300);
glutInitWindowPosition(100,100);
glutCreateWindow("gl depth buffer testing");
glutDisplayFunc(draw_loop);
glutKeyboardFunc(keyboard_loop);
glutMainLoop();

return 0;
}

To compile this under linux just save the source code as main.cc and invoke these commands on the bash shell.


$gcc -c main.cc

$gcc -o main main.o -lglut

And heres how it works.

Do IT YOURSELF.

and turn off the depth buffer and see what will happens.

–HAPPY CODING–

Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

et cetera
%d bloggers like this: