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.
This commit is contained in:
Ryan Roden-Corrent 2021-07-24 16:21:49 -04:00
parent 1996635c5b
commit 51b327fa03
No known key found for this signature in database
GPG Key ID: 435D8B10692555C9

View File

@ -1,14 +1,15 @@
// //
// models_loading // models_loading
// Zig version: // Zig version:
// Author: Nikolas Wipper // Author: Nikolas Wipper
// Date: 2020-02-15 // Date: 2020-02-15
// //
usingnamespace @import("raylib"); usingnamespace @import("raylib");
pub fn main() anyerror!void const resourceDir = "raylib/examples/models/resources/";
{
pub fn main() anyerror!void {
// Initialization // Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
const screenWidth = 800; const screenWidth = 800;
@ -17,42 +18,41 @@ pub fn main() anyerror!void
InitWindow(screenWidth, screenHeight, "raylib [models] example - models loading"); InitWindow(screenWidth, screenHeight, "raylib [models] example - models loading");
// Define the camera to look into our 3d world // Define the camera to look into our 3d world
var camera = Camera { var camera = Camera{
.position = Vector3 { .x = 50.0, .y = 50.0, .z = 50.0 }, // Camera position .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 .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) .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 .fovy = 45.0, // Camera field-of-view Y
.type = CameraType.CAMERA_PERSPECTIVE // Camera mode type .type = CameraType.CAMERA_PERSPECTIVE, // Camera mode type
}; };
var model = LoadModel("resources/models/castle.obj"); // Load model var model = LoadModel(resourceDir ++ "models/castle.obj"); // Load model
var texture = LoadTexture("resources/models/castle_diffuse.png"); // Load model texture var texture = LoadTexture(resourceDir ++ "models/castle_diffuse.png"); // Load model texture
model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set map diffuse 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, // NOTE: bounds are calculated from the original size of the model,
// if model is scaled on drawing, bounds must be also scaled // 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 // Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key while (!WindowShouldClose()) // Detect window close button or ESC key
{ {
// Update // Update
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
camera.Update(); camera.Update();
// Load new models/textures on drag&drop // Load new models/textures on drag&drop
if (IsFileDropped()) if (IsFileDropped()) {
{
var count: c_int = 0; var count: c_int = 0;
var droppedFiles = GetDroppedFiles(&count); var droppedFiles = GetDroppedFiles(&count);
@ -60,17 +60,16 @@ pub fn main() anyerror!void
{ {
if (IsFileExtension(droppedFiles[0], ".obj") or if (IsFileExtension(droppedFiles[0], ".obj") or
IsFileExtension(droppedFiles[0], ".gltf") 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 UnloadModel(model); // Unload previous model
model = LoadModel(droppedFiles[0]); // Load new model model = LoadModel(droppedFiles[0]); // Load new model
model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set current map diffuse texture model.materials[0].maps[@enumToInt(MAP_DIFFUSE)].texture = texture; // Set current map diffuse texture
bounds = MeshBoundingBox(model.meshes[0]); bounds = MeshBoundingBox(model.meshes[0]);
// TODO: Move camera position from target enough distance to visualize model properly // 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 // Unload current model texture and load new one
UnloadTexture(texture); UnloadTexture(texture);
@ -79,21 +78,17 @@ pub fn main() anyerror!void
} }
} }
ClearDroppedFiles(); // Clear internal buffers ClearDroppedFiles(); // Clear internal buffers
} }
// Select model on mouse click // Select model on mouse click
if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) if (IsMouseButtonPressed(MouseButton.MOUSE_LEFT_BUTTON)) {
{
// Check collision between ray and box // Check collision between ray and box
//if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) {
//{ selected = !selected;
// selected = !selected; } else {
//} selected = false;
//else }
//{
// selected = false;
//}
} }
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -101,24 +96,24 @@ pub fn main() anyerror!void
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
BeginDrawing(); 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); DrawText("Drag & drop model to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY);
if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN); 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(); EndDrawing();
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -126,9 +121,9 @@ pub fn main() anyerror!void
// De-Initialization // De-Initialization
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
UnloadTexture(texture); // Unload texture UnloadTexture(texture); // Unload texture
UnloadModel(model); // Unload model UnloadModel(model); // Unload model
CloseWindow(); // Close window and OpenGL context CloseWindow(); // Close window and OpenGL context
//-------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------
} }