You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

205 lines
7.6 KiB

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