SFML community forums

Help => Graphics => Topic started by: mike38 on July 26, 2013, 07:18:33 pm

Title: [SOLVED] Using SFML with OpenGL (conflicts with GL states)
Post by: mike38 on July 26, 2013, 07:18:33 pm
Hi,
I am using SFML with OpenGL in my tiny application. I have simplified the code down to the following:

int initiateGame(void) {

        vector3D screenSize(640.0f,480.0f);

        float clip_near = 0.1f, clip_far = 100.0f;
        glViewport(0, 0, (GLsizei) screenSize.x, (GLsizei) screenSize.y);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective( 75.0f, screenSize.x / screenSize.y, clip_near, clip_far);
        glMatrixMode(GL_MODELVIEW);
        return 0;
};


int main()
{
        sf::RenderWindow window(sf::VideoMode(640, 480), "Test-Project", sf::Style::Default, sf::ContextSettings(32));
        window.setVerticalSyncEnabled(true);
        window.setKeyRepeatEnabled(true);

        sf::Font font;
        font.loadFromFile("C:\\WINDOWS\\Fonts\\arial.ttf");
        sf::Text text;
        text.setFont(font);
        // (set the rest of the text attributes)

        initiateGame();

        float sceneRotation = 0.0f; // degrees

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

                glClearColor(0.5f,0.5f,0.5f,1.0f);
                glClearDepth(1.0f);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glLoadIdentity();

                glEnable(GL_DEPTH_TEST);
                glEnable(GL_LIGHTING);
                glEnable(GL_LIGHT0);

                glColor3f(1.0f,0.0f,0.0f);
                glTranslatef(0.0f,0.0f,-4.0f);

                sceneRotation += 3;
                glRotatef(sceneRotation,0.0f,1.0f,0.0f);

                if (sceneRotation > 360)
                        sceneRotation = 0;

                drawSolidSphere(1.0f,4,4);

                // Start of problem lines
                window.pushGLStates();
                window.draw(text);
                window.popGLStates();
               // End of problem lines.

                window.display();
        }
        return 0;
}

Like this, the sphere is drawn (and translated) but NOT rotated.
If I comment out the three 'problem lines', then the text disappears (obviously) and the sphere spins.

When I add in the rest of my code, if I glEnable(GL_CULL_FACE), then the SFML text dissapears. Surely any OpenGL calls should not affect SFML text, provided the drawing of the SFML text is surrounded by window.pushGLStates() and window.popGLStates()?

Why is this happening?
I read on the wiki that I must do my OpenGL stuff, call 'pushGLStates', do my SFML stuff, then call 'popGLStates'. I thought that this is what I am doing at the moment but it is evidently not working.

Any help would be much appreciated,

Thanks
Title: Re: Using SFML with OpenGL (conflicts with GL states)
Post by: Laurent on July 27, 2013, 03:08:49 pm
Could you please provide a complete and minimal code that reproduces the problem? Your drawSolidSphere function is not shown here, but I'm sure the problem would be the same with a simple triangle ;)
Title: Re: Using SFML with OpenGL (conflicts with GL states)
Post by: mike38 on July 27, 2013, 03:49:36 pm
Thanks for the reply.
Is this OK? Any ideas?
It still DOES NOT rotate when the three SFML lines exist, but DOES when they are commented out.

EDIT:When I use a texture mapped 3D model, the model stays still but the texture rotates in strange ways. As usual, when I get rid of the SFML code all is OK.

int main()
{
        sf::RenderWindow window(sf::VideoMode(640, 480), "Test-Project", sf::Style::Default, sf::ContextSettings(32));
        window.setVerticalSyncEnabled(true);
        window.setKeyRepeatEnabled(true);

        sf::Font font;
        font.loadFromFile("C:\\WINDOWS\\Fonts\\arial.ttf");
        sf::Text text;
        text.setFont(font);
        text.setCharacterSize(30);
        text.setStyle(sf::Text::Bold);
        text.setColor(sf::Color::Red);
       
        vector3D screenSize(640.0f,480.0f);

        float clip_near = 0.1f, clip_far = 100.0f;
        glViewport(0, 0, (GLsizei) screenSize.x, (GLsizei) screenSize.y);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective( 75.0f, screenSize.x / screenSize.y, clip_near, clip_far);
        glMatrixMode(GL_MODELVIEW);

        float sceneRotation = 0.0f; // degrees

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

                glClearColor(0.5f,0.5f,0.5f,1.0f);
                glClearDepth(1.0f);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glLoadIdentity();

                glEnable(GL_DEPTH_TEST);
                glEnable(GL_LIGHTING);
                glEnable(GL_LIGHT0);

                glColor3f(1.0f,0.0f,0.0f);
                glTranslatef(0.0f,0.0f,-4.0f);

                sceneRotation += 3;
                glRotatef(sceneRotation,0.0f,1.0f,0.0f);

                if (sceneRotation > 360)
                        sceneRotation = 0;

float radius = 1.0f;
                glLineWidth(3.0f);
        glBegin(GL_LINES);
        glVertex3f(-radius,-radius,-radius);
        glVertex3f(radius,-radius,-radius);
        glVertex3f(radius,radius,-radius);
        glVertex3f(-radius,radius,-radius);
        glVertex3f(-radius,-radius,radius);
        glVertex3f(radius,-radius,radius);
        glVertex3f(radius,radius,radius);
        glVertex3f(-radius,radius,radius);
        glVertex3f(-radius,-radius,radius);
        glVertex3f(-radius,-radius,-radius);
        glVertex3f(-radius,radius,-radius);
        glVertex3f(-radius,radius,radius);
        glVertex3f(-radius,-radius,-radius);
        glVertex3f(-radius,-radius,radius);
        glVertex3f(-radius,radius,radius);
        glVertex3f(-radius,radius,-radius);
        glVertex3f(-radius,-radius,-radius);
        glVertex3f(-radius,radius,-radius);
        glVertex3f(radius,-radius,-radius);
        glVertex3f(radius,radius,-radius);
        glVertex3f(radius,-radius,radius);
        glVertex3f(radius,radius,radius);
        glVertex3f(-radius,-radius,radius);
        glVertex3f(-radius,radius,radius);
        glEnd();

                // Start of problem lines
                window.pushGLStates();
                window.draw(text);
                window.popGLStates();
               // End of problem lines.

                window.display();
        }
        return 0;
}
Title: Re: Using SFML with OpenGL (conflicts with GL states)
Post by: mike38 on July 29, 2013, 02:56:10 pm
Just in case anyone is reading this- I upgraded to SFML 2.1 and the problem is fixed. Merci Laurent!
Title: Re: [SOLVED] Using SFML with OpenGL (conflicts with GL states)
Post by: Hanmac on July 29, 2013, 03:30:38 pm
sorry that i reopen this Thread, but i have a question about this ...
i thought i need to do it in the other direction like

window.pushGLStates();
  glFunctionsThere();
window.popGLStates();
window.draw(text);

or is that interchangeable?
Title: Re: [SOLVED] Using SFML with OpenGL (conflicts with GL states)
Post by: Nexus on July 29, 2013, 04:01:22 pm
The correct order is stated in the API documentation...