Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Enabling the depth buffer  (Read 10727 times)

0 Members and 1 Guest are viewing this topic.

Guni

  • Newbie
  • *
  • Posts: 20
    • View Profile
Enabling the depth buffer
« on: March 10, 2012, 08:15:41 am »
I'm using SFML 2.0, on Windows 7 Ultimate.

I want to create an OpenGL context that enables the depth buffer. Here is how I'm currently going about it:
Code: [Select]
sf::ContextSettings context(24, 8, 2, 3, 3);
sf::Window window(sf::VideoMode(500, 500, 32), "SFML Window", 7U, context);
{ //Loads OpenGL functions
glewExperimental=TRUE;

GLenum err = glewInit();
if (GLEW_OK != err)
{
 /* Problem: glewInit failed, something is seriously wrong. */
 fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
}
sf::ContextSettings windowSettings = window.GetSettings();
std::cout
<< "windowSettings.DepthBits: " << windowSettings.DepthBits << "\n"
<<  "windowSettings.StencilBits: " << windowSettings.StencilBits << "\n"
<< "windowSettings.AntialiasingLevel: " << windowSettings.AntialiasingLevel << "\n"
<< "windowSettings.MajorVersion: " << windowSettings.MajorVersion << "\n"
<< "windowSettings.MinorVersion: " << windowSettings.MinorVersion << "\n";
window.SetActive();


The output is what I expect:
Code: [Select]
Status: Using GLEW 1.7.0
windowSettings.DepthBits: 24
windowSettings.StencilBits: 8
windowSettings.AntialiasingLevel: 2
windowSettings.MajorVersion: 3
windowSettings.MinorVersion: 3


I thought creating a window with an sf::ContextSettings that has some number of bits for DepthBits automatically enables the depth buffer. But it seems writing/reading from the depth buffer does not work. When I wrap my exact same OpenGL code around a freeglut framework, rather than an SFML framework, depth testing appears to work.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Enabling the depth buffer
« Reply #1 on: March 10, 2012, 05:35:16 pm »
Allocating a depth buffer and using it are two totally different things. For the second thing, you need to call the corresponding OpenGL functions (Google will help you to find them).
Laurent Gomila - SFML developer

Guni

  • Newbie
  • *
  • Posts: 20
    • View Profile
Enabling the depth buffer
« Reply #2 on: March 10, 2012, 09:58:24 pm »
Well like, when I used freeglut, allocating the depth buffer was done via freeglut and since freeglut is cross-platform, it's a cross-platform way to allocate a depth buffer. At least that's how I think it works. Since SFML is supposed to replace freeglut, I figured there would be some function within SFML to allocate the depth buffer. I assumed it was just by setting the DepthBufferBits field in sf::ContextSettings and using that to create a window, but I guess not. Thanks anyway!

Edit: Actually allocating a depth buffer seems to be an OpenGL function, not a freeglut function.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Enabling the depth buffer
« Reply #3 on: March 10, 2012, 10:21:10 pm »
Ok, let's clarify this ;)

Allocating the depth buffer is done when creating the OpenGL context. Creating a context is done by something which relates to OpenGL, but is not OpenGL itself, because it has to be OS-specific -- you can see it as the glue between the OS's window and OpenGL.
Namely: WGL (Windows), GLX (Linux), AGL (Mac OS X), EGL (a platform-independent specification).

Using the depth buffer is done with regular OpenGL calls:
Code: [Select]
glEnable(GL_DEPTH_TEST)
glDepthFunc
glDepthRange
...

These calls configure the depth-buffer, so you can't do anything with it if you don't call them. Since sf::Window has no hidden OpenGL call, if FreeGlut behaves differently it means that it calls these functions for you internally.
Laurent Gomila - SFML developer

Guni

  • Newbie
  • *
  • Posts: 20
    • View Profile
Enabling the depth buffer
« Reply #4 on: March 10, 2012, 10:44:31 pm »
I definitely did do the whole
Code: [Select]
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glDepthRange(0.0f, 1.0f);
thing. I'm pretty sure I have put these OpenGL calls in the right place. I can still draw things to the window, they just won't be properly using depth testing (things behind other objects will appear in front of them). When I put this identical OpenGL code in a freeglut framework, depth testing works.

The reason I suspect is causing this is that while SFML is creating a context and window, it is not attaching depth buffers to the default framebuffer(s) that the context is created with. Am I incorrect?

Edit: This leads me to believe that I must allocate and and attach depth buffer renderbuffers myself using functions like glGenRenderbuffers, then glBindRenderbuffer then glRenderbufferStorage then glFramebufferRenderbuffer.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Enabling the depth buffer
« Reply #5 on: March 10, 2012, 10:51:54 pm »
Look at the OpenGL and Window SFML examples, they use the depth buffer and they work fine.

Do you clear the depth buffer every frame?

Quote
This leads me to believe that I must allocate and and attach depth buffer renderbuffers myself using functions like glGenRenderbuffers, then glBindRenderbuffer then glRenderbufferStorage then glFramebufferRenderbuffer.

These functions are for FBOs (render to texture).
Laurent Gomila - SFML developer

Guni

  • Newbie
  • *
  • Posts: 20
    • View Profile
Enabling the depth buffer
« Reply #6 on: March 10, 2012, 11:01:54 pm »
I call
Code: [Select]
glClearColor(0.0f, 0.1f, 0.0f, 0.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
before every frame, and it still does not work. I suspect this is something wrong on my part, but could there be any other issues regarding SFML?

Edit: Every frame, not every draw call.

Guni

  • Newbie
  • *
  • Posts: 20
    • View Profile
Enabling the depth buffer
« Reply #7 on: March 11, 2012, 03:59:28 am »
My problem was solved by doing by flipping the 0.0f and 1.0f in glDepthRange, so glDepthRange(1.0f, 0.0f); I guess there is something wrong with my GL code. Sorry to bother you.

 

anything