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.

114 lines
5.1 KiB

  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [shaders] example - Apply a postprocessing shader and connect a custom uniform variable
  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 - custom uniform variable")
  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/swirl.fs") -- Load postpro shader
  32. -- Get variable (uniform) location on the shader to connect with the program
  33. -- NOTE: If uniform variable could not be found in the shader, function returns -1
  34. local swirlCenterLoc = GetShaderLocation(shader, "center")
  35. local swirlCenter = { screenWidth/2, screenHeight/2 }
  36. -- Create a RenderTexture2D to be used for render to texture
  37. local target = LoadRenderTexture(screenWidth, screenHeight)
  38. -- Setup orbital camera
  39. SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode
  40. SetCameraPosition(camera.position) -- Set internal camera position to match our camera position
  41. SetCameraTarget(camera.target) -- Set internal camera target to match our camera target
  42. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  43. -------------------------------------------------------------------------------------------
  44. -- Main game loop
  45. while not WindowShouldClose() do -- Detect window close button or ESC key
  46. -- Update
  47. ---------------------------------------------------------------------------------------
  48. local mousePosition = GetMousePosition()
  49. swirlCenter[1] = mousePosition.x
  50. swirlCenter[2] = screenHeight - mousePosition.y
  51. -- Send new value to the shader to be used on drawing
  52. SetShaderValue(shader, swirlCenterLoc, swirlCenter)
  53. camera = UpdateCamera(camera) -- Update internal camera and our camera
  54. ---------------------------------------------------------------------------------------
  55. -- Draw
  56. ---------------------------------------------------------------------------------------
  57. BeginDrawing()
  58. ClearBackground(RAYWHITE)
  59. BeginTextureMode(target) -- Enable drawing to texture
  60. Begin3dMode(camera)
  61. DrawModel(dwarf, position, 2.0, WHITE) -- Draw 3d model with texture
  62. DrawGrid(10, 1.0) -- Draw a grid
  63. End3dMode()
  64. DrawText("TEXT DRAWN IN RENDER TEXTURE", 200, 10, 30, RED)
  65. EndTextureMode() -- End drawing to texture (now we have a texture available for next passes)
  66. BeginShaderMode(shader)
  67. -- NOTE: Render texture must be y-flipped due to default OpenGL coordinates (left-bottom)
  68. DrawTextureRec(target.texture, Rectangle(0, 0, target.texture.width, -target.texture.height), Vector2(0, 0), WHITE)
  69. EndShaderMode()
  70. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY)
  71. DrawFPS(10, 10)
  72. EndDrawing()
  73. ---------------------------------------------------------------------------------------
  74. end
  75. -- De-Initialization
  76. -------------------------------------------------------------------------------------------
  77. UnloadShader(shader) -- Unload shader
  78. UnloadTexture(texture) -- Unload texture
  79. UnloadModel(dwarf) -- Unload model
  80. UnloadRenderTexture(target) -- Unload render texture
  81. CloseWindow() -- Close window and OpenGL context
  82. -------------------------------------------------------------------------------------------