How to fix OpenGL Co-ordinate System in SFML?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
OmenFelix
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 59
Joined: Fri May 04, 2012 1:42 pm
Current Project: Arcadian Descent(C++, SDL, KOS)
Favorite Gaming Platforms: PS2, PC, XBOX, NDS
Programming Language of Choice: C/C++

How to fix OpenGL Co-ordinate System in SFML?

Post by OmenFelix »

My OpenGL setup is somehow configured to work like so:

(-1, 1) (0, 1) (1, 1)
(-1, 0) (0, 0) (1, 0)
(-1, -1) (0, -1) (1, -1)

How do I configure it so that it works like so:

(0, 0) (SW/2, 0) (SW, 0)
(0, SH/2) (SW/2, SH/2) (SW, SH/2)
(0, SH) (SW/2, SH) (SW/2, SH)

SW as Screen Width.
SH as Screen Height.

This solution would have to fix the problem of I can't translate significantly(>1) on the Z axis. Depth doesn't seem to be working either. The Perspective code I'm using is that of my WORKING GLUT OpenGL code which has a cool 3d grid and camera system etc. But my OpenGL setup doesn't seem to work with SFML.

Help me guys. :(

Thanks in advance. :)

Code: Select all

    #include <SFML/Window.hpp>
    #include <SFML/Graphics.hpp>
    #include <SFML/Audio.hpp>
    #include <SFML/Network.hpp>
    #include <SFML/OpenGL.hpp>
    #include "ResourcePath.hpp" //Mac-only

    #define _USE_MATH_DEFINES
    #include <cmath>

    double screen_width = 640.f;
    double screen_height = 480.f;

    int main (int argc, const char **argv) {
        sf::ContextSettings settings;
        settings.depthBits         = 24;
        settings.stencilBits       = 8;
        settings.antialiasingLevel = 2;
        sf::Window window(sf::VideoMode(screen_width, screen_height, 32), "SFML OpenGL", sf::Style::Close, settings);
        
        window.setActive();
        
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_NORMALIZE);
        glEnable(GL_COLOR_MATERIAL);
        
        glShadeModel(GL_SMOOTH);
        
        glViewport(0, 0, screen_width, screen_height);
        
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        
        //glOrtho(0.0f, screen_width, screen_height, 0.0f, -100.0f, 100.0f);
        gluPerspective(45.0f, (double) screen_width / (double) screen_height , 0.f, 100.f);
        
        glClearColor(0.f, 0.f, 1.f, 0.f); //blue
        
        while (window.isOpen()) {
            sf::Event event;
            while (window.pollEvent(event)) {
                switch (event.type) {
                    case sf::Event::Closed:
                        window.close();
                        break;
                        
                }
                
                switch (event.key.code) {
                    case sf::Keyboard::Escape:
                        window.close();
                        break;
                    case 'W':
                        break;
                    case 'S':
                        break;
                    case 'A':
                        break;
                    case 'D':
                        break;
                }
            }
            
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            
            glTranslatef(0.f, 0.f, 0.f);
            
            glPushMatrix();
            
            glBegin(GL_QUADS);
            
            glColor3f(1.f, 0.f, 0.f);
            glVertex3f(-1.f, 1.f, 0.f);
            
            glColor3f(0.f, 1.f, 0.f);
            glVertex3f(1.f, 1.f, 0.f);
            
            glColor3f(1.f, 0.f, 1.f);
            glVertex3f(1.f, -1.f, 0.f);
            
            glColor3f(0.f, 0.f, 1.f);
            glVertex3f(-1.f, -1.f, 0.f);
            
            glEnd();
            
            glPopMatrix();
            
            window.display();
        }
        
        return EXIT_SUCCESS;
    }

NOTE: I've never used SFML before, and this code worked when I used it with GLUT, so yeah. :P
Why not check out my game-development forum at: http://f1rel0ck.netai.net
Or my 2D RPG at: https://www.facebook.com/pages/Arcadian ... 6873806531
Image
Image
Image
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: How to fix OpenGL Co-ordinate System in SFML?

Post by superLED »

I just skimmed through the post (sorry, little time), but this is how I do it:

glOrtho(0.0f, Window::getWidth(), Window::getHeight(), 0.0f, -1.0f, 1.0f);

But I'm not sure if you can call all OpenGL functions without going through SFML.
Post Reply