models_loading_vox.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - magicavoxel loader and viewer
  4. *
  5. * This example has been created using raylib 3.8 (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)
  9. *
  10. * Copyright (c) 2021 Johann Nadalutti (@procfxgen)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #include "raymath.h"
  15. #include <string.h>
  16. // VOX Files to load and view
  17. #define NUM_VOX_FILES 3
  18. const char* szVoxFiles[] = {
  19. "resources/vox/chr_knight.vox",
  20. "resources/vox/chr_sword.vox",
  21. "resources/vox/monu9.vox"
  22. };
  23. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [models] example - magicavoxel loading");
  30. // Load MagicaVoxel files
  31. Model models[NUM_VOX_FILES] = { 0 };
  32. for (int i = 0; i < NUM_VOX_FILES; i++)
  33. {
  34. // Load MagicaVoxel File and build model
  35. double t0, t1;
  36. t0 = GetTime() * 1000.0;
  37. models[i] = LoadModel(szVoxFiles[i]);
  38. t1 = GetTime() * 1000.0;
  39. TraceLog(LOG_INFO, TextFormat("Vox <%s> loaded in %f ms", GetFileName(szVoxFiles[i]), t1 - t0));
  40. //Compute model's center matrix
  41. BoundingBox bb = GetModelBoundingBox(models[i]);
  42. Vector3 center;
  43. center.x = bb.min.x + (((bb.max.x - bb.min.x) / 2));
  44. center.z = bb.min.z + (((bb.max.z - bb.min.z) / 2));
  45. Matrix matP = MatrixTranslate(-center.x, 0, -center.z);
  46. models[i].transform = matP;
  47. }
  48. // Define the camera to look into our 3d world
  49. Camera camera = { { 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  50. // Model drawing position
  51. Vector3 position = { 0.0f, 0.0f, 0.0f };
  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. //--------------------------------------------------------------------------------------
  58. while (!WindowShouldClose()) // Detect window close button or ESC key
  59. {
  60. //--------------------------------------------------------------------------------------
  61. // Update
  62. //----------------------------------------------------------------------------------
  63. UpdateCamera(&camera); // Update internal camera and our camera
  64. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
  65. {
  66. currentModel = (currentModel + 1) % NUM_VOX_FILES; // Cycle between models
  67. }
  68. if (IsKeyPressed(KEY_RIGHT))
  69. {
  70. currentModel++;
  71. if (currentModel >= NUM_VOX_FILES) currentModel = 0;
  72. }
  73. else if (IsKeyPressed(KEY_LEFT))
  74. {
  75. currentModel--;
  76. if (currentModel < 0) currentModel = NUM_VOX_FILES - 1;
  77. }
  78. //----------------------------------------------------------------------------------
  79. // Draw
  80. //----------------------------------------------------------------------------------
  81. BeginDrawing();
  82. ClearBackground(RAYWHITE);
  83. //Display model
  84. BeginMode3D(camera);
  85. Vector3 rotAxis = { 1,0,0 };
  86. Vector3 scale = { 1,1,1 };
  87. DrawModelEx(models[currentModel], position, rotAxis, 0, scale, WHITE);
  88. //DrawModelWiresEx(models[currentModel], position, rotAxis, -90.0f, scale, BLACK);
  89. DrawGrid(10, 1.0);
  90. EndMode3D();
  91. //Display debug infos
  92. DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f));
  93. DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f));
  94. DrawText("MOUSE LEFT BUTTON to CYCLE VOX MODELS", 40, 410, 10, BLUE);
  95. DrawText(GetFileName(szVoxFiles[currentModel]), 100, 10, 20, DARKBLUE);
  96. EndDrawing();
  97. //----------------------------------------------------------------------------------
  98. }
  99. // De-Initialization
  100. //--------------------------------------------------------------------------------------
  101. // Unload models data (GPU VRAM)
  102. for (int i = 0; i < NUM_VOX_FILES; i++) UnloadModel(models[i]);
  103. CloseWindow(); // Close window and OpenGL context
  104. //--------------------------------------------------------------------------------------
  105. return 0;
  106. }