aboutsummaryrefslogtreecommitdiff
path: root/src/camera.h
blob: 72a0e706235aa635f55d1aa0928f233987f92ef0 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
/*******************************************************************************************
*
*   raylib Camera System - Camera Modes Setup and Control Functions
*
*   #define CAMERA_IMPLEMENTATION
*       Generates the implementation of the library into the included file.
*       If not defined, the library is in header only mode and can be included in other headers 
*       or source files without problems. But only ONE file should hold the implementation.
*
*   #define CAMERA_STANDALONE
*       If defined, the library can be used as standalone as a camera system but some
*       functions must be redefined to manage inputs accordingly.
*
*   NOTE: Memory footprint of this library is aproximately 112 bytes
*
*   Initial design by Marc Palau (2014)
*   Reviewed by Ramon Santamaria (2015-2016)
*
*   This software is provided "as-is", without any express or implied warranty. In no event
*   will the authors be held liable for any damages arising from the use of this software.
*
*   Permission is granted to anyone to use this software for any purpose, including commercial
*   applications, and to alter it and redistribute it freely, subject to the following restrictions:
*
*     1. The origin of this software must not be misrepresented; you must not claim that you
*     wrote the original software. If you use this software in a product, an acknowledgment
*     in the product documentation would be appreciated but is not required.
*
*     2. Altered source versions must be plainly marked as such, and must not be misrepresented
*     as being the original software.
*
*     3. This notice may not be removed or altered from any source distribution.
*
**********************************************************************************************/

#ifndef CAMERA_H
#define CAMERA_H

//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
//...

//----------------------------------------------------------------------------------
// Types and Structures Definition
// NOTE: Below types are required for CAMERA_STANDALONE usage
//----------------------------------------------------------------------------------
#if defined(CAMERA_STANDALONE)
    // Camera modes
    typedef enum { CAMERA_CUSTOM = 0, CAMERA_FREE, CAMERA_ORBITAL, CAMERA_FIRST_PERSON, CAMERA_THIRD_PERSON } CameraMode;

    // Vector2 type
    typedef struct Vector2 {
        float x;
        float y;
    } Vector2;

    // Vector3 type
    typedef struct Vector3 {
        float x;
        float y;
        float z;
    } Vector3;

    // Camera type, defines a camera position/orientation in 3d space
    typedef struct Camera {
        Vector3 position;
        Vector3 target;
        Vector3 up;
    } Camera;
#endif

#ifdef __cplusplus
extern "C" {            // Prevents name mangling of functions
#endif

//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
//...

//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
#if defined(CAMERA_STANDALONE)
void SetCameraMode(int mode);                               // Set camera mode (multiple camera modes available)
void UpdateCamera(Camera *camera);                          // Update camera (player position is ignored)
void UpdateCameraPlayer(Camera *camera, Vector3 *position); // Update camera and player position (1st person and 3rd person cameras)

void SetCameraPosition(Vector3 position);                   // Set internal camera position
void SetCameraTarget(Vector3 target);                       // Set internal camera target
void SetCameraFovy(float fovy);                             // Set internal camera field-of-view-y

void SetCameraPanControl(int panKey);                       // Set camera pan key to combine with mouse movement (free camera)
void SetCameraAltControl(int altKey);                       // Set camera alt key to combine with mouse movement (free camera)
void SetCameraSmoothZoomControl(int szKey);                 // Set camera smooth zoom key to combine with mouse (free camera)

void SetCameraMoveControls(int frontKey, int backKey, 
                           int leftKey, int rightKey, 
                           int upKey, int downKey);         // Set camera move controls (1st person and 3rd person cameras)
void SetCameraMouseSensitivity(float sensitivity);          // Set camera mouse sensitivity (1st person and 3rd person cameras)
#endif

#ifdef __cplusplus
}
#endif

#endif // CAMERA_H


/***********************************************************************************
*
*   CAMERA IMPLEMENTATION
*
************************************************************************************/

#if defined(CAMERA_IMPLEMENTATION)

#include <math.h>               // Required for: sqrt(), sin(), cos()

#ifndef PI
    #define PI 3.14159265358979323846
#endif

#ifndef DEG2RAD
    #define DEG2RAD (PI/180.0f)
#endif

#ifndef RAD2DEG
    #define RAD2DEG (180.0f/PI)
#endif

//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
// CAMERA_GENERIC
#define CAMERA_SCROLL_SENSITIVITY                       1.5f

// FREE_CAMERA
#define CAMERA_FREE_MOUSE_SENSITIVITY                   0.01f
#define CAMERA_FREE_DISTANCE_MIN_CLAMP                  0.3f
#define CAMERA_FREE_DISTANCE_MAX_CLAMP                  120.0f
#define CAMERA_FREE_MIN_CLAMP                           85.0f
#define CAMERA_FREE_MAX_CLAMP                          -85.0f
#define CAMERA_FREE_SMOOTH_ZOOM_SENSITIVITY             0.05f
#define CAMERA_FREE_PANNING_DIVIDER                     5.1f

// ORBITAL_CAMERA
#define CAMERA_ORBITAL_SPEED                            0.01f

// FIRST_PERSON
//#define CAMERA_FIRST_PERSON_MOUSE_SENSITIVITY           0.003f
#define CAMERA_FIRST_PERSON_FOCUS_DISTANCE              25.0f
#define CAMERA_FIRST_PERSON_MIN_CLAMP                   85.0f
#define CAMERA_FIRST_PERSON_MAX_CLAMP                  -85.0f

#define CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER  5.0f
#define CAMERA_FIRST_PERSON_STEP_DIVIDER                30.0f
#define CAMERA_FIRST_PERSON_WAVING_DIVIDER              200.0f

#define CAMERA_FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION  0.85f

// THIRD_PERSON
//#define CAMERA_THIRD_PERSON_MOUSE_SENSITIVITY           0.003f
#define CAMERA_THIRD_PERSON_DISTANCE_CLAMP              1.2f
#define CAMERA_THIRD_PERSON_MIN_CLAMP                   5.0f
#define CAMERA_THIRD_PERSON_MAX_CLAMP                  -85.0f
#define CAMERA_THIRD_PERSON_OFFSET                      (Vector3){ 0.4f, 0.0f, 0.0f }

// PLAYER (used by camera)
#define PLAYER_WIDTH                        0.4f
#define PLAYER_HEIGHT                       0.9f
#define PLAYER_DEPTH                        0.4f
#define PLAYER_MOVEMENT_DIVIDER             20.0f

//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
// Camera move modes (first person and third person cameras)
typedef enum { MOVE_FRONT = 0, MOVE_LEFT, MOVE_BACK, MOVE_RIGHT, MOVE_UP, MOVE_DOWN } CameraMove;

//----------------------------------------------------------------------------------
// Global Variables Definition
//----------------------------------------------------------------------------------
static Camera internalCamera = {{ 2.0f, 0.0f, 2.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f };

static Vector2 cameraAngle = { 0.0f, 0.0f };
static float cameraTargetDistance = 5.0f;
static Vector2 cameraMousePosition = { 0.0f, 0.0f };
static Vector2 cameraMouseVariation = { 0.0f, 0.0f };

static int cameraMoveControl[6]  = { 'W', 'A', 'S', 'D', 'E', 'Q' };
static int cameraPanControlKey = 2;                   // raylib: MOUSE_MIDDLE_BUTTON
static int cameraAltControlKey = 342;                 // raylib: KEY_LEFT_ALT
static int cameraSmoothZoomControlKey = 341;          // raylib: KEY_LEFT_CONTROL

static int cameraMoveCounter = 0;                     // Used for 1st person swinging movement
static float cameraMouseSensitivity = 0.003f;         // How sensible is camera movement to mouse movement

static int cameraMode = CAMERA_CUSTOM;                // Current internal camera mode

//----------------------------------------------------------------------------------
// Module specific Functions Declaration
//----------------------------------------------------------------------------------
static void ProcessCamera(Camera *camera, Vector3 *playerPosition);

#if defined(CAMERA_STANDALONE)
// NOTE: Camera controls depend on some raylib input functions
// TODO: Set your own input functions (used in ProcessCamera())
static Vector2 GetMousePosition() { return (Vector2){ 0.0f, 0.0f }; }
static void SetMousePosition(Vector2 pos) {} 
static int IsMouseButtonDown(int button) { return 0;}
static int GetMouseWheelMove() { return 0; }
static int GetScreenWidth() { return 1280; }
static int GetScreenHeight() { return 720; }
static void ShowCursor() {}
static void HideCursor() {}
static int IsKeyDown(int key) { return 0; }
#endif

//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------

// Select camera mode (multiple camera modes available)
// TODO: Review hardcoded values when changing modes...
void SetCameraMode(int mode)
{
    if ((cameraMode == CAMERA_FIRST_PERSON) && (mode == CAMERA_FREE))
    {
        cameraMode = CAMERA_THIRD_PERSON;
        cameraTargetDistance = 5.0f;
        cameraAngle.y = -40*DEG2RAD;
        ProcessCamera(&internalCamera, &internalCamera.position);
    }
    else if ((cameraMode == CAMERA_FIRST_PERSON) && (mode == CAMERA_ORBITAL))
    {
        cameraMode = CAMERA_THIRD_PERSON;
        cameraTargetDistance = 5.0f;
        cameraAngle.y = -40*DEG2RAD;
        ProcessCamera(&internalCamera, &internalCamera.position);
    }
    else if ((cameraMode == CAMERA_CUSTOM) && (mode == CAMERA_FREE))
    {
        cameraTargetDistance = 10.0f;
        cameraAngle.x = 45*DEG2RAD;
        cameraAngle.y = -40*DEG2RAD;
        internalCamera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
        ProcessCamera(&internalCamera, &internalCamera.position);
        
        ShowCursor();
    }
    else if ((cameraMode == CAMERA_CUSTOM) && (mode == CAMERA_ORBITAL))
    {
        cameraTargetDistance = 10.0f;
        cameraAngle.x = 225*DEG2RAD;
        cameraAngle.y = -40*DEG2RAD;
        internalCamera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
        ProcessCamera(&internalCamera, &internalCamera.position);
    }

    cameraMode = mode;
}

// Update camera (player position is ignored)
void UpdateCamera(Camera *camera)
{
    Vector3 position = { 0.0f, 0.0f, 0.0f };
    
    // Process internal camera and player position (if required)
    if (cameraMode != CAMERA_CUSTOM) ProcessCamera(&internalCamera, &position);

    *camera = internalCamera;
}

// Update camera and player position (1st person and 3rd person cameras)
void UpdateCameraPlayer(Camera *camera, Vector3 *position)
{
    // Process internal camera and player position (if required)
    if (cameraMode != CAMERA_CUSTOM) ProcessCamera(&internalCamera, position);

    *camera = internalCamera;
}

// Set internal camera position
void SetCameraPosition(Vector3 position)
{
    internalCamera.position = position;
    
    Vector3 v1 = internalCamera.position;
    Vector3 v2 = internalCamera.target;
    
    float dx = v2.x - v1.x;
    float dy = v2.y - v1.y;
    float dz = v2.z - v1.z;
    
    cameraTargetDistance = sqrt(dx*dx + dy*dy + dz*dz);
}

// Set internal camera target
void SetCameraTarget(Vector3 target)
{
    internalCamera.target = target;
    
    Vector3 v1 = internalCamera.position;
    Vector3 v2 = internalCamera.target;
    
    float dx = v2.x - v1.x;
    float dy = v2.y - v1.y;
    float dz = v2.z - v1.z;
    
    cameraTargetDistance = sqrt(dx*dx + dy*dy + dz*dz);
}

// Set internal camera fovy
void SetCameraFovy(float fovy)
{
    internalCamera.fovy = fovy;
}

// Set camera pan key to combine with mouse movement (free camera)
void SetCameraPanControl(int panKey)
{
    cameraPanControlKey = panKey;
}

// Set camera alt key to combine with mouse movement (free camera)
void SetCameraAltControl(int altKey)
{
    cameraAltControlKey = altKey;
}

// Set camera smooth zoom key to combine with mouse (free camera)
void SetCameraSmoothZoomControl(int szKey)
{
    cameraSmoothZoomControlKey = szKey;
}

// Set camera move controls (1st person and 3rd person cameras)
void SetCameraMoveControls(int frontKey, int backKey, int leftKey, int rightKey, int upKey, int downKey)
{
    cameraMoveControl[MOVE_FRONT] = frontKey;
    cameraMoveControl[MOVE_LEFT] = leftKey;
    cameraMoveControl[MOVE_BACK] = backKey;
    cameraMoveControl[MOVE_RIGHT] = rightKey;
    cameraMoveControl[MOVE_UP] = upKey;
    cameraMoveControl[MOVE_DOWN] = downKey;
}

// Set camera mouse sensitivity (1st person and 3rd person cameras)
void SetCameraMouseSensitivity(float sensitivity)
{
    cameraMouseSensitivity = (sensitivity/10000.0f);
}

//----------------------------------------------------------------------------------
// Module specific Functions Definition
//----------------------------------------------------------------------------------

// Process desired camera mode and controls
// NOTE: Camera controls depend on some raylib functions:
//       Mouse: GetMousePosition(), SetMousePosition(), IsMouseButtonDown(), GetMouseWheelMove()
//       System: GetScreenWidth(), GetScreenHeight(), ShowCursor(), HideCursor()
//       Keys:  IsKeyDown()
static void ProcessCamera(Camera *camera, Vector3 *playerPosition)
{
    // Mouse movement detection
    Vector2 mousePosition = GetMousePosition();
    int mouseWheelMove = GetMouseWheelMove();
    int panKey = IsMouseButtonDown(cameraPanControlKey);    // bool value
    
    int screenWidth = GetScreenWidth();
    int screenHeight = GetScreenHeight();
    
    if ((cameraMode != CAMERA_FREE) && (cameraMode != CAMERA_ORBITAL))
    {
        HideCursor();

        if (mousePosition.x < screenHeight/3) SetMousePosition((Vector2){ screenWidth - screenHeight/3, mousePosition.y});
        else if (mousePosition.y < screenHeight/3) SetMousePosition((Vector2){ mousePosition.x, screenHeight - screenHeight/3});
        else if (mousePosition.x > screenWidth - screenHeight/3) SetMousePosition((Vector2) { screenHeight/3, mousePosition.y});
        else if (mousePosition.y > screenHeight - screenHeight/3) SetMousePosition((Vector2){ mousePosition.x, screenHeight/3});
        else
        {
            cameraMouseVariation.x = mousePosition.x - cameraMousePosition.x;
            cameraMouseVariation.y = mousePosition.y - cameraMousePosition.y;
        }
    }
    else
    {
        ShowCursor();

        cameraMouseVariation.x = mousePosition.x - cameraMousePosition.x;
        cameraMouseVariation.y = mousePosition.y - cameraMousePosition.y;
    }

	// NOTE: We GetMousePosition() again because it can be modified by a previous SetMousePosition() call
	// If using directly mousePosition variable we have problems on CAMERA_FIRST_PERSON and CAMERA_THIRD_PERSON
    cameraMousePosition = GetMousePosition();

    // Support for multiple automatic camera modes
    switch (cameraMode)
    {
        case CAMERA_FREE:
        {
            // Camera zoom
            if ((cameraTargetDistance < CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
            {
                cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);

                if (cameraTargetDistance > CAMERA_FREE_DISTANCE_MAX_CLAMP) cameraTargetDistance = CAMERA_FREE_DISTANCE_MAX_CLAMP;
            }
            // Camera looking down
            else if ((camera->position.y > camera->target.y) && (cameraTargetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
            {
                camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
                camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
                camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
            }
            else if ((camera->position.y > camera->target.y) && (camera->target.y >= 0))
            {
                camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
                camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
                camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;

                // if (camera->target.y < 0) camera->target.y = -0.001;
            }
            else if ((camera->position.y > camera->target.y) && (camera->target.y < 0) && (mouseWheelMove > 0))
            {
                cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);
                if (cameraTargetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) cameraTargetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP;
            }
            // Camera looking up
            else if ((camera->position.y < camera->target.y) && (cameraTargetDistance == CAMERA_FREE_DISTANCE_MAX_CLAMP) && (mouseWheelMove < 0))
            {
                camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
                camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
                camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
            }
            else if ((camera->position.y < camera->target.y) && (camera->target.y <= 0))
            {
                camera->target.x += mouseWheelMove*(camera->target.x - camera->position.x)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
                camera->target.y += mouseWheelMove*(camera->target.y - camera->position.y)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;
                camera->target.z += mouseWheelMove*(camera->target.z - camera->position.z)*CAMERA_SCROLL_SENSITIVITY/cameraTargetDistance;

                // if (camera->target.y > 0) camera->target.y = 0.001;
            }
            else if ((camera->position.y < camera->target.y) && (camera->target.y > 0) && (mouseWheelMove > 0))
            {
                cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);
                if (cameraTargetDistance < CAMERA_FREE_DISTANCE_MIN_CLAMP) cameraTargetDistance = CAMERA_FREE_DISTANCE_MIN_CLAMP;
            }

            // Inputs
            if (IsKeyDown(cameraAltControlKey))
            {
                if (IsKeyDown(cameraSmoothZoomControlKey))
                {
                    // Camera smooth zoom
                    if (panKey) cameraTargetDistance += (cameraMouseVariation.y*CAMERA_FREE_SMOOTH_ZOOM_SENSITIVITY);
                }
                // Camera orientation calculation
                else if (panKey)
                {
                    // Camera orientation calculation
                    // Get the mouse sensitivity
                    cameraAngle.x += cameraMouseVariation.x*-CAMERA_FREE_MOUSE_SENSITIVITY;
                    cameraAngle.y += cameraMouseVariation.y*-CAMERA_FREE_MOUSE_SENSITIVITY;

                    // Angle clamp
                    if (cameraAngle.y > CAMERA_FREE_MIN_CLAMP*DEG2RAD) cameraAngle.y = CAMERA_FREE_MIN_CLAMP*DEG2RAD;
                    else if (cameraAngle.y < CAMERA_FREE_MAX_CLAMP*DEG2RAD) cameraAngle.y = CAMERA_FREE_MAX_CLAMP*DEG2RAD;
                }
            }
            // Paning
            else if (panKey)
            {
                camera->target.x += ((cameraMouseVariation.x*-CAMERA_FREE_MOUSE_SENSITIVITY)*cos(cameraAngle.x) + (cameraMouseVariation.y*CAMERA_FREE_MOUSE_SENSITIVITY)*sin(cameraAngle.x)*sin(cameraAngle.y))*(cameraTargetDistance/CAMERA_FREE_PANNING_DIVIDER);
                camera->target.y += ((cameraMouseVariation.y*CAMERA_FREE_MOUSE_SENSITIVITY)*cos(cameraAngle.y))*(cameraTargetDistance/CAMERA_FREE_PANNING_DIVIDER);
                camera->target.z += ((cameraMouseVariation.x*CAMERA_FREE_MOUSE_SENSITIVITY)*sin(cameraAngle.x) + (cameraMouseVariation.y*CAMERA_FREE_MOUSE_SENSITIVITY)*cos(cameraAngle.x)*sin(cameraAngle.y))*(cameraTargetDistance/CAMERA_FREE_PANNING_DIVIDER);
            }

            // Focus to center
            // TODO: Move this function out of this module?
            if (IsKeyDown('Z')) camera->target = (Vector3){ 0.0f, 0.0f, 0.0f };

            // Camera position update
            camera->position.x = sin(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.x;

            if (cameraAngle.y <= 0.0f) camera->position.y = sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;
            else camera->position.y = -sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;

            camera->position.z = cos(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.z;

        } break;
        case CAMERA_ORBITAL:
        {
            cameraAngle.x += CAMERA_ORBITAL_SPEED;

            // Camera zoom
            cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);
            
            // Camera distance clamp
            if (cameraTargetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) cameraTargetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP;

            // Focus to center
            if (IsKeyDown('Z')) camera->target = (Vector3){ 0.0f, 0.0f, 0.0f };

            // Camera position update
            camera->position.x = sin(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.x;

            if (cameraAngle.y <= 0.0f) camera->position.y = sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;
            else camera->position.y = -sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;

            camera->position.z = cos(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.z;

        } break;
        case CAMERA_FIRST_PERSON:
        case CAMERA_THIRD_PERSON:
        {
            bool isMoving = false;

            // Keyboard inputs
            if (IsKeyDown(cameraMoveControl[MOVE_FRONT]))
            {
                playerPosition->x -= sin(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
                playerPosition->z -= cos(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
                
                camera->position.y += sin(cameraAngle.y)/PLAYER_MOVEMENT_DIVIDER;

                isMoving = true;
            }
            else if (IsKeyDown(cameraMoveControl[MOVE_BACK]))
            {
                playerPosition->x += sin(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
                playerPosition->z += cos(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
                
                camera->position.y -= sin(cameraAngle.y)/PLAYER_MOVEMENT_DIVIDER;

                isMoving = true;
            }

            if (IsKeyDown(cameraMoveControl[MOVE_LEFT]))
            {
                playerPosition->x -= cos(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
                playerPosition->z += sin(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;

                isMoving = true;
            }
            else if (IsKeyDown(cameraMoveControl[MOVE_RIGHT]))
            {
                playerPosition->x += cos(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;
                playerPosition->z -= sin(cameraAngle.x)/PLAYER_MOVEMENT_DIVIDER;

                isMoving = true;
            }

            if (IsKeyDown(cameraMoveControl[MOVE_UP]))
            {
                playerPosition->y += 1.0f/PLAYER_MOVEMENT_DIVIDER;
            }
            else if (IsKeyDown(cameraMoveControl[MOVE_DOWN]))
            {
                playerPosition->y -= 1.0f/PLAYER_MOVEMENT_DIVIDER;
            }

            if (cameraMode == CAMERA_THIRD_PERSON)
            {
                // Camera orientation calculation
                cameraAngle.x += cameraMouseVariation.x*-cameraMouseSensitivity;
                cameraAngle.y += cameraMouseVariation.y*-cameraMouseSensitivity;

                // Angle clamp
                if (cameraAngle.y > CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD) cameraAngle.y = CAMERA_THIRD_PERSON_MIN_CLAMP*DEG2RAD;
                else if (cameraAngle.y < CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD) cameraAngle.y = CAMERA_THIRD_PERSON_MAX_CLAMP*DEG2RAD;

                // Camera zoom
                cameraTargetDistance -= (mouseWheelMove*CAMERA_SCROLL_SENSITIVITY);

                // Camera distance clamp
                if (cameraTargetDistance < CAMERA_THIRD_PERSON_DISTANCE_CLAMP) cameraTargetDistance = CAMERA_THIRD_PERSON_DISTANCE_CLAMP;

                // Camera is always looking at player
                camera->target.x = playerPosition->x + CAMERA_THIRD_PERSON_OFFSET.x*cos(cameraAngle.x) + CAMERA_THIRD_PERSON_OFFSET.z*sin(cameraAngle.x);
                camera->target.y = playerPosition->y + PLAYER_HEIGHT*CAMERA_FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION + CAMERA_THIRD_PERSON_OFFSET.y;
                camera->target.z = playerPosition->z + CAMERA_THIRD_PERSON_OFFSET.z*sin(cameraAngle.x) - CAMERA_THIRD_PERSON_OFFSET.x*sin(cameraAngle.x);

                // Camera position update
                camera->position.x = sin(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.x;

                if (cameraAngle.y <= 0.0f) camera->position.y = sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;
                else camera->position.y = -sin(cameraAngle.y)*cameraTargetDistance*sin(cameraAngle.y) + camera->target.y;

                camera->position.z = cos(cameraAngle.x)*cameraTargetDistance*cos(cameraAngle.y) + camera->target.z;
            }
            else    // CAMERA_FIRST_PERSON
            {
                if (isMoving) cameraMoveCounter++;

                // Camera orientation calculation
                cameraAngle.x += (cameraMouseVariation.x*-cameraMouseSensitivity);
                cameraAngle.y += (cameraMouseVariation.y*-cameraMouseSensitivity);

                // Angle clamp
                if (cameraAngle.y > CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD) cameraAngle.y = CAMERA_FIRST_PERSON_MIN_CLAMP*DEG2RAD;
                else if (cameraAngle.y < CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD) cameraAngle.y = CAMERA_FIRST_PERSON_MAX_CLAMP*DEG2RAD;

                // Camera is always looking at player
                camera->target.x = camera->position.x - sin(cameraAngle.x)*CAMERA_FIRST_PERSON_FOCUS_DISTANCE;
                camera->target.y = camera->position.y + sin(cameraAngle.y)*CAMERA_FIRST_PERSON_FOCUS_DISTANCE;
                camera->target.z = camera->position.z - cos(cameraAngle.x)*CAMERA_FIRST_PERSON_FOCUS_DISTANCE;

                camera->position.x = playerPosition->x;
                camera->position.y = (playerPosition->y + PLAYER_HEIGHT*CAMERA_FIRST_PERSON_HEIGHT_RELATIVE_EYES_POSITION) - sin(cameraMoveCounter/CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER)/CAMERA_FIRST_PERSON_STEP_DIVIDER;
                camera->position.z = playerPosition->z;

                camera->up.x = sin(cameraMoveCounter/(CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/CAMERA_FIRST_PERSON_WAVING_DIVIDER;
                camera->up.z = -sin(cameraMoveCounter/(CAMERA_FIRST_PERSON_STEP_TRIGONOMETRIC_DIVIDER*2))/CAMERA_FIRST_PERSON_WAVING_DIVIDER;
            }
        } break;
        default: break;
    }
}

#endif // CAMERA_IMPLEMENTATION