2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Window.hpp>
|
2009-03-29 00:22:58 +08:00
|
|
|
#include <SFML/OpenGL.hpp>
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
|
2013-08-26 15:17:53 +08:00
|
|
|
// Some platform-specific stuff
|
|
|
|
#ifdef SFML_OPENGL_ES
|
|
|
|
|
|
|
|
#define glClearDepth glClearDepthf
|
|
|
|
#define glFrustum glFrustumf
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
2013-05-21 22:57:02 +08:00
|
|
|
// Request a 32-bits depth buffer when creating the window
|
|
|
|
sf::ContextSettings contextSettings;
|
|
|
|
contextSettings.depthBits = 32;
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
// Create the main window
|
2013-05-21 22:57:02 +08:00
|
|
|
sf::Window window(sf::VideoMode(640, 480), "SFML window with OpenGL", sf::Style::Default, contextSettings);
|
2009-03-05 02:42:11 +08:00
|
|
|
|
2013-05-21 22:57:02 +08:00
|
|
|
// Make it the active window for OpenGL calls
|
|
|
|
window.setActive();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Set the color and depth clear values
|
|
|
|
glClearDepth(1.f);
|
2013-05-21 22:57:02 +08:00
|
|
|
glClearColor(0.f, 0.f, 0.f, 1.f);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Enable Z-buffer read and write
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
|
2013-05-21 22:57:02 +08:00
|
|
|
// Disable lighting and texturing
|
|
|
|
glDisable(GL_LIGHTING);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
// Configure the viewport (the same size as the window)
|
|
|
|
glViewport(0, 0, window.getSize().x, window.getSize().y);
|
|
|
|
|
2009-01-29 00:18:34 +08:00
|
|
|
// Setup a perspective projection
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2013-05-21 22:57:02 +08:00
|
|
|
GLfloat ratio = static_cast<float>(window.getSize().x) / window.getSize().y;
|
|
|
|
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
|
|
|
|
|
2013-06-04 02:48:24 +08:00
|
|
|
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
|
2013-05-21 22:57:02 +08:00
|
|
|
GLfloat cube[] =
|
|
|
|
{
|
|
|
|
// positions // colors (r, g, b, a)
|
|
|
|
-50, -50, -50, 0, 0, 1, 1,
|
|
|
|
-50, 50, -50, 0, 0, 1, 1,
|
|
|
|
-50, -50, 50, 0, 0, 1, 1,
|
|
|
|
-50, -50, 50, 0, 0, 1, 1,
|
|
|
|
-50, 50, -50, 0, 0, 1, 1,
|
|
|
|
-50, 50, 50, 0, 0, 1, 1,
|
|
|
|
|
|
|
|
50, -50, -50, 0, 1, 0, 1,
|
|
|
|
50, 50, -50, 0, 1, 0, 1,
|
|
|
|
50, -50, 50, 0, 1, 0, 1,
|
|
|
|
50, -50, 50, 0, 1, 0, 1,
|
|
|
|
50, 50, -50, 0, 1, 0, 1,
|
|
|
|
50, 50, 50, 0, 1, 0, 1,
|
|
|
|
|
|
|
|
-50, -50, -50, 1, 0, 0, 1,
|
|
|
|
50, -50, -50, 1, 0, 0, 1,
|
|
|
|
-50, -50, 50, 1, 0, 0, 1,
|
|
|
|
-50, -50, 50, 1, 0, 0, 1,
|
|
|
|
50, -50, -50, 1, 0, 0, 1,
|
|
|
|
50, -50, 50, 1, 0, 0, 1,
|
|
|
|
|
|
|
|
-50, 50, -50, 0, 1, 1, 1,
|
|
|
|
50, 50, -50, 0, 1, 1, 1,
|
|
|
|
-50, 50, 50, 0, 1, 1, 1,
|
|
|
|
-50, 50, 50, 0, 1, 1, 1,
|
|
|
|
50, 50, -50, 0, 1, 1, 1,
|
|
|
|
50, 50, 50, 0, 1, 1, 1,
|
|
|
|
|
|
|
|
-50, -50, -50, 1, 0, 1, 1,
|
|
|
|
50, -50, -50, 1, 0, 1, 1,
|
|
|
|
-50, 50, -50, 1, 0, 1, 1,
|
|
|
|
-50, 50, -50, 1, 0, 1, 1,
|
|
|
|
50, -50, -50, 1, 0, 1, 1,
|
|
|
|
50, 50, -50, 1, 0, 1, 1,
|
|
|
|
|
|
|
|
-50, -50, 50, 1, 1, 0, 1,
|
|
|
|
50, -50, 50, 1, 1, 0, 1,
|
|
|
|
-50, 50, 50, 1, 1, 0, 1,
|
|
|
|
-50, 50, 50, 1, 1, 0, 1,
|
|
|
|
50, -50, 50, 1, 1, 0, 1,
|
|
|
|
50, 50, 50, 1, 1, 0, 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
// Enable position and color vertex components
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
|
|
|
glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), cube);
|
|
|
|
glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), cube + 3);
|
|
|
|
|
|
|
|
// Disable normal and texture coordinates vertex components
|
|
|
|
glDisableClientState(GL_NORMAL_ARRAY);
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
|
|
|
|
// Create a clock for measuring the time elapsed
|
|
|
|
sf::Clock clock;
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Start the game loop
|
2012-03-12 02:10:37 +08:00
|
|
|
while (window.isOpen())
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
|
|
|
// Process events
|
2009-07-12 06:17:24 +08:00
|
|
|
sf::Event event;
|
2012-03-12 02:10:37 +08:00
|
|
|
while (window.pollEvent(event))
|
2009-01-29 00:18:34 +08:00
|
|
|
{
|
2013-05-21 22:57:02 +08:00
|
|
|
// Close window: exit
|
2012-03-12 02:10:37 +08:00
|
|
|
if (event.type == sf::Event::Closed)
|
|
|
|
window.close();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2013-05-21 22:57:02 +08:00
|
|
|
// Escape key: exit
|
2012-03-12 02:10:37 +08:00
|
|
|
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
|
|
|
|
window.close();
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2013-05-21 22:57:02 +08:00
|
|
|
// Resize event: adjust the viewport
|
2012-03-12 02:10:37 +08:00
|
|
|
if (event.type == sf::Event::Resized)
|
|
|
|
glViewport(0, 0, event.size.width, event.size.height);
|
2013-05-21 22:57:02 +08:00
|
|
|
}
|
2009-10-04 21:00:59 +08:00
|
|
|
|
2013-05-21 22:57:02 +08:00
|
|
|
// Clear the color and depth buffers
|
2009-01-29 00:18:34 +08:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2013-05-21 22:57:02 +08:00
|
|
|
// Apply some transformations to rotate the cube
|
2009-01-29 00:18:34 +08:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
glTranslatef(0.f, 0.f, -200.f);
|
2012-03-12 02:10:37 +08:00
|
|
|
glRotatef(clock.getElapsedTime().asSeconds() * 50, 1.f, 0.f, 0.f);
|
|
|
|
glRotatef(clock.getElapsedTime().asSeconds() * 30, 0.f, 1.f, 0.f);
|
|
|
|
glRotatef(clock.getElapsedTime().asSeconds() * 90, 0.f, 0.f, 1.f);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
2013-05-21 22:57:02 +08:00
|
|
|
// Draw the cube
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
2009-01-29 00:18:34 +08:00
|
|
|
|
|
|
|
// Finally, display the rendered frame on screen
|
2012-03-12 02:10:37 +08:00
|
|
|
window.display();
|
2009-01-29 00:18:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|