diff --git a/examples/models/models_obj_viewer.c b/examples/models/models_obj_viewer.c new file mode 100644 index 000000000..55c583c20 --- /dev/null +++ b/examples/models/models_obj_viewer.c @@ -0,0 +1,127 @@ +/******************************************************************************************* +* +* raylib [models] example - OBJ models viewer +* +* This example has been created using raylib 2.0 (www.raylib.com) +* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) +* +* Copyright (c) 2014-2018 Ramon Santamaria (@raysan5) +* +********************************************************************************************/ + +#include "raylib.h" + +#include // Required for: strcpy() + +int main() +{ + // Initialization + //-------------------------------------------------------------------------------------- + int screenWidth = 800; + int screenHeight = 450; + + InitWindow(screenWidth, screenHeight, "raylib example - obj viewer"); + + // Define the camera to look into our 3d world + Camera camera = {{ 30.0f, 30.0f, 30.0f }, { 0.0f, 10.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; + + Model model = LoadModel("resources/models/turret.obj"); // Load default model obj + Texture2D texture = LoadTexture("resources/models/turret_diffuse.png"); // Load default model texture + model.material.maps[MAP_DIFFUSE].texture = texture; // Bind texture to model + + Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position + BoundingBox bounds = MeshBoundingBox(model.mesh); // Set model bounds + bool selected = false; // Selected object flag + + SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode + + char objFilename[64] = "turret.obj"; + + SetTargetFPS(60); // Set our game to run at 60 frames-per-second + //-------------------------------------------------------------------------------------- + + // Main game loop + while (!WindowShouldClose()) // Detect window close button or ESC key + { + // Update + //---------------------------------------------------------------------------------- + if (IsFileDropped()) + { + int count = 0; + char **droppedFiles = GetDroppedFiles(&count); + + if (count == 1) + { + if (IsFileExtension(droppedFiles[0], ".obj")) + { + UnloadMesh(&model.mesh); + model.mesh = LoadMesh(droppedFiles[0]); + bounds = MeshBoundingBox(model.mesh); + } + else if (IsFileExtension(droppedFiles[0], ".png")) + { + UnloadTexture(texture); + texture = LoadTexture(droppedFiles[0]); + model.material.maps[MAP_DIFFUSE].texture = texture; + } + + strcpy(objFilename, GetFileName(droppedFiles[0])); + } + + ClearDroppedFiles(); // Clear internal buffers + } + + UpdateCamera(&camera); + + // Select model on mouse click + if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)) + { + // Check collision between ray and box + if (CheckCollisionRayBox(GetMouseRay(GetMousePosition(), camera), bounds)) selected = !selected; + else selected = false; + } + //---------------------------------------------------------------------------------- + + // Draw + //---------------------------------------------------------------------------------- + BeginDrawing(); + + ClearBackground(RAYWHITE); + + BeginMode3D(camera); + + DrawModel(model, position, 1.0f, WHITE); // Draw 3d model with texture + + DrawGrid(20.0, 10.0); // Draw a grid + + if (selected) DrawBoundingBox(bounds, GREEN); + + EndMode3D(); + + DrawText("Free camera default controls:", 10, 20, 10, DARKGRAY); + DrawText("- Mouse Wheel to Zoom in-out", 20, 40, 10, GRAY); + DrawText("- Mouse Wheel Pressed to Pan", 20, 60, 10, GRAY); + DrawText("- Alt + Mouse Wheel Pressed to Rotate", 20, 80, 10, GRAY); + DrawText("- Alt + Ctrl + Mouse Wheel Pressed for Smooth Zoom", 20, 100, 10, GRAY); + + DrawText("Drag & drop .obj/.png to load mesh/texture.", 10, GetScreenHeight() - 20, 10, DARKGRAY); + DrawText(FormatText("Current file: %s", objFilename), 250, GetScreenHeight() - 20, 10, GRAY); + if (selected) DrawText("MODEL SELECTED", GetScreenWidth() - 110, 10, 10, GREEN); + + DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY); + + EndDrawing(); + //---------------------------------------------------------------------------------- + } + + // De-Initialization + //-------------------------------------------------------------------------------------- + UnloadModel(model); // Unload model + + ClearDroppedFiles(); // Clear internal buffers + + CloseWindow(); // Close window and OpenGL context + //-------------------------------------------------------------------------------------- + + return 0; +} \ No newline at end of file diff --git a/examples/models/models_obj_viewer.png b/examples/models/models_obj_viewer.png new file mode 100644 index 000000000..6ac707675 Binary files /dev/null and b/examples/models/models_obj_viewer.png differ diff --git a/examples/models/resources/models/cube.obj b/examples/models/resources/models/cube.obj new file mode 100644 index 000000000..0e9d65979 --- /dev/null +++ b/examples/models/resources/models/cube.obj @@ -0,0 +1,98 @@ +# reference material +mtllib myCube.mtl + +# object Pau_Box + +# vertex (XZY) +v 5.5 0 1.5 +v 8.5 0 1.5 +v 5.5 0 -1.5 +v 8.5 0 -1.5 +v 5.5 3 1.5 +v 8.5 3 1.5 +v 5.5 3 -1.5 +v 8.5 3 -1.5 + +# normals (XYZ) +# red +vn 0 -1 0 +#blue +vn 0 1 0 +#top +vn 0 0 1 +#yellow +vn 1 0 0 +#bottom +vn 0 0 -1 +#green +vn -1 0 0 + + +# UVs (XY) +# yellow (1234) +vt 0.5 0 0 +vt 1 0 0 +vt 1 0.5 0 +vt 0.5 0.5 0 +# red (5678) +vt 0.5 0.5 0 +vt 1 0.5 0 +vt 0.5 1 0 +vt 1 1 0 +#bottom (9101112) +vt 0 0.5 0 +vt 1 0.5 0 +vt 1 0 0 +vt 0 0 0 +#top (13141516) +vt 0 0.5 0 +vt 1 0.5 0 +vt 1 1 0 +vt 0 1 0 +#green (17181920) +vt 0.5 0 0 +vt 0 0 0 +vt 0 0.5 0 +vt 0.5 0.5 0 +#blue (21222324) +vt 0 0.5 0 +vt 0.5 0.5 0 +vt 0.5 1 0 +vt 0 1 0 + +# merger +g Pau_Box + +# reference material +usemtl Material_1 + +# bottom +f 1/9/1 3/10/1 4/11/1 +f 4/11/1 2/12/1 1/9/1 + +# top +f 5/13/2 6/14/2 8/15/2 +f 8/15/2 7/16/2 5/13/2 + +# front-yellow +f 1/17/6 2/18/6 6/19/6 +f 6/19/6 5/20/6 1/17/6 + +# right-blue +f 2/6/1 4/5/1 8/7/1 +f 8/7/1 6/8/1 2/6/1 + +# back-green +f 4/2/3 3/1/3 7/4/3 +f 7/4/3 8/3/3 4/2/3 + +# left-red +f 3/22/5 1/21/5 5/24/5 +f 5/24/5 7/23/5 3/22/5 + + + + + + + diff --git a/examples/models/resources/models/cube_diffuse.png b/examples/models/resources/models/cube_diffuse.png new file mode 100644 index 000000000..25e0377ad Binary files /dev/null and b/examples/models/resources/models/cube_diffuse.png differ