models_obj_loading.lua 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. -------------------------------------------------------------------------------------------
  2. --
  3. -- raylib [models] example - Load and draw a 3d model (OBJ)
  4. --
  5. -- This example has been created using raylib 1.6 (www.raylib.com)
  6. -- raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. --
  8. -- Copyright (c) 2014-2016 Ramon Santamaria (@raysan5)
  9. --
  10. -------------------------------------------------------------------------------------------
  11. -- Initialization
  12. -------------------------------------------------------------------------------------------
  13. local screenWidth = 800
  14. local screenHeight = 450
  15. InitWindow(screenWidth, screenHeight, "raylib [models] example - obj model loading")
  16. -- Define the camera to look into our 3d world
  17. 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)
  18. local dwarf = LoadModel("resources/model/dwarf.obj") -- Load OBJ model
  19. local texture = LoadTexture("resources/model/dwarf_diffuse.png") -- Load model texture
  20. dwarf.material.texDiffuse = texture -- Set dwarf model diffuse texture
  21. local position = Vector3(0.0, 0.0, 0.0) -- Set model position
  22. SetTargetFPS(60) -- Set our game to run at 60 frames-per-second
  23. -------------------------------------------------------------------------------------------
  24. -- Main game loop
  25. while not WindowShouldClose() do -- Detect window close button or ESC key
  26. -- Update
  27. ---------------------------------------------------------------------------------------
  28. -- ...
  29. ---------------------------------------------------------------------------------------
  30. -- Draw
  31. ---------------------------------------------------------------------------------------
  32. BeginDrawing()
  33. ClearBackground(RAYWHITE)
  34. Begin3dMode(camera)
  35. DrawModel(dwarf, position, 2.0, WHITE) -- Draw 3d model with texture
  36. DrawGrid(10, 1.0) -- Draw a grid
  37. DrawGizmo(position) -- Draw gizmo
  38. End3dMode()
  39. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY)
  40. DrawFPS(10, 10)
  41. EndDrawing()
  42. ---------------------------------------------------------------------------------------
  43. end
  44. -- De-Initialization
  45. -------------------------------------------------------------------------------------------
  46. UnloadTexture(texture) -- Unload texture
  47. UnloadModel(dwarf) -- Unload model
  48. CloseWindow() -- Close window and OpenGL context
  49. -------------------------------------------------------------------------------------------