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.

94 lines
4.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Sieve of Eratosthenes
  4. *
  5. * 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. * This example has been created using raylib 2.5 (www.raylib.com)
  18. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  19. *
  20. * Example contributed by ProfJski and reviewed by Ramon Santamaria (@raysan5)
  21. *
  22. * Copyright (c) 2019 ProfJski and Ramon Santamaria (@raysan5)
  23. *
  24. ********************************************************************************************/
  25. #include "raylib.h"
  26. #if defined(PLATFORM_DESKTOP)
  27. #define GLSL_VERSION 330
  28. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  29. #define GLSL_VERSION 100
  30. #endif
  31. int main(void)
  32. {
  33. // Initialization
  34. //--------------------------------------------------------------------------------------
  35. const int screenWidth = 800;
  36. const int screenHeight = 450;
  37. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - Sieve of Eratosthenes");
  38. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  39. // Load Eratosthenes shader
  40. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  41. Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/eratosthenes.fs", GLSL_VERSION));
  42. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  43. //--------------------------------------------------------------------------------------
  44. // Main game loop
  45. while (!WindowShouldClose()) // Detect window close button or ESC key
  46. {
  47. // Update
  48. //----------------------------------------------------------------------------------
  49. // Nothing to do here, everything is happening in the shader
  50. //----------------------------------------------------------------------------------
  51. // Draw
  52. //----------------------------------------------------------------------------------
  53. BeginDrawing();
  54. ClearBackground(RAYWHITE);
  55. BeginTextureMode(target); // Enable drawing to texture
  56. ClearBackground(BLACK); // Clear the render texture
  57. // Draw a rectangle in shader mode to be used as shader canvas
  58. // NOTE: Rectangle uses font white character texture coordinates,
  59. // so shader can not be applied here directly because input vertexTexCoord
  60. // do not represent full screen coordinates (space where want to apply shader)
  61. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK);
  62. EndTextureMode(); // End drawing to texture (now we have a blank texture available for the shader)
  63. BeginShaderMode(shader);
  64. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  65. DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0.0f, 0.0f }, WHITE);
  66. EndShaderMode();
  67. EndDrawing();
  68. //----------------------------------------------------------------------------------
  69. }
  70. // De-Initialization
  71. //--------------------------------------------------------------------------------------
  72. UnloadShader(shader); // Unload shader
  73. UnloadRenderTexture(target); // Unload texture
  74. CloseWindow(); // Close window and OpenGL context
  75. //--------------------------------------------------------------------------------------
  76. return 0;
  77. }