After I tried a lot to solve it I decided to show this to you guys.
I am pretty sure the code should be correct this way.
Sorry, but it can't be smaller:
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
int main()
{
sf::Clock clock;
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML + OpenGL", sf::Style::Default, sf::ContextSettings(32));
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glClearDepth(1.f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70.f, App.GetWidth() / App.GetHeight(), 1.f, 600.f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
float red[] = {102.f/255.f, 51.f/255.f, 51.f/255.f};
float blue[] = {51.f/255.f, 51.f/255.f, 102.f/255.f};
float green[] = {51.f/255.f, 102.f/255.f, 51.f/255.f};
while (App.IsOpened())
{
sf::Event Event;
while (App.PollEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
if (Event.Type == sf::Event::Resized)
glViewport(0, 0, Event.Size.Width, Event.Size.Height);
}
App.SaveGLStates();
App.Clear(sf::Color(0, 0, 0, 1));
App.RestoreGLStates();
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
glRotatef(clock.GetElapsedTime() * 50, 1.f, 0.f, 0.f);
glRotatef(clock.GetElapsedTime() * 30, 0.f, 1.f, 0.f);
glRotatef(clock.GetElapsedTime() * 90, 0.f, 0.f, 1.f);
// Draw a cube
glBegin(GL_QUADS);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, red);
glColor3f(1.f, 0.f, 0.f);
glNormal3f(0.f, 0.f, -1.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);
glColor3f(1.f, 0.f, 0.f);
glNormal3f(0.f, 0.f, 1.f);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f( 50.f, -50.f, 50.f);
glVertex3f( 50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, 50.f);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, green);
glColor3f(0.f, 1.f, 0.f);
glNormal3f(-1.f, 0.f, 0.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, -50.f, 50.f);
glColor3f(0.f, 1.f, 0.f);
glNormal3f(1.f, 0.f, 0.f);
glVertex3f(50.f, -50.f, -50.f);
glVertex3f(50.f, -50.f, 50.f);
glVertex3f(50.f, 50.f, 50.f);
glVertex3f(50.f, 50.f, -50.f);
glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
glColor3f(0.f, 0.f, 1.f);
glNormal3f(0.f, -1.f, 0.f);
glVertex3f(-50.f, -50.f, 50.f);
glVertex3f(-50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f, -50.f);
glVertex3f( 50.f, -50.f, 50.f);
glColor3f(0.f, 1.f, 0.f);
glNormal3f(0.f, 1.f, 0.f);
glVertex3f(-50.f, 50.f, 50.f);
glVertex3f(-50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, -50.f);
glVertex3f( 50.f, 50.f, 50.f);
glEnd();
App.Display();
}
return EXIT_SUCCESS;
}
The bug is:
The two red quads don't show up.
This only happens when:
- you use lighting
- you use sf::RenderWindow instead of sf::Window (I wanted to display text, so I had to change to RenderWindow)
Crazy thing about it:
- When you change it to this the blue quads won't show up:
float blue[] = {51.f/255.f, 51.f/255.f, 102.f/255.f};
float red[] = {102.f/255.f, 51.f/255.f, 51.f/255.f};
float green[] = {51.f/255.f, 102.f/255.f, 51.f/255.f};
I am using SFML2 and CodeBlocks + MinGW.
Any ideas?