Ok, I have stripped out everything that is unneeded, e.g. the snowman and the angular dependency of the camera position. Now the camera always looks at (0,0,0) from (0,0,5), with (0,1,0) being the up-vector needed by gluLookat(). What remains of the drawing is a wireframe box occupying the space [-1,1]x[-1,1]x[-1,1].
The problem is that the wireframe box is off-center after a window resize. The effect depends on which side or corner of the window that is resized. My point was that it works perfectly when not using RenderWindow + PreserveOpenGLStates(true).
Here is the code:
#include <SFML/Graphics.hpp>
#include <cmath>
using namespace std;
double l = 1.0;
double xmin = -l, xmax = l, ymin = -l, ymax = l, zmin = -l, zmax = l;
void setView(int w, int h)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45, float(w)/h, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
GLint* mkDispList() {
GLint* dispList = new GLint;
*dispList = glGenLists(1);
glNewList(*dispList, GL_COMPILE);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex3f(xmin, ymin, zmin); glVertex3f(xmax, ymin, zmin);
glVertex3f(xmax, ymax, zmin); glVertex3f(xmin, ymax, zmin);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(xmin, ymin, zmax); glVertex3f(xmax, ymin, zmax);
glVertex3f(xmax, ymax, zmax); glVertex3f(xmin, ymax, zmax);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(xmax, ymax, zmin); glVertex3f(xmax, ymax, zmax);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(xmin, ymax, zmin); glVertex3f(xmin, ymax, zmax);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(xmin, ymin, zmin); glVertex3f(xmin, ymin, zmax);
glEnd();
glBegin(GL_LINE_STRIP);
glVertex3f(xmax, ymin, zmin); glVertex3f(xmax, ymin, zmax);
glEnd();
glEndList();
return(dispList);
}
int main()
{
sf::RenderWindow App(sf::VideoMode(640, 480, 32), "Snowman!");
App.PreserveOpenGLStates(true);
GLint* dispList = mkDispList();
glEnable(GL_DEPTH_TEST);
setView(640, 480);
while(App.IsOpened())
{
sf::Event Event;
while(App.GetEvent(Event)) {
if(Event.Type == sf::Event::Resized)
setView(Event.Size.Width, Event.Size.Height);
}
App.SetActive();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glCallList(*dispList);
App.Display();
}
return(0);
}