|
Untitled
|
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 |
Functions for camera.
Definition in file camera.c.
| #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().
| #define STEP_HEIGHT 0.6f |
| #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().
Calculates a stride based on how stretched the legs are from the center position.
| player_pos | The center of the player but with z = where the feet should be. |
| left_leg | The left leg. |
| right_leg | The right leg. |
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().
| 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.
| start | Where the Player starts at. |
| proposed | Where the Player is moving to. |
| p | The array of CollisionPoints that is being addded to during the collision check. |
| contact_count | The amount of contacts emitted so far. |
Definition at line 710 of file camera.c.
References BVHNode::aabb, Mesh::aabb, aabbs_intersect(), Vector::buffer, SceneData::bvh_indices, SceneData::bvh_nodes, Mesh::bvh_prim, CAST_VEC, PhysicsComponent::cd, COL_CAPSULE, COL_HULL, COL_TRIANGLE, collision(), compose_trs_from_vectors(), BVHNode::count, Vector::count, CollisionData::data, EW, BVHNode::first_index, MeshNode::free, GJK(), MeshNode::global_matrix, GLOBALS, ConvexHull::hull_faces, Mesh::hulls, MeshNode::id, BVHNode::left, m4_inverse(), mat4_mul_direction(), mat4_mul_point(), MAX_PLAYER_CONTACTS, MeshNode::mesh, Player::mesh, Globals::mesh_nodes, Globals::meshes, BVHNode::next, Mesh::nodes, MeshNode::parent, MeshNode::pc, player, player_find_collision(), BVHNode::right, Globals::scene_data, transform_aabb_by_position(), traverse_collision_bvh(), CollisionData::type, vec3_normalize(), vec3_sub(), ConvexHull::vertices, vec3::x, vec3::y, and vec3::z.
Referenced by player_find_collision(), and resolve_collisions().
Refer to Ken Perlin's 2 joint IK implmentation.
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().
Refer to Ken Perlin's 2 joint IK implmentation.
Definition at line 1004 of file camera.c.
References rot(), vec3_dot(), vec3::x, vec3::y, and vec3::z.
Referenced by build_light_bvh(), compose_trs_from_vectors(), init_new_asset_instance(), OBB_vectors(), pos_to_pv_local(), render_mat4_widget(), rot(), rotate_sh(), solve_Q(), update_physics_components(), update_player_ir(), and update_skeleton().
Refer to Ken Perlin's 2 joint IK implmentation.
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().
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.
| leg | The leg that is updating. |
| other_leg | The other leg. |
| vel | The input velocity. |
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().
| void update_spring | ( | Spring * | s | ) |
Updates a Spring's velocity and position based on the difference between its current position and target position.
| s | The 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().
| 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.
| s | The spring to update. |
| dt | The 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().
Calculates camera vectors and roll based on the angular Spring attached to it.
| cam | The camera to update. |
| s | The spring to update. |
| input_acceleration | Movement vectors to modify roll. |
| dt | The 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().
Computes camera vectors based on yaw, pitch, and roll.
| cam | The camera to effect. |
| yaw | The direction left-right in radians. |
| pitch | The direction up-down in radians. |
| roll | The rotation in radians. |
| world_up | The 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().
| mat4 make_ortho | ( | float | left, |
| float | right, | ||
| float | bottom, | ||
| float | top, | ||
| float | near, | ||
| float | far ) |
Makes an 4x4 orthographic matrix.
| left | The left dist. |
| right | The right dist. |
| bottom | The bottom dist. |
| top | The top dist. |
| near | The near plane. |
| far | The far plane. |
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().
| mat4 make_projection | ( | float | fov_deg, |
| float | aspect, | ||
| float | near, | ||
| float | far ) |
Makes an 4x4 projection matrix.
| fov_deg | FOV in degrees. |
| aspect | The aspect ratio (W/H). |
| near | The near plane. |
| far | The far plane. |
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().
Makes a 4x4 view matrix using camera vectors.
| cam | The camera to use. |
| eye | The camera's "eye" position. |
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().
Updates a directional light camera and locks it to texel size.
| lcam | Pointer to the light camera. |
| light | Pointer 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().
| bool collision | ( | vec3 | proposed, |
| vec3 | pts[3], | ||
| vec3 | normal, | ||
| CollisionPoints * | p ) |
Definition at line 443 of file camera.c.
References CollisionPoints::a, closest_point_triangle(), collision(), CollisionPoints::d, vec3_length(), and vec3_sub().
Referenced by collision(), GJK(), player_find_collision(), and process_collision_node().
Updates a camera based on a skeleton's current neck and body position.
| s | The skeleton to use to determine camera position. |
| cam | The camera to effect. |
Definition at line 476 of file camera.c.
References mat4::c0, mat4::c1, cam, Camera::cam_data, CamData::cam_pos, compute_camera_basis(), EDITOR_SETTINGS, Camera::forward, FOV, GLOBALS, HALTON_8, CamData::inv_proj, CamData::inv_view, JITTER_OFFSET, m4_inverse(), make_look_at(), make_projection(), master_frame, Skeleton::neck, Globals::noclip, Camera::pitch, player, Player::pos, Spring::pos, CamData::proj, RENDER_HEIGHT, RENDER_WIDTH, Camera::right, Camera::roll, Spring::rotation, EditorSettings::TAA, Camera::target, CamData::unjittered_inv_proj, CamData::unjittered_proj, Camera::up, update_camera(), vec3_add(), vec3_length(), vec3_to_vec4(), CamData::view, VIEW_FAR, VIEW_NEAR, vec2::x, vec3::x, vec4::x, vec2::y, vec3::y, vec4::y, Camera::yaw, vec3::z, and vec4::z.
Referenced by main(), and update_camera().
Applies normal velocity to player with gravity and friction applied.
| move_dir | The move vectors from input. |
| dt | The 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().
Applies no-clip mode velocity to player with no gravity applied.
| move_dir | The move vectors from input. |
| dt | The 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().
Resets a skeleton to be relative to a new position.
| s | The skeleton to effect. |
| pos | The 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().
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.
| left_leg | Left leg IK. |
| right_leg | Right leg IK. |
| intended_vel | The raw input direction. |
| dt | The 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().
Moves feet, then solves a skeleton's leg IKs and updates the springs accordingly.
| s | The skeleton to effect. |
| cam | The camera attached used to derive hip offsets. |
| vel | The incoming move input raw direction. |
Definition at line 1287 of file camera.c.
References PhysicsComponent::accel, ACCEL_BETA, calculate_camera_angles(), cam, D, dt, Camera::forward, Skeleton::hip_height, Skeleton::left_hip_pos, Skeleton::left_leg, left_leg_offset, mat3_mul_vec3(), MAX, Skeleton::neck, Skeleton::neck_pelvis_diff, PhysicsComponent::old_vel, Skeleton::p_left, Skeleton::p_right, Player::pc, Skeleton::pelvis, Skeleton::pelvis_height, player, LegIK::pos, Spring::pos, Skeleton::q_left, Skeleton::q_right, Camera::right, Skeleton::right_hip_pos, Skeleton::right_leg, right_leg_offset, rot(), Player::skeleton, solve_Q(), Spring::target, Camera::up, update_leg(), update_skeleton(), update_spring(), vec3_add(), vec3_length(), vec3_normalize(), vec3_scale(), vec3_sub(), PhysicsComponent::vel, vec3::x, vec3::y, and vec3::z.
Referenced by process_input(), and update_skeleton().
| void resolve_collisions | ( | Camera * | cam | ) |
Resolves collisions between camera and geometry, uses player.
Definition at line 1377 of file camera.c.
References CollisionPoints::a, PhysicsComponent::at_rest, CollisionPoints::b, SceneData::bvh_nodes, cam, Player::cam, CAST_VEC, CollisionPoints::d, dt, GLOBALS, ground_hit, hit, MeshNode::id, MeshNode::local_matrix, mat4_mul_point(), MAX_PLAYER_CONTACTS, Globals::mesh_nodes, Globals::meshes, MIN_WALKABLE_SURFACE, CollisionPoints::mnB, CollisionPoints::normal, Camera::on_ground, MeshNode::parent, MeshNode::pc, Player::pc, player, player_find_collision(), player_resolve_impulse(), player_resolve_impulse_static(), Player::pos, RAYCAST_HEIGHT, raycast_scene(), resolve_collisions(), Globals::scene_data, MeshNode::static_mesh, Mesh::tris, vec3_add(), vec3_scale(), PhysicsComponent::vel, Mesh::vertices, vec3::x, vec3::y, and vec3::z.
Referenced by process_input(), and resolve_collisions().
| const float HALTON_8[8][2] |
Definition at line 464 of file camera.c.
Referenced by update_camera().
| bool ground_hit |
Definition at line 704 of file camera.c.
Referenced by resolve_collisions().