Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

127 rader
5.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - OBJ models viewer
  4. *
  5. * This example has been created using raylib 2.0 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #include <string.h> // Required for: strcpy()
  13. int main(void)
  14. {
  15. // Initialization
  16. //--------------------------------------------------------------------------------------
  17. const int screenWidth = 800;
  18. const int screenHeight = 450;
  19. InitWindow(screenWidth, screenHeight, "raylib example - obj viewer");
  20. // Define the camera to look into our 3d world
  21. Camera camera = { { 30.0f, 30.0f, 30.0f }, { 0.0f, 10.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  22. Model model = LoadModel("resources/models/turret.obj"); // Load default model obj
  23. Texture2D texture = LoadTexture("resources/models/turret_diffuse.png"); // Load default model texture
  24. model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Bind texture to model
  25. Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position
  26. BoundingBox bounds = MeshBoundingBox(model.meshes[0]); // Set model bounds
  27. bool selected = false; // Selected object flag
  28. SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
  29. char objFilename[64] = "turret.obj";
  30. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  31. //--------------------------------------------------------------------------------------
  32. // Main game loop
  33. while (!WindowShouldClose()) // Detect window close button or ESC key
  34. {
  35. // Update
  36. //----------------------------------------------------------------------------------
  37. if (IsFileDropped())
  38. {
  39. int count = 0;
  40. char **droppedFiles = GetDroppedFiles(&count);
  41. if (count == 1)
  42. {
  43. if (IsFileExtension(droppedFiles[0], ".obj"))
  44. {
  45. for (int i = 0; i < model.meshCount; i++) UnloadMesh(model.meshes[i]);
  46. model.meshes = LoadMeshes(droppedFiles[0], &model.meshCount);
  47. bounds = MeshBoundingBox(model.meshes[0]);
  48. }
  49. else if (IsFileExtension(droppedFiles[0], ".png"))
  50. {
  51. UnloadTexture(texture);
  52. texture = LoadTexture(droppedFiles[0]);
  53. model.materials[0].maps[MAP_DIFFUSE].texture = texture;
  54. }
  55. strcpy(objFilename, GetFileName(droppedFiles[0]));
  56. }
  57. ClearDroppedFiles(); // Clear internal buffers
  58. }
  59. UpdateCamera(&camera);
  60. // Select model on mouse click
  61. if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON))
  62. {
  63. // Check collision between ray and box
  64. if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) selected = !selected;
  65. else selected = false;
  66. }
  67. //----------------------------------------------------------------------------------
  68. // Draw
  69. //----------------------------------------------------------------------------------
  70. BeginDrawing();
  71. ClearBackground(RAYWHITE);
  72. BeginMode3D(camera);
  73. DrawModel(model, position, 1.0f, WHITE); // Draw 3d model with texture
  74. DrawGrid(20.0, 10.0); // Draw a grid
  75. if (selected) DrawBoundingBox(bounds, GREEN);
  76. EndMode3D();
  77. DrawText("Free camera default controls:", 10, 20, 10, DARKGRAY);
  78. DrawText("- Mouse Wheel to Zoom in-out", 20, 40, 10, GRAY);
  79. DrawText("- Mouse Wheel Pressed to Pan", 20, 60, 10, GRAY);
  80. DrawText("- Alt + Mouse Wheel Pressed to Rotate", 20, 80, 10, GRAY);
  81. DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 20, 100, 10, GRAY);
  82. DrawText("Drag & drop .obj/.png to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY);
  83. DrawText(FormatText("Current file: %s", objFilename), 250, GetScreenHeight() - 20, 10, GRAY);
  84. if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN);
  85. DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
  86. EndDrawing();
  87. //----------------------------------------------------------------------------------
  88. }
  89. // De-Initialization
  90. //--------------------------------------------------------------------------------------
  91. UnloadTexture(texture); // Unload texture
  92. UnloadModel(model); // Unload model
  93. ClearDroppedFiles(); // Clear internal buffers
  94. CloseWindow(); // Close window and OpenGL context
  95. //--------------------------------------------------------------------------------------
  96. return 0;
  97. }