diff --git a/examples/Makefile b/examples/Makefile index 8fb92ac2b..36faed3b3 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -607,6 +607,7 @@ TEXT = \ MODELS = \ models/models_animation_gpu_skinning \ models/models_animation_playing \ + models/models_basic_voxel \ models/models_billboard_rendering \ models/models_bone_socket \ models/models_box_collisions \ diff --git a/examples/Makefile.Web b/examples/Makefile.Web index 87c2866c6..b1b1f0779 100644 --- a/examples/Makefile.Web +++ b/examples/Makefile.Web @@ -607,6 +607,7 @@ TEXT = \ MODELS = \ models/models_animation_gpu_skinning \ models/models_animation_playing \ + models/models_basic_voxel \ models/models_billboard_rendering \ models/models_bone_socket \ models/models_box_collisions \ @@ -1049,6 +1050,9 @@ models/models_animation_playing: models/models_animation_playing.c --preload-file models/resources/models/iqm/guytex.png@resources/models/iqm/guytex.png \ --preload-file models/resources/models/iqm/guyanim.iqm@resources/models/iqm/guyanim.iqm +models/models_basic_voxel: models/models_basic_voxel.c + $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) + models/models_billboard_rendering: models/models_billboard_rendering.c $(CC) -o $@$(EXT) $< $(CFLAGS) $(INCLUDE_PATHS) $(LDFLAGS) $(LDLIBS) -D$(PLATFORM) \ --preload-file models/resources/billboard.png@resources/billboard.png diff --git a/examples/README.md b/examples/README.md index d7f56d8d4..48e83c852 100644 --- a/examples/README.md +++ b/examples/README.md @@ -145,7 +145,7 @@ Examples using raylib text functionality, including sprite fonts loading/generat | [text_codepoints_loading](text/text_codepoints_loading.c) | text_codepoints_loading | ⭐⭐⭐☆ | 4.2 | 4.2 | [Ramon Santamaria](https://github.com/raysan5) | | [text_inline_styling](text/text_inline_styling.c) | text_inline_styling | ⭐⭐⭐☆ | 5.6-dev | 5.6-dev | [Wagner Barongello](https://github.com/SultansOfCode) | -### category: models [23] +### category: models [24] Examples using raylib models functionality, including models loading/generation and drawing, provided by raylib [models](../src/rmodels.c) module. @@ -174,6 +174,7 @@ Examples using raylib models functionality, including models loading/generation | [models_animation_gpu_skinning](models/models_animation_gpu_skinning.c) | models_animation_gpu_skinning | ⭐⭐⭐☆ | 4.5 | 4.5 | [Daniel Holden](https://github.com/orangeduck) | | [models_bone_socket](models/models_bone_socket.c) | models_bone_socket | ⭐⭐⭐⭐️ | 4.5 | 4.5 | [iP](https://github.com/ipzaur) | | [models_tesseract_view](models/models_tesseract_view.c) | models_tesseract_view | ⭐⭐☆☆ | 5.6-dev | 5.6-dev | [Timothy van der Valk](https://github.com/arceryz) | +| [models_basic_voxel](models/models_basic_voxel.c) | models_basic_voxel | ⭐⭐☆☆ | 5.5 | 5.5 | [Tim Little](https://github.com/timlittle) | ### category: shaders [29] diff --git a/examples/examples_list.txt b/examples/examples_list.txt index adaa12390..b5682123f 100644 --- a/examples/examples_list.txt +++ b/examples/examples_list.txt @@ -128,6 +128,7 @@ models;models_textured_cube;★★☆☆;4.5;4.5;2022;2025;"Ramon Santamaria";@r models;models_animation_gpu_skinning;★★★☆;4.5;4.5;2024;2025;"Daniel Holden";@orangeduck models;models_bone_socket;★★★★;4.5;4.5;2024;2025;"iP";@ipzaur models;models_tesseract_view;★★☆☆;5.6-dev;5.6-dev;2024;2025;"Timothy van der Valk";@arceryz +models;models_basic_voxel;★★☆☆;5.5;5.5;2025;2025;"Tim Little";@timlittle shaders;shaders_basic_lighting;★★★★;3.0;4.2;2019;2025;"Chris Camacho";@chriscamacho shaders;shaders_model_shader;★★☆☆;1.3;3.7;2014;2025;"Ramon Santamaria";@raysan5 shaders;shaders_shapes_textures;★★☆☆;1.7;3.7;2015;2025;"Ramon Santamaria";@raysan5 diff --git a/examples/models/models_basic_voxel.c b/examples/models/models_basic_voxel.c new file mode 100644 index 000000000..8df15d73d --- /dev/null +++ b/examples/models/models_basic_voxel.c @@ -0,0 +1,152 @@ +/******************************************************************************************* +* +* raylib [models] example - basic voxel +* +* Example complexity rating: [★★☆☆] 2/4 +* +* Example originally created with raylib 5.5, last time updated with raylib 5.5 +* +* Example contributed by Tim Little (@timlittle) and reviewed by Ramon Santamaria (@raysan5) +* +* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified, +* BSD-like license that allows static linking with closed source software +* +* Copyright (c) 2025 Tim Little (@timlittle) +* +********************************************************************************************/ + +#include "raylib.h" +#include "raymath.h" + +#define WORLD_SIZE 8 // Size of our voxel world (8x8x8 cubes) + +//------------------------------------------------------------------------------------ +// Program main entry point +//------------------------------------------------------------------------------------ +int main(void) +{ + // Initialization + //-------------------------------------------------------------------------------------- + const int screenWidth = 800; + const int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib [models] example - basic voxel"); + + DisableCursor(); // Lock mouse to window center + + // Define the camera to look into our 3d world (first person) + Camera3D camera = { 0 }; + camera.position = (Vector3){ -2.0f, 0.0f, -2.0f }; // Camera position at ground level + camera.target = (Vector3){ 0.0f, 0.0f, 0.0f }; // Camera looking at point + camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector + camera.fovy = 45.0f; // Camera field-of-view Y + camera.projection = CAMERA_PERSPECTIVE; // Camera projection type + + // Create a cube model + Mesh cubeMesh = GenMeshCube(1.0f, 1.0f, 1.0f); // Create a unit cube mesh + Model cubeModel = LoadModelFromMesh(cubeMesh); // Convert mesh to a model + cubeModel.materials[0].maps[MATERIAL_MAP_DIFFUSE].color = BEIGE; + + // Initialize voxel world - fill with voxels + bool voxels[WORLD_SIZE][WORLD_SIZE][WORLD_SIZE] = { false }; + for (int x = 0; x < WORLD_SIZE; x++) + { + for (int y = 0; y < WORLD_SIZE; y++) + { + for (int z = 0; z < WORLD_SIZE; z++) + { + voxels[x][y][z] = true; + } + } + } + + SetTargetFPS(60); + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + UpdateCamera(&camera, CAMERA_FIRST_PERSON); + + // Handle voxel removal with mouse click + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) + { + // Cast a ray from the screen center (where crosshair would be) + Vector2 screenCenter = { screenWidth/2.0f, screenHeight/2.0f }; + Ray ray = GetMouseRay(screenCenter, camera); + + // Check ray collision with all voxels + bool voxelRemoved = false; + for (int x = 0; (x < WORLD_SIZE) && !voxelRemoved; x++) + { + for (int y = 0; (y < WORLD_SIZE) && !voxelRemoved; y++) + { + for (int z = 0; (z < WORLD_SIZE) && !voxelRemoved; z++) + { + if (!voxels[x][y][z]) continue; // Skip empty voxels + + // Build a bounding box for this voxel + Vector3 position = { x, y, z }; + BoundingBox box = { + (Vector3){ position.x - 0.5f, position.y - 0.5f, position.z - 0.5f }, + (Vector3){ position.x + 0.5f, position.y + 0.5f, position.z + 0.5f } + }; + + // Check ray-box collision + RayCollision collision = GetRayCollisionBox(ray, box); + if (collision.hit) + { + voxels[x][y][z] = false; // Remove this voxel + voxelRemoved = true; // Exit all loops + } + } + } + } + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginMode3D(camera); + + DrawGrid(10, 1.0f); + + // Draw all voxels + for (int x = 0; x < WORLD_SIZE; x++) + { + for (int y = 0; y < WORLD_SIZE; y++) + { + for (int z = 0; z < WORLD_SIZE; z++) + { + if (!voxels[x][y][z]) continue; + + Vector3 position = { x, y, z }; + DrawModel(cubeModel, position, 1.0f, BEIGE); + DrawCubeWires(position, 1.0f, 1.0f, 1.0f, BLACK); + } + } + } + + EndMode3D(); + + DrawText("Left-click a voxel to remove it!", 10, 10, 20, DARKGRAY); + DrawText("WASD to move, mouse to look around", 10, 35, 10, GRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadModel(cubeModel); + CloseWindow(); + //-------------------------------------------------------------------------------------- + + return 0; +} diff --git a/examples/models/models_basic_voxel.png b/examples/models/models_basic_voxel.png new file mode 100644 index 000000000..8bbcaf8da Binary files /dev/null and b/examples/models/models_basic_voxel.png differ