shaders_eratosthenes_sieve.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - eratosthenes sieve
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * NOTE: Sieve of Eratosthenes, the earliest known (ancient Greek) prime number sieve
  8. *
  9. * "Sift the twos and sift the threes,
  10. * The Sieve of Eratosthenes.
  11. * When the multiples sublime,
  12. * the numbers that are left are prime."
  13. *
  14. * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  15. * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
  16. *
  17. * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
  18. *
  19. * Example originally created with raylib 2.5, last time updated with raylib 4.0
  20. *
  21. * Example contributed by ProfJski (@ProfJski) and reviewed by Ramon Santamaria (@raysan5)
  22. *
  23. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  24. * BSD-like license that allows static linking with closed source software
  25. *
  26. * Copyright (c) 2019-2025 ProfJski (@ProfJski) and Ramon Santamaria (@raysan5)
  27. *
  28. ********************************************************************************************/
  29. #include "raylib.h"
  30. #if defined(PLATFORM_DESKTOP)
  31. #define GLSL_VERSION 330
  32. #else // PLATFORM_ANDROID, PLATFORM_WEB
  33. #define GLSL_VERSION 100
  34. #endif
  35. //------------------------------------------------------------------------------------
  36. // Program main entry point
  37. //------------------------------------------------------------------------------------
  38. int main(void)
  39. {
  40. // Initialization
  41. //--------------------------------------------------------------------------------------
  42. const int screenWidth = 800;
  43. const int screenHeight = 450;
  44. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - eratosthenes sieve");
  45. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  46. // Load Eratosthenes shader
  47. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  48. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/eratosthenes.fs", GLSL_VERSION));
  49. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. // Nothing to do here, everything is happening in the shader
  57. //----------------------------------------------------------------------------------
  58. // Draw
  59. //----------------------------------------------------------------------------------
  60. BeginTextureMode(target); // Enable drawing to texture
  61. ClearBackground(BLACK); // Clear the render texture
  62. // Draw a rectangle in shader mode to be used as shader canvas
  63. // NOTE: Rectangle uses font white character texture coordinates,
  64. // so shader can not be applied here directly because input vertexTexCoord
  65. // do not represent full screen coordinates (space where want to apply shader)
  66. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK);
  67. EndTextureMode(); // End drawing to texture (now we have a blank texture available for the shader)
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE); // Clear screen background
  70. BeginShaderMode(shader);
  71. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  72. DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE);
  73. EndShaderMode();
  74. EndDrawing();
  75. //----------------------------------------------------------------------------------
  76. }
  77. // De-Initialization
  78. //--------------------------------------------------------------------------------------
  79. UnloadShader(shader); // Unload shader
  80. UnloadRenderTexture(target); // Unload render texture
  81. CloseWindow(); // Close window and OpenGL context
  82. //--------------------------------------------------------------------------------------
  83. return 0;
  84. }