camera
This commit is contained in:
32
inc/engine/camera.hpp
Normal file
32
inc/engine/camera.hpp
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef CAMERA_HPP
|
||||||
|
#define CAMERA_HPP
|
||||||
|
|
||||||
|
// Includes
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
|
|
||||||
|
// Camera Class
|
||||||
|
class Camera {
|
||||||
|
public:
|
||||||
|
Camera(const glm::vec3& position, const glm::vec3& target, const glm::vec3& up);
|
||||||
|
|
||||||
|
glm::mat4 getViewMatrix() const;
|
||||||
|
glm::mat4 getProjectionMatrix() const;
|
||||||
|
void setProjectionMatrix(float fov, float aspectRatio, float nearPlane, float farPlane);
|
||||||
|
void setPosition(const glm::vec3& position);
|
||||||
|
void setTarget(const glm::vec3& target);
|
||||||
|
void setUp(const glm::vec3& up);
|
||||||
|
glm::vec3 getPosition() const;
|
||||||
|
|
||||||
|
float fov = 45.0f;
|
||||||
|
|
||||||
|
private:
|
||||||
|
glm::vec3 position;
|
||||||
|
glm::vec3 target;
|
||||||
|
glm::vec3 up;
|
||||||
|
float aspectRatio = 4.0f / 3.0f;
|
||||||
|
float nearPlane = 0.1f;
|
||||||
|
float farPlane = 100.0f;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "engine/sdl.hpp"
|
#include "engine/sdl.hpp"
|
||||||
#include "engine/buffers.hpp"
|
#include "engine/buffers.hpp"
|
||||||
#include "engine/shaders.hpp"
|
#include "engine/shaders.hpp"
|
||||||
|
#include "engine/camera.hpp"
|
||||||
|
|
||||||
// OpenGL Context Class
|
// OpenGL Context Class
|
||||||
class OpenGLContext {
|
class OpenGLContext {
|
||||||
|
|||||||
44
src/engine/camera.cpp
Normal file
44
src/engine/camera.cpp
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
#include "engine/camera.hpp"
|
||||||
|
|
||||||
|
// Camera Constructor
|
||||||
|
Camera::Camera(const glm::vec3& position, const glm::vec3& target, const glm::vec3& up)
|
||||||
|
: position(position), target(target), up(up) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get View Matrix
|
||||||
|
glm::mat4 Camera::getViewMatrix() const {
|
||||||
|
return glm::lookAt(position, target, up);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Position
|
||||||
|
void Camera::setPosition(const glm::vec3& position) {
|
||||||
|
this->position = position;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Target
|
||||||
|
void Camera::setTarget(const glm::vec3& target) {
|
||||||
|
this->target = target;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Up Vector
|
||||||
|
void Camera::setUp(const glm::vec3& up) {
|
||||||
|
this->up = up;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Position
|
||||||
|
glm::vec3 Camera::getPosition() const {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get Projection Matrix
|
||||||
|
glm::mat4 Camera::getProjectionMatrix() const {
|
||||||
|
return glm::perspective(glm::radians(fov), aspectRatio, nearPlane, farPlane);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set Projection Matrix Parameters
|
||||||
|
void Camera::setProjectionMatrix(float fov, float aspectRatio, float nearPlane, float farPlane) {
|
||||||
|
this->fov = fov;
|
||||||
|
this->aspectRatio = aspectRatio;
|
||||||
|
this->nearPlane = nearPlane;
|
||||||
|
this->farPlane = farPlane;
|
||||||
|
}
|
||||||
25
src/main.cpp
25
src/main.cpp
@@ -12,17 +12,22 @@ int main(int argc, char* argv[]) {
|
|||||||
OpenGLContext glContext(window);
|
OpenGLContext glContext(window);
|
||||||
if (!glContext.isInitialized()) { return -1; }
|
if (!glContext.isInitialized()) { return -1; }
|
||||||
|
|
||||||
glm::mat4 viewMatrix = glm::lookAt(
|
|
||||||
glm::vec3(0.0f, 0.0f, 3.0f), // Camera position
|
Camera camera(
|
||||||
glm::vec3(0.0f, 0.0f, 0.0f), // Look at point
|
glm::vec3(0.0f, 0.0f, 3.0f), // Position
|
||||||
|
glm::vec3(0.0f, 0.0f, 0.0f), // Target
|
||||||
glm::vec3(0.0f, 1.0f, 0.0f) // Up vector
|
glm::vec3(0.0f, 1.0f, 0.0f) // Up vector
|
||||||
); // View matrix
|
);
|
||||||
glm::mat4 projectionMatrix = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); // Projection matrix
|
|
||||||
glm::mat4 modelMatrix = glm::mat4(1.0f); // Model matrix
|
camera.setProjectionMatrix(
|
||||||
|
45.0f, // Field of View
|
||||||
|
800.0f / 600.0f, // Aspect Ratio
|
||||||
|
0.1f, // Near Plane
|
||||||
|
100.0f // Far Plane
|
||||||
|
);
|
||||||
|
|
||||||
Shader shader_program("shaders/triangle.vert", "shaders/triangle.frag");
|
Shader shader_program("shaders/triangle.vert", "shaders/triangle.frag");
|
||||||
|
|
||||||
// test setup
|
|
||||||
// vertex, color
|
// vertex, color
|
||||||
float vertexData[] = {
|
float vertexData[] = {
|
||||||
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // top right
|
||||||
@@ -53,9 +58,9 @@ int main(int argc, char* argv[]) {
|
|||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
shader_program.use();
|
shader_program.use();
|
||||||
shader_program.setMat4("view", viewMatrix);
|
shader_program.setMat4("view", camera.getViewMatrix());
|
||||||
shader_program.setMat4("projection", projectionMatrix);
|
shader_program.setMat4("projection", camera.getProjectionMatrix());
|
||||||
shader_program.setMat4("model", modelMatrix);
|
shader_program.setMat4("model", glm::mat4(1.0f));
|
||||||
|
|
||||||
VAO.draw();
|
VAO.draw();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user