camera
This commit is contained in:
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);
|
||||
if (!glContext.isInitialized()) { return -1; }
|
||||
|
||||
glm::mat4 viewMatrix = glm::lookAt(
|
||||
glm::vec3(0.0f, 0.0f, 3.0f), // Camera position
|
||||
glm::vec3(0.0f, 0.0f, 0.0f), // Look at point
|
||||
|
||||
Camera camera(
|
||||
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
|
||||
); // 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");
|
||||
|
||||
// test setup
|
||||
// vertex, color
|
||||
float vertexData[] = {
|
||||
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);
|
||||
|
||||
shader_program.use();
|
||||
shader_program.setMat4("view", viewMatrix);
|
||||
shader_program.setMat4("projection", projectionMatrix);
|
||||
shader_program.setMat4("model", modelMatrix);
|
||||
shader_program.setMat4("view", camera.getViewMatrix());
|
||||
shader_program.setMat4("projection", camera.getProjectionMatrix());
|
||||
shader_program.setMat4("model", glm::mat4(1.0f));
|
||||
|
||||
VAO.draw();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user