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.

141 regels
6.0 KiB

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