Untitled
Loading...
Searching...
No Matches
camera.c File Reference

Functions for camera. More...

#include <math.h>
#include <omp.h>
#include <assert.h>
#include "core/acceleration.h"
#include "audio/audio.h"
#include "core/engine_types.h"
#include "mesh.h"
#include "utilities/types.h"
#include "utilities/mathf.h"
#include "core/camera.h"
#include "load/init.h"
#include "utilities/utilities.h"
#include <stdio.h>
#include "physics/physics.h"
#include "physics/GJK.h"

Go to the source code of this file.

Macros

#define MIN_WALKABLE_SURFACE   0.5f
 How steep the surface is for it to be walkable.
#define STEP_HEIGHT   0.6f
 How high legs are raised during step.
#define RAYCAST_HEIGHT   0.6f
 How high legs are raised to detect for steps.

Functions

float calculate_stride_governor (vec3 player_pos, LegIK *left_leg, LegIK *right_leg)
 Calculates a stride based on how stretched the legs are from the center position.
void player_find_collision (vec3 start, vec3 proposed, CollisionPoints p[MAX_PLAYER_CONTACTS], int *contact_count)
 Finds collisions against the Player's proposed position through a SceneData.
void define_M (vec3 P, vec3 D, vec3 minv[3], vec3 mfwd[3])
 Refer to Ken Perlin's 2 joint IK implmentation.
vec3 rot (vec3 M[3], vec3 a)
 Refer to Ken Perlin's 2 joint IK implmentation.
bool solve_Q (Skeleton *s, vec3 D, vec3 *Q, vec3 *P, vec3 hip_pos)
 Refer to Ken Perlin's 2 joint IK implmentation.
bool update_leg (LegIK *leg, LegIK *other_leg, vec3 vel)
 Raycasts forward and downward to determine how far a leg can go and also updates the leg position if it is already taking a step.
void update_spring (Spring *s)
 Updates a Spring's velocity and position based on the difference between its current position and target position.
void update_angular_spring (Spring *s, float dt)
 Updates a Spring's angular velocity and rotation based on the difference between its current rotation and target rotation.
void calculate_camera_angles (Camera *cam, Spring *s, vec3 input_acceleration, float dt)
 Calculates camera vectors and roll based on the angular Spring attached to it.
void compute_camera_basis (Camera *cam, float yaw, float pitch, float roll, vec3 world_up)
 Computes camera vectors based on yaw, pitch, and roll.
mat4 make_ortho (float left, float right, float bottom, float top, float near, float far)
 Makes an 4x4 orthographic matrix.
mat4 make_projection (float fov_deg, float aspect, float near, float far)
 Makes an 4x4 projection matrix.
mat4 make_look_at (Camera *cam, vec3 eye)
 Makes a 4x4 view matrix using camera vectors.
void update_light_camera (Camera *lcam, Light *light)
 Updates a directional light camera and locks it to texel size.
bool collision (vec3 proposed, vec3 pts[3], vec3 normal, CollisionPoints *p)
void update_camera (Skeleton *s, Camera *cam)
 Updates a camera based on a skeleton's current neck and body position.
void apply_planet_velocity (Camera *cam, vec3 move_dir, float dt)
 Applies normal velocity to player with gravity and friction applied.
void apply_velocity (Camera *cam, vec3 move_dir, float dt)
 Applies no-clip mode velocity to player with no gravity applied.
void reset_skeleton (Skeleton *s, vec3 pos)
 Resets a skeleton to be relative to a new position.
void update_body_from_legs (LegIK *left_leg, LegIK *right_leg, vec3 intended_vel, float dt)
 Calculates velocity based on leg positions and integrates them into the player.
void update_skeleton (Skeleton *s, Camera *cam, vec3 vel)
 Moves feet, then solves a skeleton's leg IKs and updates the springs accordingly.
void resolve_collisions (Camera *cam)
 Resolves collisions between camera and geometry, uses player.

Variables

const float HALTON_8 [8][2]
bool ground_hit

Detailed Description

Functions for camera.

Definition in file camera.c.

Macro Definition Documentation

◆ MIN_WALKABLE_SURFACE

#define MIN_WALKABLE_SURFACE   0.5f

How steep the surface is for it to be walkable.

Definition at line 1371 of file camera.c.

Referenced by resolve_collisions().

◆ STEP_HEIGHT

#define STEP_HEIGHT   0.6f

How high legs are raised during step.

Definition at line 1372 of file camera.c.

◆ RAYCAST_HEIGHT

#define RAYCAST_HEIGHT   0.6f

How high legs are raised to detect for steps.

Definition at line 1373 of file camera.c.

Referenced by resolve_collisions().

Function Documentation

◆ calculate_stride_governor()

float calculate_stride_governor ( vec3 player_pos,
LegIK * left_leg,
LegIK * right_leg )

Calculates a stride based on how stretched the legs are from the center position.

Parameters
player_posThe center of the player but with z = where the feet should be.
left_legThe left leg.
right_legThe right leg.
Returns
The value that determines speed for this timestep.

Definition at line 561 of file camera.c.

References calculate_stride_governor(), MIN, LegIK::pos, LegIK::stepping, vec2_length(), vec2_sub(), vec3_add(), vec3_scale(), vec3::x, and vec3::y.

Referenced by apply_planet_velocity(), and calculate_stride_governor().

◆ player_find_collision()

◆ define_M()

void define_M ( vec3 P,
vec3 D,
vec3 minv[3],
vec3 mfwd[3] )

Refer to Ken Perlin's 2 joint IK implmentation.

See also
https://mrl.cs.nyu.edu/~perlin/ik/ik.java.html

Definition at line 982 of file camera.c.

References D, define_M(), vec3_cross(), vec3_dot(), vec3_normalize(), vec3_scale(), vec3_sub(), vec3::x, vec3::y, and vec3::z.

Referenced by define_M(), and solve_Q().

◆ rot()

◆ solve_Q()

bool solve_Q ( Skeleton * s,
vec3 D,
vec3 * Q,
vec3 * P,
vec3 hip_pos )

Refer to Ken Perlin's 2 joint IK implmentation.

See also
https://mrl.cs.nyu.edu/~perlin/ik/ik.java.html

Definition at line 1016 of file camera.c.

References A, B, C, D, define_M(), Skeleton::lower_leg_length, MAX, MIN, rot(), solve_Q(), Skeleton::upper_leg_length, vec3_add(), vec3_dot(), and vec3_sub().

Referenced by solve_Q(), and update_skeleton().

◆ update_leg()

bool update_leg ( LegIK * leg,
LegIK * other_leg,
vec3 vel )

Raycasts forward and downward to determine how far a leg can go and also updates the leg position if it is already taking a step.

Parameters
legThe leg that is updating.
other_legThe other leg.
velThe input velocity.
Returns
true if footstep is completed. \begin{aligned} &\text{Def:} \\ & \quad D = \text{ Desired Stride} \\ & \quad H = \text{ Step Height} \\ \hat{f_0} &= \begin{bmatrix} \cos(y) \\ \sin(y) \\ \sin(p) \end{bmatrix} \\ \hat{r_0} &= \begin{bmatrix} -\sin(y) \\ \cos(y) \\ 0 \end{bmatrix} \\ \hat{f} &= \frac{\hat{f_0}}{||\hat{f_0}||} \\ \hat{r} &= \frac{\hat{r_0}}{||\hat{r_0}||} \\ w_0 &= \begin{bmatrix} v_x \hat{f_x} + v_y \hat{r_x} \\ v_x \hat{f_y} + v_y \hat{r_y} \end{bmatrix} \\ w &= \frac{w_0}{||w_0||} \\ t &= \begin{bmatrix} P_{B,x} + w_x \cdot D \\ P_{B,y} + w_y \cdot D \\ P_{B,z} \end{bmatrix} \\ s_s &= \begin{bmatrix} P_{A,x} \\ P_{A,y} \\ P_{A,z} + H \end{bmatrix} \\ s_e &= \begin{bmatrix} t_x \\ t_y \\ t_z + H \end{bmatrix} \\ \vec{h_{d,0}} &= s_e - s_s \\ h_l &= ||\vec{h_{d,0}}|| \\ \vec{h_d} &= \frac{\vec{h_{d,0}}}{h_l} \\ & R(t, \vec{v}, O) = O + t\vec{v} \\ t_{\text{hit}} &= \begin{cases} & min({t \ge 0 | R(t, \vec{h_d}, s_s) \in \mathcal S }), & \text{if }R(t, \vec{h_d}, s_s) \text{ hits }\mathcal S\\ & h_l, & \text{otherwise} \\ \end{cases} \\ d_s &= \begin{cases} R(t_{\text{hit}}, \vec{h_d}, s_s), & \text{ if } t_{\text{hit}} < h_l \\ s_e, & \text{otherwise} \end{cases} \\ \vec{d_d} &= \begin{bmatrix} 0 \\ 0\\ -1 \end{bmatrix}\\ t_{\text{hit},1} &= \begin{cases} & min({t \ge 0 | R(t, \vec{d_d}, d_s) \in \mathcal S }), & \text{if }R(t, \vec{d_d}, d_s) \text{ hits }\mathcal S\\ & h_l, & \text{otherwise} \\ \end{cases} \\ t_1 &= R(t_{\text{hit},1}, \vec{d_d}, d_s) \\ \end{aligned}

Definition at line 1135 of file camera.c.

References cam, clamp(), dt, LegIK::duration, FOOTSTEP_EMITTER, hit, RayHit::hit, MAX, MIN, Camera::pitch, play_emitter(), LegIK::pos, RayHit::pos, raycast_scene(), LegIK::start_pos, LegIK::stepping, LegIK::stepping_off_ledge, LegIK::target_pos, LegIK::time, update_leg(), vec2_normalize(), vec3_add(), vec3_dist(), vec3_length(), vec3_lerp(), vec3_normalize(), vec3_scale(), vec3_sub(), vec2::x, vec3::x, vec2::y, vec3::y, Camera::yaw, and vec3::z.

Referenced by update_leg(), and update_skeleton().

◆ update_spring()

void update_spring ( Spring * s)

Updates a Spring's velocity and position based on the difference between its current position and target position.

Parameters
sThe spring to update.

\begin{aligned} F &= -s (p - p_t) \\ a &= F + -d \vec{v} \\ \vec{v} &= \vec{v} + a \Delta t \\ p &= p + \vec{v} \Delta t \\ \end{aligned}

Definition at line 1239 of file camera.c.

References Spring::damp, dt, Spring::pos, Spring::stiff, Spring::target, update_spring(), vec3_add(), vec3_scale(), vec3_sub(), and Spring::velocity.

Referenced by update_skeleton(), and update_spring().

◆ update_angular_spring()

void update_angular_spring ( Spring * s,
float dt )

Updates a Spring's angular velocity and rotation based on the difference between its current rotation and target rotation.

Parameters
sThe spring to update.
dtThe dt for the step.

\begin{aligned} d &= (r - t) \in (0, \pi)\\ \tau &= \omega d + d s \\ \omega &= \omega + (\tau \Delta t) \\ r &= r + (\omega \Delta t) \\ \end{aligned}

Definition at line 1253 of file camera.c.

References Spring::angular_vel, Spring::damp_rot, dt, Spring::rotation, Spring::stiff_rot, Spring::target_rot, update_angular_spring(), vec3_add(), vec3_scale(), vec3_sub(), wrap_angle_pi(), vec3::x, vec3::y, and vec3::z.

Referenced by calculate_camera_angles(), and update_angular_spring().

◆ calculate_camera_angles()

void calculate_camera_angles ( Camera * cam,
Spring * s,
vec3 input_acceleration,
float dt )

Calculates camera vectors and roll based on the angular Spring attached to it.

Parameters
camThe camera to update.
sThe spring to update.
input_accelerationMovement vectors to modify roll.
dtThe simulation time.

Definition at line 1272 of file camera.c.

References calculate_camera_angles(), cam, dt, Camera::pitch, Camera::right, Camera::roll, Spring::target_rot, update_angular_spring(), vec3_dot(), Camera::yaw, and vec3::z.

Referenced by calculate_camera_angles(), and update_skeleton().

◆ compute_camera_basis()

void compute_camera_basis ( Camera * cam,
float yaw,
float pitch,
float roll,
vec3 world_up )

Computes camera vectors based on yaw, pitch, and roll.

Parameters
camThe camera to effect.
yawThe direction left-right in radians.
pitchThe direction up-down in radians.
rollThe rotation in radians.
world_upThe vector to cross with, normally (0,0,1).

Definition at line 135 of file camera.c.

References cam, compute_camera_basis(), Camera::forward, Camera::right, Camera::up, vec3_add(), vec3_cross(), vec3_normalize(), vec3_scale(), vec3_sub(), vec3::x, vec3::y, and vec3::z.

Referenced by bake_reflection_probes(), compute_camera_basis(), update_camera(), and write_point_light_shadows().

◆ make_ortho()

mat4 make_ortho ( float left,
float right,
float bottom,
float top,
float near,
float far )

Makes an 4x4 orthographic matrix.

Parameters
leftThe left dist.
rightThe right dist.
bottomThe bottom dist.
topThe top dist.
nearThe near plane.
farThe far plane.
Returns
The 4x4 orthographic matrix.

Definition at line 178 of file camera.c.

References mat4::a0, mat4::a1, mat4::a2, mat4::a3, mat4::b0, mat4::b1, mat4::b2, mat4::b3, mat4::c0, mat4::c1, mat4::c2, mat4::c3, mat4::d0, mat4::d1, mat4::d2, mat4::d3, and make_ortho().

Referenced by make_ortho(), render(), and update_light_camera().

◆ make_projection()

mat4 make_projection ( float fov_deg,
float aspect,
float near,
float far )

Makes an 4x4 projection matrix.

Parameters
fov_degFOV in degrees.
aspectThe aspect ratio (W/H).
nearThe near plane.
farThe far plane.
Returns
The 4x4 projection matrix.

Definition at line 220 of file camera.c.

References mat4::a0, mat4::a1, mat4::a2, mat4::a3, mat4::b0, mat4::b1, mat4::b2, mat4::b3, mat4::c0, mat4::c1, mat4::c2, mat4::c3, mat4::d0, mat4::d1, mat4::d2, mat4::d3, M_PI, and make_projection().

Referenced by bake_reflection_probes(), make_projection(), update_camera(), and write_point_light_shadows().

◆ make_look_at()

mat4 make_look_at ( Camera * cam,
vec3 eye )

Makes a 4x4 view matrix using camera vectors.

Parameters
camThe camera to use.
eyeThe camera's "eye" position.
Returns
The 4x4 view matrix.

Definition at line 257 of file camera.c.

References mat4::a0, mat4::a1, mat4::a2, mat4::a3, mat4::b0, mat4::b1, mat4::b2, mat4::b3, mat4::c0, mat4::c1, mat4::c2, mat4::c3, cam, mat4::d0, mat4::d1, mat4::d2, mat4::d3, Camera::forward, make_look_at(), Camera::right, Camera::up, vec3_dot(), vec3::x, vec3::y, and vec3::z.

Referenced by bake_reflection_probes(), make_look_at(), update_camera(), update_light_camera(), and write_point_light_shadows().

◆ update_light_camera()

void update_light_camera ( Camera * lcam,
Light * light )

Updates a directional light camera and locks it to texel size.

Parameters
lcamPointer to the light camera.
lightPointer to directional light.

Definition at line 297 of file camera.c.

References cam, Camera::cam_data, Light::dir, Camera::forward, FOV, M_PI, make_look_at(), make_ortho(), mat4_mul_point(), Skeleton::neck, player, Spring::pos, CamData::proj, RENDER_HEIGHT, RENDER_WIDTH, Camera::right, SHADOW_MAP_FBO, Player::skeleton, Camera::target, Camera::up, update_light_camera(), vec3_add(), vec3_cross(), vec3_dot(), vec3_normalize(), vec3_scale(), vec3_sub(), CamData::view, Framebuffer::w, vec3::x, vec3::y, and vec3::z.

Referenced by main(), and update_light_camera().

◆ collision()

bool collision ( vec3 proposed,
vec3 pts[3],
vec3 normal,
CollisionPoints * p )

◆ update_camera()

◆ apply_planet_velocity()

void apply_planet_velocity ( Camera * current_cam,
vec3 move_dir,
float dt )

Applies normal velocity to player with gravity and friction applied.

Parameters
move_dirThe move vectors from input.
dtThe frame time / simulation step time.

Definition at line 593 of file camera.c.

References apply_planet_velocity(), calculate_stride_governor(), cam, dt, Skeleton::left_leg, Camera::on_ground, Player::pc, Camera::pitch, player, Player::pos, Skeleton::right_leg, Player::skeleton, Player::sprinting, vec3_normalize(), vec3_scale(), PhysicsComponent::vel, vec3::x, vec3::y, Camera::yaw, and vec3::z.

Referenced by apply_planet_velocity().

◆ apply_velocity()

void apply_velocity ( Camera * current_cam,
vec3 move_dir,
float dt )

Applies no-clip mode velocity to player with no gravity applied.

Parameters
move_dirThe move vectors from input.
dtThe frame time / simulation step time.

Definition at line 659 of file camera.c.

References apply_velocity(), cam, dt, Player::pc, Camera::pitch, player, vec3_normalize(), vec3_scale(), PhysicsComponent::vel, vec3::x, vec3::y, Camera::yaw, and vec3::z.

Referenced by apply_velocity(), and process_input().

◆ reset_skeleton()

void reset_skeleton ( Skeleton * s,
vec3 pos )

Resets a skeleton to be relative to a new position.

Parameters
sThe skeleton to effect.
posThe target position.

Definition at line 963 of file camera.c.

References Spring::angular_vel, Skeleton::left_leg, Skeleton::neck, Skeleton::neck_height, Skeleton::pelvis, Skeleton::pelvis_height, LegIK::pos, Spring::pos, reset_skeleton(), Skeleton::right_leg, LegIK::stepping, Spring::velocity, and vec3::z.

Referenced by process_input(), and reset_skeleton().

◆ update_body_from_legs()

void update_body_from_legs ( LegIK * left_leg,
LegIK * right_leg,
vec3 intended_vel,
float dt )

Calculates velocity based on leg positions and integrates them into the player.

Also handles falling off ledges by initiating a drop when raycasts to ground fail above a certain margin.

Parameters
left_legLeft leg IK.
right_legRight leg IK.
intended_velThe raw input direction.
dtThe frame time / simulation frame time.

Definition at line 1052 of file camera.c.

References SceneData::bvh_nodes, cam, CAST_VEC, dt, GLOBALS, hit, RayHit::hit, Player::pc, Camera::pitch, player, LegIK::pos, Player::pos, RayHit::pos, raycast_scene(), Globals::scene_data, LegIK::stepping, LegIK::stepping_off_ledge, update_body_from_legs(), vec3_add(), vec3_normalize(), vec3_scale(), vec3_sub(), PhysicsComponent::vel, vec3::x, vec3::y, Camera::yaw, and vec3::z.

Referenced by process_input(), and update_body_from_legs().

◆ update_skeleton()

◆ resolve_collisions()

Variable Documentation

◆ HALTON_8

const float HALTON_8[8][2]
Initial value:
= {
{ 0.000000f, -0.333333f },
{-0.500000f, 0.333333f },
{ 0.250000f, -0.777778f },
{-0.750000f, -0.111111f },
{ 0.125000f, 0.555556f },
{-0.375000f, -0.555556f },
{ 0.375000f, 0.111111f },
{-0.625000f, 0.777778f }
}

Definition at line 464 of file camera.c.

Referenced by update_camera().

◆ ground_hit

bool ground_hit

Definition at line 704 of file camera.c.

Referenced by resolve_collisions().