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

Author Topic: text doesn't draw  (Read 2025 times)

0 Members and 1 Guest are viewing this topic.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
text doesn't draw
« 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();
        }
}

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: text doesn't draw
« Reply #1 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.

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: text doesn't draw
« Reply #2 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).

dabbertorres

  • Hero Member
  • *****
  • Posts: 506
    • View Profile
    • website/blog
Re: text doesn't draw
« Reply #3 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).

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 878
    • View Profile
Re: text doesn't draw
« Reply #4 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().

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: text doesn't draw
« Reply #5 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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything