Depth Buffering
Depth buffering in OpenGL will sort the display of passed vertices by a single floating point value. Used primarily in 3D rendering, depth buffering can also be used in 2D rendering for sorting of sprites in isometric games.
For enabling SFML depth buffering see (SFML Depth Buffering)[../sfml/sfml2_depth_buffering].
Set Up
All depth testing requires at minimum the following OpenGL enabled options and functions:
#!c
glEnable(GL_DEPTH_TEST); // Turns on Depth Testing.
glEnable(GL_ALPHA_TEST); // Turns on Alpha Blending.
glDepthMask(GL_TRUE); // Specifies whether the depth buffer is enabled for writing.
glDepthFunc(GL_LEQUAL); // Specifies the depth comparison function using OpenGL symbolic constants.
glDepthRange(1.0f, 0.0f); // The range of values, first argument is the top, second is the bottom.
glAlphaFunc(GL_GREATER, 0.0f); // The function to allow Alpha Blending.
Generally these calls can be made one time after the window is created and depth testing will work for any draw loops. Such is not the case for SFML 2.0.
Draw Loop
With depth buffering enabled and your functions defined, some calls must be applied each rendering loop.
#!c
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Specify the red, green, blue, and alpha values used when the color buffers are cleared.
glClearDepth(1.0f); // Specifies the depth value used when the depth buffer is cleared.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clears Depth and Color Buffers.
Example of Simple Quad
Here is an example of a rendering loop using a basic vertex for rendering a single quad using normal OpenGL calls.
#!c
// Clear the buffers
glClearColor(0.0f, 0.1f, 0.0f, 0.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Draw the quad
glBegin(GL_QUADS);
// Set the color to blue
glColor3ub(0, 0, 255);
//Draw our four points, clockwise. (rectangle a few pixels below top, covers top left AND top right quadrants)
float depthValue = .555;
glVertex3f(1.0, 0.9, depthValue);
glVertex3f(-1.0, 0.9, depthValue);
glVertex3f(-1.0, -0.1, depthValue);
glVertex3f(1.0, -0.1, depthValue);
glEnd();
Example of Vertex Array
#!c
// Clear the buffers
glClearColor(0.0f, 0.1f, 0.0f, 0.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Begin the Vertex Array
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
// Define 2 rectangles with x, y, z coordinates for each point
GLfloat vertices[24] = {
-.1, -.1, .5,
.1, -.1, .5,
.1, .1, .5,
-.1, . 1, .5,
-.05, -.05, .6,
.15, -.05, .6,
.15, .15, .6,
-.05, .15, .6,
};
// Define the 2 rectangles, first one red, second green
GLfloat colors[24] = {
1, 0, 0,
1, 0, 0,
1, 0, 0,
1, 0, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
};
// Assign the vertices
glVertexPointer(
3, // 3 components per vertex (x,y,z)
GL_FLOAT,
0,
vertices);
// Assign the colors
glColorPointer(
3, // 3 components per vertex (r,g,b)
GL_FLOAT,
0,
colors);
// Draw to OpenGL
glDrawArrays(GL_QUADS, 0, 8);
// End Vertex Array
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);