Calling window.pushGLStates and then popGLStates doesn't set the glMatrixMode properly. Well, I an OpenGL newbie so I'm not sure if this is expected or not. Basically, writing text to the window requires me to use push/pop, but pop doesn't return me to my previously expected state!
g++ main.cpp -lsfml-graphics -lsfml-audio -lsfml-window -lsfml-graphics -lsfml-system -lglut -lGLU -lGL -lm -o run
#include <GL/gl.h>
#include <GL/glu.h>
int main() {
sf::RenderWindow window(sf::VideoMode(800, 800), "SFML window");
sf::Font font;
if (!font.loadFromFile("tahoma.ttf"))
return EXIT_FAILURE;
sf::Text text("Hello SFML", font, 50);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, 2.f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
int i = 0;
while (window.isOpen()) {
i++;
window.setActive();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
window.close();
}
window.clear();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.f,0.0f,-1.0f * i);
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd();
window.pushGLStates();
window.draw(text);
window.popGLStates();
// Why is this required?
glMatrixMode(GL_MODELVIEW);
window.display();
}
return EXIT_SUCCESS;
}
At the end of main, I have to call
glMatrixMode(GL_MODELVIEW);
in order to get "movement" working. If you comment out the line, you will see a static screen. If you uncomment the line, you will see the triangle fade into the distance.