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

Author Topic: SFML 2.3.2 (RenderWindow) + GLEW2 + sfimGUI + opengl 3.3 context problems  (Read 2504 times)

0 Members and 3 Guests are viewing this topic.

stawrocek

  • Newbie
  • *
  • Posts: 2
    • View Profile
Hi!
I have hard time connecting all libraries.
sfimgui -> https://github.com/eliasdaler/imgui-sfml
I can use both RenderWindow + GLEW2 (it renders a simple rectangle), but whenever i add ImGui::SFML::Init it stops rendering my rectangle and displays following messages:

An internal OpenGL call failed in Texture.cpp(563).
Expression:
   glBindTexture(GL_TEXTURE_2D, 0)
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.

I've also tried changing glew2 to glad, but integrating imgui still fails

My code (shortened) looks this way:

#define GLEW_STATIC
#include "GL/glew.h"
#include "include/imgui/imgui.h"
#include "include/imgui/imgui-SFML.h"

#include "my_opengl_wrapper.hpp"                        //vao & shader loader
#include <SFML/Graphics.hpp>

std::vector<GLfloat> cube_Pos = {...};     //my rect vertices

//prints version of opengl
void getOpenglVersion(){
    int v1=31337, v2=1337;
    glGetIntegerv(GL_MAJOR_VERSION, &v1);
    glGetIntegerv(GL_MINOR_VERSION, &v2);
    std::cout << "versions (imGUI): " << v1 << " " << v2 << "\n";
}

int main(){
    sf::ContextSettings settings;
    settings.majorVersion = 3;
    settings.minorVersion = 3;
    sf::RenderWindow window(sf::VideoMode(1280, 720), "sf_glew", sf::Style::Default, settings);
    getOpenglVersion();                         //prints 3 3
    ImGui::SFML::Init(window);          //this causes all problems, root of all evil
    getOpenglVersion();                         //prints 4 0, if ImGui::SFML::Init is commented it prints 3 3
    glewExperimental=GL_TRUE;
    if(glewInit() == GLEW_OK)
        std::cout << "ok! " << x << "\n";
   getOpenglVersion();                          //prints 4 0, if ImGui::SFML::Init is commented it prints 3 3

        //my sh1 and vao loaders, it works great
    chaos::ShaderProgram sh1({std::make_pair("chaos/files/shaders/shader2.vs", GL_VERTEX_SHADER),
                    std::make_pair("chaos/files/shaders/shader2.fs", GL_FRAGMENT_SHADER)});
    chaos::VertexArray vao(3, 0, 2, 0, &cube_Pos);

    while(window.isOpen()){
        window.clear(sf::Color::Green);
        sf::Event event;
        while(window.pollEvent(event)){
            if(event.type == sf::Event::Closed ){
                window.close();
                return 0;
            }
            if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
                return 0;
            }
        }
                //this all works as expected if imgui isn't enabled
        sh1.run();
        sh1.setUniform("uniColor", glm::vec4(1.0f, 0.0f, 0.5f, 1.0f));
        sh1.setUniform("mx",glm::mat4());
        vao.bind();
        glDrawArrays(GL_TRIANGLES, 0, vao.countVertices());
        vao.unbind();
        glUseProgram(0);

        window.display();
    }
    return 0;
}

 

I have to add that if i use only sfml2(RenderWindow) + imgui then it works, also sfml2(RenderWindow) + glew works fine, the problem is in using all three at once.
« Last Edit: December 30, 2016, 01:45:29 am by stawrocek »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: SFML 2.4 (RenderWindow) + GLEW2 + sfimGUI + opengl 3.3 context problems
« Reply #1 on: December 30, 2016, 12:07:46 am »
Are you using SFML 2.4.0 or SFML 2.4.1?

Try to call setActive on the render window before calling the Imgui Init function.

Try to init glew before calling the Imgui Init function.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

stawrocek

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: SFML 2.4 (RenderWindow) + GLEW2 + sfimGUI + opengl 3.3 context problems
« Reply #2 on: December 30, 2016, 01:43:18 am »
Ok, It works!
I solved it this way:
1. I moved GLEW initializing before imgui
2. window.setActive() before imgui
3. some gl state push/pop

Here is the code:
#define GLEW_STATIC
#include "GL/glew.h"

#include "include/imgui/imgui.h"
#include "include/imgui/imgui-SFML.h"

#include "my_gl_warappers.hpp"

#include <SFML/Graphics.hpp>

#include <iostream>
#include <cstdio>

using namespace std;

std::vector<GLfloat> cube_Pos = {...}

int main(){
    sf::ContextSettings settings;
    settings.majorVersion = 3;
    settings.minorVersion = 3;
    sf::RenderWindow window(sf::VideoMode(640, 480), "sf_glew",sf::Style::Default, settings);
    window.setFramerateLimit(60);

    window.setActive();
    glewExperimental=GL_TRUE;
    GLenum x = glewInit();

    ImGui::SFML::Init(window);
    window.resetGLStates();
    window.pushGLStates();

    chaos::ShaderProgram sh1({std::make_pair("chaos/files/shaders/shader2.vs", GL_VERTEX_SHADER),
                    std::make_pair("chaos/files/shaders/shader2.fs", GL_FRAGMENT_SHADER)});
    chaos::VertexArray vao(3, 0, 2, 0, &cube_Pos);

    sf::Clock deltaClock;
    while(window.isOpen()){
        sf::Event event;
        while(window.pollEvent(event)){
            ImGui::SFML::ProcessEvent(event);
            if(event.type == sf::Event::Closed ){
                window.close();
                return 0;
            }
            if(event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
                return 0;
            }
        }
        ImGui::SFML::Update(window, deltaClock.restart());
        ImGui::Begin("text input test");
        ImGui::Button("plz work");
        ImGui::End();

        window.clear();
        window.popGLStates();
        ImGui::Render();
        window.pushGLStates();
        sh1.run();
        sh1.setUniform("uniColor", glm::vec4(1.0f, 0.0f, 0.5f, 1.0f));
        sh1.setUniform("mx",glm::mat4());
        vao.bind();
        glDrawArrays(GL_TRIANGLES, 0, vao.countVertices());
        vao.unbind();
        glUseProgram(0);

        window.display();
    }
    ImGui::SFML::Shutdown();
    return 0;
}
 

Thanks exploit3r

It was SFML 2.3.2 not 2.4 i made a thinko or typo in title

By the way: how expensive are gl push/pop in execution time?
« Last Edit: December 30, 2016, 01:45:11 am by stawrocek »

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: SFML 2.3.2 (RenderWindow) + GLEW2 + sfimGUI + opengl 3.3 context problems
« Reply #3 on: December 30, 2016, 07:24:01 am »
push/popGLStates are pretty expensive. Previously ImGui::Render() had them, but then I removed them after finding what OpenGL state ImGui changed. Seems like it wasn't enough.

push/popGLStates is a brute force solution, so it will be nice if you tried to figure out what state ImGui-SFML doesn't restore as it will improve the library.
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler