SFML community forums

Help => General => Topic started by: Mortal on February 03, 2016, 02:23:04 am

Title: text doesn't draw
Post by: Mortal on February 03, 2016, 02:23:04 am
i tried to display some text on screen with simple triangle shape by using opengl but for some reasons that doesn't work with my code. how to fixed this

here my code:

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

int main()
{
        sf::RenderWindow window(sf::VideoMode(640, 480), "OpenGL");

        window.setActive();

        glewExperimental = GL_TRUE;
        if (glewInit() != GLEW_OK)
                return 1;

        sf::Font font;
        if (!font.loadFromFile("Media/Sansation.ttf"))
                return 1;

        sf::Text text("test", font);
        text.setPosition(5.f, 5.f);

        GLuint vboID;
        glGenBuffers(1, &vboID);
        GLfloat Vertices[] =
        {
                -1, -1, 0,
                 0,  1, 0,
                 1, -1, 0,
        };

        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);

        glClearColor(0.f, 0.f, 0.f, 1.f);

        while (window.isOpen())
        {
                sf::Event windowEvent;
                while (window.pollEvent(windowEvent))
                {
                        if (windowEvent.type == sf::Event::Closed)
                                window.close();
                }

                // opengl drawing
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glEnableVertexAttribArray(0);
                glBindBuffer(GL_ARRAY_BUFFER, vboID);
                glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

                glDrawArrays(GL_TRIANGLES, 0, 3);

                glDisableVertexAttribArray(0);

                // sfml drawing : failed ???????????
                window.pushGLStates();
                window.draw(text);
                window.popGLStates();

                window.display();
        }
}
Title: Re: text doesn't draw
Post by: GraphicsWhale on February 03, 2016, 07:41:31 am
That's interesting.

It appears as if adding the following line:

glBindBuffer(GL_ARRAY_BUFFER, 0);
 

Before drawing the text fixes it. Not sure why you binding a buffer effects SFML, though.
Title: Re: text doesn't draw
Post by: Mortal on February 03, 2016, 10:20:18 am
That's interesting.

It appears as if adding the following line:

glBindBuffer(GL_ARRAY_BUFFER, 0);
 

Before drawing the text fixes it. Not sure why you binding a buffer effects SFML, though.
excellent, that indeed fixed it,
i have tried also glBindBuffer(GL_ELEMENT_ARRAY_BUFFER) it seems work fine with sfml, the only buffer that cause the problem is glBindBuffer(GL_ARRAY_BUFFER).
Title: Re: text doesn't draw
Post by: dabbertorres on February 03, 2016, 06:12:17 pm
Calling glBindBuffer(..., 0) fixes it because you forgot to call it in the first place. You bound your vboID buffer. It's still bound when SFML goes to draw text, so OpenGL then tries to use the vboID buffer for drawing text.

Generally, once you are finished with a buffer (after drawing, pushing data to it, etc), you unbind it with a call to glBindBuffer(..., 0).
Title: Re: text doesn't draw
Post by: Mario on February 03, 2016, 06:32:14 pm
You're using pushGlStates() and popGlStates() the wrong way around.

Call pushGlStates() before you run your custom code and popGlStates() after your custom code.

If you push before calling SFML's draw() method, you essentially keep the "broken" state, which you'd have to restore using popGlStates().
Title: Re: text doesn't draw
Post by: Hapax on February 03, 2016, 10:24:32 pm
You're using pushGlStates() and popGlStates() the wrong way around.
Call pushGlStates() before you run your custom code and popGlStates() after your custom code.
If you push before calling SFML's draw() method, you essentially keep the "broken" state, which you'd have to restore using popGlStates().
This is how it shown in the SFML tutorial:
http://www.sfml-dev.org/tutorials/2.3/window-opengl.php#using-opengl-together-with-the-graphics-module