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.

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