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.

108 lines
4.8 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. int main()
  20. {
  21. // Initialization
  22. //--------------------------------------------------------------------------------------
  23. int screenWidth = 800;
  24. int screenHeight = 450;
  25. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  26. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader");
  27. // Define the camera to look into our 3d world
  28. Camera camera = {{ 3.0f, 3.0f, 3.0f }, { 0.0f, 1.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };
  29. Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  30. Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture (diffuse map)
  31. dwarf.material.texDiffuse = texture; // Set dwarf model diffuse texture
  32. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  33. Shader shader = LoadShader("resources/shaders/glsl330/base.vs",
  34. "resources/shaders/glsl330/bloom.fs"); // Load postpro shader
  35. // Create a RenderTexture2D to be used for render to texture
  36. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  37. // Setup orbital camera
  38. SetCameraMode(CAMERA_ORBITAL); // Set an orbital camera mode
  39. SetCameraPosition(camera.position); // Set internal camera position to match our camera position
  40. SetCameraTarget(camera.target); // Set internal camera target to match our camera target
  41. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  42. //--------------------------------------------------------------------------------------
  43. // Main game loop
  44. while (!WindowShouldClose()) // Detect window close button or ESC key
  45. {
  46. // Update
  47. //----------------------------------------------------------------------------------
  48. UpdateCamera(&camera); // Update internal camera and our camera
  49. //----------------------------------------------------------------------------------
  50. // Draw
  51. //----------------------------------------------------------------------------------
  52. BeginDrawing();
  53. ClearBackground(RAYWHITE);
  54. BeginTextureMode(target); // Enable drawing to texture
  55. Begin3dMode(camera);
  56. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  57. DrawGrid(10, 1.0f); // Draw a grid
  58. End3dMode();
  59. DrawText("HELLO POSTPROCESSING!", 70, 190, 50, RED);
  60. EndTextureMode(); // End drawing to texture (now we have a texture available for next passes)
  61. BeginShaderMode(shader);
  62. // NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  63. DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, -target.texture.height }, (Vector2){ 0, 0 }, WHITE);
  64. EndShaderMode();
  65. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, DARKGRAY);
  66. DrawFPS(10, 10);
  67. EndDrawing();
  68. //----------------------------------------------------------------------------------
  69. }
  70. // De-Initialization
  71. //--------------------------------------------------------------------------------------
  72. UnloadShader(shader); // Unload shader
  73. UnloadTexture(texture); // Unload texture
  74. UnloadModel(dwarf); // Unload model
  75. UnloadRenderTexture(target); // Unload render texture
  76. CloseWindow(); // Close window and OpenGL context
  77. //--------------------------------------------------------------------------------------
  78. return 0;
  79. }