summaryrefslogtreecommitdiffhomepage
path: root/examples/web/models/models_obj_viewer.c
blob: 8254294255f471f9e8885f2d0ebf1c5d9f781d47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*******************************************************************************************
*
*   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-2019 Ramon Santamaria (@raysan5)
*
********************************************************************************************/

#include "raylib.h"

#include <string.h>        // Required for: strcpy()

#if defined(PLATFORM_WEB)
    #include <emscripten/emscripten.h>
#endif

//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;

// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)

Camera camera = { 0 };

Model model = { 0 };
Texture2D texture = { 0 };

Vector3 position = { 0.0f, 0.0f, 0.0f };
BoundingBox bounds = { 0 };
bool selected = false;              // Selected object flag

char objFilename[64] = "turret.obj";

//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
void UpdateDrawFrame(void);     // Update and Draw one frame

//----------------------------------------------------------------------------------
// Program Main Entry Point
//----------------------------------------------------------------------------------
int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    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 = LoadModel("resources/models/turret.obj");               // Load default model obj
    texture = LoadTexture("resources/models/turret_diffuse.png");   // Load default model texture
    model.materials[0].maps[MAP_DIFFUSE].texture = texture;         // Bind texture to model

    bounds = MeshBoundingBox(model.meshes[0]);  // Set model bounds

    SetCameraMode(camera, CAMERA_FREE);         // Set a free camera mode

#if defined(PLATFORM_WEB)
    emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
#else
    SetTargetFPS(60);               // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())    // Detect window close button or ESC key
    {
        UpdateDrawFrame();
    }
#endif

    // De-Initialization
    //--------------------------------------------------------------------------------------
    UnloadModel(model);         // Unload model

    ClearDroppedFiles();        // Clear internal buffers

    CloseWindow();              // Close window and OpenGL context
    //--------------------------------------------------------------------------------------

    return 0;
}

//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
void UpdateDrawFrame(void)
{
    // Update
    //----------------------------------------------------------------------------------
    if (IsFileDropped())
    {
        int count = 0;
        char **droppedFiles = GetDroppedFiles(&count);

        if (count == 1)
        {
            if (IsFileExtension(droppedFiles[0], ".obj"))
            {
                for (int i = 0; i < model.meshCount; i++) UnloadMesh(&model.meshes[i]);
                model.meshes = LoadMeshes(droppedFiles[0], &model.meshCount);
                bounds = MeshBoundingBox(model.meshes[0]);
            }
            else if (IsFileExtension(droppedFiles[0], ".png"))
            {
                UnloadTexture(texture);
                texture = LoadTexture(droppedFiles[0]);
                model.materials[0].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();
    //----------------------------------------------------------------------------------
}