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

Author Topic: Text transformations and OpenGL  (Read 1731 times)

0 Members and 1 Guest are viewing this topic.

djackson3789

  • Newbie
  • *
  • Posts: 1
    • View Profile
Text transformations and OpenGL
« on: October 11, 2013, 05:21:10 pm »
Hello,

I want to do some transformations (rotations around all three axes) to some text and display it with SFML. I don't see a good way to do this. The problem is that the RenderWindow::draw() method appears to clear the modelview matrix.

I have some code here which works if I change Transform::m_matrix to public.

It would be nice to be able to set all of the transform's matrix. Is there a way to do this without hacking the library?

Code: [Select]
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
#include <time.h>

int main()
{
    // create the window
    sf::RenderWindow window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
    window.setVerticalSyncEnabled(true);

    // load resources, initialize the OpenGL states, ...
    sf::Font font;
    if (!font.loadFromFile("c:/windows/fonts/Arial.ttf"))
    {
        // error...
    }

    sf::Text text;

    // create a transform
    sf::Transform t;

    // select the font
    text.setFont(font); // font is a sf::Font

    // set the string to display
    text.setString("Hello world");

    // set the character size
    text.setCharacterSize(24); // in pixels, not points!
 
    // set the color
    text.setColor(sf::Color::Red);

    // set the text style
    text.setStyle(sf::Text::Bold | sf::Text::Underlined);


    // run the main loop
    bool running = true;
    while (running)
    {
        // handle events
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                // end the program
                running = false;
            }
            else if (event.type == sf::Event::Resized)
            {
                // adjust the viewport when the window is resized
                glViewport(0, 0, event.size.width, event.size.height);
            }
        }

        // clear the buffers
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // draw using SFML... rotate the text starting 1080 ms after program launch
        t=sf::Transform::Identity;
        t.translate(200,300);
        if (clock()>1080)
        {
           t.rotate((float)(clock()%360));
        }

        // apply the transform to the text
        window.draw(text,t);

        // do our own rotation
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(500.0, 300.0, 100.0);
        if (clock()>1080)
           glRotatef((float)(clock()%360),0.0,0.0,1.0); // around z axis

        // save the matrix
        float matrix[16];
        glGetFloatv(GL_MODELVIEW_MATRIX, matrix);

        // set the transform's matrix equal to the saved modelview matrix
        // (i had to make m_matrix public to do this :(
        for (int i=0; i<16; i++)
           t.m_matrix[i]=matrix[i];

        window.draw(text,t);

        // end the current frame (internally swaps the front and back buffers)
        window.display();
    }

    // release resources...

    return 0;
}


eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10821
    • View Profile
    • development blog
    • Email
AW: Text transformations and OpenGL
« Reply #1 on: October 11, 2013, 05:29:14 pm »
SFML is a pure 2D library, you can't draw in all 3 dimensions. If you want to do that, you'll have to do it with OpenGL directly, plus I'm not sure if you can keep using the text object.

One way that might still work with SFML functions is to draw the text onto an render texture and apply a shader onto the render texture before drawing it onto the screen. I'm not experienced there, maybe you could apply the shader directly as well. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/