Almost figured out roll

This commit is contained in:
NaifBanana 2024-11-21 01:09:21 -06:00
parent d75f449538
commit bcb8f8f4ea
4 changed files with 50 additions and 10 deletions

View File

@ -14,13 +14,17 @@ Camera::Camera(const Vec3& pos, const Vec3& tar, const Vec3& up) : _camera_type(
}
void Camera::_calcLook() {
_look = glm::lookAt(_pos, _tar, _world_up);
_look = glm::lookAt(_pos, _tar, _up);
}
glm::vec3 Camera::normCross(const Vec3& a, const Vec3& b) {
return glm::normalize(glm::cross(a, b));
}
glm::vec3 Camera::transformVec(const Mat4& mat, const Vec3& vec) {
return Vec3(mat * glm::vec4(vec, 1.0f));
}
glm::vec3 Camera::getPos() const { return _pos; }
glm::vec3 Camera::getTarget() const { return _tar; }
@ -51,7 +55,7 @@ void Camera::setPos(const Vec3& pos) {
void Camera::setTarget(const Vec3& tar) {
_tar = tar;
_dir = glm::normalize(_tar-_pos);
_right = normCross(_dir, _world_up);
_right = normCross(_dir, ((LockUp)?_world_up:_up));
_up = normCross(_right, _dir);
_calcLook();
}
@ -67,11 +71,15 @@ void Camera::setWorldUp(const Vec3& up) {
_calcLook();
}
void Camera::resetUp() {
_up = _world_up;
}
void Camera::addPos(const Vec3& add) {
_pos += add;
_dir = glm::normalize(_tar-_pos);
_right = normCross(_dir, _world_up);
_up = normCross(_right, _dir);
_right = normCross(_dir, ((LockUp)?_world_up:_up));
_calcLook();
}
@ -87,26 +95,33 @@ void Camera::panPosRelative(const Vec3& add) {
void Camera::addTarget(const Vec3& add) {
_tar += add;
_dir = glm::normalize(_tar-_pos);
_right = normCross(_dir, _world_up);
_right = normCross(_dir, (LockUp) ? _world_up : _up);
_up = normCross(_right, _dir);
_calcLook();
}
void Camera::roll(float ang) {
if (LockUp) {
return;
}
Mat4 rot_mat = glm::rotate(Mat4(1.0f), ang, _dir);
_right = transformVec(rot_mat, _right);
_up = normCross(_right, _dir);
_calcLook();
}
void Camera::pitch(float ang) {
Mat4 rot_mat = glm::rotate(Mat4(1.0f), ang, _right);
_dir = Vec3(rot_mat * glm::vec4(_dir, 1.0f));
_dir = transformVec(rot_mat, _dir);
_up = normCross(_right, _dir);
_tar = _pos + _dir;
_calcLook();
}
void Camera::yaw(float ang) {
Mat4 rot_mat = glm::rotate(Mat4(1.0f), ang, _up);
_dir = Vec3(rot_mat * glm::vec4(_dir, 1.0f));
_right = Vec3(rot_mat * glm::vec4(_right, 1.0f));
_dir = transformVec(rot_mat, _dir);
_right = normCross(_dir, _right);
_tar = _pos + _dir;
_calcLook();
}

View File

@ -20,6 +20,12 @@ void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
cam->pitch(-1.0f*deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_PERIOD) == GLFW_PRESS) {
cam->roll(1.0f*deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_COMMA) == GLFW_PRESS) {
cam->roll(-1.0f*deltaTime);
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
cam->panPosRelative(glm::vec3(-1.5f, 0.0f, 0.0f)*deltaTime);
}
@ -41,6 +47,13 @@ void processInput(GLFWwindow* window) {
if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) {
cam->setPos(glm::vec3(0.0f, 0.0f, -2.0f));
cam->setTarget(glm::vec3(0.0f, 0.0f, 0.0f));
cam->setWorldUp(glm::vec3(0.0f, 1.0f, 0.0f));
cam->resetUp();
}
if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
cam->LockUp=true;
cam->resetUp();
}
if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS) {
cam->LockUp=false;
}
}

View File

@ -22,6 +22,7 @@ public:
};
static Vec3 normCross(const Vec3&, const Vec3&);
static Vec3 transformVec(const Mat4&, const Vec3&);
Camera(
const Vec3& pos=Vec3(1.0f, 0.0f, 0.0f),
@ -29,6 +30,8 @@ public:
const Vec3& up =Vec3(0.0f, 1.0f, 0.0f)
);
bool LockUp = true;
Vec3 getPos() const;
Vec3 getTarget() const;
Vec3 getDirection() const;
@ -44,6 +47,7 @@ public:
void setTarget(const Vec3&);
void setDirection(const Vec3&);
void setWorldUp(const Vec3&);
void resetUp();
void addPos(const Vec3&);
void panPos(const Vec3&);
void panPosRelative(const Vec3&);

View File

@ -66,6 +66,14 @@ int main() {
NB_GL_DEBUG_THROW("nothing", "during");
// Input checking
processInput(window);
std::cout << "xy: " << glm::dot(myCam.getRight(), myCam.getDirection());
std::cout << "\n\tyz : " << glm::dot(myCam.getDirection(), myCam.getUp());
std::cout << "\n\tzx : " << glm::dot(myCam.getUp(), myCam.getRight());
std::cout << "\n\tdir : " << glm::length(myCam.getDirection());
std::cout << "\n\tright: " << glm::length(myCam.getRight());
std::cout << "\n\tup : " << glm::length(myCam.getUp());
std::cout << "\n\n";
// Rendering Loop
myShader.use();