models_loading.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [models] example - models loading");
  30. // Define the camera to look into our 3d world
  31. Camera camera = { 0 };
  32. camera.position = (Vector3){ 50.0f, 50.0f, 50.0f }; // Camera position
  33. camera.target = (Vector3){ 0.0f, 10.0f, 0.0f }; // Camera looking at point
  34. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  35. camera.fovy = 45.0f; // Camera field-of-view Y
  36. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  37. Model model = LoadModel("resources/models/obj/castle.obj"); // Load model
  38. Texture2D texture = LoadTexture("resources/models/obj/castle_diffuse.png"); // Load model texture
  39. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  40. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  41. BoundingBox bounds = GetMeshBoundingBox(model.meshes[0]); // Set model bounds
  42. // NOTE: bounds are calculated from the original size of the model,
  43. // if model is scaled on drawing, bounds must be also scaled
  44. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  45. bool selected = false; // Selected object flag
  46. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  47. //--------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. UpdateCamera(&camera);
  54. // Load new models/textures on drag&drop
  55. if (IsFileDropped())
  56. {
  57. int count = 0;
  58. char **droppedFiles = GetDroppedFiles(&count);
  59. if (count == 1) // Only support one file dropped
  60. {
  61. if (IsFileExtension(droppedFiles[0], ".obj") ||
  62. IsFileExtension(droppedFiles[0], ".gltf") ||
  63. IsFileExtension(droppedFiles[0], ".glb") ||
  64. IsFileExtension(droppedFiles[0], ".vox") ||
  65. IsFileExtension(droppedFiles[0], ".iqm")) // Model file formats supported
  66. {
  67. UnloadModel(model); // Unload previous model
  68. model = LoadModel(droppedFiles[0]); // Load new model
  69. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set current map diffuse texture
  70. bounds = GetMeshBoundingBox(model.meshes[0]);
  71. // TODO: Move camera position from target enough distance to visualize model properly
  72. }
  73. else if (IsFileExtension(droppedFiles[0], ".png")) // Texture file formats supported
  74. {
  75. // Unload current model texture and load new one
  76. UnloadTexture(texture);
  77. texture = LoadTexture(droppedFiles[0]);
  78. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  79. }
  80. }
  81. ClearDroppedFiles(); // Clear internal buffers
  82. }
  83. // Select model on mouse click
  84. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  85. {
  86. // Check collision between ray and box
  87. if (GetRayCollisionBox(GetMouseRay(GetMousePosition(), camera), bounds).hit) selected = !selected;
  88. else selected = false;
  89. }
  90. //----------------------------------------------------------------------------------
  91. // Draw
  92. //----------------------------------------------------------------------------------
  93. BeginDrawing();
  94. ClearBackground(RAYWHITE);
  95. BeginMode3D(camera);
  96. DrawModel(model, position, 1.0f, WHITE); // Draw 3d model with texture
  97. DrawGrid(20, 10.0f); // Draw a grid
  98. if (selected) DrawBoundingBox(bounds, GREEN); // Draw selection box
  99. EndMode3D();
  100. DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY);
  101. if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN);
  102. DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  103. DrawFPS(10, 10);
  104. EndDrawing();
  105. //----------------------------------------------------------------------------------
  106. }
  107. // De-Initialization
  108. //--------------------------------------------------------------------------------------
  109. UnloadTexture(texture); // Unload texture
  110. UnloadModel(model); // Unload model
  111. CloseWindow(); // Close window and OpenGL context
  112. //--------------------------------------------------------------------------------------
  113. return 0;
  114. }