1
Graphics / 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?
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;
}