1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
////////////////////////////////////////////////////////////
// Headers
////////////////////////////////////////////////////////////
#include <SFML/Graphics.hpp>
#include <array>
#include <filesystem>
#include <iostream>
#include <cstdlib>
#define GLAD_GL_IMPLEMENTATION
#include <gl.h>
#ifdef SFML_SYSTEM_IOS
#include <SFML/Main.hpp>
#endif
#ifndef GL_SRGB8_ALPHA8
#define GL_SRGB8_ALPHA8 0x8C43
#endif
namespace
{
std::filesystem::path resourcesDir()
{
#ifdef SFML_SYSTEM_IOS
return "";
#else
return "resources";
#endif
}
} // namespace
////////////////////////////////////////////////////////////
/// Entry point of application
///
/// \return Application exit code
///
////////////////////////////////////////////////////////////
int main()
{
bool exit = false;
bool sRgb = false;
while (!exit)
{
// Request a 24-bits depth buffer when creating the window
sf::ContextSettings contextSettings;
contextSettings.depthBits = 24;
contextSettings.sRgbCapable = sRgb;
// Create the main window
sf::RenderWindow window(sf::VideoMode({800, 600}),
"SFML graphics with OpenGL",
sf::Style::Default,
sf::State::Windowed,
contextSettings);
window.setVerticalSyncEnabled(true);
window.setMinimumSize(sf::Vector2u(400, 300));
window.setMaximumSize(sf::Vector2u(1200, 900));
// Create a sprite for the background
const sf::Texture backgroundTexture(resourcesDir() / "background.jpg", sRgb);
const sf::Sprite background(backgroundTexture);
// Create some text to draw on top of our OpenGL object
const sf::Font font(resourcesDir() / "tuffy.ttf");
sf::Text text(font, "SFML / OpenGL demo");
sf::Text sRgbInstructions(font, "Press space to toggle sRGB conversion");
sf::Text mipmapInstructions(font, "Press return to toggle mipmapping");
text.setFillColor(sf::Color(255, 255, 255, 170));
sRgbInstructions.setFillColor(sf::Color(255, 255, 255, 170));
mipmapInstructions.setFillColor(sf::Color(255, 255, 255, 170));
text.setPosition({280.f, 450.f});
sRgbInstructions.setPosition({175.f, 500.f});
mipmapInstructions.setPosition({200.f, 550.f});
// Load a texture to apply to our 3D cube
sf::Texture texture(resourcesDir() / "logo.png");
// Attempt to generate a mipmap for our cube texture
// We don't check the return value here since
// mipmapping is purely optional in this example
(void)texture.generateMipmap();
// Make the window the active window for OpenGL calls
if (!window.setActive(true))
{
std::cerr << "Failed to set window to active" << std::endl;
return EXIT_FAILURE;
}
// Load OpenGL or OpenGL ES entry points using glad
#ifdef SFML_OPENGL_ES
gladLoadGLES1(sf::Context::getFunction);
#else
gladLoadGL(sf::Context::getFunction);
#endif
// Enable Z-buffer read and write
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
#ifdef SFML_OPENGL_ES
glClearDepthf(1.f);
#else
glClearDepth(1.f);
#endif
// Disable lighting
glDisable(GL_LIGHTING);
// Configure the viewport (the same size as the window)
glViewport(0, 0, static_cast<GLsizei>(window.getSize().x), static_cast<GLsizei>(window.getSize().y));
// Setup a perspective projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const GLfloat ratio = static_cast<float>(window.getSize().x) / static_cast<float>(window.getSize().y);
#ifdef SFML_OPENGL_ES
glFrustumf(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
#else
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
#endif
// Bind the texture
glEnable(GL_TEXTURE_2D);
sf::Texture::bind(&texture);
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
// clang-format off
constexpr std::array<GLfloat, 180> cube =
{
// positions // texture coordinates
-20, -20, -20, 0, 0,
-20, 20, -20, 1, 0,
-20, -20, 20, 0, 1,
-20, -20, 20, 0, 1,
-20, 20, -20, 1, 0,
-20, 20, 20, 1, 1,
20, -20, -20, 0, 0,
20, 20, -20, 1, 0,
20, -20, 20, 0, 1,
20, -20, 20, 0, 1,
20, 20, -20, 1, 0,
20, 20, 20, 1, 1,
-20, -20, -20, 0, 0,
20, -20, -20, 1, 0,
-20, -20, 20, 0, 1,
-20, -20, 20, 0, 1,
20, -20, -20, 1, 0,
20, -20, 20, 1, 1,
-20, 20, -20, 0, 0,
20, 20, -20, 1, 0,
-20, 20, 20, 0, 1,
-20, 20, 20, 0, 1,
20, 20, -20, 1, 0,
20, 20, 20, 1, 1,
-20, -20, -20, 0, 0,
20, -20, -20, 1, 0,
-20, 20, -20, 0, 1,
-20, 20, -20, 0, 1,
20, -20, -20, 1, 0,
20, 20, -20, 1, 1,
-20, -20, 20, 0, 0,
20, -20, 20, 1, 0,
-20, 20, 20, 0, 1,
-20, 20, 20, 0, 1,
20, -20, 20, 1, 0,
20, 20, 20, 1, 1
};
// clang-format on
// Enable position and texture coordinates vertex components
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), cube.data());
glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), cube.data() + 3);
// Disable normal and color vertex components
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
// Make the window no longer the active window for OpenGL calls
if (!window.setActive(false))
{
std::cerr << "Failed to set window to inactive" << std::endl;
return EXIT_FAILURE;
}
// Create a clock for measuring the time elapsed
const sf::Clock clock;
// Flag to track whether mipmapping is currently enabled
bool mipmapEnabled = true;
// Start game loop
while (window.isOpen())
{
// Process events
while (const std::optional event = window.pollEvent())
{
// 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))
{
exit = true;
window.close();
}
// Return key: toggle mipmapping
if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>();
keyPressed && keyPressed->code == sf::Keyboard::Key::Enter)
{
if (mipmapEnabled)
{
// We simply reload the texture to disable mipmapping
texture = sf::Texture(resourcesDir() / "logo.png");
// Rebind the texture
sf::Texture::bind(&texture);
mipmapEnabled = false;
}
else if (texture.generateMipmap())
{
mipmapEnabled = true;
}
}
// Space key: toggle sRGB conversion
if (const auto* keyPressed = event->getIf<sf::Event::KeyPressed>();
keyPressed && keyPressed->code == sf::Keyboard::Key::Space)
{
sRgb = !sRgb;
window.close();
}
// Adjust the viewport when the window is resized
if (const auto* resized = event->getIf<sf::Event::Resized>())
{
const sf::Vector2u textureSize = backgroundTexture.getSize();
// Make the window the active window for OpenGL calls
if (!window.setActive(true))
{
std::cerr << "Failed to set window to active" << std::endl;
return EXIT_FAILURE;
}
const auto [width, height] = resized->size;
glViewport(0, 0, static_cast<GLsizei>(width), static_cast<GLsizei>(height));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const GLfloat newRatio = static_cast<float>(width) / static_cast<float>(height);
#ifdef SFML_OPENGL_ES
glFrustumf(-newRatio, newRatio, -1.f, 1.f, 1.f, 500.f);
#else
glFrustum(-newRatio, newRatio, -1.f, 1.f, 1.f, 500.f);
#endif
// Make the window no longer the active window for OpenGL calls
if (!window.setActive(false))
{
std::cerr << "Failed to set window to inactive" << std::endl;
return EXIT_FAILURE;
}
sf::View view;
view.setSize(sf::Vector2f(textureSize));
view.setCenter(sf::Vector2f(textureSize) / 2.f);
window.setView(view);
}
}
// Draw the background
window.pushGLStates();
window.draw(background);
window.popGLStates();
// Make the window the active window for OpenGL calls
if (!window.setActive(true))
{
// On failure, try re-creating the window, as it is intentionally
// closed when changing color space.
continue;
}
// Clear the depth buffer
glClear(GL_DEPTH_BUFFER_BIT);
// We get the position of the mouse cursor (or touch), so that we can move the box accordingly
sf::Vector2i pos;
#ifdef SFML_SYSTEM_IOS
pos = sf::Touch::getPosition(0);
#else
pos = sf::Mouse::getPosition(window);
#endif
const float x = static_cast<float>(pos.x) * 200.f / static_cast<float>(window.getSize().x) - 100.f;
const float y = -static_cast<float>(pos.y) * 200.f / static_cast<float>(window.getSize().y) + 100.f;
// Apply some transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(x, y, -100.f);
glRotatef(clock.getElapsedTime().asSeconds() * 50.f, 1.f, 0.f, 0.f);
glRotatef(clock.getElapsedTime().asSeconds() * 30.f, 0.f, 1.f, 0.f);
glRotatef(clock.getElapsedTime().asSeconds() * 90.f, 0.f, 0.f, 1.f);
// Draw the cube
glDrawArrays(GL_TRIANGLES, 0, 36);
// Make the window no longer the active window for OpenGL calls
if (!window.setActive(false))
{
std::cerr << "Failed to set window to inactive" << std::endl;
return EXIT_FAILURE;
}
// Draw some text on top of our OpenGL object
window.pushGLStates();
window.draw(text);
window.draw(sRgbInstructions);
window.draw(mipmapInstructions);
window.popGLStates();
// Finally, display the rendered frame on screen
window.display();
}
}
return EXIT_SUCCESS;
}
|