WoW Model Viewer
Your premiere tool for viewing, equipping and animating World of Warcraft models.
Loading...
Searching...
No Matches
OrbitCamera.cpp
Go to the documentation of this file.
1#include "OrbitCamera.h"
2#include "glm/gtc/matrix_transform.hpp"
3
4constexpr float CAMERA_DEFAULT_YAW = 0.0f;
5constexpr float CAMERA_DEFAULT_PITCH = 90.0f;
6constexpr float CAMERA_DEFAULT_RADIUS = 5.0f;
7constexpr float CAMERA_MIN_RADIUS = 0.5f;
8constexpr float CAMERA_MAX_RADIUS = 150.0f;
9
11 : pos_(glm::vec3(0.0f)),
12 target_(glm::vec3(0.0f)),
13 up_(glm::vec3(0.0f)),
14 right_(glm::vec3(0.0f)),
15 yaw_(0.0f),
16 pitch_(0.0f),
17 radius_(0.0f)
18{
19 reset();
20}
21
23{
24 return glm::lookAt(pos_, target_, up_);
25}
26
28{
29 pos_ = glm::vec3(0.0f, 0.0f, 0.0f);
30 target_ = glm::vec3(0.0f, 0.0f, 0.0f);
31 up_ = glm::vec3(0.0f, 0.0f, 1.0f);
36}
37
38void OrbitCamera::resetFromBounds(float zMin, float zMax, float fovDegrees)
39{
40 reset();
41
42 // by default, creatures/characters are "on the ground", consider 0 as minimal possible z value
43 if (zMin < 0.0f)
44 zMin = 0.0f;
45
46 target_.z = (zMin + zMax) / 2.0f;
47 setRadius((zMin + zMax) / 2.0f * 1.3f / sinf(glm::radians(fovDegrees / 2.0f)));
49}
50
51void OrbitCamera::setLookAt(const glm::vec3& target)
52{
53 target_ = target;
55}
56
57void OrbitCamera::setRadius(float radius)
58{
61}
62
63void OrbitCamera::setPosition(const glm::vec3& position)
64{
65 pos_ = position;
66}
67
68void OrbitCamera::setYawAndPitch(float yaw, float pitch)
69{
70 yaw_ = yaw;
71
72 if (yaw_ > 360.0f)
73 yaw_ -= 360.0f;
74
75 if (yaw_ < 0.0)
76 yaw_ = 360.0f - yaw_;
77
80}
81
82void OrbitCamera::setYaw(float yaw)
83{
84 yaw_ = yaw;
86}
87
88void OrbitCamera::setPitch(float pitch)
89{
90 pitch_ = glm::clamp(pitch, CAMERA_DEFAULT_PITCH - 90.0f + 0.1f, CAMERA_DEFAULT_PITCH + 90.0f - 0.1f);
92}
93
95{
96 pos_.x = target_.x + radius_ * sinf(glm::radians(pitch_)) * cosf(glm::radians(yaw_));
97 pos_.y = target_.y + radius_ * sinf(glm::radians(pitch_)) * sinf(glm::radians(yaw_));
98 pos_.z = target_.z + radius_ * cosf(glm::radians(pitch_));
99 right_ = glm::normalize(glm::cross(target_ - pos_, up_));
100}
constexpr float CAMERA_MAX_RADIUS
constexpr float CAMERA_DEFAULT_PITCH
constexpr float CAMERA_DEFAULT_RADIUS
constexpr float CAMERA_MIN_RADIUS
constexpr float CAMERA_DEFAULT_YAW
void reset()
Reset all parameters to defaults.
float radius() const
Current orbit radius (distance to target).
Definition OrbitCamera.h:60
glm::vec3 position() const
Current camera world position.
Definition OrbitCamera.h:30
glm::vec3 right_
Definition OrbitCamera.h:68
void setLookAt(const glm::vec3 &target)
Set the point the camera orbits around.
float yaw() const
Current yaw angle in radians.
Definition OrbitCamera.h:45
glm::vec3 target_
Definition OrbitCamera.h:66
glm::vec3 pos_
Definition OrbitCamera.h:65
void setPitch(float pitch)
Set the pitch angle (vertical elevation).
void setRadius(float radius)
Set the distance from the camera to the target.
float radius_
Definition OrbitCamera.h:72
glm::vec3 up_
Definition OrbitCamera.h:67
void resetFromBounds(float zMin, float zMax, float fovDegrees)
Reset the camera to frame a model whose bounding box spans [zMin, zMax].
void setPosition(const glm::vec3 &position)
Set the camera world position directly.
void updatePosition()
float pitch() const
Current pitch angle in radians.
Definition OrbitCamera.h:48
glm::mat4 getViewMatrix() const
Compute the current view matrix from yaw, pitch, and radius.
void setYaw(float yaw)
Set the yaw angle (horizontal rotation around the target).
void setYawAndPitch(float yaw, float pitch)
Set both yaw and pitch simultaneously.