shaders_model_shader.lua 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [shaders] example - Apply a shader to a 3d model
  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(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
  28. local shader = LoadShader("resources/shaders/glsl330/base.vs",
  29. "resources/shaders/glsl330/grayscale.fs") -- Load model shader
  30. dwarf.material.shader = shader -- Set shader effect to 3d model
  31. dwarf.material.texDiffuse = texture -- Bind texture to model
  32. local position = Vector3(0.0, 0.0, 0.0) -- Set model position
  33. -- Setup orbital camera
  34. SetCameraMode(camera, CameraMode.ORBITAL) -- Set an orbital camera mode
  35. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  36. -------------------------------------------------------------------------------------------
  37. -- Main game loop
  38. while not WindowShouldClose() do -- Detect window close button or ESC key
  39. -- Update
  40. ---------------------------------------------------------------------------------------
  41. camera = UpdateCamera(camera) -- Update camera
  42. ---------------------------------------------------------------------------------------
  43. -- Draw
  44. ---------------------------------------------------------------------------------------
  45. BeginDrawing()
  46. ClearBackground(RAYWHITE)
  47. Begin3dMode(camera)
  48. DrawModel(dwarf, position, 2.0, WHITE) -- Draw 3d model with texture
  49. DrawGrid(10, 1.0) -- Draw a grid
  50. End3dMode()
  51. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY)
  52. DrawFPS(10, 10)
  53. EndDrawing()
  54. ---------------------------------------------------------------------------------------
  55. end
  56. -- De-Initialization
  57. -------------------------------------------------------------------------------------------
  58. UnloadShader(shader) -- Unload shader
  59. UnloadTexture(texture) -- Unload texture
  60. UnloadModel(dwarf) -- Unload model
  61. CloseWindow() -- Close window and OpenGL context
  62. -------------------------------------------------------------------------------------------