models_loading_vox.c 4.8 KB

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