shaders_multi_sample2d.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Multiple sample2D with default batch system
  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 3.5, last time updated with raylib 3.5
  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) 2020-2025 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 - multiple sample2D");
  38. Image imRed = GenImageColor(800, 450, (Color){ 255, 0, 0, 255 });
  39. Texture texRed = LoadTextureFromImage(imRed);
  40. UnloadImage(imRed);
  41. Image imBlue = GenImageColor(800, 450, (Color){ 0, 0, 255, 255 });
  42. Texture texBlue = LoadTextureFromImage(imBlue);
  43. UnloadImage(imBlue);
  44. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/color_mix.fs", GLSL_VERSION));
  45. // Get an additional sampler2D location to be enabled on drawing
  46. int texBlueLoc = GetShaderLocation(shader, "texture1");
  47. // Get shader uniform for divider
  48. int dividerLoc = GetShaderLocation(shader, "divider");
  49. float dividerValue = 0.5f;
  50. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. if (IsKeyDown(KEY_RIGHT)) dividerValue += 0.01f;
  58. else if (IsKeyDown(KEY_LEFT)) dividerValue -= 0.01f;
  59. if (dividerValue < 0.0f) dividerValue = 0.0f;
  60. else if (dividerValue > 1.0f) dividerValue = 1.0f;
  61. SetShaderValue(shader, dividerLoc, &dividerValue, SHADER_UNIFORM_FLOAT);
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. BeginDrawing();
  66. ClearBackground(RAYWHITE);
  67. BeginShaderMode(shader);
  68. // WARNING: Additional samplers are enabled for all draw calls in the batch,
  69. // EndShaderMode() forces batch drawing and consequently resets active textures
  70. // to let other sampler2D to be activated on consequent drawings (if required)
  71. SetShaderValueTexture(shader, texBlueLoc, texBlue);
  72. // We are drawing texRed using default sampler2D texture0 but
  73. // an additional texture units is enabled for texBlue (sampler2D texture1)
  74. DrawTexture(texRed, 0, 0, WHITE);
  75. EndShaderMode();
  76. DrawText("Use KEY_LEFT/KEY_RIGHT to move texture mixing in shader!", 80, GetScreenHeight() - 40, 20, RAYWHITE);
  77. EndDrawing();
  78. //----------------------------------------------------------------------------------
  79. }
  80. // De-Initialization
  81. //--------------------------------------------------------------------------------------
  82. UnloadShader(shader); // Unload shader
  83. UnloadTexture(texRed); // Unload texture
  84. UnloadTexture(texBlue); // Unload texture
  85. CloseWindow(); // Close window and OpenGL context
  86. //--------------------------------------------------------------------------------------
  87. return 0;
  88. }