2014-09-30 22:07:25 +08:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Window.hpp>
|
2022-07-05 00:20:58 +08:00
|
|
|
|
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
|
|
|
|
|
2022-06-13 00:41:53 +08:00
|
|
|
#include <array>
|
2022-07-05 00:20:58 +08:00
|
|
|
#include <iostream>
|
2021-12-10 01:11:37 +08:00
|
|
|
|
2023-04-24 20:13:52 +08:00
|
|
|
#include <cstdlib>
|
|
|
|
|
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
|
2024-01-02 06:38:14 +08:00
|
|
|
sf::Window window(sf::VideoMode({640, 480}), "SFML window with OpenGL", sf::Style::Default, sf::State::Windowed, contextSettings);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// 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
|
2023-11-27 06:06:37 +08:00
|
|
|
gladLoadGLES1(sf::Context::getFunction);
|
2019-04-13 19:16:32 +08:00
|
|
|
#else
|
2023-11-27 06:06:37 +08:00
|
|
|
gladLoadGL(sf::Context::getFunction);
|
2019-04-13 19:16:32 +08:00
|
|
|
#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();
|
2023-02-15 11:42:12 +08:00
|
|
|
const 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)
|
2022-02-18 03:32:28 +08:00
|
|
|
// clang-format off
|
2022-06-13 00:41:53 +08:00
|
|
|
constexpr std::array<GLfloat, 252> cube =
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
|
|
|
// 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,
|
|
|
|
};
|
2022-02-18 03:32:28 +08:00
|
|
|
// clang-format on
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Enable position and color vertex components
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
2022-06-13 00:41:53 +08:00
|
|
|
glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), cube.data());
|
|
|
|
glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), cube.data() + 3);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Disable normal and texture coordinates vertex components
|
|
|
|
glDisableClientState(GL_NORMAL_ARRAY);
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
|
|
|
|
// Create a clock for measuring the time elapsed
|
2023-02-15 11:42:12 +08:00
|
|
|
const sf::Clock clock;
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Start the game loop
|
|
|
|
while (window.isOpen())
|
|
|
|
{
|
|
|
|
// Process events
|
2024-06-23 18:30:02 +08:00
|
|
|
while (const std::optional event = window.pollEvent())
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2024-06-23 18:30:02 +08:00
|
|
|
// Window closed or escape key pressed: exit
|
|
|
|
if (event->is<sf::Event::Closed>() ||
|
|
|
|
(event->is<sf::Event::KeyPressed>() &&
|
|
|
|
event->getIf<sf::Event::KeyPressed>()->code == sf::Keyboard::Key::Escape))
|
|
|
|
{
|
2014-09-30 22:07:25 +08:00
|
|
|
window.close();
|
2024-06-23 18:30:02 +08:00
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
// Resize event: adjust the viewport
|
2024-06-23 18:30:02 +08:00
|
|
|
if (const auto* resized = event->getIf<sf::Event::Resized>())
|
2019-01-18 23:04:03 +08:00
|
|
|
{
|
2023-11-04 11:47:14 +08:00
|
|
|
const auto [width, height] = resized->size;
|
|
|
|
glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
|
2019-01-18 23:04:03 +08:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
2023-11-04 11:47:14 +08:00
|
|
|
const GLfloat newRatio = static_cast<float>(width) / static_cast<float>(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;
|
|
|
|
}
|