From d98b057a67cb92d7fe841c769771b20254cb7edd Mon Sep 17 00:00:00 2001 From: Linloir <3145078758@qq.com> Date: Sun, 18 Dec 2022 16:44:34 +0800 Subject: [PATCH] [CORE][ADD] Camera ray generation --- FinalProject/camera.cpp | 13 +++++++++++ FinalProject/camera.h | 50 +++++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 24 deletions(-) diff --git a/FinalProject/camera.cpp b/FinalProject/camera.cpp index 0951b2a..c87c81e 100644 --- a/FinalProject/camera.cpp +++ b/FinalProject/camera.cpp @@ -32,6 +32,19 @@ Camera::Camera(glm::vec3 position, float yaw, float pitch) : updateCameraState(); } +Ray Camera::generateRay(glm::vec2 mouseRelativePosition, float aspectRatio) const { + // Calculate the near plane basic geometry + float nearPlaneHeight = 2.0f * glm::tan(glm::radians(_fovy) / 2.0f) * _nearPlane; + float nearPlaneWidth = nearPlaneHeight * aspectRatio; + // Calculate the vector that points from camera to left bottom corner of the near plane + glm::vec3 basic = _front * _nearPlane - _right * nearPlaneWidth / 2.0f - _up * nearPlaneHeight / 2.0f; + // Get the offset vector on the near plane from left bottom corner + glm::vec3 offset = _right * mouseRelativePosition.x * nearPlaneWidth + _up * mouseRelativePosition.y * nearPlaneHeight; + // Calculate the final ray direction + glm::vec3 direction = glm::normalize(basic + offset); + return Ray(_position, direction); +} + void Camera::updateCameraState() { // Update front vector diff --git a/FinalProject/camera.h b/FinalProject/camera.h index e1b0ee5..6da3081 100644 --- a/FinalProject/camera.h +++ b/FinalProject/camera.h @@ -3,8 +3,10 @@ #include #include -#define CAMERA_MAX_ZOOM 90.0f -#define CAMERA_MIN_ZOOM 1.0f +#include "ray.h" + +#define CAMERA_MAX_FOVY 90.0f +#define CAMERA_MIN_FOVY 30.0f class Camera { public: @@ -19,7 +21,9 @@ private: glm::vec3 _up; float _yaw = 0.0f; float _pitch = 0.0f; - float _zoom = 45.0f; + float _fovy = 45.0f; + float _nearPlane = 0.1f; + float _farPlane = 100.0f; private: // World settings @@ -33,8 +37,15 @@ public: public: inline glm::vec3 front() const { return _front; } - inline float zoomVal() const { return _zoom; } + + inline float fovy() const { return _fovy; } + inline float nearPlane() const { return _nearPlane; } + inline float farPlane() const { return _farPlane; } + inline glm::mat4 viewMatrix(); + inline glm::mat4 projectionMatrix(float aspectRatio); + + Ray generateRay(glm::vec2 mouseRelativePosition, float aspectRatio) const; inline glm::vec3 position() const { return _position; } @@ -49,8 +60,7 @@ public: inline void setRotation(float pitchAngle, float yawAngle); void rotate(glm::vec3 center, float deltaPitchAngle, float deltaYawAngle); void setRotation(glm::vec3 center, float pitchAngle, float yawAngle); - inline void zoom(float deltaZoom); - inline void setZoom(float zoom); + inline void setFovy(float fovy); inline void push(float distance); private: @@ -114,26 +124,14 @@ inline void Camera::setRotation(float pitchAngle, float yawAngle) { updateCameraState(); } -inline void Camera::zoom(float deltaZoom) { - _zoom += deltaZoom; - if (_zoom > CAMERA_MAX_ZOOM) { - _zoom = CAMERA_MAX_ZOOM; +inline void Camera::setFovy(float fovy) { + _fovy = fovy; + if (_fovy > CAMERA_MAX_FOVY) { + _fovy = CAMERA_MAX_FOVY; } - else if (_zoom < CAMERA_MIN_ZOOM) { - _zoom = CAMERA_MIN_ZOOM; + if (_fovy < CAMERA_MIN_FOVY) { + _fovy = CAMERA_MIN_FOVY; } - updateCameraState(); -} - -inline void Camera::setZoom(float zoom) { - _zoom = zoom; - if (_zoom > CAMERA_MAX_ZOOM) { - _zoom = CAMERA_MAX_ZOOM; - } - else if (_zoom < CAMERA_MIN_ZOOM) { - _zoom = CAMERA_MIN_ZOOM; - } - updateCameraState(); } inline void Camera::push(float distance) { @@ -144,3 +142,7 @@ inline void Camera::push(float distance) { inline glm::mat4 Camera::viewMatrix() { return glm::lookAt(_position, _position + _front, _up); } + +inline glm::mat4 Camera::projectionMatrix(float aspectRatio) { + return glm::perspective(glm::radians(_fovy), aspectRatio, _nearPlane, _farPlane); +}