shaders_postprocessing.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Apply a postprocessing shader to a scene
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  8. * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
  9. *
  10. * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3), to test this example
  11. * on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders
  12. * raylib comes with shaders ready for both versions, check raylib/shaders install folder
  13. *
  14. * Example originally created with raylib 1.3, last time updated with raylib 4.0
  15. *
  16. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  17. * BSD-like license that allows static linking with closed source software
  18. *
  19. * Copyright (c) 2015-2025 Ramon Santamaria (@raysan5)
  20. *
  21. ********************************************************************************************/
  22. #include "raylib.h"
  23. #if defined(PLATFORM_DESKTOP)
  24. #define GLSL_VERSION 330
  25. #else // PLATFORM_ANDROID, PLATFORM_WEB
  26. #define GLSL_VERSION 100
  27. #endif
  28. #define MAX_POSTPRO_SHADERS 12
  29. typedef enum {
  30. FX_GRAYSCALE = 0,
  31. FX_POSTERIZATION,
  32. FX_DREAM_VISION,
  33. FX_PIXELIZER,
  34. FX_CROSS_HATCHING,
  35. FX_CROSS_STITCHING,
  36. FX_PREDATOR_VIEW,
  37. FX_SCANLINES,
  38. FX_FISHEYE,
  39. FX_SOBEL,
  40. FX_BLOOM,
  41. FX_BLUR,
  42. //FX_FXAA
  43. } PostproShader;
  44. static const char *postproShaderText[] = {
  45. "GRAYSCALE",
  46. "POSTERIZATION",
  47. "DREAM_VISION",
  48. "PIXELIZER",
  49. "CROSS_HATCHING",
  50. "CROSS_STITCHING",
  51. "PREDATOR_VIEW",
  52. "SCANLINES",
  53. "FISHEYE",
  54. "SOBEL",
  55. "BLOOM",
  56. "BLUR",
  57. //"FXAA"
  58. };
  59. //------------------------------------------------------------------------------------
  60. // Program main entry point
  61. //------------------------------------------------------------------------------------
  62. int main(void)
  63. {
  64. // Initialization
  65. //--------------------------------------------------------------------------------------
  66. const int screenWidth = 800;
  67. const int screenHeight = 450;
  68. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  69. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader");
  70. // Define the camera to look into our 3d world
  71. Camera camera = { 0 };
  72. camera.position = (Vector3){ 2.0f, 3.0f, 2.0f }; // Camera position
  73. camera.target = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera looking at point
  74. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  75. camera.fovy = 45.0f; // Camera field-of-view Y
  76. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  77. Model model = LoadModel("resources/models/church.obj"); // Load OBJ model
  78. Texture2D texture = LoadTexture("resources/models/church_diffuse.png"); // Load model texture (diffuse map)
  79. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set model diffuse texture
  80. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  81. // Load all postpro shaders
  82. // NOTE 1: All postpro shader use the base vertex shader (DEFAULT_VERTEX_SHADER)
  83. // NOTE 2: We load the correct shader depending on GLSL version
  84. Shader shaders[MAX_POSTPRO_SHADERS] = { 0 };
  85. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  86. shaders[FX_GRAYSCALE] = LoadShader(0, TextFormat("resources/shaders/glsl%i/grayscale.fs", GLSL_VERSION));
  87. shaders[FX_POSTERIZATION] = LoadShader(0, TextFormat("resources/shaders/glsl%i/posterization.fs", GLSL_VERSION));
  88. shaders[FX_DREAM_VISION] = LoadShader(0, TextFormat("resources/shaders/glsl%i/dream_vision.fs", GLSL_VERSION));
  89. shaders[FX_PIXELIZER] = LoadShader(0, TextFormat("resources/shaders/glsl%i/pixelizer.fs", GLSL_VERSION));
  90. shaders[FX_CROSS_HATCHING] = LoadShader(0, TextFormat("resources/shaders/glsl%i/cross_hatching.fs", GLSL_VERSION));
  91. shaders[FX_CROSS_STITCHING] = LoadShader(0, TextFormat("resources/shaders/glsl%i/cross_stitching.fs", GLSL_VERSION));
  92. shaders[FX_PREDATOR_VIEW] = LoadShader(0, TextFormat("resources/shaders/glsl%i/predator.fs", GLSL_VERSION));
  93. shaders[FX_SCANLINES] = LoadShader(0, TextFormat("resources/shaders/glsl%i/scanlines.fs", GLSL_VERSION));
  94. shaders[FX_FISHEYE] = LoadShader(0, TextFormat("resources/shaders/glsl%i/fisheye.fs", GLSL_VERSION));
  95. shaders[FX_SOBEL] = LoadShader(0, TextFormat("resources/shaders/glsl%i/sobel.fs", GLSL_VERSION));
  96. shaders[FX_BLOOM] = LoadShader(0, TextFormat("resources/shaders/glsl%i/bloom.fs", GLSL_VERSION));
  97. shaders[FX_BLUR] = LoadShader(0, TextFormat("resources/shaders/glsl%i/blur.fs", GLSL_VERSION));
  98. int currentShader = FX_GRAYSCALE;
  99. // Create a RenderTexture2D to be used for render to texture
  100. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  101. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  102. //--------------------------------------------------------------------------------------
  103. // Main game loop
  104. while (!WindowShouldClose()) // Detect window close button or ESC key
  105. {
  106. // Update
  107. //----------------------------------------------------------------------------------
  108. UpdateCamera(&camera, CAMERA_ORBITAL);
  109. if (IsKeyPressed(KEY_RIGHT)) currentShader++;
  110. else if (IsKeyPressed(KEY_LEFT)) currentShader--;
  111. if (currentShader >= MAX_POSTPRO_SHADERS) currentShader = 0;
  112. else if (currentShader < 0) currentShader = MAX_POSTPRO_SHADERS - 1;
  113. //----------------------------------------------------------------------------------
  114. // Draw
  115. //----------------------------------------------------------------------------------
  116. BeginTextureMode(target); // Enable drawing to texture
  117. ClearBackground(RAYWHITE); // Clear texture background
  118. BeginMode3D(camera); // Begin 3d mode drawing
  119. DrawModel(model, position, 0.1f, WHITE); // Draw 3d model with texture
  120. DrawGrid(10, 1.0f); // Draw a grid
  121. EndMode3D(); // End 3d mode drawing, returns to orthographic 2d mode
  122. EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
  123. BeginDrawing();
  124. ClearBackground(RAYWHITE); // Clear screen background
  125. // Render generated texture using selected postprocessing shader
  126. BeginShaderMode(shaders[currentShader]);
  127. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  128. DrawTextureRec(target.texture, (Rectangle){ 0, 0, (float)target.texture.width, (float)-target.texture.height }, (Vector2){ 0, 0 }, WHITE);
  129. EndShaderMode();
  130. // Draw 2d shapes and text over drawn texture
  131. DrawRectangle(0, 9, 580, 30, Fade(LIGHTGRAY, 0.7f));
  132. DrawText("(c) Church 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  133. DrawText("CURRENT POSTPRO SHADER:", 10, 15, 20, BLACK);
  134. DrawText(postproShaderText[currentShader], 330, 15, 20, RED);
  135. DrawText("< >", 540, 10, 30, DARKBLUE);
  136. DrawFPS(700, 15);
  137. EndDrawing();
  138. //----------------------------------------------------------------------------------
  139. }
  140. // De-Initialization
  141. //--------------------------------------------------------------------------------------
  142. // Unload all postpro shaders
  143. for (int i = 0; i < MAX_POSTPRO_SHADERS; i++) UnloadShader(shaders[i]);
  144. UnloadTexture(texture); // Unload texture
  145. UnloadModel(model); // Unload model
  146. UnloadRenderTexture(target); // Unload render texture
  147. CloseWindow(); // Close window and OpenGL context
  148. //--------------------------------------------------------------------------------------
  149. return 0;
  150. }