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

Author Topic: Issue with text rendering in specifc case involving glScissor  (Read 3668 times)

0 Members and 2 Guests are viewing this topic.

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
I found a case in which text rendering is broken. After spending most of my day minimizing the code, I came up with the following minimal example.
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    sf::Font font;
    font.loadFromFile("DejaVuSans.ttf");

    sf::Text text("Hello", font, 17);

    font.getGlyph('g', 130, false); // A character that does not occur in the text

    for (const auto c : text.getString())
        font.getGlyph(c, 130, false);

    text.setCharacterSize(130);

    sf::Text anotherText("Hello", font, 17); // Same string
    anotherText.setCharacterSize(130);

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

        window.clear();

        glEnable(GL_SCISSOR_TEST);
        GLint m_scissor[4] = {};
        glGetIntegerv(GL_SCISSOR_BOX, m_scissor);
        glScissor(0, 150, 400, 450);

        // Clear again with different color just to show clipping area
        window.clear(sf::Color::Blue);

        window.draw(anotherText);

        glScissor(m_scissor[0], m_scissor[1], m_scissor[2], m_scissor[3]);

        window.display();
    }

    return EXIT_SUCCESS;
}

Instead of showing "Hello", it shows only the top part of some letters (the "L"s are ccompletely gone). I've attached a screenshot showing the issue.

I traced back at which SFML version this problem was introduced and I found that it only started since 6b71456. SFML versions before this commit show the text correctly.

It looks like glScissor has an effect on the internal font rendering, calling glDisable(GL_SCISSOR_TEST) at the start of Font::loadGlyph and calling glEnable(GL_SCISSOR_TEST) at the end of the function seems to fix the issue.
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Issue with text rendering in specifc case involving glScissor
« Reply #1 on: July 04, 2017, 07:37:33 pm »
You're suppose to manage your OpenGL states correctly, possibly with the help of the 3 dedicated functions in sf::RenderTarget (pushGLStates, popGLStates and resetGLStates). Leaving the scissor rectangle enabled (like any other GL state) while drawing with SFML can have unexpected results -- such as what you see.
Laurent Gomila - SFML developer

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: Issue with text rendering in specifc case involving glScissor
« Reply #2 on: July 04, 2017, 07:42:36 pm »
For other opengl states I understand that, but don't I need the scissor rectangle if I only want part of the SFML rendering to show up on the screen? For instance if I don't want text drawn in an edit box to be drawn outside of the box when the text is too long? Or is there another method to clip the SFML rendering?
TGUI: C++ SFML GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Issue with text rendering in specifc case involving glScissor
« Reply #3 on: July 05, 2017, 07:45:22 am »
Configuring OpenGL states externally and then expecting that SFML will make good use of them automagically is not a good strategy. Depending on implementation details, it may work (like in the past) or not (like now with the new texture copy), so you should really never rely on it.

Admitedly, there's no way to clip things with SFML. But there's a workaround though: use a view with size and viewport set to the clipping rectangle.
Laurent Gomila - SFML developer

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: Issue with text rendering in specifc case involving glScissor
« Reply #4 on: July 05, 2017, 11:45:16 am »
Using views may work, but it will require a lot of testing to get the code right (as I need to deal with intersecting areas and having objects still drawn at the right location after changing the view). I'm currently going to leave it as is, I'm not looking forward to waste my time working around something that has to be fixed afterwards anyway (as #846 also uses glScissors). I can always go back to shipping modified SFML versions until then if I have to.
TGUI: C++ SFML GUI

 

anything