Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

207 linhas
7.6 KiB

3 anos atrás
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - Load models vox (MagicaVoxel)
  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 - magicavoxel loading");
  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. //--------------------------------------------------------------------------------------
  51. // Load MagicaVoxel files
  52. Model models[MAX_VOX_FILES] = { 0 };
  53. for (int i = 0; i < MAX_VOX_FILES; i++)
  54. {
  55. // Load VOX file and measure time
  56. double t0 = GetTime() * 1000.0;
  57. models[i] = LoadModel(voxFileNames[i]);
  58. double t1 = GetTime() * 1000.0;
  59. TraceLog(LOG_WARNING, TextFormat("[%s] File loaded in %.3f ms", voxFileNames[i], t1 - t0));
  60. // Compute model translation matrix to center model on draw position (0, 0 , 0)
  61. BoundingBox bb = GetModelBoundingBox(models[i]);
  62. Vector3 center = { 0 };
  63. center.x = bb.min.x + (((bb.max.x - bb.min.x) / 2));
  64. center.z = bb.min.z + (((bb.max.z - bb.min.z) / 2));
  65. Matrix matTranslate = MatrixTranslate(-center.x, 0, -center.z);
  66. models[i].transform = matTranslate;
  67. }
  68. int currentModel = 0;
  69. //--------------------------------------------------------------------------------------
  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. Model m = models[i];
  85. for (int j = 0; j < m.materialCount; j++)
  86. {
  87. m.materials[j].shader = shader;
  88. }
  89. }
  90. // Create lights
  91. Light lights[MAX_LIGHTS] = { 0 };
  92. lights[0] = CreateLight(LIGHT_POINT, (Vector3) { -20, 20, -20 }, Vector3Zero(), GRAY, shader);
  93. lights[1] = CreateLight(LIGHT_POINT, (Vector3) { 20, -20, 20 }, Vector3Zero(), GRAY, shader);
  94. lights[2] = CreateLight(LIGHT_POINT, (Vector3) { -20, 20, 20 }, Vector3Zero(), GRAY, shader);
  95. lights[3] = CreateLight(LIGHT_POINT, (Vector3) { 20, -20, -20 }, Vector3Zero(), GRAY, shader);
  96. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  97. //--------------------------------------------------------------------------------------
  98. Vector3 modelpos = { 0 };
  99. Vector3 camerarot = { 0 };
  100. // Main game loop
  101. while (!WindowShouldClose()) // Detect window close button or ESC key
  102. {
  103. // Update
  104. //----------------------------------------------------------------------------------
  105. if (IsMouseButtonDown(MOUSE_BUTTON_MIDDLE))
  106. {
  107. const Vector2 mouseDelta = GetMouseDelta();
  108. camerarot.x = mouseDelta.x * 0.05f;
  109. camerarot.y = mouseDelta.y * 0.05f;
  110. }
  111. else
  112. {
  113. camerarot.x = 0;
  114. camerarot.y = 0;
  115. }
  116. UpdateCameraPro(&camera,
  117. (Vector3) {
  118. (IsKeyDown(KEY_W) || IsKeyDown(KEY_UP)) * 0.1f - // Move forward-backward
  119. (IsKeyDown(KEY_S) || IsKeyDown(KEY_DOWN)) * 0.1f,
  120. (IsKeyDown(KEY_D) || IsKeyDown(KEY_RIGHT)) * 0.1f - // Move right-left
  121. (IsKeyDown(KEY_A) || IsKeyDown(KEY_LEFT)) * 0.1f,
  122. 0.0f // Move up-down
  123. },
  124. camerarot,
  125. GetMouseWheelMove() * -2.0f); // Move to target (zoom)
  126. // Cycle between models on mouse click
  127. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) currentModel = (currentModel + 1) % MAX_VOX_FILES;
  128. // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
  129. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  130. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  131. // Update light values (actually, only enable/disable them)
  132. for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(shader, lights[i]);
  133. //----------------------------------------------------------------------------------
  134. // Draw
  135. //----------------------------------------------------------------------------------
  136. BeginDrawing();
  137. ClearBackground(RAYWHITE);
  138. // Draw 3D model
  139. BeginMode3D(camera);
  140. DrawModel(models[currentModel], modelpos, 1.0f, WHITE);
  141. DrawGrid(10, 1.0);
  142. // Draw spheres to show where the lights are
  143. for (int i = 0; i < MAX_LIGHTS; i++)
  144. {
  145. if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lights[i].color);
  146. else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f));
  147. }
  148. EndMode3D();
  149. // Display info
  150. DrawRectangle(10, 400, 340, 60, Fade(SKYBLUE, 0.5f));
  151. DrawRectangleLines(10, 400, 340, 60, Fade(DARKBLUE, 0.5f));
  152. DrawText("MOUSE LEFT BUTTON to CYCLE VOX MODELS", 40, 410, 10, BLUE);
  153. DrawText("MOUSE MIDDLE BUTTON to ZOOM OR ROTATE CAMERA", 40, 420, 10, BLUE);
  154. DrawText("UP-DOWN-LEFT-RIGHT KEYS to MOVE CAMERA", 40, 430, 10, BLUE);
  155. DrawText(TextFormat("File: %s", GetFileName(voxFileNames[currentModel])), 10, 10, 20, GRAY);
  156. EndDrawing();
  157. //----------------------------------------------------------------------------------
  158. }
  159. // De-Initialization
  160. //--------------------------------------------------------------------------------------
  161. // Unload models data (GPU VRAM)
  162. for (int i = 0; i < MAX_VOX_FILES; i++) UnloadModel(models[i]);
  163. CloseWindow(); // Close window and OpenGL context
  164. //--------------------------------------------------------------------------------------
  165. return 0;
  166. }