How to fix OpenGL Co-ordinate System in SFML?
Posted
by
Marc Alexander Reed
on Game Development
See other posts from Game Development
or by Marc Alexander Reed
Published on 2012-06-14T16:11:34Z
Indexed on
2012/06/15
15:28 UTC
Read the original article
Hit count: 414
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. :)
#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;
}
© Game Development or respective owner