Skip to content

Instantly share code, notes, and snippets.

@kenpower
Created November 5, 2012 16:27
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kenpower/4018118 to your computer and use it in GitHub Desktop.
Save kenpower/4018118 to your computer and use it in GitHub Desktop.
3D OpenGL Cube in SFML
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include "stdafx.h"
#ifdef _DEBUG
#pragma comment(lib,"sfml-graphics-d.lib")
#pragma comment(lib,"sfml-audio-d.lib")
#pragma comment(lib,"sfml-system-d.lib")
#pragma comment(lib,"sfml-window-d.lib")
#pragma comment(lib,"sfml-network-d.lib")
#else
#pragma comment(lib,"sfml-graphics.lib")
#pragma comment(lib,"sfml-audio.lib")
#pragma comment(lib,"sfml-system.lib")
#pragma comment(lib,"sfml-window.lib")
#pragma comment(lib,"sfml-network.lib")
#endif
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#include "SFML/Graphics.hpp"
#include "SFML/OpenGL.hpp"
#include <iostream>
////////////////////////////////////////////////////////////
/// Entry point of application
////////////////////////////////////////////////////////////
int main()
{
// Create the main window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML OpenGL");
// Create a clock for measuring time elapsed
sf::Clock Clock;
//prepare OpenGL surface for HSR
glClearDepth(1.f);
glClearColor(0.3f, 0.3f, 0.3f, 0.f);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
//// Setup a perspective projection & Camera position
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 300.0f);//fov, aspect, zNear, zFar
bool rotate=true;
float angle;
// Start game loop
while (App.isOpen())
{
// Process events
sf::Event Event;
while (App.pollEvent(Event))
{
// Close window : exit
if (Event.type == sf::Event::Closed)
App.close();
// Escape key : exit
if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape))
App.close();
if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::A)){
rotate=!rotate;
}
}
//Prepare for drawing
// Clear color and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Apply some transformations for the cube
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -200.f);
if(rotate){
angle=Clock.getElapsedTime().asSeconds();
}
glRotatef(angle * 50, 1.f, 0.f, 0.f);
glRotatef(angle * 30, 0.f, 1.f, 0.f);
glRotatef(angle * 90, 0.f, 0.f, 1.f);
//Draw a cube
glBegin(GL_QUADS);//draw some squares
glColor3i(0,1,1);
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,0,1);
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,0,1);
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,1,0);
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,1,0);
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,0,0);
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();
// Finally, display rendered frame on screen
App.display();
}
return EXIT_SUCCESS;
}
@patrick38894
Copy link

this is just what i needed. Thanks!

@MossFrog
Copy link

MossFrog commented Nov 9, 2015

Nice.

@DedovKonstantin
Copy link

How to draw cube right (that front size drawing at last turn)?

@albertvaka
Copy link

In your code some quads are facing in the wrong direction (ie: normal is flipped). This works on some graphic cards but not on others, like my integrated Intel. I think this might also be the problem @DedovKonstantin encountered. I fixed it by reordering the glVertex calls, like this:

        glBegin(GL_QUADS);//draw some squares
            glColor3f(0,1,1); //cyan
            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,0,1); //blue
            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,0,1); //magenta
            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,1,0); //green
            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,1,0); //yellow
            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,0,0); //red
            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();

@kim366
Copy link

kim366 commented Aug 23, 2017

How do I compile this in MinGW with g++? I tried OpenGL, glew, glut. Nothing's working..

Copy link

ghost commented Aug 29, 2017

@kim366 It is compiled using the SFML library. https://www.sfml-dev.org/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment