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