mirror of
https://github.com/Linloir/SceneEditor.git
synced 2025-12-18 07:58:11 +08:00
[CORE][ADD] Camera ray generation
This commit is contained in:
parent
60ecd08ed1
commit
d98b057a67
@ -32,6 +32,19 @@ Camera::Camera(glm::vec3 position, float yaw, float pitch) :
|
|||||||
updateCameraState();
|
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() {
|
void Camera::updateCameraState() {
|
||||||
|
|
||||||
// Update front vector
|
// Update front vector
|
||||||
|
|||||||
@ -3,8 +3,10 @@
|
|||||||
#include <GLM/glm.hpp>
|
#include <GLM/glm.hpp>
|
||||||
#include <GLM/ext/matrix_transform.hpp>
|
#include <GLM/ext/matrix_transform.hpp>
|
||||||
|
|
||||||
#define CAMERA_MAX_ZOOM 90.0f
|
#include "ray.h"
|
||||||
#define CAMERA_MIN_ZOOM 1.0f
|
|
||||||
|
#define CAMERA_MAX_FOVY 90.0f
|
||||||
|
#define CAMERA_MIN_FOVY 30.0f
|
||||||
|
|
||||||
class Camera {
|
class Camera {
|
||||||
public:
|
public:
|
||||||
@ -19,7 +21,9 @@ private:
|
|||||||
glm::vec3 _up;
|
glm::vec3 _up;
|
||||||
float _yaw = 0.0f;
|
float _yaw = 0.0f;
|
||||||
float _pitch = 0.0f;
|
float _pitch = 0.0f;
|
||||||
float _zoom = 45.0f;
|
float _fovy = 45.0f;
|
||||||
|
float _nearPlane = 0.1f;
|
||||||
|
float _farPlane = 100.0f;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// World settings
|
// World settings
|
||||||
@ -33,8 +37,15 @@ public:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
inline glm::vec3 front() const { return _front; }
|
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 viewMatrix();
|
||||||
|
inline glm::mat4 projectionMatrix(float aspectRatio);
|
||||||
|
|
||||||
|
Ray generateRay(glm::vec2 mouseRelativePosition, float aspectRatio) const;
|
||||||
|
|
||||||
inline glm::vec3 position() const { return _position; }
|
inline glm::vec3 position() const { return _position; }
|
||||||
|
|
||||||
@ -49,8 +60,7 @@ public:
|
|||||||
inline void setRotation(float pitchAngle, float yawAngle);
|
inline void setRotation(float pitchAngle, float yawAngle);
|
||||||
void rotate(glm::vec3 center, float deltaPitchAngle, float deltaYawAngle);
|
void rotate(glm::vec3 center, float deltaPitchAngle, float deltaYawAngle);
|
||||||
void setRotation(glm::vec3 center, float pitchAngle, float yawAngle);
|
void setRotation(glm::vec3 center, float pitchAngle, float yawAngle);
|
||||||
inline void zoom(float deltaZoom);
|
inline void setFovy(float fovy);
|
||||||
inline void setZoom(float zoom);
|
|
||||||
inline void push(float distance);
|
inline void push(float distance);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -114,26 +124,14 @@ inline void Camera::setRotation(float pitchAngle, float yawAngle) {
|
|||||||
updateCameraState();
|
updateCameraState();
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Camera::zoom(float deltaZoom) {
|
inline void Camera::setFovy(float fovy) {
|
||||||
_zoom += deltaZoom;
|
_fovy = fovy;
|
||||||
if (_zoom > CAMERA_MAX_ZOOM) {
|
if (_fovy > CAMERA_MAX_FOVY) {
|
||||||
_zoom = CAMERA_MAX_ZOOM;
|
_fovy = CAMERA_MAX_FOVY;
|
||||||
}
|
}
|
||||||
else if (_zoom < CAMERA_MIN_ZOOM) {
|
if (_fovy < CAMERA_MIN_FOVY) {
|
||||||
_zoom = CAMERA_MIN_ZOOM;
|
_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) {
|
inline void Camera::push(float distance) {
|
||||||
@ -144,3 +142,7 @@ inline void Camera::push(float distance) {
|
|||||||
inline glm::mat4 Camera::viewMatrix() {
|
inline glm::mat4 Camera::viewMatrix() {
|
||||||
return glm::lookAt(_position, _position + _front, _up);
|
return glm::lookAt(_position, _position + _front, _up);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline glm::mat4 Camera::projectionMatrix(float aspectRatio) {
|
||||||
|
return glm::perspective(glm::radians(_fovy), aspectRatio, _nearPlane, _farPlane);
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user