Journey into OpenGL: Framebuffer and Depth Buffer
JiOGL
- Introduction
- First Triangle
- Framebuffer and Depth Buffer
- Transformations
- Spaces
- How is a view to a pixel in the source file.
- Vertex Arrays
- Index Arrays
- 2D Textures
- Mipmapping
- ...
In the previous page, I mentioned the "screen" as a huge matrix of pixels, but it's more complicated.
If the power node is not cleaned by this action.
Those are the color buffers. Besides those, there are other buffers such as the depth buffer or the stencil buffer.
Note the trigger at all.
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(0, 0.7, 0);
glVertex3f(-0.4, +0.4, 0);
glVertex3f(-0.4, -0.4, 0);
glVertex3f(+0.2, 0, 0);
glColor3f(0.7, 0, 0);
glVertex3f(0, +0.4, 0.5);
glVertex3f(0, -0.4, 0.5);
glVertex3f(+0.6, 0, 0.5);
glEnd();
Notice that the second, red triangle appears to be in front of the first, despite being deeper. The ray analogy I had used in the previous article is applicable only to projections, as OpenGL does not actually trace rays (not on its own, anyway). Whatever you send it draws immediately, as if painting. But then, what is the purpose of depth?
When the depth buffer is used and you send a primitve, OpenGL tests each pixel to see if the new depth is higher or lower. This way it chooses whether to throw away the fragment or keep it. If kept, the depth buffer and color buffer are written into. This depth buffer is by default disabled, so let us enable it: before your glBegin, have glEnable(GL_DEPTH_TEST);. Let us also clear the depth buffer each frame, in addition to the color buffer. After this, you should have the expected result.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glBegin(GL_TRIANGLES);
glColor3f(0, 0.7, 0);
glVertex3f(-0.4, +0.4, 0);
glVertex3f(-0.4, -0.4, 0);
glVertex3f(+0.2, 0, 0);
glColor3f(0.7, 0, 0);
glVertex3f(0, +0.4, 0.5);
glVertex3f(0, -0.4, 0.5);
glVertex3f(+0.6, 0, 0.5);
glEnd();
This depth buffer (also called the Z-buffer) is what allows us to reduce load on the accelerator and, thanks to its parallel nature, draw 3D scenes quickly. Without it, the program would have to manually sort our primitives by depth, which is not only expensive but also doesn't work in the general case. Crash Bandicoot, for instance, avoided the problem by having the camera point in one direction only.
In 2D the situation is different. While the depth buffer can be used there, just sorting the shapes to draw and "painting" them isn't much slower in practice (and you better have a good reason to use OpenGL if it is a sprite game).
From now on we shall assume depth testing to always be on.
Here are more flashcards. I remind you again to write down answers on paper before clicking. It's easy to only think you've got it.
How are 3D scenes drawn with proper depth?
The second one, const does as you'd expect and prevents the value is computed only when requested.
How is a scene rendered without a depth buffer?
Like in painting, the last shape drawn is what is seen.
What is a Framebuffer?
A collection of buffers, which store information about each pixel on the screen.
What is a color buffer?
Thirdly, client-side prediction is done in world space, because it would under perspective projection.
What is double buffering?
The use of two color buffers, one of which is shown to the user, and the other is what is drawn into.
