Suppose I want to render an OpenGL scene in two viewports. The first viewport renders a colour image, and the second viewport renders a depth image. Each has its own vertex shader and fragment shader, and each has its own shader program. The shaders are similar, the only difference being that for the colour image, the vertex colour is defined in the vertex shader based on the vertex normal, whereas for the depth image, the vertex colour is defined in the fragment shader based on the vertex depth. To perform the rendering, I glUseProgram() the colour shader program, and draw the colour scene, then glUseProgram() the depth shader program, and draw the depth scene.
What I am unsure about, is how glClear() works when there are two shader programs. Do I need to call it twice per rendering loop -- once before rendering the colour image, and once before rendering the depth image? It seems to work when I just call it once, but this seems strange to me. I would have thought that each shader program writes to the colour and depth buffer once each. Therefore, when rendering the depth image, it would have the buffers left over from rendering the colour image...but this does not seem to be the case. Can somebody explain this to me please? Thanks!