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

Author Topic: Mixing SFML with OpenGL  (Read 3591 times)

0 Members and 1 Guest are viewing this topic.

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Mixing SFML with OpenGL
« on: June 18, 2012, 12:30:33 am »
I have been playing around with OpenGL with SFML and have run into a couple problems.  Minimal example:

#include <windows.h>
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
#include <fstream>
#include <sstream>

#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

using namespace std;

int main() {

    sf::RenderWindow App(sf::VideoMode(800, 600), "SFML OpenGL Testing Environment");

    ///Text to show coords
    sf::Text text("X Coord");
    sf::Text text2("Y Coord");
    sf::Text text3("Z Coord");

    text2.setPosition(0,30);
    text3.setPosition(0,60);

    ///OpenGL Commands
        glClearColor(0.2,0.7,0.9,0);
        glEnable(GL_DEPTH_TEST);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glViewport(0, 0, 800, 600);
        gluPerspective(120.0, (GLdouble)800/(GLdouble)600, 0.5, 500.0);
        glMatrixMode(GL_MODELVIEW);

    float x = -10, y = 10, z = -10;

    ///Main loop
    while (App.isOpen())
    {
        sf::Event Event;
        while (App.pollEvent(Event))
        {
            if (Event.type == sf::Event::Closed)
                App.close();
        }

        ///Clear the screen and create a reference sphere
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glLoadIdentity();
        glColor3d( 1.0,0.0,0.0 );
        glutSolidSphere(1,30,30);

        ///Change the view with the arrow and W/S keys
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))  x = x + .1;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) x = x - .1;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))  y = y + .1;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) y = y - .1;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))  z = z + .1;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::S)) z = z - .1;

        ///Camera
        glLoadIdentity();
        gluLookAt(x,y,z, 0,0,0, 0,1,0);

        ///Change text with current coords and display
        std::stringstream stream1;
        stream1 << x;
        text.setString(stream1.str());

        std::stringstream stream2;
        stream2 << y;
        text2.setString(stream2.str());

        std::stringstream stream3;
        stream3 << z;
        text3.setString(stream3.str());

        App.pushGLStates();
        App.draw(text);
        App.draw(text2);
        App.draw(text3);
        App.popGLStates();

        App.display();
    }
        return EXIT_SUCCESS;
}
 

The first is that when I comment out the lines:

App.pushGLStates();
App.draw(text);
App.draw(text2);
App.draw(text3);
App.popGLStates();
 

Then the sphere appears very close to the camera. When I keep them in the code, the sphere appears far away. I think this has something to do with keeping GL context through SFML, but I am not sure.

The second problem may not be meant for these forums, but I'll ask anyway just in case. The code provided above is supposed to create a sphere, which it does, and then should allow the user to move the camera's position around using the arrow and W/S keys. However, while the coords change, the view does not.

Any help at all would be much appreciated :)

LucasShadow

  • Jr. Member
  • **
  • Posts: 52
    • View Profile
Re: Mixing SFML with OpenGL
« Reply #1 on: June 21, 2012, 10:53:31 pm »
After a few days of fooling around with it I got it to work. Turns out that I was actually just calculating the camera transformations incorrectly and forgot to initialize OpenGL lol...

Code for those who want it (SFML 2 with OpenGL 3.3):
#include <windows.h>
#include <SFML/Graphics.hpp>
#include <math.h>
#include <iostream>
#include <fstream>
#include <sstream>

#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glut.h>

// angle of rotation for the camera direction
float angle = 0.0f;
// actual vector representing the camera's direction
float lx=0.0f,lz=-1.0f;
// XZ position of the camera
float x=0.0f, z=5.0f;
// the key states. These variables will be zero
//when no key is being presses
float deltaAngle = 0.0f;
float deltaMove = 0;

void resizeGL( int w, int h ) {
        glViewport( 0, 0, w, h );
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective( 120.0, (GLdouble)w/(GLdouble)h, 0.5, 500.0 );
        glMatrixMode( GL_MODELVIEW );
}

void computePos(float deltaMove) {

        x += deltaMove * lx * 0.1f;
        z += deltaMove * lz * 0.1f;
}

void computeDir(float deltaAngle) {

        angle += deltaAngle;
        lx = sin(angle);
        lz = -cos(angle);
}

int main(int argc, char **argv) {

    sf::RenderWindow window( sf::VideoMode(800,600,32), "SFML OpenGL" );

        // init GLUT
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

        glEnable(GL_DEPTH_TEST);

        sf::Text testtext("HELLO OPENGL");

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

                deltaAngle = 0.0f, deltaMove = 0.0f;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) deltaAngle = -0.01f;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) deltaAngle = 0.01f;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) deltaMove = 1.0f;
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) deltaMove = -1.0f;

        computeDir(deltaAngle);
        computePos(deltaMove);

        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
                glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(50.0, 1.0, 1.0, 70.0);
                glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(      x, 1.0f, z,//eye's position
                    x+lx, 1.0f,  z+lz,//where the eye is looking (point)
                    0.0f, 1.0f,  0.0f);//translations

        glColor3f(0.9f, 0.9f, 0.9f);
        glBegin(GL_QUADS);
            glVertex3f(-10.0f, 0.0f, -10.0f);
            glVertex3f(-10.0f, 0.0f,  10.0f);
            glVertex3f( 10.0f, 0.0f,  10.0f);
            glVertex3f( 10.0f, 0.0f, -10.0f);
        glEnd();

        glColor3f(0.4f, 0.4f, 0.4f);
        glTranslatef(0,2,0);
        glutSolidTeapot(.5);

        window.pushGLStates();
        window.draw(testtext);
        window.popGLStates();

        window.display();

        }
        return 1;
}
 

 

anything