You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
6.9 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Models loading
  4. *
  5. * NOTE: 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. * - M3D > Binary file format. Model 3D format:
  16. * https://bztsrc.gitlab.io/model3d
  17. *
  18. * Example originally created with raylib 2.0, last time updated with raylib 4.2
  19. *
  20. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  21. * BSD-like license that allows static linking with closed source software
  22. *
  23. * Copyright (c) 2014-2024 Ramon Santamaria (@raysan5)
  24. *
  25. ********************************************************************************************/
  26. #include "raylib.h"
  27. //------------------------------------------------------------------------------------
  28. // Program main entry point
  29. //------------------------------------------------------------------------------------
  30. int main(void)
  31. {
  32. // Initialization
  33. //--------------------------------------------------------------------------------------
  34. const int screenWidth = 800;
  35. const int screenHeight = 450;
  36. InitWindow(screenWidth, screenHeight, "raylib [models] example - models loading");
  37. // Define the camera to look into our 3d world
  38. Camera camera = { 0 };
  39. camera.position = (Vector3){ 50.0f, 50.0f, 50.0f }; // Camera position
  40. camera.target = (Vector3){ 0.0f, 10.0f, 0.0f }; // Camera looking at point
  41. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  42. camera.fovy = 45.0f; // Camera field-of-view Y
  43. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  44. Model model = LoadModel("resources/models/obj/castle.obj"); // Load model
  45. Texture2D texture = LoadTexture("resources/models/obj/castle_diffuse.png"); // Load model texture
  46. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set map diffuse texture
  47. Vector3 position = { 0.0f, 0.0f, 0.0f }; // Set model position
  48. BoundingBox bounds = GetMeshBoundingBox(model.meshes[0]); // Set model bounds
  49. // NOTE: bounds are calculated from the original size of the model,
  50. // if model is scaled on drawing, bounds must be also scaled
  51. bool selected = false; // Selected object flag
  52. DisableCursor(); // Limit cursor to relative movement inside the window
  53. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  54. //--------------------------------------------------------------------------------------
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. // Update
  59. //----------------------------------------------------------------------------------
  60. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  61. // Load new models/textures on drag&drop
  62. if (IsFileDropped())
  63. {
  64. FilePathList droppedFiles = LoadDroppedFiles();
  65. if (droppedFiles.count == 1) // Only support one file dropped
  66. {
  67. if (IsFileExtension(droppedFiles.paths[0], ".obj") ||
  68. IsFileExtension(droppedFiles.paths[0], ".gltf") ||
  69. IsFileExtension(droppedFiles.paths[0], ".glb") ||
  70. IsFileExtension(droppedFiles.paths[0], ".vox") ||
  71. IsFileExtension(droppedFiles.paths[0], ".iqm") ||
  72. IsFileExtension(droppedFiles.paths[0], ".m3d")) // Model file formats supported
  73. {
  74. UnloadModel(model); // Unload previous model
  75. model = LoadModel(droppedFiles.paths[0]); // Load new model
  76. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture; // Set current map diffuse texture
  77. bounds = GetMeshBoundingBox(model.meshes[0]);
  78. // TODO: Move camera position from target enough distance to visualize model properly
  79. }
  80. else if (IsFileExtension(droppedFiles.paths[0], ".png")) // Texture file formats supported
  81. {
  82. // Unload current model texture and load new one
  83. UnloadTexture(texture);
  84. texture = LoadTexture(droppedFiles.paths[0]);
  85. model.materials[0].maps[MATERIAL_MAP_DIFFUSE].texture = texture;
  86. }
  87. }
  88. UnloadDroppedFiles(droppedFiles); // Unload filepaths from memory
  89. }
  90. // Select model on mouse click
  91. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  92. {
  93. // Check collision between ray and box
  94. if (GetRayCollisionBox(GetScreenToWorldRay(GetMousePosition(), camera), bounds).hit) selected = !selected;
  95. else selected = false;
  96. }
  97. //----------------------------------------------------------------------------------
  98. // Draw
  99. //----------------------------------------------------------------------------------
  100. BeginDrawing();
  101. ClearBackground(RAYWHITE);
  102. BeginMode3D(camera);
  103. DrawModel(model, position, 1.0f, WHITE); // Draw 3d model with texture
  104. DrawGrid(20, 10.0f); // Draw a grid
  105. if (selected) DrawBoundingBox(bounds, GREEN); // Draw selection box
  106. EndMode3D();
  107. DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY);
  108. if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN);
  109. DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  110. DrawFPS(10, 10);
  111. EndDrawing();
  112. //----------------------------------------------------------------------------------
  113. }
  114. // De-Initialization
  115. //--------------------------------------------------------------------------------------
  116. UnloadTexture(texture); // Unload texture
  117. UnloadModel(model); // Unload model
  118. CloseWindow(); // Close window and OpenGL context
  119. //--------------------------------------------------------------------------------------
  120. return 0;
  121. }