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.

177 lines
8.1 KiB

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