models_loading_vox.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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-2023 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 projection 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. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  59. //--------------------------------------------------------------------------------------
  60. // Main game loop
  61. while (!WindowShouldClose()) // Detect window close button or ESC key
  62. {
  63. // Update
  64. //----------------------------------------------------------------------------------
  65. UpdateCamera(&camera, CAMERA_ORBITAL);
  66. // Cycle between models on mouse click
  67. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) currentModel = (currentModel + 1)%MAX_VOX_FILES;
  68. // Cycle between models on key pressed
  69. if (IsKeyPressed(KEY_RIGHT))
  70. {
  71. currentModel++;
  72. if (currentModel >= MAX_VOX_FILES) currentModel = 0;
  73. }
  74. else if (IsKeyPressed(KEY_LEFT))
  75. {
  76. currentModel--;
  77. if (currentModel < 0) currentModel = MAX_VOX_FILES - 1;
  78. }
  79. //----------------------------------------------------------------------------------
  80. // Draw
  81. //----------------------------------------------------------------------------------
  82. BeginDrawing();
  83. ClearBackground(RAYWHITE);
  84. // Draw 3D model
  85. BeginMode3D(camera);
  86. DrawModel(models[currentModel], (Vector3){ 0, 0, 0 }, 1.0f, WHITE);
  87. DrawGrid(10, 1.0);
  88. EndMode3D();
  89. // Display info
  90. DrawRectangle(10, 400, 310, 30, Fade(SKYBLUE, 0.5f));
  91. DrawRectangleLines(10, 400, 310, 30, Fade(DARKBLUE, 0.5f));
  92. DrawText("MOUSE LEFT BUTTON to CYCLE VOX MODELS", 40, 410, 10, BLUE);
  93. DrawText(TextFormat("File: %s", GetFileName(voxFileNames[currentModel])), 10, 10, 20, GRAY);
  94. EndDrawing();
  95. //----------------------------------------------------------------------------------
  96. }
  97. // De-Initialization
  98. //--------------------------------------------------------------------------------------
  99. // Unload models data (GPU VRAM)
  100. for (int i = 0; i < MAX_VOX_FILES; i++) UnloadModel(models[i]);
  101. CloseWindow(); // Close window and OpenGL context
  102. //--------------------------------------------------------------------------------------
  103. return 0;
  104. }