Hello everybody. I’m pretty new to OpenGL and my question is definitely a simple one. Anyway, I’ve already spent about two days trying to find the answer. So I will be grateful, if someone could help me…
I’ve created a simple scene and placed several objects there. Each object is wrapped in a C+±class (say class Foo), which contains all the data needed: the mesh of the object (an array of vertices, an array of indices, an array of normals, …), the associated material, the local coordinate system of the object and so on. The Foo-class has a method, called Draw, which sets all the data to the shader and calls glDrawArrays:
glDrawElements (_mesh->GetRenderMode (), _mesh->GetIndexArray ().size (), GL_UNSIGNED_INT, 0);
Next, I’ve registered the following function as a callback to call it every frame (as a render and idle function):
void GLWrapper::RenderCallback () const
{
glClear (GL_COLOR_BUFFER_BIT);
if (_scene != nullptr)
_scene->Draw ();
glutSwapBuffers ();
}
Here I just call the Draw-method of the Scene class, which simply calls the Draw-method of Foo class (described above) for every object in the scene.
And here I encounter my problem: how to switch on the depth test? It seems, that because each object is drawn separately, something is wrong conceptually with my approach. On the other hand, all the objects should be drawn to the same framebuffer, so they should share the same DepthBuffer. Keeping that in my mind, I thought, that simply swithing the depth test on would work correctly. So, I placed the following calls to my code:
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
glClearDepth (1.0f);
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LEQUAL);
glDepthRange (0.0f, 1.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
…and nothing changed. Nothing happens at all.
So the question is: how to implement the depth test properly in the given conditions (described above)?