raylib/examples/models/models_obj_viewer.c

128 lines
5.1 KiB
C
Raw Normal View History

2018-12-20 11:52:52 +03:00
/*******************************************************************************************
*
* 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)
*
2019-04-07 18:49:12 +03:00
* Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
2018-12-20 11:52:52 +03:00
*
********************************************************************************************/
#include "raylib.h"
2018-12-20 13:50:21 +03:00
#include <string.h> // Required for: strcpy()
2018-12-20 11:52:52 +03:00
2019-05-20 17:36:42 +03:00
int main(void)
2018-12-20 11:52:52 +03:00
{
// Initialization
//--------------------------------------------------------------------------------------
2019-05-20 17:36:42 +03:00
const int screenWidth = 800;
const int screenHeight = 450;
2018-12-20 11:52:52 +03:00
InitWindow(screenWidth, screenHeight, "raylib example - obj viewer");
// Define the camera to look into our 3d world
2019-05-27 01:18:15 +03:00
Camera camera = { { 30.0f, 30.0f, 30.0f }, { 0.0f, 10.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
2018-12-20 11:52:52 +03:00
Model model = LoadModel("resources/models/turret.obj"); // Load default model obj
Texture2D texture = LoadTexture("resources/models/turret_diffuse.png"); // Load default model texture
2019-03-29 22:22:50 +03:00
model.materials[0].maps[MAP_DIFFUSE].texture = texture; // Bind texture to model
2019-04-05 14:13:42 +03:00
2019-03-29 22:22:50 +03:00
Vector3 position = { 0.0, 0.0, 0.0 }; // Set model position
2019-04-05 14:13:42 +03:00
BoundingBox bounds = MeshBoundingBox(model.meshes[0]); // Set model bounds
2019-03-29 22:22:50 +03:00
bool selected = false; // Selected object flag
2019-04-05 14:13:42 +03:00
2019-03-29 22:22:50 +03:00
SetCameraMode(camera, CAMERA_FREE); // Set a free camera mode
2018-12-20 11:52:52 +03:00
char objFilename[64] = "turret.obj";
2019-05-20 17:36:42 +03:00
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
2018-12-20 11:52:52 +03:00
//--------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
if (IsFileDropped())
{
int count = 0;
char **droppedFiles = GetDroppedFiles(&count);
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
if (count == 1)
{
if (IsFileExtension(droppedFiles[0], ".obj"))
{
for (int i = 0; i < model.meshCount; i++) UnloadMesh(model.meshes[i]);
2019-04-05 14:13:42 +03:00
model.meshes = LoadMeshes(droppedFiles[0], &model.meshCount);
2019-03-29 22:22:50 +03:00
bounds = MeshBoundingBox(model.meshes[0]);
2018-12-20 11:52:52 +03:00
}
else if (IsFileExtension(droppedFiles[0], ".png"))
{
UnloadTexture(texture);
texture = LoadTexture(droppedFiles[0]);
2019-03-29 22:22:50 +03:00
model.materials[0].maps[MAP_DIFFUSE].texture = texture;
2018-12-20 11:52:52 +03:00
}
strcpy(objFilename, GetFileName(droppedFiles[0]));
}
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
ClearDroppedFiles(); // Clear internal buffers
}
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
UpdateCamera(&camera);
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
// 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
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
if (selected) DrawBoundingBox(bounds, GREEN);
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
EndMode3D();
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
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);
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
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);
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
DrawText("(c) Turret 3D model by Alberto Cano", screenWidth - 200, screenHeight - 20, 10, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texture); // Unload texture
2018-12-20 11:52:52 +03:00
UnloadModel(model); // Unload model
ClearDroppedFiles(); // Clear internal buffers
2019-04-05 14:13:42 +03:00
2018-12-20 11:52:52 +03:00
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}