summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorprocfxgen <[email protected]>2021-09-04 19:55:09 +0200
committerGitHub <[email protected]>2021-09-04 19:55:09 +0200
commitdfc465ca6d5a22abe558c15af97376a955dc1954 (patch)
tree8b2c78b22e4339e2613ff0cac01da8f0233a2a17 /examples
parent93168304cd377fabec9dfa88469f2cc635cd6326 (diff)
downloadraylib-dfc465ca6d5a22abe558c15af97376a955dc1954.tar.gz
raylib-dfc465ca6d5a22abe558c15af97376a955dc1954.zip
new models_magicavoxel_loading example (#1940)
* new models_magicavoxel_loading example * Portable header-only file "magicavoxel_loader.h" for MagicaVoxel loader example. * models_magicavoxel_loading example added to CMakeLists.txt and Makefile * fix models_magicavoxel_loading example for linux. * * vox_loader into "src/external/vox_loader.h" * vox file support for "models.c" * updated example "models/models_magicavoxel_loading.c" * * Fix Vox_FreeArrays (removed memory leak) * * removed magicavoxel_loader.h * * Revert vs2019 solution
Diffstat (limited to 'examples')
-rw-r--r--examples/CMakeLists.txt2
-rw-r--r--examples/Makefile3
-rw-r--r--examples/models/models_magicavoxel_loading.c153
-rw-r--r--examples/models/resources/vox/chr_knight.voxbin0 -> 31031 bytes
-rw-r--r--examples/models/resources/vox/chr_sword.voxbin0 -> 30775 bytes
-rw-r--r--examples/models/resources/vox/monu9.voxbin0 -> 160767 bytes
6 files changed, 157 insertions, 1 deletions
diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt
index ad391edd..d102eb4c 100644
--- a/examples/CMakeLists.txt
+++ b/examples/CMakeLists.txt
@@ -86,7 +86,9 @@ if (${PLATFORM} MATCHES "Android")
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_obj_viewer.c)
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_animation.c)
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_first_person_maze.c)
+ list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/models/models_magicavoxel_loading.c)
+
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_custom_uniform.c)
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_model_shader.c)
list(REMOVE_ITEM example_sources ${CMAKE_CURRENT_SOURCE_DIR}/shaders/shaders_postprocessing.c)
diff --git a/examples/Makefile b/examples/Makefile
index 5abf0c70..f5cb2175 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -466,7 +466,8 @@ MODELS = \
models/models_skybox \
models/models_yaw_pitch_roll \
models/models_heightmap \
- models/models_waving_cubes
+ models/models_waving_cubes \
+ models/models_magicavoxel_loading
SHADERS = \
shaders/shaders_model_shader \
diff --git a/examples/models/models_magicavoxel_loading.c b/examples/models/models_magicavoxel_loading.c
new file mode 100644
index 00000000..bb1389d6
--- /dev/null
+++ b/examples/models/models_magicavoxel_loading.c
@@ -0,0 +1,153 @@
+/*******************************************************************************************
+*
+* raylib [models] example - magicavoxel loader and viewer
+*
+* This example has been created using raylib 3.8 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Example contributed by Johann Nadalutti
+*
+* Copyright (c) 2021 Johann Nadalutti
+*
+********************************************************************************************/
+
+#include "raylib.h"
+#include "raymath.h"
+
+#include <string.h>
+
+
+// VOX Files to load and view
+
+#define NUM_VOX_FILES 3
+
+const char* szVoxFiles[] = {
+ "resources/vox/chr_knight.vox",
+ "resources/vox/chr_sword.vox",
+ "resources/vox/monu9.vox"
+};
+
+
+int main(void)
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ const int screenWidth = 800;
+ const int screenHeight = 450;
+
+ InitWindow(screenWidth, screenHeight, "raylib [models] example - magicavoxel loading");
+
+ // Load MagicaVoxel files
+ Model models[NUM_VOX_FILES] = { 0 };
+
+ for (int i = 0; i < NUM_VOX_FILES; i++)
+ {
+ // Load MagicaVoxel File and build model
+ double t0, t1;
+ t0 = GetTime() * 1000.0;
+
+ models[i] = LoadModel(szVoxFiles[i]);
+
+ t1 = GetTime() * 1000.0;
+ TraceLog(LOG_INFO, TextFormat("Vox <%s> loaded in %f ms", GetFileName(szVoxFiles[i]), t1 - t0));
+
+ //Compute model matrix
+ BoundingBox bb = GetModelBoundingBox(models[i]);
+ Vector3 center;
+ center.x = -(((bb.max.x - bb.min.x) / 2));
+ center.y = -(((bb.max.y - bb.min.y) / 2));
+ center.z = -(((bb.max.z - bb.min.z) / 2));
+
+ Matrix matP = MatrixTranslate(center.x, center.z, 0);
+ Matrix matR = MatrixRotateX(90 * DEG2RAD);
+ models[i].transform = MatrixMultiply(matP, matR);
+
+
+ }
+
+
+ // Define the camera to look into our 3d world
+ Camera camera = { { 0.0f, 10.0f, 10.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
+
+ // Model drawing position
+ Vector3 position = { 0.0f, 0.0f, 0.0f };
+
+ int currentModel = 0;
+
+
+
+ SetCameraMode(camera, CAMERA_ORBITAL); // Set a orbital camera mode
+
+ SetTargetFPS(60); // Set our game to run at 60 frames-per-second
+
+ //--------------------------------------------------------------------------------------
+ // Main game loop
+ //--------------------------------------------------------------------------------------
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ //--------------------------------------------------------------------------------------
+ // Update
+ //----------------------------------------------------------------------------------
+ UpdateCamera(&camera); // Update internal camera and our camera
+
+ if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
+ {
+ currentModel = (currentModel + 1) % NUM_VOX_FILES; // Cycle between models
+ }
+
+ if (IsKeyPressed(KEY_RIGHT))
+ {
+ currentModel++;
+ if (currentModel >= NUM_VOX_FILES) currentModel = 0;
+ }
+ else if (IsKeyPressed(KEY_LEFT))
+ {
+ currentModel--;
+ if (currentModel < 0) currentModel = NUM_VOX_FILES - 1;
+ }
+
+ //----------------------------------------------------------------------------------
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ //Display model
+ BeginMode3D(camera);
+
+ Vector3 rotAxis = { 1,0,0 };
+ Vector3 scale = { 1,1,1 };
+
+
+ DrawModelEx(models[currentModel], position, rotAxis, 0, scale, WHITE);
+ //DrawModelWiresEx(models[currentModel], position, rotAxis, -90.0f, scale, BLACK);
+
+ DrawGrid(10, 1.0);
+
+ EndMode3D();
+
+ //Display debug infos
+ DrawRectangle(30, 400, 310, 30, Fade(SKYBLUE, 0.5f));
+ DrawRectangleLines(30, 400, 310, 30, Fade(DARKBLUE, 0.5f));
+ DrawText("MOUSE LEFT BUTTON to CYCLE VOX MODELS", 40, 410, 10, BLUE);
+
+ DrawText(GetFileName(szVoxFiles[currentModel]), 100, 10, 20, DARKBLUE);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+ }
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+
+ // Unload models data (GPU VRAM)
+ for (int i = 0; i < NUM_VOX_FILES; i++) UnloadModel(models[i]);
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
+
+
diff --git a/examples/models/resources/vox/chr_knight.vox b/examples/models/resources/vox/chr_knight.vox
new file mode 100644
index 00000000..c921bf5c
--- /dev/null
+++ b/examples/models/resources/vox/chr_knight.vox
Binary files differ
diff --git a/examples/models/resources/vox/chr_sword.vox b/examples/models/resources/vox/chr_sword.vox
new file mode 100644
index 00000000..05fc4826
--- /dev/null
+++ b/examples/models/resources/vox/chr_sword.vox
Binary files differ
diff --git a/examples/models/resources/vox/monu9.vox b/examples/models/resources/vox/monu9.vox
new file mode 100644
index 00000000..fd771112
--- /dev/null
+++ b/examples/models/resources/vox/monu9.vox
Binary files differ