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

Author Topic: [SOLVED] Android / OpenGL ES / depthBits = 0 / Depth buffer not working  (Read 5177 times)

0 Members and 1 Guest are viewing this topic.

DragonRock

  • Newbie
  • *
  • Posts: 14
    • View Profile
Hi everyone,

Recently, I tried to use the SFML port for Android, and I tried to use OpenGL ES code in order to do some 3D rendering on my phone.

For the moment, I'm just trying to draw a cube with different colors for each face, but I seem to have a problem with the depth buffer. It seems that, even though I try to create a window with a depthBits of 24, when my window is created, the depthBits is 0, and I guess that's why my depth buffer doesn't work.

Here is my minimal example :

#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <android/log.h>

#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG  , "MyApp", __VA_ARGS__)

int main(int argc, char *argv[])
{
    sf::ContextSettings settings(24, 8, 0, 1, 1);
    sf::RenderWindow window(sf::VideoMode::getDesktopMode(), "", sf::Style::Default, settings);

    LOGD("OpenGL Version : %s", glGetString(GL_VERSION));
    LOGD("DepthBits = %d", window.getSettings().depthBits);

    return 0;
}

My output is
D/MyApp   (22428): OpenGL Version : OpenGL ES-CM 1.1
D/MyApp   (22428): DepthBits = 0

I have seen that the the tutorials say that

Quote
OpenGL versions above 3.0 are supported by SFML (as long as your graphics driver can handle them), but you can't set flags for now. This means that you can't create debug or forward compatible contexts; in fact SFML automatically creates contexts with the "compatibility" flag, because it uses deprecated functions internally. This should be improved soon, and flags will then be exposed in the public API.

Is this the cause of my problems ? Is there any way to enable the depth buffer on Android ?

Thank you !
« Last Edit: January 07, 2016, 10:53:40 am by DragonRock »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: Android / OpenGL ES / depthBits = 0
« Reply #1 on: January 06, 2016, 07:05:42 pm »
After looking through EglContext.cpp it seems like the EGL context implementation doesn't update the sf::ContextSettings from the selected frame buffer configuration ::). Thus, you currently cannot rely on the depth value in sf::ContextSettings. An Android maintainer might have to take a look at this.

Even without this information though, there are alternate ways of determining whether a depth buffer is available. The simplest would be to draw 2 triangles, the one you draw second being positioned behind the one you draw first. With depth testing enabled and functioning correctly, OpenGL ES should discard the fragments which are obscured by the first triangle. The code that does this isn't too complicated. You can post it here for people to review if you want.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Android / OpenGL ES / depthBits = 0
« Reply #2 on: January 06, 2016, 10:26:02 pm »
New branch/commit with a bugfix:

https://github.com/SFML/SFML/commit/b136272e2d3c303801f59b36a142fdf085fbd49e

Only tested the depth buffer, but the returned value is fine.

Would be nice if you could do some more testing. Also I'm not 100% sure we should just assume that the created context is 1.1, but don't know enough about OpenGL ES to really judge this without trying to find some information about this first.
« Last Edit: January 06, 2016, 10:30:48 pm by Mario »

DragonRock

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Android / OpenGL ES / depthBits = 0
« Reply #3 on: January 07, 2016, 09:55:36 am »
Hi everyone,

thank you for trying to help me. So I switched to bugfix/android_contextinfo, recompiled everything, and now, my depthBits seems correct (meaning that it gets the value that I try to give him).

However, I still have problems with the depth buffer.

Here is the program I try to run.

I tried to simplify it as much as possible. It is supposed to draw a cube with two red faces, two green faces and two blue faces. but I get this kind of results :



To me, it really looks like the depth buffer is not enabled (but it should be though).

Thanks guys !
« Last Edit: November 06, 2017, 05:02:19 pm by DragonRock »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Android / OpenGL ES / depthBits = 0
« Reply #4 on: January 07, 2016, 10:39:06 am »
I had to change the constructors for the example to compile, but I got it running.

Depth buffer testing works, but only if I re-enable it every iteration - probably reset somewhere… having a look.

Btw. you don't have to call SFML's sf::RenderWindow::clear() (since you call glClear()) and you may skip glFlush() as well (IIRC it's called by SFML anyway).

DragonRock

  • Newbie
  • *
  • Posts: 14
    • View Profile
Re: Android / OpenGL ES / depthBits = 0
« Reply #5 on: January 07, 2016, 10:53:22 am »
Hi,

Sorry for the constructors, I did a last minute change and it broke up everything... and thanks for all this information, everything works now !
« Last Edit: January 07, 2016, 01:41:50 pm by DragonRock »

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: [SOLVED] Android / OpenGL ES / depthBits = 0 / Depth buffer not working
« Reply #6 on: January 09, 2016, 04:02:21 pm »
Contrary to popular belief, when you construct an sf::RenderWindow, it doesn't ensure that its OpenGL context is also activated. If you intend to perform OpenGL operations right after window construction (and no other prior sfml-graphics operations are performed on the window), you will need to explicitly call .setActive(true) in order to make sure you have a context that your operations will apply to. When you enable depth testing (and all the other stuff as well for that matter) in your example, it is being performed, however in some other internal context that you will never use again.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SOLVED] Android / OpenGL ES / depthBits = 0 / Depth buffer not working
« Reply #7 on: January 09, 2016, 07:17:44 pm »
Quote
Contrary to popular belief, when you construct an sf::RenderWindow, it doesn't ensure that its OpenGL context is also activated
?
https://github.com/SFML/SFML/blob/master/src/SFML/Window/Window.cpp#L427
Laurent Gomila - SFML developer

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: [SOLVED] Android / OpenGL ES / depthBits = 0 / Depth buffer not working
« Reply #8 on: January 09, 2016, 08:56:07 pm »
I remember there was a version in the past where some code path led to an internal context being left active after the sf::RenderWindow was constructed. This wasn't the case with sf::Window. It might have been fixed since then, but since activation of the RenderWindow's context (not some internal one) after construction isn't documented, you shouldn't count on it always being the case anyway.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: [SOLVED] Android / OpenGL ES / depthBits = 0 / Depth buffer not working
« Reply #9 on: January 09, 2016, 11:42:09 pm »
It's different for the current Android implementation I think. Completely forgot about this, but after starting it's indeed possible you'll get past the window initialization while not having a valid context (which might also happen once the app isn't in foreground anymore).

 

anything