A CUBEE-- wait what the fuck is that

This commit is contained in:
maelstrom 2024-09-27 22:29:51 +02:00
parent 2025cf4270
commit 76c503e2c3
3 changed files with 126 additions and 1 deletions

49
src/camera.cpp Normal file
View file

@ -0,0 +1,49 @@
#include "camera.h"
#include <glm/ext/matrix_transform.hpp>
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;
}

28
src/camera.h Normal file
View file

@ -0,0 +1,28 @@
#pragma once
#include <glm/glm.hpp>
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);
};

View file

@ -1,13 +1,18 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <glm/ext.hpp>
#include <glm/glm.hpp>
#include "shader.h" #include "shader.h"
#include "mesh.h" #include "mesh.h"
#include "defaultmeshes.h" #include "defaultmeshes.h"
#include "camera.h"
#include "renderer.h" #include "renderer.h"
Shader *shader = NULL; Shader *shader = NULL;
Camera camera(glm::vec3(0.0, 0.0, 3.0));
void renderInit(GLFWwindow* window) { void renderInit(GLFWwindow* window) {
glViewport(0, 0, 500, 500); glViewport(0, 0, 500, 500);
@ -15,7 +20,7 @@ void renderInit(GLFWwindow* window) {
initMeshes(); initMeshes();
// Compile shader // 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) { void render(GLFWwindow* window) {
@ -24,6 +29,49 @@ void render(GLFWwindow* window) {
// Use shader // Use shader
shader->use(); 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(); CUBE_MESH->bind();
glDrawArrays(GL_TRIANGLES, 0, 36); glDrawArrays(GL_TRIANGLES, 0, 36);