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

Author Topic: mixing SFML Graphics and OpenGL / GLEW  (Read 4933 times)

0 Members and 1 Guest are viewing this topic.

glurbi

  • Newbie
  • *
  • Posts: 5
    • View Profile
mixing SFML Graphics and OpenGL / GLEW
« on: September 12, 2013, 08:33:33 pm »
Hi,

I'm trying to combine SFML graphics and modern OpenGL with GLEW.
My application compiles fine, and my opengl calls are applied and work fine. However, the SFML graphics draw operations seem to be ignored...

I followed the information about mixing SFML Graphics and opengl GLEW in http://www.sfml-dev.org/tutorials/2.1/window-opengl.php

The SFML graphics calls seem to be ignored as soon as I call glewInit()...
If I don't call glewInit(), my opengl code crashes (extension functions not initialized)...

I know that SFML uses GLEW internally. So my question is: is it possible to combine SFML Graphics and OpenGL / GLEW in my code? If yes, how?

Vincent
« Last Edit: September 12, 2013, 09:39:51 pm by glurbi »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: mixing SFML Graphics and OpenGL / GLEW
« Reply #1 on: September 12, 2013, 08:54:25 pm »
Yes you can mix them, thing is though that all the GL states > 2.something need to be reset manually since SFML doesn't know about them. If you search the forzm a bit you should find some further info, otherwise I'll let others answer that question (better) since I've no experience with OpenGl. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

glurbi

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: mixing SFML Graphics and OpenGL / GLEW
« Reply #2 on: September 12, 2013, 09:38:10 pm »
Thanks for your answer, you put me on the right track!
I was using pushGLStates() and popGLStates() to surround my opengl code. This is not enough apparently.

Now I just added a resetGLStates() call before using SFML graphics functions and it works fine.

So I just misunderstood the tutorial I guess...


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: mixing SFML Graphics and OpenGL / GLEW
« Reply #3 on: September 12, 2013, 09:50:31 pm »
Quote
I was using pushGLStates() and popGLStates() to surround my opengl code.
You must use them to surround SFML code. And no need to call resetGLStates(), it's done in pushGLStates().
Laurent Gomila - SFML developer

glurbi

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: mixing SFML Graphics and OpenGL / GLEW
« Reply #4 on: September 12, 2013, 10:32:24 pm »
Hmm, not what see: if I surround my SFML code with push/pop, only my opengl related code gets on screen.
If I surround my opengl code with push/pop and add resetGLStates() before calling my SFML code, both opengl and SFML related code get on screen...

I'm using shaders, can this somehow be linked to that?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: mixing SFML Graphics and OpenGL / GLEW
« Reply #5 on: September 13, 2013, 07:38:16 am »
Please show a complete and minimal code that reproduces the problem.
Laurent Gomila - SFML developer

glurbi

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: mixing SFML Graphics and OpenGL / GLEW
« Reply #6 on: September 13, 2013, 07:36:51 pm »
Here it is. Not so minimal, but hard to make it short when using the programmable pipeline...
On my computer, if I comment out the push/pop surrounding the sfml code and uncomment the push/pop/reset, then I can see the sfml drawings. I can see the opengl drawing in any case.

#include <GL/glew.h>
#include <SFML/Graphics.hpp>

int main()
{
        const int POSITION_ATTRIBUTE_INDEX = 0;

        const int width = 800;
        const int height = 600;

        sf::RenderWindow window(sf::VideoMode(width, height), "SFML!");
        glewInit();

////window.pushGLStates();

        // compile vertex shader source
    const char* vertexShaderSource =
                "#version 330\n\
                 uniform mat4 mvpMatrix;\
        uniform vec4 color;\
        in vec3 vpos;\
        out vec4 vcolor;\
                 void main(void) {\
                        gl_Position = mvpMatrix * vec4(vpos, 1.0f);\
                        vcolor = color;\
                }"
;
    int vertexShaderSourceLength = strlen(vertexShaderSource);
    GLuint vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShaderId, 1, &vertexShaderSource, &vertexShaderSourceLength);
    glCompileShader(vertexShaderId);

        // compile fragment shader source
    const GLchar* fragmentShaderSource =
                "#version 330\n\
                 in vec4 vcolor;\
        out vec4 fcolor;\
                 void main(void) {\
                        fcolor = vcolor;\
                 }"
;
    int fragmentShaderSourceLength = strlen(fragmentShaderSource);
    GLuint fragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShaderId, 1, &fragmentShaderSource, &fragmentShaderSourceLength);
    glCompileShader(fragmentShaderId);

        // link shader program
    GLuint programId = glCreateProgram();
    glAttachShader(programId, vertexShaderId);
    glAttachShader(programId, fragmentShaderId);
    glBindAttribLocation(programId, POSITION_ATTRIBUTE_INDEX, "position");
    glLinkProgram(programId);

        // create the triangle vertex buffer
        GLuint trianglesId;
    float triangleVertices[] = {
            -0.5f, -0.5f, 0.0f,
            1.0f, -0.5f, 0.0f,
            -0.5f, 1.0f, 0.0f
    };
    glGenBuffers(1, &trianglesId);
    glBindBuffer(GL_ARRAY_BUFFER, trianglesId);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW);

        // defines the orthographic projection matrix
        float m[16];
        const float left = -1.5f;
        const float right = 1.5f;
        const float bottom = -1.5f;
        const float top = 1.5f;
        const float nearPlane = 1.0f;
        const float farPlane = -1.0f;
        m[0] = 2 / (right - left);
    m[1] = 0.0f;
    m[2] = 0.0f;
    m[3] = 0.0f;
    m[4] = 0.0f;
    m[5] = 2 / (top - bottom);
    m[6] = 0.0f;
    m[7] = 0.0f;
    m[8] = 0.0f;
    m[9] = 0.0f;
    m[10] = 2 / (farPlane - nearPlane);
    m[11] = 0.0f;
    m[12] = -(right + left) / (right - left);
    m[13] = -(top + bottom) / (top - bottom);
    m[14] = -(farPlane + nearPlane) / (farPlane - nearPlane);
    m[15] = 1.0f;

////window.popGLStates();

        bool running = true;
    while (running)
    {
                sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                                running = false;
        }
        window.clear();
               
////////window.resetGLStates();
                window.pushGLStates();
                sf::CircleShape shape(200.f);
                shape.setFillColor(sf::Color::Green);
        window.draw(shape);
                window.popGLStates();

////////window.pushGLStates();
                glUseProgram(programId);
                GLuint matrixUniform = glGetUniformLocation(programId, "mvpMatrix");
                glUniformMatrix4fv(matrixUniform, 1, false, m);
                GLuint color = glGetUniformLocation(programId, "color");
                glUniform4f(color, 1.0f, 1.0f, 0.0f, 0.7f);
                glEnableVertexAttribArray(POSITION_ATTRIBUTE_INDEX);
                glBindBuffer(GL_ARRAY_BUFFER, trianglesId);
                glVertexAttribPointer(POSITION_ATTRIBUTE_INDEX, 3, GL_FLOAT, GL_FALSE, 0, 0);
                glDrawArrays(GL_TRIANGLES, 0, 3);
                glDisableVertexAttribArray(POSITION_ATTRIBUTE_INDEX);
////////window.popGLStates();

                window.display();
    }

    return 0;
}

 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: mixing SFML Graphics and OpenGL / GLEW
« Reply #7 on: September 13, 2013, 08:55:40 pm »
As eXpl0it3r said, you need to reset yourself any OpenGL 2.x+ state since SFML is not aware of them. This would include, for example, glEnableVertexAttribArray and glBindBuffer.
Laurent Gomila - SFML developer

glurbi

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: mixing SFML Graphics and OpenGL / GLEW
« Reply #8 on: September 13, 2013, 09:02:38 pm »
Correct, I can get rid of all push/pop/reset in my example by adding

glBindBuffer(GL_ARRAY_BUFFER, 0);
glUseProgram(0);

after the opengl code and then all gets displayed as expected.

Now that all make sense!

Thanks all for your help.

 

anything