您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

154 行
6.9 KiB

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