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

135 行
5.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load models vox (MagicaVoxel)
  4. *
  5. * Example originally created with raylib 4.0, last time updated with raylib 4.0
  6. *
  7. * Example contributed by Johann Nadalutti (@procfxgen) and reviewed by Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2021-2022 Johann Nadalutti (@procfxgen) and Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "raymath.h" // Required for: MatrixTranslate()
  17. #define MAX_VOX_FILES 3
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. const char *voxFileNames[] = {
  28. "resources/models/vox/chr_knight.vox",
  29. "resources/models/vox/chr_sword.vox",
  30. "resources/models/vox/monu9.vox"
  31. };
  32. InitWindow(screenWidth, screenHeight, "raylib [models] example - magicavoxel loading");
  33. // Define the camera to look into our 3d world
  34. Camera camera = { 0 };
  35. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  36. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  37. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  38. camera.fovy = 45.0f; // Camera field-of-view Y
  39. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  40. // Load MagicaVoxel files
  41. Model models[MAX_VOX_FILES] = { 0 };
  42. for (int i = 0; i < MAX_VOX_FILES; i++)
  43. {
  44. // Load VOX file and measure time
  45. double t0 = GetTime()*1000.0;
  46. models[i] = LoadModel(voxFileNames[i]);
  47. double t1 = GetTime()*1000.0;
  48. TraceLog(LOG_WARNING, TextFormat("[%s] File loaded in %.3f ms", voxFileNames[i], t1 - t0));
  49. // Compute model translation matrix to center model on draw position (0, 0 , 0)
  50. BoundingBox bb = GetModelBoundingBox(models[i]);
  51. Vector3 center = { 0 };
  52. center.x = bb.min.x + (((bb.max.x - bb.min.x)/2));
  53. center.z = bb.min.z + (((bb.max.z - bb.min.z)/2));
  54. Matrix matTranslate = MatrixTranslate(-center.x, 0, -center.z);
  55. models[i].transform = matTranslate;
  56. }
  57. int currentModel = 0;
  58. SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
  59. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  60. //--------------------------------------------------------------------------------------
  61. // Main game loop
  62. while (!WindowShouldClose()) // Detect window close button or ESC key
  63. {
  64. // Update
  65. //----------------------------------------------------------------------------------
  66. UpdateCamera(&camera);
  67. // Cycle between models on mouse click
  68. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) currentModel = (currentModel + 1)%MAX_VOX_FILES;
  69. // Cycle between models on key pressed
  70. if (IsKeyPressed(KEY_RIGHT))
  71. {
  72. currentModel++;
  73. if (currentModel >= MAX_VOX_FILES) currentModel = 0;
  74. }
  75. else if (IsKeyPressed(KEY_LEFT))
  76. {
  77. currentModel--;
  78. if (currentModel < 0) currentModel = MAX_VOX_FILES - 1;
  79. }
  80. //----------------------------------------------------------------------------------
  81. // Draw
  82. //----------------------------------------------------------------------------------
  83. BeginDrawing();
  84. ClearBackground(RAYWHITE);
  85. // Draw 3D model
  86. BeginMode3D(camera);
  87. DrawModel(models[currentModel], (Vector3){ 0, 0, 0 }, 1.0f, WHITE);
  88. DrawGrid(10, 1.0);
  89. EndMode3D();
  90. // Display info
  91. DrawRectangle(10, 400, 310, 30, Fade(SKYBLUE, 0.5f));
  92. DrawRectangleLines(10, 400, 310, 30, Fade(DARKBLUE, 0.5f));
  93. DrawText("MOUSE LEFT BUTTON to CYCLE VOX MODELS", 40, 410, 10, BLUE);
  94. DrawText(TextFormat("File: %s", GetFileName(voxFileNames[currentModel])), 10, 10, 20, GRAY);
  95. EndDrawing();
  96. //----------------------------------------------------------------------------------
  97. }
  98. // De-Initialization
  99. //--------------------------------------------------------------------------------------
  100. // Unload models data (GPU VRAM)
  101. for (int i = 0; i < MAX_VOX_FILES; i++) UnloadModel(models[i]);
  102. CloseWindow(); // Close window and OpenGL context
  103. //--------------------------------------------------------------------------------------
  104. return 0;
  105. }