From 51b327fa032845cad38a04803df3030d85ae627f Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Sat, 24 Jul 2021 16:21:49 -0400 Subject: [PATCH] Complete models_loading example. This had a few things missing/commented, but after fixing the load paths and uncommenting code, it seems to work fine for me. I also ran `zig format` on the example, I can submit a PR without that if it isn't ideal. --- examples/models/models_loading.zig | 93 ++++++++++++++---------------- 1 file changed, 44 insertions(+), 49 deletions(-) diff --git a/examples/models/models_loading.zig b/examples/models/models_loading.zig index eb3f98d..4e34a2b 100755 --- a/examples/models/models_loading.zig +++ b/examples/models/models_loading.zig @@ -1,14 +1,15 @@ // // models_loading -// Zig version: +// Zig version: // Author: Nikolas Wipper // Date: 2020-02-15 // usingnamespace @import("raylib"); -pub fn main() anyerror!void -{ +const resourceDir = "raylib/examples/models/resources/"; + +pub fn main() anyerror!void { // Initialization //-------------------------------------------------------------------------------------- const screenWidth = 800; @@ -17,42 +18,41 @@ pub fn main() anyerror!void InitWindow(screenWidth, screenHeight, "raylib [models] example - models loading"); // Define the camera to look into our 3d world - var camera = Camera { - .position = Vector3 { .x = 50.0, .y = 50.0, .z = 50.0 }, // Camera position - .target = Vector3 { .x = 0.0, .y = 10.0, .z = 0.0 }, // Camera looking at point - .up = Vector3 { .x = 0.0, .y = 1.0, .z = 0.0 }, // Camera up vector (rotation towards target) - .fovy = 45.0, // Camera field-of-view Y - .type = CameraType.CAMERA_PERSPECTIVE // Camera mode type + var camera = Camera{ + .position = Vector3{ .x = 50.0, .y = 50.0, .z = 50.0 }, // Camera position + .target = Vector3{ .x = 0.0, .y = 10.0, .z = 0.0 }, // Camera looking at point + .up = Vector3{ .x = 0.0, .y = 1.0, .z = 0.0 }, // Camera up vector (rotation towards target) + .fovy = 45.0, // Camera field-of-view Y + .type = CameraType.CAMERA_PERSPECTIVE, // Camera mode type }; - var model = LoadModel("resources/models/castle.obj"); // Load model - var texture = LoadTexture("resources/models/castle_diffuse.png"); // Load model texture - model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set map diffuse texture + var model = LoadModel(resourceDir ++ "models/castle.obj"); // Load model + var texture = LoadTexture(resourceDir ++ "models/castle_diffuse.png"); // Load model texture + model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set map diffuse texture - //var position = Vector3 { .x = 0.0, .y = 0.0, .z = 0.0 }; // Set model position + var position = Vector3{ .x = 0.0, .y = 0.0, .z = 0.0 }; // Set model position - var bounds = MeshBoundingBox(model.meshes[0]); // Set model bounds + var bounds = MeshBoundingBox(model.meshes[0]); // Set model bounds // NOTE: bounds are calculated from the original size of the model, // if model is scaled on drawing, bounds must be also scaled - camera.SetMode(CameraMode.CAMERA_FREE); // Set a free camera mode + camera.SetMode(CameraMode.CAMERA_FREE); // Set a free camera mode - var selected = false; // Selected object flag + var selected = false; // Selected object flag - SetTargetFPS(60); // Set our game to run at 60 frames-per-second + SetTargetFPS(60); // Set our game to run at 60 frames-per-second //-------------------------------------------------------------------------------------- // Main game loop - while (!WindowShouldClose()) // Detect window close button or ESC key + while (!WindowShouldClose()) // Detect window close button or ESC key { // Update //---------------------------------------------------------------------------------- camera.Update(); // Load new models/textures on drag&drop - if (IsFileDropped()) - { + if (IsFileDropped()) { var count: c_int = 0; var droppedFiles = GetDroppedFiles(&count); @@ -60,17 +60,16 @@ pub fn main() anyerror!void { if (IsFileExtension(droppedFiles[0], ".obj") or IsFileExtension(droppedFiles[0], ".gltf") or - IsFileExtension(droppedFiles[0], ".iqm")) // Model file formats supported + IsFileExtension(droppedFiles[0], ".iqm")) // Model file formats supported { - UnloadModel(model); // Unload previous model - model = LoadModel(droppedFiles[0]); // Load new model + UnloadModel(model); // Unload previous model + model = LoadModel(droppedFiles[0]); // Load new model model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set current map diffuse texture bounds = MeshBoundingBox(model.meshes[0]); // TODO: Move camera position from target enough distance to visualize model properly - } - else if (IsFileExtension(droppedFiles[0], ".png")) // Texture file formats supported + } else if (IsFileExtension(droppedFiles[0], ".png")) // Texture file formats supported { // Unload current model texture and load new one UnloadTexture(texture); @@ -79,21 +78,17 @@ pub fn main() anyerror!void } } - ClearDroppedFiles(); // Clear internal buffers + ClearDroppedFiles(); // Clear internal buffers } // Select model on mouse click - if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) - { + if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) { // Check collision between ray and box - //if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) - //{ - // selected = !selected; - //} - //else - //{ - // selected = false; - //} + if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) { + selected = !selected; + } else { + selected = false; + } } //---------------------------------------------------------------------------------- @@ -101,24 +96,24 @@ pub fn main() anyerror!void //---------------------------------------------------------------------------------- BeginDrawing(); - ClearBackground(RAYWHITE); + ClearBackground(RAYWHITE); - camera.Begin(); + camera.Begin(); - //DrawModel(model, position, 1.0, WHITE); // Draw 3d model with texture + DrawModel(model, position, 1.0, WHITE); // Draw 3d model with texture - DrawGrid(20, 10.0); // Draw a grid + DrawGrid(20, 10.0); // Draw a grid - if (selected) DrawBoundingBox(bounds, GREEN); // Draw selection box + if (selected) DrawBoundingBox(bounds, GREEN); // Draw selection box - camera.End(); + camera.End(); - DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY); - if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN); + DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY); + if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN); - DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY); + DrawText("(c) Castle 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY); - DrawFPS(10, 10); + DrawFPS(10, 10); EndDrawing(); //---------------------------------------------------------------------------------- @@ -126,9 +121,9 @@ pub fn main() anyerror!void // De-Initialization //-------------------------------------------------------------------------------------- - UnloadTexture(texture); // Unload texture - UnloadModel(model); // Unload model + UnloadTexture(texture); // Unload texture + UnloadModel(model); // Unload model - CloseWindow(); // Close window and OpenGL context + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- }