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.

109 lines
4.6 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Multiple sample2D with default batch system
  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 3.5, last time updated with raylib 3.5
  13. *
  14. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  15. * BSD-like license that allows static linking with closed source software
  16. *
  17. * Copyright (c) 2020-2024 Ramon Santamaria (@raysan5)
  18. *
  19. ********************************************************************************************/
  20. #include "raylib.h"
  21. #if defined(PLATFORM_DESKTOP)
  22. #define GLSL_VERSION 330
  23. #else // PLATFORM_ANDROID, PLATFORM_WEB
  24. #define GLSL_VERSION 100
  25. #endif
  26. //------------------------------------------------------------------------------------
  27. // Program main entry point
  28. //------------------------------------------------------------------------------------
  29. int main(void)
  30. {
  31. // Initialization
  32. //--------------------------------------------------------------------------------------
  33. const int screenWidth = 800;
  34. const int screenHeight = 450;
  35. InitWindow(screenWidth, screenHeight, "raylib - multiple sample2D");
  36. Image imRed = GenImageColor(800, 450, (Color){ 255, 0, 0, 255 });
  37. Texture texRed = LoadTextureFromImage(imRed);
  38. UnloadImage(imRed);
  39. Image imBlue = GenImageColor(800, 450, (Color){ 0, 0, 255, 255 });
  40. Texture texBlue = LoadTextureFromImage(imBlue);
  41. UnloadImage(imBlue);
  42. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/color_mix.fs", GLSL_VERSION));
  43. // Get an additional sampler2D location to be enabled on drawing
  44. int texBlueLoc = GetShaderLocation(shader, "texture1");
  45. // Get shader uniform for divider
  46. int dividerLoc = GetShaderLocation(shader, "divider");
  47. float dividerValue = 0.5f;
  48. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. if (IsKeyDown(KEY_RIGHT)) dividerValue += 0.01f;
  56. else if (IsKeyDown(KEY_LEFT)) dividerValue -= 0.01f;
  57. if (dividerValue < 0.0f) dividerValue = 0.0f;
  58. else if (dividerValue > 1.0f) dividerValue = 1.0f;
  59. SetShaderValue(shader, dividerLoc, &dividerValue, SHADER_UNIFORM_FLOAT);
  60. //----------------------------------------------------------------------------------
  61. // Draw
  62. //----------------------------------------------------------------------------------
  63. BeginDrawing();
  64. ClearBackground(RAYWHITE);
  65. BeginShaderMode(shader);
  66. // WARNING: Additional samplers are enabled for all draw calls in the batch,
  67. // EndShaderMode() forces batch drawing and consequently resets active textures
  68. // to let other sampler2D to be activated on consequent drawings (if required)
  69. SetShaderValueTexture(shader, texBlueLoc, texBlue);
  70. // We are drawing texRed using default sampler2D texture0 but
  71. // an additional texture units is enabled for texBlue (sampler2D texture1)
  72. DrawTexture(texRed, 0, 0, WHITE);
  73. EndShaderMode();
  74. DrawText("Use KEY_LEFT/KEY_RIGHT to move texture mixing in shader!", 80, GetScreenHeight() - 40, 20, RAYWHITE);
  75. EndDrawing();
  76. //----------------------------------------------------------------------------------
  77. }
  78. // De-Initialization
  79. //--------------------------------------------------------------------------------------
  80. UnloadShader(shader); // Unload shader
  81. UnloadTexture(texRed); // Unload texture
  82. UnloadTexture(texBlue); // Unload texture
  83. CloseWindow(); // Close window and OpenGL context
  84. //--------------------------------------------------------------------------------------
  85. return 0;
  86. }