[CORE][ADD] Camera ray generation

This commit is contained in:
Linloir 2022-12-18 16:44:34 +08:00
parent 60ecd08ed1
commit d98b057a67
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
2 changed files with 39 additions and 24 deletions

View File

@ -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

View File

@ -3,8 +3,10 @@
#include <GLM/glm.hpp>
#include <GLM/ext/matrix_transform.hpp>
#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);
}