2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
// Headers
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
#include <SFML/Graphics.hpp>
|
2019-04-13 19:16:32 +08:00
|
|
|
|
|
|
|
#define GLAD_GL_IMPLEMENTATION
|
|
|
|
#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
|
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
#ifndef GL_SRGB8_ALPHA8
|
|
|
|
#define GL_SRGB8_ALPHA8 0x8C43
|
|
|
|
#endif
|
|
|
|
|
2018-04-13 02:39:55 +08:00
|
|
|
std::string resourcesDir()
|
|
|
|
{
|
|
|
|
#ifdef SFML_SYSTEM_IOS
|
|
|
|
return "";
|
|
|
|
#else
|
|
|
|
return "resources/";
|
|
|
|
#endif
|
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// Entry point of application
|
|
|
|
///
|
|
|
|
/// \return Application exit code
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
int main()
|
|
|
|
{
|
2015-10-01 16:13:16 +08:00
|
|
|
bool exit = false;
|
|
|
|
bool sRgb = false;
|
|
|
|
|
2015-09-23 23:05:39 +08:00
|
|
|
while (!exit)
|
2014-09-30 22:07:25 +08:00
|
|
|
{
|
2015-10-01 16:13:16 +08:00
|
|
|
// 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, contextSettings);
|
|
|
|
window.setVerticalSyncEnabled(true);
|
|
|
|
|
|
|
|
// Create a sprite for the background
|
|
|
|
sf::Texture backgroundTexture;
|
|
|
|
backgroundTexture.setSrgb(sRgb);
|
2018-04-13 02:39:55 +08:00
|
|
|
if (!backgroundTexture.loadFromFile(resourcesDir() + "background.jpg"))
|
2014-09-30 22:07:25 +08:00
|
|
|
return EXIT_FAILURE;
|
2015-10-01 16:13:16 +08:00
|
|
|
sf::Sprite background(backgroundTexture);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Create some text to draw on top of our OpenGL object
|
|
|
|
sf::Font font;
|
2020-12-13 04:40:52 +08:00
|
|
|
if (!font.loadFromFile(resourcesDir() + "tuffy.ttf"))
|
2015-10-01 16:13:16 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
sf::Text text("SFML / OpenGL demo", font);
|
2015-09-23 23:05:39 +08:00
|
|
|
sf::Text sRgbInstructions("Press space to toggle sRGB conversion", font);
|
|
|
|
sf::Text mipmapInstructions("Press return to toggle mipmapping", font);
|
2015-11-22 01:44:38 +08:00
|
|
|
text.setFillColor(sf::Color(255, 255, 255, 170));
|
2015-09-23 23:05:39 +08:00
|
|
|
sRgbInstructions.setFillColor(sf::Color(255, 255, 255, 170));
|
|
|
|
mipmapInstructions.setFillColor(sf::Color(255, 255, 255, 170));
|
2020-12-13 04:40:52 +08:00
|
|
|
text.setPosition(280.f, 450.f);
|
|
|
|
sRgbInstructions.setPosition(175.f, 500.f);
|
|
|
|
mipmapInstructions.setPosition(200.f, 550.f);
|
2015-10-01 16:13:16 +08:00
|
|
|
|
2015-09-23 23:05:39 +08:00
|
|
|
// Load a texture to apply to our 3D cube
|
|
|
|
sf::Texture texture;
|
2020-12-13 04:40:52 +08:00
|
|
|
if (!texture.loadFromFile(resourcesDir() + "logo.png"))
|
2015-09-23 23:05:39 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
texture.generateMipmap();
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2016-10-07 05:44:56 +08:00
|
|
|
// Make the window the active window for OpenGL calls
|
|
|
|
window.setActive(true);
|
|
|
|
|
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
|
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Enable Z-buffer read and write
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthMask(GL_TRUE);
|
2019-04-13 19:16:32 +08:00
|
|
|
#ifdef SFML_OPENGL_ES
|
|
|
|
glClearDepthf(1.f);
|
|
|
|
#else
|
2015-10-01 16:13:16 +08:00
|
|
|
glClearDepth(1.f);
|
2019-04-13 19:16:32 +08:00
|
|
|
#endif
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Disable lighting
|
|
|
|
glDisable(GL_LIGHTING);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Configure the viewport (the same size as the window)
|
|
|
|
glViewport(0, 0, window.getSize().x, window.getSize().y);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Setup a perspective projection
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
2014-09-30 22:07:25 +08:00
|
|
|
glLoadIdentity();
|
2015-10-01 16:13:16 +08:00
|
|
|
GLfloat ratio = static_cast<float>(window.getSize().x) / 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
|
2015-10-01 16:13:16 +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
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Bind the texture
|
|
|
|
glEnable(GL_TEXTURE_2D);
|
2015-09-23 23:05:39 +08:00
|
|
|
sf::Texture::bind(&texture);
|
2014-09-30 22:07:25 +08:00
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices)
|
|
|
|
static const GLfloat 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
|
|
|
|
};
|
|
|
|
|
|
|
|
// Enable position and texture coordinates vertex components
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), cube);
|
|
|
|
glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), cube + 3);
|
|
|
|
|
|
|
|
// Disable normal and color vertex components
|
|
|
|
glDisableClientState(GL_NORMAL_ARRAY);
|
|
|
|
glDisableClientState(GL_COLOR_ARRAY);
|
|
|
|
|
2016-10-07 05:44:56 +08:00
|
|
|
// Make the window no longer the active window for OpenGL calls
|
|
|
|
window.setActive(false);
|
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Create a clock for measuring the time elapsed
|
|
|
|
sf::Clock clock;
|
|
|
|
|
2015-09-23 23:05:39 +08:00
|
|
|
// Flag to track whether mipmapping is currently enabled
|
|
|
|
bool mipmapEnabled = true;
|
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Start game loop
|
|
|
|
while (window.isOpen())
|
|
|
|
{
|
|
|
|
// Process events
|
|
|
|
sf::Event event;
|
|
|
|
while (window.pollEvent(event))
|
|
|
|
{
|
|
|
|
// Close window: exit
|
|
|
|
if (event.type == sf::Event::Closed)
|
|
|
|
{
|
|
|
|
exit = true;
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Escape key: exit
|
|
|
|
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
|
|
|
|
{
|
|
|
|
exit = true;
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
|
2015-09-23 23:05:39 +08:00
|
|
|
// Return key: toggle mipmapping
|
2018-03-21 14:16:13 +08:00
|
|
|
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Enter))
|
2015-09-23 23:05:39 +08:00
|
|
|
{
|
|
|
|
if (mipmapEnabled)
|
|
|
|
{
|
|
|
|
// We simply reload the texture to disable mipmapping
|
2020-12-13 04:40:52 +08:00
|
|
|
if (!texture.loadFromFile(resourcesDir() + "logo.png"))
|
2015-09-23 23:05:39 +08:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
|
|
|
mipmapEnabled = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
texture.generateMipmap();
|
|
|
|
|
|
|
|
mipmapEnabled = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Space key: toggle sRGB conversion
|
|
|
|
if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Space))
|
|
|
|
{
|
|
|
|
sRgb = !sRgb;
|
|
|
|
window.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust the viewport when the window is resized
|
|
|
|
if (event.type == sf::Event::Resized)
|
2016-10-07 05:44:56 +08:00
|
|
|
{
|
2019-01-18 23:04:03 +08:00
|
|
|
sf::Vector2u textureSize = backgroundTexture.getSize();
|
2019-04-13 19:16:32 +08:00
|
|
|
|
2016-10-07 05:44:56 +08:00
|
|
|
// Make the window the active window for OpenGL calls
|
|
|
|
window.setActive(true);
|
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
glViewport(0, 0, event.size.width, event.size.height);
|
2019-01-18 23:04:03 +08:00
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
GLfloat ratio = static_cast<float>(event.size.width) / event.size.height;
|
2019-04-13 19:16:32 +08:00
|
|
|
#ifdef SFML_OPENGL_ES
|
|
|
|
glFrustumf(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
|
|
|
|
#else
|
2019-01-18 23:04:03 +08:00
|
|
|
glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f);
|
2019-04-13 19:16:32 +08:00
|
|
|
#endif
|
2016-10-07 05:44:56 +08:00
|
|
|
|
|
|
|
// Make the window no longer the active window for OpenGL calls
|
|
|
|
window.setActive(false);
|
2019-04-13 19:16:32 +08:00
|
|
|
|
2019-01-18 23:04:03 +08:00
|
|
|
sf::View view;
|
|
|
|
view.setSize(textureSize.x, textureSize.y);
|
|
|
|
view.setCenter(textureSize.x/2.f, textureSize.y/2.f);
|
|
|
|
window.setView(view);
|
2016-10-07 05:44:56 +08:00
|
|
|
}
|
2015-10-01 16:13:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the background
|
|
|
|
window.pushGLStates();
|
|
|
|
window.draw(background);
|
|
|
|
window.popGLStates();
|
|
|
|
|
2016-10-07 05:44:56 +08:00
|
|
|
// Make the window the active window for OpenGL calls
|
|
|
|
window.setActive(true);
|
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Clear the depth buffer
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2019-01-18 23:04:03 +08:00
|
|
|
// We get the position of the mouse cursor (or touch), so that we can move the box accordingly
|
|
|
|
sf::Vector2i pos;
|
2019-04-13 19:16:32 +08:00
|
|
|
|
2019-01-18 23:04:03 +08:00
|
|
|
#ifdef SFML_SYSTEM_IOS
|
|
|
|
pos = sf::Touch::getPosition(0);
|
|
|
|
#else
|
2020-12-13 04:40:52 +08:00
|
|
|
pos = sf::Mouse::getPosition(window);
|
2019-01-18 23:04:03 +08:00
|
|
|
#endif
|
2019-04-13 19:16:32 +08:00
|
|
|
|
2019-01-18 23:04:03 +08:00
|
|
|
float x = pos.x * 200.f / window.getSize().x - 100.f;
|
|
|
|
float y = -pos.y * 200.f / window.getSize().y + 100.f;
|
2015-10-01 16:13:16 +08:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
2016-10-07 05:44:56 +08:00
|
|
|
// Make the window no longer the active window for OpenGL calls
|
|
|
|
window.setActive(false);
|
|
|
|
|
2015-10-01 16:13:16 +08:00
|
|
|
// Draw some text on top of our OpenGL object
|
|
|
|
window.pushGLStates();
|
|
|
|
window.draw(text);
|
2015-09-23 23:05:39 +08:00
|
|
|
window.draw(sRgbInstructions);
|
|
|
|
window.draw(mipmapInstructions);
|
2015-10-01 16:13:16 +08:00
|
|
|
window.popGLStates();
|
|
|
|
|
|
|
|
// Finally, display the rendered frame on screen
|
|
|
|
window.display();
|
|
|
|
}
|
2014-09-30 22:07:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|