models_loading_vox.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - loading vox
  4. *
  5. * Example complexity rating: [★☆☆☆] 1/4
  6. *
  7. * Example originally created with raylib 4.0, last time updated with raylib 4.0
  8. *
  9. * Example contributed by Johann Nadalutti (@procfxgen) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2021-2025 Johann Nadalutti (@procfxgen) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "raymath.h" // Required for: MatrixTranslate()
  19. #define MAX_VOX_FILES 4
  20. #define RLIGHTS_IMPLEMENTATION
  21. #include "rlights.h"
  22. #if defined(PLATFORM_DESKTOP)
  23. #define GLSL_VERSION 330
  24. #else // PLATFORM_ANDROID, PLATFORM_WEB
  25. #define GLSL_VERSION 100
  26. #endif
  27. //------------------------------------------------------------------------------------
  28. // Program main entry point
  29. //------------------------------------------------------------------------------------
  30. int main(void)
  31. {
  32. // Initialization
  33. //--------------------------------------------------------------------------------------
  34. const int screenWidth = 800;
  35. const int screenHeight = 450;
  36. const char *voxFileNames[] = {
  37. "resources/models/vox/chr_knight.vox",
  38. "resources/models/vox/chr_sword.vox",
  39. "resources/models/vox/monu9.vox",
  40. "resources/models/vox/fez.vox"
  41. };
  42. InitWindow(screenWidth, screenHeight, "raylib [models] example - loading vox");
  43. // Define the camera to look into our 3d world
  44. Camera camera = { 0 };
  45. camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
  46. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point
  47. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  48. camera.fovy = 45.0f; // Camera field-of-view Y
  49. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  50. // Load MagicaVoxel files
  51. Model models[MAX_VOX_FILES] = { 0 };
  52. for (int i = 0; i < MAX_VOX_FILES; i++)
  53. {
  54. // Load VOX file and measure time
  55. double t0 = GetTime()*1000.0;
  56. models[i] = LoadModel(voxFileNames[i]);
  57. double t1 = GetTime()*1000.0;
  58. TraceLog(LOG_INFO, TextFormat("[%s] Model file loaded in %.3f ms", voxFileNames[i], t1 - t0));
  59. // Compute model translation matrix to center model on draw position (0, 0 , 0)
  60. BoundingBox bb = GetModelBoundingBox(models[i]);
  61. Vector3 center = { 0 };
  62. center.x = bb.min.x + (((bb.max.x - bb.min.x)/2));
  63. center.z = bb.min.z + (((bb.max.z - bb.min.z)/2));
  64. Matrix matTranslate = MatrixTranslate(-center.x, 0, -center.z);
  65. models[i].transform = matTranslate;
  66. }
  67. int currentModel = 0;
  68. Vector3 modelpos = { 0 };
  69. Vector3 camerarot = { 0 };
  70. // Load voxel shader
  71. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/voxel_lighting.vs", GLSL_VERSION),
  72. TextFormat("resources/shaders/glsl%i/voxel_lighting.fs", GLSL_VERSION));
  73. // Get some required shader locations
  74. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  75. // NOTE: "matModel" location name is automatically assigned on shader loading,
  76. // no need to get the location again if using that uniform name
  77. //shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(shader, "matModel");
  78. // Ambient light level (some basic lighting)
  79. int ambientLoc = GetShaderLocation(shader, "ambient");
  80. SetShaderValue(shader, ambientLoc, (float[4]) { 0.1f, 0.1f, 0.1f, 1.0f }, SHADER_UNIFORM_VEC4);
  81. // Assign out lighting shader to model
  82. for (int i = 0; i < MAX_VOX_FILES; i++)
  83. {
  84. for (int j = 0; j < models[i].materialCount; j++) models[i].materials[j].shader = shader;
  85. }
  86. // Create lights
  87. Light lights[MAX_LIGHTS] = { 0 };
  88. lights[0] = CreateLight(LIGHT_POINT, (Vector3) { -20, 20, -20 }, Vector3Zero(), GRAY, shader);
  89. lights[1] = CreateLight(LIGHT_POINT, (Vector3) { 20, -20, 20 }, Vector3Zero(), GRAY, shader);
  90. lights[2] = CreateLight(LIGHT_POINT, (Vector3) { -20, 20, 20 }, Vector3Zero(), GRAY, shader);
  91. lights[3] = CreateLight(LIGHT_POINT, (Vector3) { 20, -20, -20 }, Vector3Zero(), GRAY, shader);
  92. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  93. //--------------------------------------------------------------------------------------
  94. // Main game loop
  95. while (!WindowShouldClose()) // Detect window close button or ESC key
  96. {
  97. // Update
  98. //----------------------------------------------------------------------------------
  99. if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE))
  100. {
  101. const Vector2 mouseDelta = GetMouseDelta();
  102. camerarot.x = mouseDelta.x*0.05f;
  103. camerarot.y = mouseDelta.y*0.05f;
  104. }
  105. else
  106. {
  107. camerarot.x = 0;
  108. camerarot.y = 0;
  109. }
  110. UpdateCameraPro(&camera,
  111. (Vector3){ (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP))*0.1f - (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN))*0.1f, // Move forward-backward
  112. (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT))*0.1f - (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT))*0.1f, // Move right-left
  113. 0.0f }, // Move up-down
  114. camerarot, // Camera rotation
  115. GetMouseWheelMove()*-2.0f); // Move to target (zoom)
  116. // Cycle between models on mouse click
  117. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) currentModel = (currentModel + 1)%MAX_VOX_FILES;
  118. // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
  119. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  120. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  121. // Update light values (actually, only enable/disable them)
  122. for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(shader, lights[i]);
  123. //----------------------------------------------------------------------------------
  124. // Draw
  125. //----------------------------------------------------------------------------------
  126. BeginDrawing();
  127. ClearBackground(RAYWHITE);
  128. // Draw 3D model
  129. BeginMode3D(camera);
  130. DrawModel(models[currentModel], modelpos, 1.0f, WHITE);
  131. DrawGrid(10, 1.0);
  132. // Draw spheres to show where the lights are
  133. for (int i = 0; i < MAX_LIGHTS; i++)
  134. {
  135. if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lights[i].color);
  136. else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f));
  137. }
  138. EndMode3D();
  139. // Display info
  140. DrawRectangle(10, 40, 340, 70, Fade(SKYBLUE, 0.5f));
  141. DrawRectangleLines(10, 40, 340, 70, Fade(DARKBLUE, 0.5f));
  142. DrawText("- MOUSE LEFT BUTTON: CYCLE VOX MODELS", 20, 50, 10, BLUE);
  143. DrawText("- MOUSE MIDDLE BUTTON: ZOOM OR ROTATE CAMERA", 20, 70, 10, BLUE);
  144. DrawText("- UP-DOWN-LEFT-RIGHT KEYS: MOVE CAMERA", 20, 90, 10, BLUE);
  145. DrawText(TextFormat("Model file: %s", GetFileName(voxFileNames[currentModel])), 10, 10, 20, GRAY);
  146. EndDrawing();
  147. //----------------------------------------------------------------------------------
  148. }
  149. // De-Initialization
  150. //--------------------------------------------------------------------------------------
  151. // Unload models data (GPU VRAM)
  152. for (int i = 0; i < MAX_VOX_FILES; i++) UnloadModel(models[i]);
  153. CloseWindow(); // Close window and OpenGL context
  154. //--------------------------------------------------------------------------------------
  155. return 0;
  156. }