shaders_postprocessing.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.6 (www.raylib.com)
  13. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  14. --
  15. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  16. --
  17. -------------------------------------------------------------------------------------------
  18. -- Initialization
  19. -------------------------------------------------------------------------------------------
  20. local screenWidth = 800
  21. local screenHeight = 450
  22. SetConfigFlags(FLAG.MSAA_4X_HINT) -- Enable Multi Sampling Anti Aliasing 4x (if available)
  23. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - postprocessing shader")
  24. -- Define the camera to look into our 3d world
  25. local camera = Camera(Vector3(3.0, 3.0, 3.0), Vector3(0.0, 1.5, 0.0), Vector3(0.0, 1.0, 0.0), 45.0)
  26. local dwarf = LoadModel("resources/model/dwarf.obj") -- Load OBJ model
  27. local texture = LoadTexture("resources/model/dwarf_diffuse.png") -- Load model texture (diffuse map)
  28. dwarf.material.texDiffuse = texture -- Set dwarf model diffuse texture
  29. local position = Vector3(0.0, 0.0, 0.0) -- Set model position
  30. local shader = LoadShader("resources/shaders/glsl330/base.vs",
  31. "resources/shaders/glsl330/bloom.fs") -- Load postpro shader
  32. -- Create a RenderTexture2D to be used for render to texture
  33. local target = LoadRenderTexture(screenWidth, screenHeight)
  34. -- Setup orbital camera
  35. SetCameraMode(camera, CameraMode.ORBITAL) -- Set an orbital camera mode
  36. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  37. -------------------------------------------------------------------------------------------
  38. -- Main game loop
  39. while not WindowShouldClose() do -- Detect window close button or ESC key
  40. -- Update
  41. ---------------------------------------------------------------------------------------
  42. camera = UpdateCamera(camera) -- Update camera
  43. ---------------------------------------------------------------------------------------
  44. -- Draw
  45. ---------------------------------------------------------------------------------------
  46. BeginDrawing()
  47. ClearBackground(RAYWHITE)
  48. BeginTextureMode(target) -- Enable drawing to texture
  49. Begin3dMode(camera)
  50. DrawModel(dwarf, position, 2.0, WHITE) -- Draw 3d model with texture
  51. DrawGrid(10, 1.0) -- Draw a grid
  52. End3dMode()
  53. DrawText("HELLO POSTPROCESSING!", 70, 190, 50, RED)
  54. EndTextureMode() -- End drawing to texture (now we have a texture available for next passes)
  55. BeginShaderMode(shader)
  56. -- NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  57. DrawTextureRec(target.texture, Rectangle(0, 0, target.texture.width, -target.texture.height), Vector2(0, 0), WHITE)
  58. EndShaderMode()
  59. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, DARKGRAY)
  60. DrawFPS(10, 10)
  61. EndDrawing()
  62. ---------------------------------------------------------------------------------------
  63. end
  64. -- De-Initialization
  65. -------------------------------------------------------------------------------------------
  66. UnloadShader(shader) -- Unload shader
  67. UnloadTexture(texture) -- Unload texture
  68. UnloadModel(dwarf) -- Unload model
  69. UnloadRenderTexture(target) -- Unload render texture
  70. CloseWindow() -- Close window and OpenGL context
  71. -------------------------------------------------------------------------------------------