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.

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