models_obj_loading.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load and draw a 3d model (OBJ)
  4. *
  5. * This example has been created using raylib 1.3 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2014 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. int main()
  13. {
  14. // Initialization
  15. //--------------------------------------------------------------------------------------
  16. int screenWidth = 800;
  17. int screenHeight = 450;
  18. InitWindow(screenWidth, screenHeight, "raylib [models] example - obj model loading");
  19. // Define the camera to look into our 3d world
  20. Camera camera = {{ 3.0, 3.0, 3.0 }, { 0.0, 1.5, 0.0 }, { 0.0, 1.0, 0.0 }};
  21. Texture2D texture = LoadTexture("resources/model/dwarf_diffuse.png"); // Load model texture
  22. Model dwarf = LoadModel("resources/model/dwarf.obj"); // Load OBJ model
  23. SetModelTexture(&dwarf, texture); // Bind texture to model
  24. Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position
  25. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  26. //--------------------------------------------------------------------------------------
  27. // Main game loop
  28. while (!WindowShouldClose()) // Detect window close button or ESC key
  29. {
  30. // Update
  31. //----------------------------------------------------------------------------------
  32. if (IsKeyDown(KEY_LEFT)) position.x -= 0.2;
  33. if (IsKeyDown(KEY_RIGHT)) position.x += 0.2;
  34. if (IsKeyDown(KEY_UP)) position.z -= 0.2;
  35. if (IsKeyDown(KEY_DOWN)) position.z += 0.2;
  36. //----------------------------------------------------------------------------------
  37. // Draw
  38. //----------------------------------------------------------------------------------
  39. BeginDrawing();
  40. ClearBackground(RAYWHITE);
  41. Begin3dMode(camera);
  42. DrawModel(dwarf, position, 2.0f, WHITE); // Draw 3d model with texture
  43. DrawGrid(10.0, 1.0); // Draw a grid
  44. DrawGizmo(position); // Draw gizmo
  45. End3dMode();
  46. DrawText("(c) Dwarf 3D model by David Moreno", screenWidth - 200, screenHeight - 20, 10, GRAY);
  47. DrawFPS(10, 10);
  48. EndDrawing();
  49. //----------------------------------------------------------------------------------
  50. }
  51. // De-Initialization
  52. //--------------------------------------------------------------------------------------
  53. UnloadTexture(texture); // Unload texture
  54. UnloadModel(dwarf); // Unload model
  55. CloseWindow(); // Close window and OpenGL context
  56. //--------------------------------------------------------------------------------------
  57. return 0;
  58. }