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.

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