Add camera controls

This commit is contained in:
Jack Christensen 2024-08-21 17:14:35 -04:00
parent e3a67c6d00
commit 59bd0c30b1
8 changed files with 156 additions and 10 deletions

View File

@ -4,8 +4,8 @@ Size=400,400
Collapsed=0
[Window][Terrain Controls]
Pos=868,19
Size=412,701
Pos=863,19
Size=417,701
Collapsed=0
DockId=0x00000002,0
@ -24,12 +24,12 @@ Collapsed=0
[Window][Scene Window]
Pos=0,19
Size=866,701
Size=861,701
Collapsed=0
DockId=0x00000001,0
[Docking][Data]
DockSpace ID=0x6F42A598 Window=0xE80F322C Pos=0,19 Size=1280,701 Split=X Selected=0x9F2D9299
DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=866,701 CentralNode=1 Selected=0x9F2D9299
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=412,701 Selected=0xF69494A7
DockNode ID=0x00000001 Parent=0x6F42A598 SizeRef=861,701 CentralNode=1 Selected=0x9F2D9299
DockNode ID=0x00000002 Parent=0x6F42A598 SizeRef=417,701 Selected=0xF69494A7

39
include/camera.h Normal file
View File

@ -0,0 +1,39 @@
#ifndef CAMERA_H_
#define CAMERA_H_
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "transform.h"
struct Camera {
Transform transform;
float focalLength; // In mm
float sensorHeight;
float nearPlane;
float farPlane;
};
namespace CameraControls {
void TranslateCamera(Camera& camera, const glm::vec3& translationDelta);
void RotateCamera(Camera& camera, const glm::vec3& rotationDelta);
void SetPosition(Camera& camera, const glm::vec3& position);
void SetRotation(Camera& camera, const glm::vec3& rotation);
glm::vec3 GetPosition(Camera& camera);
glm::vec3 GetCenter(Camera& camera);
float GetFOV(Camera& camera);
void AdjustFocalLength(Camera& camera, float focalLength);
void AdjustSensorHeight(Camera& camera, float sensorHeight);
void AdjustNearPlane(Camera& camera, float nearPlane);
void AdjustFarPlane(Camera& camera, float farPlane);
glm::mat4 GetViewMatrix(Camera& camera);
}
#endif // CAMERA_H_

View File

@ -7,6 +7,7 @@
#include <string>
#include "fbo.h"
#include "camera.h"
struct Vertex {
glm::vec3 position;
@ -34,6 +35,7 @@ class Scene {
void InitQuadBuffers();
void InitShaders();
Camera activeCamera_;
Framebuffer geo_fbo_;
Framebuffer lht_fbo_;
GLuint shader_program_;

22
include/transform.h Normal file
View File

@ -0,0 +1,22 @@
#ifndef TRANSFORM_H_
#define TRANSFORM_H_
#include <glm/glm.hpp>
struct Transform {
glm::vec3 translation;
glm::vec3 rotation;
glm::vec3 scale;
};
namespace TransformControls {
void SetPosition(Transform& transform, const glm::vec3& newPosition);
void SetRotation(Transform& transform, const glm::vec3& newRotation);
void SetScale(Transform& transform, const glm::vec3& newScale);
void Translate(Transform& transform, const glm::vec3& translationDelta);
void Rotate(Transform& transform, const glm::vec3& rotationDelta);
void Scale(Transform& transform, const glm::vec3& scaleDelta);
}
#endif // TRANSFORM_H_

53
source/camera.cpp Normal file
View File

@ -0,0 +1,53 @@
#include "camera.h"
namespace CameraControls {
void TranslateCamera(Camera& camera, const glm::vec3& translationDelta) {
TransformControls::Translate(camera.transform, translationDelta);
}
void RotateCamera(Camera& camera, const glm::vec3& rotationDelta) {
TransformControls::Rotate(camera.transform, rotationDelta);
}
void SetPosition(Camera& camera, const glm::vec3& position) {
TransformControls::SetPosition(camera.transform, position);
}
void SetRotation(Camera& camera, const glm::vec3& rotation) {
TransformControls::SetRotation(camera.transform, rotation);
}
glm::vec3 GetPosition(Camera& camera) {
return camera.transform.translation;
}
glm::vec3 GetRotation(Camera& camera) {
return camera.transform.rotation;
}
glm::vec3 GetCenter(Camera& camera) {
glm::vec3 forward;
forward.x = cos(camera.transform.rotation.y) * cos(camera.transform.rotation.x);
forward.y = sin(camera.transform.rotation.x);
forward.z = sin(camera.transform.rotation.y) * cos(camera.transform.rotation.x);
return GetPosition(camera) + GetRotation(camera);
}
float GetFOV(Camera& camera) {
return 2.0f * atan(camera.sensorHeight / (2.0f * camera.focalLength));
}
void AdjustFocalLength(Camera& camera, float focalLength) {
camera.focalLength = focalLength;
}
void AdjustSensorHeight(Camera& camera, float sensorHeight) {
camera.sensorHeight = sensorHeight;
}
void AdjustNearPlane(Camera& camera, float nearPlane) {
camera.nearPlane = nearPlane;
}
void AdjustFarPlane(Camera& camera, float farPlane) {
camera.farPlane = farPlane;
}
glm::mat4 GetViewMatrix(Camera& camera) {
//return glm::lookAt(glm::vec3(0.0f, 1.4f, 1.4f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
return glm::lookAt(GetPosition(camera), GetCenter(camera), glm::vec3(0.0f, 1.0f, 0.0f));
}
}

View File

@ -48,7 +48,7 @@ namespace {
glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, GL_TEXTURE_2D, textureId, 0);
}
}; // namespace
} // namespace
namespace FBO {
Framebuffer GenerateFramebuffer(int width, int height) {
@ -102,4 +102,4 @@ namespace FBO {
glDeleteFramebuffers(1, &fbo.id);
}
}
}; // namespace
} // namespace FBO

View File

@ -22,6 +22,7 @@
#include <algorithm>
#include "scene.h"
#include "camera.h"
#include "Uniforms.h"
#include "InitShader.h"
#include "DebugCallback.h"
@ -85,6 +86,13 @@ void Scene::Init() {
ReloadShader();
InitQuadBuffers();
CameraControls::SetPosition(activeCamera_, glm::vec3(1.4f, 1.4f, 0.0f));
CameraControls::SetRotation(activeCamera_, -glm::normalize(CameraControls::GetPosition(activeCamera_)));
CameraControls::AdjustFocalLength(activeCamera_, 50.0f); // Temporary hardcode focal length
CameraControls::AdjustSensorHeight(activeCamera_, 24.0f); // Temporary hardcode focal length
CameraControls::AdjustNearPlane(activeCamera_, near_z_);
CameraControls::AdjustFarPlane(activeCamera_, far_z_);
UpdateCamera();
Uniforms::Init();
}
@ -211,8 +219,7 @@ void Scene::Display(GLFWwindow* window) {
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
view_matrix_ = glm::lookAt(glm::vec3(0.0f, 1.4f, 1.4f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
Uniforms::SceneData.PV = projection_matrix_ * view_matrix_; // Projection-View matrix
Uniforms::SceneData.PV = projection_matrix_ * CameraControls::GetViewMatrix(activeCamera_); // Projection-View matrix
Uniforms::BufferSceneData();
glm::mat4 model_matrix = glm::rotate(angle_, glm::vec3(1.0f, 0.0f, 0.0f)) * glm::scale(glm::vec3(scale_));
@ -366,7 +373,7 @@ std::vector<int16_t> Scene::LoadHGT(const std::string& filename, int width, int
}
void Scene::UpdateCamera() {
projection_matrix_ = glm::perspective(fov_, aspect_, near_z_, far_z_);
projection_matrix_ = glm::perspective(CameraControls::GetFOV(activeCamera_), aspect_, activeCamera_.nearPlane, activeCamera_.farPlane);
}
GLuint Scene::CreateHeightmapTexture(std::vector<int16_t> data, int width, int height) {

23
source/transform.cpp Normal file
View File

@ -0,0 +1,23 @@
#include "transform.h"
namespace TransformControls {
void SetPosition(Transform& transform, const glm::vec3& newPosition) {
transform.translation = newPosition;
}
void SetRotation(Transform& transform, const glm::vec3& newRotation) {
transform.rotation = newRotation;
}
void SetScale(Transform& transform, const glm::vec3& newScale) {
transform.scale = newScale;
}
void Translate(Transform& transform, const glm::vec3& translationDelta) {
transform.translation += translationDelta;
}
void Rotate(Transform& transform, const glm::vec3& rotationDelta) {
transform.rotation += rotationDelta;
}
void Scale(Transform& transform, const glm::vec3& scaleDelta) {
transform.scale *= scaleDelta;
}
}