I wrote some Code with SFML c++ and OGL that should display a rotatable Cube ....... it doesnt .. WHY?
thx 4 help guys :D
#include<SFML/OpenGL.hpp>
#include<SFML/Graphics.hpp>
#include<iostream>
void display();
void specialKeys();
sf::RenderWindow window;
double rotate_y=0;
double rotate_x=0;
void display()
{
// Clear screen and Z-buffer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Rotate when user changes rotate_x and rotate_y
glRotatef(rotate_x, 1.0, 0.0, 0.0 );
glRotatef(rotate_y, 0.0, 1.0, 0.0 );
glColor3f(0.5f, 0, 0);
//Multi-colored side - FRONT
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, -0.5, -0.5 ); // P1 is red
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( 0.5, 0.5, -0.5 ); // P2 is green
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( -0.5, 0.5, -0.5 ); // P3 is blue
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( -0.5, -0.5, -0.5 ); // P4 is purple
glEnd();
// White side - BACK
glBegin(GL_POLYGON);
glColor3f( 1.0, 1.0, 1.0 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glEnd();
// Purple side - RIGHT
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 1.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glEnd();
// Green side - LEFT
glBegin(GL_POLYGON);
glColor3f( 0.0, 1.0, 0.0 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
// Blue side - TOP
glBegin(GL_POLYGON);
glColor3f( 0.0, 0.0, 1.0 );
glVertex3f( 0.5, 0.5, 0.5 );
glVertex3f( 0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, -0.5 );
glVertex3f( -0.5, 0.5, 0.5 );
glEnd();
// Red side - BOTTOM
glBegin(GL_POLYGON);
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 0.5, -0.5, -0.5 );
glVertex3f( 0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, 0.5 );
glVertex3f( -0.5, -0.5, -0.5 );
glEnd();
}
void specialKeys()
{
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
rotate_y += 5;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
rotate_y -= 5;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
rotate_x += 5;
else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
rotate_x -= 5;
}
int main()
{
sf::RenderWindow window (sf::VideoMode(800, 600), "CUBE");
while(true)
{
specialKeys();
window.display();
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
// Enable Z-buffer depth test
glEnable(GL_DEPTH_TEST);
return 0;
}
Last edited on