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

Author Topic: [Solved] Combining OpenGL stuff and graphics module  (Read 7418 times)

0 Members and 1 Guest are viewing this topic.

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
[Solved] Combining OpenGL stuff and graphics module
« on: January 03, 2019, 03:46:05 pm »
Hi,

I am trying to use both OpenGL 3.0+ drawing (for 3D) and SFML for texts and 2d stuff. Currently, I am stuck with it. It is possible to render one of them, but not both at once. I followed recommendations in the tutorial concerning mixing sfml with opengl: https://en.sfml-dev.org/tutorials/2.5/window-opengl.php#using-opengl-together-with-the-graphics-module and added push/pop calls to my code, but it still does not work.

To minimize amount of possible factors, I've created minimal example to test (attached in a zip).

Please, check out this code and tell me where I made the mistake or how can I figure it out. I will appreciate that.

My setup:
Windows 7
SFML 2.5.0 (pre-built binaries from sfml-dev.org)
Visual Studio 2017
glad OpenGL loader

ZIP file contains glad and the source code. In the source code, there are some #defines, which you can
 tweak to test different cases


Check Main.cpp comments for more details. (sorry for messy code and globals, I think it is fine for testing purposes)

Thanks in advance,
MrOnlineCoder
« Last Edit: January 06, 2019, 11:44:07 am by MrOnlineCoder »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Combining OpenGL stuff and graphics module
« Reply #1 on: January 03, 2019, 03:56:27 pm »
If your code is truly minimal, then you'd better paste it directly on the forum. If you can't, it means that it's not really minimal ;)

For that kind of problem, I'd say you can easily setup a simple main() that opens a window and draws an OpenGL quad + some sfml-graphics entity. No need for a 34 kB zipped project.
Laurent Gomila - SFML developer

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Combining OpenGL stuff and graphics module
« Reply #2 on: January 03, 2019, 04:12:19 pm »
If your code is truly minimal, then you'd better paste it directly on the forum. If you can't, it means that it's not really minimal ;)

For that kind of problem, I'd say you can easily setup a simple main() that opens a window and draws an OpenGL quad + some sfml-graphics entity. No need for a 34 kB zipped project.
The real Main is just 150 lines, 34 kB are made of glad headers, which are added for the sake of simplicity. :D

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Combining OpenGL stuff and graphics module
« Reply #3 on: January 03, 2019, 04:16:52 pm »
Does the OpenGL example of the SFML SDK works for you?
Laurent Gomila - SFML developer

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Combining OpenGL stuff and graphics module
« Reply #4 on: January 03, 2019, 04:33:35 pm »
It didn't compile at start, but I downloaded glad with compatibility settings and now it works, the background and text are shown, and the cube is rotating, but it spams my console with errors:

An internal OpenGL call failed in RenderTextureImplFBO.cpp(192).
Expression:
   GLEXT_glBindFramebuffer(GLEXT_GL_FRAMEBUFFER, 0)
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Combining OpenGL stuff and graphics module
« Reply #5 on: January 03, 2019, 06:18:33 pm »
Just in case, have you tried with SFML 2.5.1?
Laurent Gomila - SFML developer

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Combining OpenGL stuff and graphics module
« Reply #6 on: January 03, 2019, 06:39:52 pm »
Solution found

1. In my project, from which I came here with this issue, I was setting shader uniforms to a wrong shader, that caused error.
2. What about my example, the solution is simple: do not use window.clear() when mixing with opengl. Seems that it internally unbinds texture (by binding texture with id 0) and this somehow causes problems in your code (at least my debugger thinks so :D ).
3. Also, calling window.resetGLStates() before pushing doesn't fix the problem, instead you have to reset gl objects manually:

Example:

//Application loop (while window.isOpen())

//If you start drawing your OpenGL (in my case 3d scene) before SFML, use glClear instead of window.clear
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //or your favourite color

//Drawing your stuff
glDrawArrays(...)
glDrawElements(....);

//Switching to sfml

//Clearing objects
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
glUseProgram(0);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, 0);

//SFML drawing
window.pushGLStates();
window.draw(...);
window.popGLStates();
 

Probably, I've missed something, because SFML is written and tested better than my code, but I don't know why it didn't help.

Anyway, thanks Laurent for your help.

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Combining OpenGL stuff and graphics module
« Reply #7 on: January 03, 2019, 07:44:32 pm »
Quote
do not use window.clear() when mixing with opengl. Seems that it internally unbinds texture (by binding texture with id 0)
Yes, that's true. https://github.com/SFML/SFML/blob/master/src/SFML/Graphics/RenderTarget.cpp#L150
Back to C++ gamedev with SFML in May 2023

MrOnlineCoder

  • Jr. Member
  • **
  • Posts: 87
    • View Profile
Re: Combining OpenGL stuff and graphics module
« Reply #8 on: January 03, 2019, 08:16:04 pm »
How it can cause errors, isn't that just "harmless" code?

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Combining OpenGL stuff and graphics module
« Reply #9 on: January 03, 2019, 08:39:41 pm »
I meant that you're correct that clear unbinds the texture.
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Combining OpenGL stuff and graphics module
« Reply #10 on: January 03, 2019, 08:59:55 pm »
Quote
Also, calling window.resetGLStates() before pushing doesn't fix the problem, instead you have to reset gl objects manually:
In theory, only OpenGL 2+ stuff, since SFML still only cares about 1.x things.
Laurent Gomila - SFML developer