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.

113 lines
4.8 KiB

  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [shaders] example - Standard lighting (materials and lights)
  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 - model shader")
  24. -- Define the camera to look into our 3d world
  25. local camera = Camera(Vector3(4.0, 4.0, 4.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 position = Vector3(0.0, 0.0, 0.0) -- Set model position
  28. local material = LoadStandardMaterial()
  29. material.texDiffuse = LoadTexture("resources/model/dwarf_diffuse.png") -- Load model diffuse texture
  30. material.texNormal = LoadTexture("resources/model/dwarf_normal.png") -- Load model normal texture
  31. material.texSpecular = LoadTexture("resources/model/dwarf_specular.png") -- Load model specular texture
  32. material.colDiffuse = WHITE
  33. material.colAmbient = Color(0, 0, 10, 255)
  34. material.colSpecular = WHITE
  35. material.glossiness = 50.0
  36. dwarf.material = material -- Apply material to model
  37. local spotLight = CreateLight(LightType.SPOT, Vector3(3.0, 5.0, 2.0), Color(255, 255, 255, 255))
  38. spotLight.target = Vector3(0.0, 0.0, 0.0)
  39. spotLight.intensity = 2.0
  40. spotLight.diffuse = Color(255, 100, 100, 255)
  41. spotLight.coneAngle = 60.0
  42. local dirLight = CreateLight(LightType.DIRECTIONAL, Vector3(0.0, -3.0, -3.0), Color(255, 255, 255, 255))
  43. dirLight.target = Vector3(1.0, -2.0, -2.0)
  44. dirLight.intensity = 2.0
  45. dirLight.diffuse = Color(100, 255, 100, 255)
  46. local pointLight = CreateLight(LightType.POINT, Vector3(0.0, 4.0, 5.0), Color(255, 255, 255, 255))
  47. pointLight.intensity = 2.0
  48. pointLight.diffuse = Color(100, 100, 255, 255)
  49. pointLight.radius = 3.0
  50. -- Setup orbital camera
  51. SetCameraMode(CameraMode.ORBITAL) -- Set an orbital camera mode
  52. SetCameraPosition(camera.position) -- Set internal camera position to match our camera position
  53. SetCameraTarget(camera.target) -- Set internal camera target to match our camera target
  54. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  55. -------------------------------------------------------------------------------------------
  56. -- Main game loop
  57. while not WindowShouldClose() do -- Detect window close button or ESC key
  58. -- Update
  59. ---------------------------------------------------------------------------------------
  60. camera = UpdateCamera(camera) -- Update internal camera and our camera
  61. ---------------------------------------------------------------------------------------
  62. -- Draw
  63. ---------------------------------------------------------------------------------------
  64. BeginDrawing()
  65. ClearBackground(RAYWHITE)
  66. Begin3dMode(camera)
  67. DrawModel(dwarf, position, 2.0, WHITE) -- Draw 3d model with texture
  68. DrawLight(spotLight) -- Draw spot light
  69. DrawLight(dirLight) -- Draw directional light
  70. DrawLight(pointLight) -- Draw point light
  71. DrawGrid(10, 1.0) -- Draw a grid
  72. End3dMode()
  73. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY)
  74. DrawFPS(10, 10)
  75. EndDrawing()
  76. ---------------------------------------------------------------------------------------
  77. end
  78. -- De-Initialization
  79. -------------------------------------------------------------------------------------------
  80. UnloadMaterial(material) -- Unload material and assigned textures
  81. UnloadModel(dwarf) -- Unload model
  82. -- Destroy all created lights
  83. DestroyLight(pointLight)
  84. DestroyLight(dirLight)
  85. DestroyLight(spotLight)
  86. CloseWindow() -- Close window and OpenGL context
  87. -------------------------------------------------------------------------------------------