You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.7 KiB

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