diff --git a/src/camera.cpp b/src/camera.cpp new file mode 100644 index 0000000..c98c9a2 --- /dev/null +++ b/src/camera.cpp @@ -0,0 +1,49 @@ +#include "camera.h" +#include + +Camera::Camera(glm::vec3 initalPosition) { + this->cameraPos = initalPosition; +} + +glm::mat4 Camera::getLookAt() { + glm::vec3 direction; + direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch)); + direction.y = sin(glm::radians(pitch)); + direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch)); + cameraFront = glm::normalize(direction); + + return glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); +} + +void Camera::processMovement(Direction direction, float deltaTime) { + float speed = this->movementSpeed * deltaTime; + + switch (direction) { + case DIRECTION_FORWARD: + cameraPos += speed * cameraFront; + break; + case DIRECTION_BACKWARDS: + cameraPos -= speed * cameraFront; + break; + case DIRECTION_LEFT: + cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * speed; + break; + case DIRECTION_RIGHT: + cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * speed; + break; + } +} + +void Camera::processRotation(float deltaX, float deltaY) { + deltaX *= this->mouseSensitivity; + deltaY *= this->mouseSensitivity; + + yaw += deltaX; + pitch += -deltaY; + + // Prevent world flipping if pitch exceeds 90deg + if(pitch > 89.0f) + pitch = 89.0f; + if(pitch < -89.0f) + pitch = -89.0f; +} diff --git a/src/camera.h b/src/camera.h new file mode 100644 index 0000000..9e7158a --- /dev/null +++ b/src/camera.h @@ -0,0 +1,28 @@ +#pragma once +#include + +enum Direction { + DIRECTION_FORWARD, + DIRECTION_BACKWARDS, + DIRECTION_RIGHT, + DIRECTION_LEFT, +}; + +class Camera { +public: + glm::vec3 cameraPos; + glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); + glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); + + float pitch = 0., yaw = -90., roll = 0.; + + float movementSpeed = 2.5f; + float mouseSensitivity = 0.2f; + + Camera(glm::vec3 initialPosition); + + glm::mat4 getLookAt(); + void processRotation(float deltaX, float deltaY); + void processMovement(Direction direction, float deltaTime); + +}; diff --git a/src/renderer.cpp b/src/renderer.cpp index b4585bc..a547fb3 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,13 +1,18 @@ #include #include #include +#include +#include + #include "shader.h" #include "mesh.h" #include "defaultmeshes.h" +#include "camera.h" #include "renderer.h" Shader *shader = NULL; +Camera camera(glm::vec3(0.0, 0.0, 3.0)); void renderInit(GLFWwindow* window) { glViewport(0, 0, 500, 500); @@ -15,7 +20,7 @@ void renderInit(GLFWwindow* window) { initMeshes(); // Compile shader - shader = new Shader("assets/shaders/orange.vs", "assets/shaders/orange.fs"); + shader = new Shader("assets/shaders/phong.vs", "assets/shaders/phong.fs"); } void render(GLFWwindow* window) { @@ -24,6 +29,49 @@ void render(GLFWwindow* window) { // Use shader shader->use(); + shader->set("objectColor", glm::vec3(1.0f, 0.5f, 0.31f)); + shader->set("lightColor", glm::vec3(1.0f, 1.0f, 1.0f)); + + // view/projection transformations + glm::mat4 projection = glm::perspective(glm::radians(45.f), (float)1200 / (float)900, 0.1f, 100.0f); + glm::mat4 view = camera.getLookAt(); + shader->set("projection", projection); + shader->set("view", view); + shader->set("material", Material { + // .ambient = glm::vec3(1.0f, 0.5f, 0.31f), + .diffuse = glm::vec3(1.0f, 0.5f, 0.31f), + .specular = glm::vec3(0.5f, 0.5f, 0.5f), + .shininess = 32.0f, + }); + shader->set("sunLight", DirLight { + .direction = glm::vec3(-0.2f, -1.0f, -0.3f), + .ambient = glm::vec3(0.2f, 0.2f, 0.2f), + .diffuse = glm::vec3(0.5f, 0.5f, 0.5f), + .specular = glm::vec3(1.0f, 1.0f, 1.0f), + }); + shader->set("numPointLights", 0); + // shader->set("pointLights[0]", PointLight { + // .position = lightPos, + // .ambient = glm::vec3(0.4f, 0.4f, 0.4f), + // .diffuse = glm::vec3(1.0f, 1.0f, 1.0f), + // .specular = glm::vec3(1.0f, 1.0f, 1.0f), + // .constant = 1.0, + // .linear = 0.9, + // .quadratic = 0.32, + // }); + + // Pre-calculate the normal matrix for the shader + + // Pass in the camera position + shader->set("viewPos", camera.cameraPos); + + glm::mat4 model = glm::mat4(1.0f); + model = glm::translate(model, glm::vec3( 0.0f, 0.0f, 0.0f)); + // float angle = 20.0f * i; + // model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); + shader->set("model", model); + glm::mat3 normalMatrix = glm::mat3(glm::transpose(glm::inverse(model))); + shader->set("normalMatrix", normalMatrix); CUBE_MESH->bind(); glDrawArrays(GL_TRIANGLES, 0, 36);