Skip to content

Instantly share code, notes, and snippets.

@pr0g
Last active September 26, 2021 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pr0g/6d6cd11c0278291e79e3f6eca073d0bd to your computer and use it in GitHub Desktop.
Save pr0g/6d6cd11c0278291e79e3f6eca073d0bd to your computer and use it in GitHub Desktop.
Free look and custom pivot orbit camera implementation
#include <as/as-math-ops.hpp> // https://github.com/pr0g/as
struct Camera
{
as::vec3 pivot = as::vec3::zero(); // pivot point to rotate about
as::vec3 offset = as::vec3::zero(); // offset relative to pivot
float yaw = 0.0f; // yaw rotation in radians
float pitch = 0.0f; // pitch rotation in radians
// view camera transform (v in MVP)
as::affine view() const;
// world camera transform
as::affine transform() const;
};
inline as::affine Camera::view() const
{
return as::affine_inverse(transform());
}
inline as::affine Camera::transform() const
{
// multiplication order left to right
return as::affine_mul(
as::affine_mul(
as::affine_from_vec3(offset), as::affine_from_mat3(as::mat3_rotation_zxy(
pitch, yaw, 0.0f))),
as::affine_from_vec3(pivot));
}
// see https://github.com/pr0g/as-camera/blob/0ed6ea8284bede968d20392b792d35ad6d33c3bc/include/as-camera/as-camera.hpp
// for a more complete implementation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment