Merge pull request #13 from rcorre/models_loading

Complete models_loading example.
This commit is contained in:
Nikolas 2021-07-25 01:14:18 +02:00 committed by GitHub
commit aa23317a17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -7,8 +7,9 @@
usingnamespace @import("raylib");
pub fn main() anyerror!void
{
const resourceDir = "raylib/examples/models/resources/";
pub fn main() anyerror!void {
// Initialization
//--------------------------------------------------------------------------------------
const screenWidth = 800;
@ -22,14 +23,14 @@ pub fn main() anyerror!void
.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
.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
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
@ -51,8 +52,7 @@ pub fn main() anyerror!void
camera.Update();
// Load new models/textures on drag&drop
if (IsFileDropped())
{
if (IsFileDropped()) {
var count: c_int = 0;
var droppedFiles = GetDroppedFiles(&count);
@ -69,8 +69,7 @@ pub fn main() anyerror!void
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);
@ -83,17 +82,13 @@ pub fn main() anyerror!void
}
// 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;
}
}
//----------------------------------------------------------------------------------
@ -105,7 +100,7 @@ pub fn main() anyerror!void
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