0

I've been struggling with OpenGL's Z-Buffer, i can't get it to work. This is the code (i narrowed it down to the minimum necessary to show the problem):

#include <stdio.h>
#include <stdlib.h>
#include <glad\glad.h>
#include <SFML\Graphics.hpp>

#define WIDTH 800
#define HEIGHT 600

int main(void) {
  sf::ContextSettings settings;
  settings.antialiasingLevel = 16;
  settings.majorVersion = 4;
  settings.minorVersion = 6;
  sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Test", sf::Style::Close, settings);

  if (!gladLoadGL()) {
    printf("COULD NOT INITALIZE OPENGL CONTEXT\n");
  }

  window.setActive(true);
  window.setVerticalSyncEnabled(true);

  glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

  glEnable(GL_DEPTH_TEST);

  glDepthFunc(GL_LESS);

  while (window.isOpen()) {
    sf::Event event;
    while (window.pollEvent(event)) {
      if (event.type == sf::Event::Closed) {
        window.close();
      }
    }

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_TRIANGLES);
      glColor3f(1.0f, 0.0f, 0.0f); // red
      glVertex3f(-1.0f, -1.0f, 0.25f); // first triangle
      glVertex3f( 1.0f, -1.0f, 0.25f);
      glVertex3f( 0.0f,  1.0f, 0.25f);

      glColor3f(0.0f, 0.0f, 1.0f); // blue
      glVertex3f(-1.0f,  1.0f, 0.5f); // second triangle
      glVertex3f( 1.0f,  1.0f, 0.5f);
      glVertex3f( 0.0f, -1.0f, 0.5f);
    glEnd();

    window.display();
  }

  return(0);
}

Output: enter image description here

The blue triangle is on top of the red one cause i drawed it for second, even though it should stay behind. I first noticed this behaviour on my main OpenGL project where i would import 3D models and i would have triangles being rendered in front of others, suggesting that the Z-Buffer was not doing its thing. I tried many solutions, such as enabling GL_CULL_FACE, playing around with the near plane from the projection matrix (on the 3d project), but nothing seems to work. Every fix i found online that works for almost everyone doesn't seem to work for me, so i'm kind of desperate... If anyone knows the issue let me know!

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Just a suggestion but it looks like you're using 10+ year deprecated OpenGL. [Consider using modern OpenGL](https://learnopengl.com/) – gman Mar 05 '19 at 18:03
  • i'm using OpenGL 4.6, which came out in 2017. Or are you refering to the functions i use? – joshua micheletti Mar 05 '19 at 18:12
  • You are using OpenGL 1.X functionality - `glBegin/glEnd` which are deprecated. Correct way would be to use modern 3.3+ API. The 4.6 version is just upper bound on the API. Lower bound can be specified by creating core context instead of compatible one. – Quimby Mar 05 '19 at 18:22
  • i used glBegin and glEnd just to make this simple program to show the issue, i had the problem on the other program as well where i use moder OpenGL functions – joshua micheletti Mar 05 '19 at 18:29

3 Answers3

1

As commented by @Quimby, the solution can be found here

The issue was that SFML needs you to set a depth buffer through the window settings.

0

It's been a while, but I think it's not the Z buffer issues. OpenGL uses right handed coordinate system and positive Z is away from the screen. So your blue triangle having larger z values would means it should be in front. Smaller z values should be farther away right? So this is correct behavior I believe.

noobius
  • 1,529
  • 7
  • 14
0

This seems like correct output to me. OpenGL has flipped Z axis compared to DirectX. So +1 is front, -1 is back so the "camera" looks into the negative Z direction.

Quimby
  • 17,735
  • 4
  • 35
  • 55
  • i tested flipping the z values as well, thinking i flipped the order, but i get the same output. So if i set the z values of the first triangle to 0.5 and the z values of the second triangle to 0.25, i still get the second triangle drawing on top of the first triangle – joshua micheletti Mar 05 '19 at 18:10
  • Well, that's weird. What happens if some vertices are behind and some in front? Do they intersect each other? I don't have SFML so unfortunately cannot reproduce the issue. – Quimby Mar 05 '19 at 18:16
  • 1
    @joshuamicheletti Relevant? https://stackoverflow.com/a/37858190 The answer by Jobin – Quimby Mar 05 '19 at 18:20
  • i tried setting the z value of the top vertex of the first triangle (third vertex of the red triangle) to a value above the z values of the second triangle but nothing changes, i tried playing around with the z values of the intersecting vertices of each triangle mix matching as many tests as i could but nothing happens, openGL just ignores the Z value and renders the second triangle on top cause it was called for second in the code – joshua micheletti Mar 05 '19 at 18:21
  • IT WORKED, THANK YOU SO MUCH!!! The issue was that i wasn't setting up the sfml window to have a depth buffer. thank you very much!!!! – joshua micheletti Mar 05 '19 at 18:25
  • @joshuamicheletti You're welcome, I know very well how frustrating OpenGL bugs can be :D – Quimby Mar 05 '19 at 18:28