|
| 1 | +/******************************************************************************************* |
| 2 | +* |
| 3 | +* raylib [shaders] example - Texture Waves |
| 4 | +* |
| 5 | +* NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, |
| 6 | +* OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. |
| 7 | +* |
| 8 | +* NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example |
| 9 | +* on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders |
| 10 | +* raylib comes with shaders ready for both versions, check raylib/shaders install folder |
| 11 | +* |
| 12 | +* This example has been created using raylib 2.5 (www.raylib.com) |
| 13 | +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) |
| 14 | +* |
| 15 | +* Example contributed by Anata (@anatagawa) and reviewed by Ramon Santamaria (@raysan5) |
| 16 | +* |
| 17 | +* Copyright (c) 2019 Anata (@anatagawa) and Ramon Santamaria (@raysan5) |
| 18 | +* |
| 19 | +********************************************************************************************/ |
| 20 | + |
| 21 | +#include "raylib-cpp.hpp" |
| 22 | + |
| 23 | +#if defined(PLATFORM_DESKTOP) |
| 24 | + #define GLSL_VERSION 330 |
| 25 | +#else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB |
| 26 | + #define GLSL_VERSION 100 |
| 27 | +#endif |
| 28 | + |
| 29 | +int main(void) |
| 30 | +{ |
| 31 | + // Initialization |
| 32 | + //-------------------------------------------------------------------------------------- |
| 33 | + const int screenWidth = 800; |
| 34 | + const int screenHeight = 450; |
| 35 | + |
| 36 | + raylib::Window window(screenWidth, screenHeight, "raylib-cpp [shaders] example - texture waves"); |
| 37 | + |
| 38 | + // Load texture texture to apply shaders |
| 39 | + raylib::Texture2D texture("resources/space.png"); |
| 40 | + |
| 41 | + // Load shader and setup location points and values |
| 42 | + raylib::Shader shader(0, TextFormat("resources/shaders/glsl%i/wave.fs", GLSL_VERSION)); |
| 43 | + |
| 44 | + int secondsLoc = shader.GetLocation("secondes"); |
| 45 | + int freqXLoc = shader.GetLocation("freqX"); |
| 46 | + int freqYLoc = shader.GetLocation("freqY"); |
| 47 | + int ampXLoc = shader.GetLocation("ampX"); |
| 48 | + int ampYLoc = shader.GetLocation("ampY"); |
| 49 | + int speedXLoc = shader.GetLocation("speedX"); |
| 50 | + int speedYLoc = shader.GetLocation("speedY"); |
| 51 | + |
| 52 | + // Shader uniform values that can be updated at any time |
| 53 | + float freqX = 25.0f; |
| 54 | + float freqY = 25.0f; |
| 55 | + float ampX = 5.0f; |
| 56 | + float ampY = 5.0f; |
| 57 | + float speedX = 8.0f; |
| 58 | + float speedY = 8.0f; |
| 59 | + |
| 60 | + float screenSize[2] = { (float)window.GetWidth(), (float)window.GetHeight() }; |
| 61 | + shader.SetValue(shader.GetLocation("size"), &screenSize, SHADER_UNIFORM_VEC2); |
| 62 | + shader.SetValue(freqXLoc, &freqX, SHADER_UNIFORM_FLOAT); |
| 63 | + shader.SetValue(freqYLoc, &freqY, SHADER_UNIFORM_FLOAT); |
| 64 | + shader.SetValue(ampXLoc, &X, SHADER_UNIFORM_FLOAT); |
| 65 | + shader.SetValue(ampYLoc, &Y, SHADER_UNIFORM_FLOAT); |
| 66 | + shader.SetValue(speedXLoc, &speedX, SHADER_UNIFORM_FLOAT); |
| 67 | + shader.SetValue(speedYLoc, &speedY, SHADER_UNIFORM_FLOAT); |
| 68 | + |
| 69 | + float seconds = 0.0f; |
| 70 | + |
| 71 | + window.SetTargetFPS(60); // Set our game to run at 60 frames-per-second |
| 72 | + // ------------------------------------------------------------------------------------------------------------- |
| 73 | + |
| 74 | + // Main game loop |
| 75 | + while (!window.ShouldClose()) // Detect window close button or ESC key |
| 76 | + { |
| 77 | + // Update |
| 78 | + //---------------------------------------------------------------------------------- |
| 79 | + seconds += GetFrameTime(); |
| 80 | + |
| 81 | + shader.SetValue(secondsLoc, &seconds, SHADER_UNIFORM_FLOAT); |
| 82 | + //---------------------------------------------------------------------------------- |
| 83 | + |
| 84 | + // Draw |
| 85 | + //---------------------------------------------------------------------------------- |
| 86 | + BeginDrawing(); |
| 87 | + |
| 88 | + ClearBackground(RAYWHITE); |
| 89 | + |
| 90 | + shader.BeginMode(); |
| 91 | + |
| 92 | + texture.Draw(0, 0, WHITE); |
| 93 | + texture.Draw(texture.GetWidth(), 0, WHITE); |
| 94 | + |
| 95 | + EndShaderMode(); |
| 96 | + |
| 97 | + EndDrawing(); |
| 98 | + //---------------------------------------------------------------------------------- |
| 99 | + } |
| 100 | + |
| 101 | + return 0; |
| 102 | +} |
0 commit comments