瀏覽代碼

Add models_basic_voxel example (#5212)

Tim Little 2 月之前
父節點
當前提交
c551fb7118
共有 6 個文件被更改,包括 160 次插入1 次删除
  1. 1 0
      examples/Makefile
  2. 4 0
      examples/Makefile.Web
  3. 2 1
      examples/README.md
  4. 1 0
      examples/examples_list.txt
  5. 152 0
      examples/models/models_basic_voxel.c
  6. 二進制
      examples/models/models_basic_voxel.png

+ 1 - 0
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 \

+ 4 - 0
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

+ 2 - 1
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) | <img src="text/text_codepoints_loading.png" alt="text_codepoints_loading" width="80"> | ⭐⭐⭐☆ | 4.2 | 4.2 | [Ramon Santamaria](https://github.com/raysan5) |
 | [text_inline_styling](text/text_inline_styling.c) | <img src="text/text_inline_styling.png" alt="text_inline_styling" width="80"> | ⭐⭐⭐☆ | 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) | <img src="models/models_animation_gpu_skinning.png" alt="models_animation_gpu_skinning" width="80"> | ⭐⭐⭐☆ | 4.5 | 4.5 | [Daniel Holden](https://github.com/orangeduck) |
 | [models_bone_socket](models/models_bone_socket.c) | <img src="models/models_bone_socket.png" alt="models_bone_socket" width="80"> | ⭐⭐⭐⭐️ | 4.5 | 4.5 | [iP](https://github.com/ipzaur) |
 | [models_tesseract_view](models/models_tesseract_view.c) | <img src="models/models_tesseract_view.png" alt="models_tesseract_view" width="80"> | ⭐⭐☆☆ | 5.6-dev | 5.6-dev | [Timothy van der Valk](https://github.com/arceryz) |
+| [models_basic_voxel](models/models_basic_voxel.c) | <img src="models/models_basic_voxel.png" alt="models_basic_voxel" width="80"> | ⭐⭐☆☆ | 5.5 | 5.5 | [Tim Little](https://github.com/timlittle) |
 
 ### category: shaders [29]
 

+ 1 - 0
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

+ 152 - 0
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;
+}

二進制
examples/models/models_basic_voxel.png