2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Window.hpp>
|
2021-12-10 01:11:37 +08:00
|
|
|
#include <cstdlib>
|
2019-04-13 19:16:32 +08:00
|
|
|
|
|
|
|
#define GLAD_GL_IMPLEMENTATION
|
2021-04-20 21:59:24 +08:00
|
|
|
#include <gl.h>
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2018-04-12 03:45:36 +08:00
|
|
|
#ifdef SFML_SYSTEM_IOS
|
|
|
|
#include <SFML/Main.hpp>
|
|
|
|
#endif
|
|
|
|
|
2021-12-10 01:11:37 +08:00
|
|
|
#include <iostream>
|
2021-12-10 03:55:33 +08:00
|
|
|
#include <cstdlib>
|
2021-12-10 01:11:37 +08:00
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
2015-01-13 01:00:44 +08:00
|
|
|
// Request a 24-bits depth buffer when creating the window
|
2014-09-30 22:07:25 +08:00
|
|
|
sf::ContextSettings contextSettings;
|
2015-01-13 01:00:44 +08:00
|
|
|
contextSettings.depthBits = 24;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Create the main window
|
|
|
|
sf::Window window(sf::VideoMode(640, 480), "SFML window with OpenGL", sf::Style::Default, contextSettings);
|
|
|
|
|
|
|
|
// Make it the active window for OpenGL calls
|
2021-12-10 01:11:37 +08:00
|
|
|
if (!window.setActive())
|
|
|
|
{
|
|
|
|
std::cerr << "Failed to set the window as active" << std::endl;
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2019-04-13 19:16:32 +08:00
|
|
|
// Load OpenGL or OpenGL ES entry points using glad
|
|
|
|
#ifdef SFML_OPENGL_ES
|
|
|
|
gladLoadGLES1(reinterpret_cast<GLADloadfunc>(sf::Context::getFunction));
|
|
|
|
#else
|
|
|
|
gladLoadGL(reinterpret_cast<GLADloadfunc>(sf::Context::getFunction));
|
|
|
|
#endif
|
|
|
|
|
2014-09-30 22:07:25 +08:00
|
|
|
// Set the color and depth clear values
|
2019-04-13 19:16:32 +08:00
|
|
|
#ifdef SFML_OPENGL_ES
|
|
|
|
glClearDepthf(1.f);
|
|
|
|
#else
|
2014-09-30 22:07:25 +08:00
|
|
|
glClearDepth(1.f);
|
2019-04-13 19:16:32 +08:00
|
|
|
#endif
|
2014-09-30 22:07:25 +08:00
|
|
|
glClearColor(0.f, 0.f, 0.f, 1.f);
|
|
|
|
|
|
|
|
// Enable Z-buffer read and write
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
|
|
|
|
// Disable lighting and texturing
|
|
|
|
glDisable(GL_LIGHTING);
|
|
|
|
glDisable(GL_TEXTURE_2D);
|
|
|
|
|
|
|
|
// Configure the viewport (the same size as the window)
|
2021-04-20 21:59:24 +08:00
|
|
|
glViewport(0, 0, static_cast<GLsizei>(window.getSize().x), static_cast<GLsizei>(window.getSize().y));
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Setup a perspective projection
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2021-04-20 21:59:24 +08:00
|
|
|
GLfloat ratio = static_cast<float>(window.getSize().x) / static_cast<float>(window.getSize().y);
|
2019-04-13 19:16:32 +08:00
|
|
|
#ifdef SFML_OPENGL_ES
|
|
|
|
glFrustumf(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
|
|
|
|
#else
|
2014-09-30 22:07:25 +08:00
|
|
|
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
|
2019-04-13 19:16:32 +08:00
|
|
|
#endif
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
|
|
|
|
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;
|
|
|
|
|
|
|
|
// Start the game loop
|
|
|
|
while (window.isOpen())
|
|
|
|
{
|
|
|
|
// Process events
|
|
|
|
sf::Event event;
|
|
|
|
while (window.pollEvent(event))
|
|
|
|
{
|
|
|
|
// Close window: exit
|
|
|
|
if (event.type == sf::Event::Closed)
|
|
|
|
window.close();
|
|
|
|
|
|
|
|
// Escape key: exit
|
|
|
|
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
|
|
|
|
window.close();
|
|
|
|
|
|
|
|
// Resize event: adjust the viewport
|
|
|
|
if (event.type == sf::Event::Resized)
|
2019-01-18 23:04:03 +08:00
|
|
|
{
|
2021-04-20 21:59:24 +08:00
|
|
|
glViewport(0, 0, static_cast<GLsizei>(event.size.width), static_cast<GLsizei>(event.size.height));
|
2019-01-18 23:04:03 +08:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2021-04-20 21:59:24 +08:00
|
|
|
GLfloat newRatio = static_cast<float>(event.size.width) / static_cast<float>(event.size.height);
|
2019-04-13 19:16:32 +08:00
|
|
|
#ifdef SFML_OPENGL_ES
|
2021-04-20 21:59:24 +08:00
|
|
|
glFrustumf(-newRatio, newRatio, -1.f, 1.f, 1.f, 500.f);
|
2019-04-13 19:16:32 +08:00
|
|
|
#else
|
2021-04-20 21:59:24 +08:00
|
|
|
glFrustum(-newRatio, newRatio, -1.f, 1.f, 1.f, 500.f);
|
2019-04-13 19:16:32 +08:00
|
|
|
#endif
|
2019-01-18 23:04:03 +08:00
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the color and depth buffers
|
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
|
|
|
// Apply some transformations to rotate the cube
|
|
|
|
glMatrixMode(GL_MODELVIEW);
|
|
|
|
glLoadIdentity();
|
|
|
|
glTranslatef(0.f, 0.f, -200.f);
|
|
|
|
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);
|
|
|
|
|
|
|
|
// Draw the cube
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, 36);
|
|
|
|
|
|
|
|
// Finally, display the rendered frame on screen
|
|
|
|
window.display();
|
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|