Skip to content

Commit

Permalink
Restructure some things regarding Shader.cpp
Browse files Browse the repository at this point in the history
Add PhysicsEngine.cpp/.h
Add CollisionEngine.cpp/.h

Update Camera.h
  • Loading branch information
ConniBug committed Sep 19, 2023
1 parent 937f922 commit 0ca8daa
Show file tree
Hide file tree
Showing 8 changed files with 211 additions and 72 deletions.
61 changes: 54 additions & 7 deletions include/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
#include "glm.hpp"
#include "gtc/matrix_transform.hpp"
#include "gtc/type_ptr.hpp"
#include "logging.h"


#include <iostream>

enum CAMERA_TYPE {
NONE,
PERSPECTIVE,
ORTHOGRAPHIC
};

class Camera {
public:
Expand All @@ -32,10 +42,14 @@ class Camera {
float mouse_sensitivity;
float fov;

CAMERA_TYPE type;

Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f),
glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f),
float yaw = DEFAULT_YAW,
float pitch = DEFAULT_PITCH) {
float pitch = DEFAULT_PITCH,
CAMERA_TYPE type = CAMERA_TYPE::NONE) {


front = glm::vec3(0.0f, 0.0f, -1.0f);
movement_speed = DEFAULT_SPEED;
Expand All @@ -47,6 +61,7 @@ class Camera {
this->global_up = up;
this->yaw = yaw;
this->pitch = pitch;
this->type = type;

updateCamera();
}
Expand Down Expand Up @@ -82,12 +97,24 @@ class Camera {
this->yaw += x_offset * this->mouse_sensitivity;
this->pitch += y_offset * this->mouse_sensitivity;

if (this->pitch > 89.0f) {
this->pitch = 89.0f;
}
if (this->pitch < -89.0f) {
this->pitch = -89.0f;
}
// const float pitch_max = 5.f;
// const float yaw_max = 5.f;
// if (this->pitch > pitch_max)
// this->pitch = pitch_max;
// if (this->pitch < -pitch_max)
// this->pitch = -pitch_max;
//
// if (this->yaw < -90.f - yaw_max) {
// this->yaw = -90.f - yaw_max;
// }
// if (this->yaw > -90.f + yaw_max) {
// this->yaw = -90.f + yaw_max;
// }

if(this->yaw > 360.f)
this->yaw -= 360.f;
if(this->yaw < -360.f)
this->yaw += 360.f;

updateCamera();
}
Expand All @@ -101,6 +128,26 @@ class Camera {
this->fov = 120.0f;
}

glm::mat4 getProjectionMatrix(float width, float height) {
auto projection = glm::mat4(1.0f);
if(this->type == CAMERA_TYPE::NONE) {
logging::error("Camera::getProjectionMatrix() - Camera type not set");
std::getchar();
exit(-1);
}
switch (type) {
case CAMERA_TYPE::ORTHOGRAPHIC:
projection *= glm::ortho(0.0f, width, 0.0f, height, -100.0f, 100.0f);
break;
case CAMERA_TYPE::PERSPECTIVE:
projection *= glm::perspective(glm::radians(fov), width / height, 0.1f, 100.0f);
break;
case NONE:
break;
}
return projection;
}

private:
void updateCamera() {
glm::vec3 front = {
Expand Down
62 changes: 62 additions & 0 deletions include/CollisionEngine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//
// Created by Conni Bilham on 25/07/2023.
//

#ifndef INC_2D_ENGINE_COLLISIONENGINE_H
#define INC_2D_ENGINE_COLLISIONENGINE_H

#include "vec3.hpp"
#include "glm.hpp"
#include "logging.h"

class BoxCollider;
class CollisionEngine {
public:
CollisionEngine() {

}
glm::vec3 test;


static unsigned int side(glm::vec2 A, glm::vec2 B, glm::vec2 P);

static bool isInside(BoxCollider A, glm::vec3 P);
};

class BoxCollider {

public:
glm::vec3 A = {};
glm::vec3 B;
glm::vec3 C;
glm::vec3 D;

bool contains(glm::vec3 P) {
const int aUV = 1;
const int bUV = 1;
const int cUV = 1;
const int dUV = 1;

auto a = CollisionEngine::side(A, B, P);
if(a != aUV) {
logging::info("Side check for A to B failed");
}
auto b = CollisionEngine::side(B, C, P);
if(b != bUV) {
logging::info("Side check for B to C failed");
}
auto c = CollisionEngine::side(C, D, P);
if(c != cUV) {
logging::info("Side check for C to D failed");
}
auto d = CollisionEngine::side(D, A, P);
if(d != dUV) {
logging::info("Side check for D to A failed");
}

// TODO: Return a value
}
};


#endif //INC_2D_ENGINE_COLLISIONENGINE_H
14 changes: 10 additions & 4 deletions include/Entity_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,29 @@
#include "logging.h"
//#include "PhysicsEngine.h"

#include "Shader.h"

class PhysicsEngine;
class Entity_t {
private:
unsigned int VBO, VAO, EBO;

public:
double timer;
unsigned int shader_id;
Shader* shader;

PhysicsEngine* physicsEngine;
class Physics {
public:
bool enabled = false;
PhysicsEngine* engine;
} physics;

glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 rotation = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 scale = glm::vec3(1.0f, 1.0f, 1.0f);

Entity_t(unsigned int shader_id);
Entity_t(unsigned int shader_id, glm::vec3 position, glm::vec3 rotation, glm::vec3 scale);
Entity_t(Shader* shader);
Entity_t(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 scale);

~Entity_t();

Expand Down
8 changes: 3 additions & 5 deletions include/PhysicsEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@
#include <iostream>
#include "Entity_t.h"

class Entity_t;
class PhysicsEngine {
private:
Entity_t* entity;

public:
Entity_t* entity{};

PhysicsEngine();
PhysicsEngine(Entity_t* entity_i);
PhysicsEngine(Entity_t* entity);

void ApplyPhysics(double deltaTime);
};
Expand Down
10 changes: 6 additions & 4 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ int main() {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // For Mac OS X

storage->window.title = "d";

storage->window.title = "";
storage->scene_list.emplace_back(new main_scene());
storage->scene_list.emplace_back(new main_scene());
storage->scene_list.emplace_back(new orth_scene());
storage->scene_list.emplace_back(new main_scene());

auto camera = new Camera(glm::vec3(0.0f, 0.0f, 3.0f));
storage->window.width = 800;
storage->window.height = 600;

auto main_window = new Window(800, 600, storage->window.title, storage->window.fullscreen, camera);
auto main_window = new Window(storage->window.width, storage->window.height, storage->window.title, storage->window.fullscreen, camera);
main_window->set_camera(camera);
main_window->window_size_callback(nullptr, storage->window.width, storage->window.height);

return 0;
}
19 changes: 19 additions & 0 deletions src/CollisionEngine.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Created by Conni Bilham on 25/07/2023.
//

#include "CollisionEngine.h"

unsigned int CollisionEngine::side(glm::vec2 A, glm::vec2 B, glm::vec2 P) {
auto d = (P.x - A.x) * (B.y - A.y) - (P.y - A.y) * (B.x - A.x);

if(d == 0)
return 0;
else
return d > 0 ? 1 : 2;
}

bool CollisionEngine::isInside(BoxCollider A, glm::vec3 P) {

}

98 changes: 51 additions & 47 deletions src/Entity_t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@

#include "Entity_t.h"

Entity_t::Entity_t(unsigned int shader_id) {
this->shader_id = shader_id;

Entity_t::Entity_t(Shader* shader) {
this->shader = shader;
load();
}

Expand All @@ -14,8 +15,8 @@ Entity_t::~Entity_t() {
glDeleteBuffers(1, &VBO);
}

Entity_t::Entity_t(unsigned int shader_id, glm::vec3 position, glm::vec3 rotation, glm::vec3 scale) {
this->shader_id = shader_id;
Entity_t::Entity_t(Shader* shader, glm::vec3 position, glm::vec3 rotation, glm::vec3 scale) {
this->shader = shader;

this->position = position;
this->rotation = rotation;
Expand All @@ -26,47 +27,47 @@ Entity_t::Entity_t(unsigned int shader_id, glm::vec3 position, glm::vec3 rotatio

void Entity_t::load() {
float vertices[] = {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,

-0.5f, -0.5f, 0.5f, 0.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0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,

-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f
};

// unsigned int indices[] = { // note that we start from 0!
Expand Down Expand Up @@ -104,6 +105,8 @@ void Entity_t::load() {
}

void Entity_t::draw() {
// Apply shader
// glUseProgram(shader->id);

glBindVertexArray(VAO);
glm::mat4 model = glm::mat4(1.0f);
Expand All @@ -114,13 +117,14 @@ void Entity_t::draw() {
}
model = glm::scale(model, scale);

glUniformMatrix4fv(glGetUniformLocation(shader_id, "model"), 1, GL_FALSE, &model[0][0]);
// Set model matrix
glUniformMatrix4fv(glGetUniformLocation(shader->id, "model"), 1, GL_FALSE, &model[0][0]);


glDrawArrays(GL_TRIANGLES, 0, 36);

}

void Entity_t::init() {
logging::log("Initializing entity");
timer = 0;
}
Loading

0 comments on commit 0ca8daa

Please sign in to comment.