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.

97 lines
4.4 KiB

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