summaryrefslogtreecommitdiff
path: root/platformer/src/camera.h
blob: cdceaec07cacf701e0cbbd29ad2b7ac04d9bae95 (plain) (blame)
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
/*
 * ============================================================
 * FILE:        camera.h
 *
 * PURPOSE:     Public interface for the third-person orbit camera: it sits
 *              on a sphere around the player and the right stick moves it
 *              around that sphere.
 *
 * FITS IN:     main.c calls camera_update() each tick AFTER player_update().
 *              player_update() receives camera_yaw() so "stick up" means "away
 *              from the camera". render.c reads camera_view()/camera_projection().
 *
 * INTERFACE:   Use these exact names:
 *
 *   typedef struct Camera {
 *       Vec3  target;     the smoothed point we look at (near the player)
 *       float yaw;        orbit angle around the target, radians
 *       float pitch;      angle above horizontal, radians
 *       float distance;   meters from target
 *       Vec3  position;   derived eye position (recomputed in camera_update)
 *   } Camera;
 *
 *   void  camera_init(Camera *c, Vec3 follow_point);
 *   void  camera_update(Camera *c, const Input *in, Vec3 follow_point, float dt);
 *             follow_point = the player's feet position; the camera adds
 *             CAMERA_TARGET_HEIGHT itself.
 *   float camera_yaw(const Camera *c);
 *   Mat4  camera_view(const Camera *c);
 *   Mat4  camera_projection(const Camera *c, float aspect);
 *
 *   YAW CONVENTION (player.c relies on this, so keep it exact):
 *     position = target + distance * ( sin(yaw)*cos(pitch),
 *                                      sin(pitch),
 *                                      cos(yaw)*cos(pitch) )
 *     So on the ground plane, the camera's
 *       forward (toward target) = (-sin(yaw), 0, -cos(yaw))
 *       right                   = ( cos(yaw), 0, -sin(yaw))
 *
 *   Include "math3d.h" and "controls.h".
 *
 * CONVENTIONS: Include guard: CAMERA_H.
 *
 * NOT IN SCOPE: Collision with geometry, automatic "behind the player"
 *              recentering, cutscene/fixed cameras, zoom levels.
 *
 * DONE WHEN:   player.c and render.c can be written against this header alone.
 * ============================================================
 */
#ifndef CAMERA_H
#define CAMERA_H

#include "controls.h"
#include "math3d.h"

typedef struct Camera {
    Vec3  target;     /* the smoothed point we look at (near the player) */
    float yaw;        /* orbit angle around the target, radians */
    float pitch;      /* angle above horizontal, radians */
    float distance;   /* meters from target */
    Vec3  position;   /* derived eye position (recomputed in camera_update) */
} Camera;

void  camera_init(Camera *c, Vec3 follow_point);
void  camera_update(Camera *c, const Input *in, Vec3 follow_point, float dt);
float camera_yaw(const Camera *c);
Mat4  camera_view(const Camera *c);
Mat4  camera_projection(const Camera *c, float aspect);

#endif /* CAMERA_H */