shaders_texture_waves.c 4.7 KB

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