shaders_multi_sample2d.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 texRedLoc = GetShaderLocation(shader, "texture1");
  40. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  41. //--------------------------------------------------------------------------------------
  42. // Main game loop
  43. while (!WindowShouldClose()) // Detect window close button or ESC key
  44. {
  45. // Update
  46. //----------------------------------------------------------------------------------
  47. // ...
  48. //----------------------------------------------------------------------------------
  49. // Draw
  50. //----------------------------------------------------------------------------------
  51. BeginDrawing();
  52. ClearBackground(RAYWHITE);
  53. BeginShaderMode(shader);
  54. // WARNING: Additional samplers are enabled for all draw calls in the batch,
  55. // EndShaderMode() forces batch drawing and consequently resets active textures
  56. // to let other sampler2D to be activated on consequent drawings (if required)
  57. SetShaderValueTexture(shader, texRedLoc, texBlue);
  58. // We are drawing texRed using default sampler2D texture0 but
  59. // an additional texture units is enabled for texBlue (sampler2D texture1)
  60. DrawTexture(texRed, 0, 0, WHITE);
  61. EndShaderMode();
  62. EndDrawing();
  63. //----------------------------------------------------------------------------------
  64. }
  65. // De-Initialization
  66. //--------------------------------------------------------------------------------------
  67. UnloadShader(shader); // Unload shader
  68. UnloadTexture(texRed); // Unload texture
  69. UnloadTexture(texBlue); // Unload texture
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }