2
0

models_loading.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Models loading
  4. *
  5. * raylib supports multiple models file formats:
  6. *
  7. * - OBJ > Text file format. Must include vertex position-texcoords-normals information,
  8. * if files references some .mtl materials file, it will be loaded (or try to).
  9. * - GLTF > Text/binary file format. Includes lot of information and it could
  10. * also reference external files, raylib will try loading mesh and materials data.
  11. * - IQM > Binary file format. Includes mesh vertex data but also animation data,
  12. * raylib can load .iqm animations.
  13. * - VOX > Binary file format. MagikaVoxel mesh format:
  14. * https://github.com/ephtracy/voxel-model/blob/master/MagicaVoxel-file-format-vox.txt
  15. *
  16. * This example has been created using raylib 4.0 (www.raylib.com)
  17. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  18. *
  19. * Copyright (c) 2014-2021 Ramon Santamaria (@raysan5)
  20. *
  21. ********************************************************************************************/
  22. #include "raylib.h"
  23. //------------------------------------------------------------------------------------
  24. // Program main entry point
  25. //------------------------------------------------------------------------------------
  26. int main(void)
  27. {
  28. // Initialization
  29. //--------------------------------------------------------------------------------------
  30. const int screenWidth = 800;
  31. const int screenHeight = 450;
  32. InitWindow(screenWidth, screenHeight, "raylib [models] example - models loading");
  33. // Define the camera to look into our 3d world
  34. Camera camera = { 0 };
  35. camera.position = (Vector3){ 50.0f, 50.0f, 50.0f }; // Camera position
  36. camera.target = (Vector3){ 0.0f, 10.0f, 0.0f }; // Camera looking at point
  37. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  38. camera.fovy = 45.0f; // Camera field-of-view Y
  39. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  40. Model model = LoadModel("resources/models/obj/castle.obj"); // Load model
  41. Texture2D texture = LoadTexture("resources/models/obj/castle_diffuse.png"); // Load model texture
  42. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  43. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  44. BoundingBox bounds = GetMeshBoundingBox(model.meshes[0]); // Set model bounds
  45. // NOTE: bounds are calculated from the original size of the model,
  46. // if model is scaled on drawing, bounds must be also scaled
  47. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  48. bool selected = false; // Selected object flag
  49. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  50. //--------------------------------------------------------------------------------------
  51. // Main game loop
  52. while (!WindowShouldClose()) // Detect window close button or ESC key
  53. {
  54. // Update
  55. //----------------------------------------------------------------------------------
  56. UpdateCamera(&camera);
  57. // Load new models/textures on drag&drop
  58. if (IsFileDropped())
  59. {
  60. FilePathList droppedFiles = LoadDroppedFiles();
  61. if (droppedFiles.count == 1) // Only support one file dropped
  62. {
  63. if (IsFileExtension(droppedFiles.paths[0], ".obj") ||
  64. IsFileExtension(droppedFiles.paths[0], ".gltf") ||
  65. IsFileExtension(droppedFiles.paths[0], ".glb") ||
  66. IsFileExtension(droppedFiles.paths[0], ".vox") ||
  67. IsFileExtension(droppedFiles.paths[0], ".iqm")) // Model file formats supported
  68. {
  69. UnloadModel(model); // Unload previous model
  70. model = LoadModel(droppedFiles.paths[0]); // Load new model
  71. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set current map diffuse texture
  72. bounds = GetMeshBoundingBox(model.meshes[0]);
  73. // TODO: Move camera position from target enough distance to visualize model properly
  74. }
  75. else if (IsFileExtension(droppedFiles.paths[0], ".png")) // Texture file formats supported
  76. {
  77. // Unload current model texture and load new one
  78. UnloadTexture(texture);
  79. texture = LoadTexture(droppedFiles.paths[0]);
  80. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  81. }
  82. }
  83. UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
  84. }
  85. // Select model on mouse click
  86. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  87. {
  88. // Check collision between ray and box
  89. if (GetRayCollisionBox(GetMouseRay(GetMousePosition(), camera), bounds).hit) selected = !selected;
  90. else selected = false;
  91. }
  92. //----------------------------------------------------------------------------------
  93. // Draw
  94. //----------------------------------------------------------------------------------
  95. BeginDrawing();
  96. ClearBackground(RAYWHITE);
  97. BeginMode3D(camera);
  98. DrawModel(model, position, 1.0f, WHITE); // Draw 3d model with texture
  99. DrawGrid(20, 10.0f); // Draw a grid
  100. if (selected) DrawBoundingBox(bounds, GREEN); // Draw selection box
  101. EndMode3D();
  102. DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY);
  103. if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN);
  104. DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  105. DrawFPS(10, 10);
  106. EndDrawing();
  107. //----------------------------------------------------------------------------------
  108. }
  109. // De-Initialization
  110. //--------------------------------------------------------------------------------------
  111. UnloadTexture(texture); // Unload texture
  112. UnloadModel(model); // Unload model
  113. CloseWindow(); // Close window and OpenGL context
  114. //--------------------------------------------------------------------------------------
  115. return 0;
  116. }