As you can see from the images below, I have two triangles side by side (viewed from front). But when I view it from the right, the yellow triangle shows as if it is in front of the green triangle where it should not be visible because it is behind the green one (see right view), why is this happening? Viewing it from the left is fine (see left view). Thanks.
FrontView:

Right View (why is the yellow triangle is still visible?):

Left view (viewing from here is fine):

This is my render method. Even though I don’t use glColor4f and do the coloring in a modern way, I still have the same result. I have also set glEnable(GL_DEPTH); and
glDepthFunc(GL_LESS);
void display(){
glClear(GL_COLOR_BUFFER_BIT);
control(0.2, 0.2, mi);
view = updateCam();
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);
model = glm::rotate(glm::mat4(1.0), 45, glm::vec3(0, 1, 0));
glColor4f(0, 1, 0, 1);
glDrawArrays(GL_TRIANGLES, 0, 3);
resetMatrix();
model = glm::translate(glm::mat4(1.0), glm::vec3(5, 0, 0));
model = glm::rotate(model, 45, glm::vec3(0, 1, 0));
glColor4f(1, 1, 0, 1);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
resetMatrix();
SDL_GL_SwapBuffers();
}
You’re only clearing your color buffer. The depth buffer still contains the depth values from previous frames, so your Z-test doesn’t work as intended.
Clear them both: