shaders_postprocessing.c 7.8 KB

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