Skip to content

Commit

Permalink
Remake temp phys/collision engine
Browse files Browse the repository at this point in the history
Create and use Shader::link_shaders
Use Shader::compile_shader within Shader::load_shader function

Small cleaning and minor fixes in window.cpp
  • Loading branch information
ConniBug committed Oct 20, 2023
1 parent 90f514d commit a7db0a1
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 85 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.25)

project("2D-Engine")
project("3D-Engine")
set(CMAKE_CXX_STANDARD 11)


Expand Down
7 changes: 5 additions & 2 deletions include/CollisionEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,24 @@ class BoxCollider {
auto a = CollisionEngine::side(A, B, P);
if(a != aUV) {
logging::info("Side check for A to B failed");
return false;
}
auto b = CollisionEngine::side(B, C, P);
if(b != bUV) {
logging::info("Side check for B to C failed");
return false;
}
auto c = CollisionEngine::side(C, D, P);
if(c != cUV) {
logging::info("Side check for C to D failed");
return false;
}
auto d = CollisionEngine::side(D, A, P);
if(d != dUV) {
logging::info("Side check for D to A failed");
return false;
}

// TODO: Return a value
return true;
}
};

Expand Down
3 changes: 3 additions & 0 deletions include/PhysicsEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ class PhysicsEngine {
public:
Entity_t* entity{};

glm::vec3 velocity = glm::vec3(0.0f, 0.0f, 0.0f);
glm::vec3 acceleration = glm::vec3(0.0f, 0.0f, 0.0f);

PhysicsEngine(Entity_t* entity);

void ApplyPhysics(double deltaTime);
Expand Down
5 changes: 2 additions & 3 deletions include/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
#include "Camera.h"
#include "DebugRendering/DebugEntry.h"
#include "Entity_t.h"
#include "Shader.h"
#include "Camera.h"
#include "DebugRendering.h"
#include "GLFW/glfw3.h"
#include "Shader.h"
#include "Storage.h"
#include <iostream>

class Window;
class Scene {
Expand Down
20 changes: 11 additions & 9 deletions include/Shader.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
#ifndef INC_2D_ENGINE_SHADER_H
#define INC_2D_ENGINE_SHADER_H

#include <iostream>
#include <fstream>
#include "glad/glad.h"
#include "logging.h"
#include "glm.hpp"
#include "logging.h"
#include <fstream>
#include <iostream>

class Shader {
private:
static unsigned int compile_shader(unsigned int type, const std::string& source);
static unsigned int compile_shader(unsigned int type, const std::string &source);
static unsigned int link_shaders(unsigned int fragment_id, unsigned int vertex_id);

static bool check_compile_status(unsigned int shader_id);

public:
unsigned int id;

Expand All @@ -24,16 +27,15 @@ class Shader {
std::string fragment_path;
std::string fragment_raw;

Shader(const std::string& vertex_path,
const std::string& fragment_path
);
Shader(const std::string &vertex_path,
const std::string &fragment_path);
~Shader();

void reload();
void load_shader(const std::string& vertex_path_r, const std::string& fragment_path_r);
void load_shader(const std::string &vertex_path_r, const std::string &fragment_path_r);

void apply(glm::mat4 view = glm::mat4(0.0f), glm::mat4 projection = glm::mat4(0.0f)) const;
};


#endif //INC_2D_ENGINE_SHADER_H
#endif//INC_2D_ENGINE_SHADER_H
23 changes: 19 additions & 4 deletions src/PhysicsEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,31 @@
// Created by Conni Bilham on 24/07/2023.
//

#define G -9.81

#include "PhysicsEngine.h"

PhysicsEngine::PhysicsEngine(Entity_t *entity) {
this->entity = entity;
}

void PhysicsEngine::ApplyPhysics(double deltaTime) {
// std::cout << "PhysicsEngine::ApplyPhysics(Entity_t* entity) Called" << std::endl;
// logging::verbose("PhysicsEngine::ApplyPhysics()");

auto* acceleration = &this->entity->physics.engine->acceleration;
auto* velocity = &this->entity->physics.engine->velocity;
if(!this->entity->physics.enabled)
return;

acceleration->y = G;

// Hate this but glm::vec3 doesn't have operators setup correctly
velocity->x += acceleration->x * (float)deltaTime;
velocity->y += acceleration->y * (float)deltaTime;
velocity->z += acceleration->z * (float)deltaTime;

if(entity->physics.enabled) {
this->entity->position.y = this->entity->position.y - (9.81 * deltaTime); // Haha gravity
}
// Hate this but glm::vec3 doesn't have operators setup correctly
this->entity->position.x += velocity->x;
this->entity->position.y += velocity->y;
this->entity->position.z += velocity->z;
}
102 changes: 56 additions & 46 deletions src/Shader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,50 +44,16 @@ void Shader::load_shader(const std::string &vertex_path_r, const std::string &fr
this->fragment_raw = std::string((std::istreambuf_iterator<char>(fragment_shader_file)),
std::istreambuf_iterator<char>());

const char* vertex_raw = this->vertex_raw.c_str();
const char* fragment_raw = this->fragment_raw.c_str();

// Compile vertex shader
logging::verbose("Compiling vertex shader");
unsigned int vertex_shader_id = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader_id, 1, &vertex_raw, nullptr);
glCompileShader(vertex_shader_id);
if(!check_compile_status(vertex_shader_id)) {
logging::error("ERROR::SHADER::VERTEX::COMPILATION_FAILED");
std::cout << "Press enter to continue..." << std::endl;
std::cin.get();
}
// Compile fragment shader
logging::verbose("Compiling fragment shader");
unsigned int fragment_shader_id = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader_id, 1, &fragment_raw, nullptr);
glCompileShader(fragment_shader_id);
if(!check_compile_status(fragment_shader_id)) {
logging::error("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED");
std::cout << "Press enter to continue..." << std::endl;
std::cin.get();
}
// Link shaders
logging::verbose("Linking vertex and fragment shaders");
this->id = glCreateProgram();
glAttachShader(this->id, vertex_shader_id);
glAttachShader(this->id, fragment_shader_id);
glLinkProgram(this->id);
logging::debug("Compiling vertex shader");
unsigned int vertex_shader_id = compile_shader(GL_VERTEX_SHADER, this->vertex_raw);

// Check for linking errors
int success;
char info_log[512];
glGetProgramiv(this->id, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(this->id, 512, nullptr, info_log);
logging::error("ERROR::SHADER::PROGRAM::LINKING_FAILED");
std::cout << info_log << std::endl;
}
logging::debug("Shaders linked successfully");
// Compile fragment shader
logging::debug("Compiling fragment shader");
unsigned int fragment_shader_id = compile_shader(GL_FRAGMENT_SHADER, this->fragment_raw);

// Delete shaders
glDeleteShader(vertex_shader_id);
glDeleteShader(fragment_shader_id);
// Link shaders
this->id = Shader::link_shaders(fragment_shader_id, vertex_shader_id);
}

bool Shader::check_compile_status(unsigned int shader_id) {
Expand Down Expand Up @@ -118,24 +84,68 @@ void Shader::apply(glm::mat4 view, glm::mat4 projection) const {
int phaseID = glGetUniformLocation(this->id, "phase");
glUniform1f(phaseID, value + 1);

if(view != glm::mat4(0))
if (view != glm::mat4(0))
glUniformMatrix4fv(glGetUniformLocation(this->id, "view"), 1, GL_FALSE, &view[0][0]);
if(projection != glm::mat4(0))
if (projection != glm::mat4(0))
glUniformMatrix4fv(glGetUniformLocation(this->id, "projection"), 1, GL_FALSE, &projection[0][0]);
}

std::string ShaderTypeRaw(unsigned int type) {
switch (type) {
case (GL_VERTEX_SHADER):
return "VERTEX";
case (GL_FRAGMENT_SHADER):
return "FRAGMENT";
default:
return "UNKNOWN_TYPE";
}
}

unsigned int Shader::compile_shader(unsigned int type, const std::string &source) {
logging::debug("Shader::compile_shader() - " + std::to_string(type) + " - " + source);
auto type_e = ShaderTypeRaw(type);
logging::verbose("Shader::compile_shader() - " + type_e);
unsigned int shader_id = glCreateShader(type);

const char* CSource = source.c_str();
const char *CSource = source.c_str();
glShaderSource(shader_id, 1, &CSource, nullptr);
glCompileShader(shader_id);
check_compile_status(shader_id);
if (!check_compile_status(shader_id)) {
logging::error("ERROR::SHADER::" + type_e + "::COMPILATION_FAILED");

std::cout << "Press enter to continue..." << std::endl;
std::cin.get();
}
return shader_id;
}

unsigned int Shader::link_shaders(unsigned int fragment_id, unsigned int vertex_id) {
// Link shaders
logging::verbose("Linking vertex and fragment shaders");
auto id = glCreateProgram();
glAttachShader(id, vertex_id);
glAttachShader(id, fragment_id);
glLinkProgram(id);

// Check for linking errors
int success;
char info_log[512];
glGetProgramiv(id, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(id, 512, nullptr, info_log);
logging::error("ERROR::SHADER::PROGRAM::LINKING_FAILED");
std::cout << info_log << std::endl;

std::cout << "Press enter to continue..." << std::endl;
std::cin.get();
} else
logging::debug("Shaders linked successfully");

// Delete shaders
glDeleteShader(vertex_id);
glDeleteShader(fragment_id);
return id;
}

void Shader::reload() {
logging::debug("Shader::reload()");
glDeleteProgram(this->id);
Expand Down
39 changes: 19 additions & 20 deletions src/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// Created by Conni Bilham on 10/07/2023.
//

#include <thread>
#include "Storage.h"
#include "../include/Window.h"
#include "Storage.h"
#include <thread>

void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
if (storage->loaded_scene)
Expand Down Expand Up @@ -69,7 +69,7 @@ Window::Window(int width, int height, std::string title, int fullscreen, Camera

Window::~Window() {
logging::info("Destroying window");
for (auto* scene : storage->scene_list) {
for (auto *scene: storage->scene_list) {
delete scene;
}

Expand Down Expand Up @@ -104,7 +104,7 @@ static bool vsync = true;

float fps_history[100];
static bool full = false;
static int fps_history_index = 0;
static int frame_number = 0;

// timing
float deltaTime = 0.0f;
Expand Down Expand Up @@ -137,25 +137,25 @@ void Window::window_loop_callback(GLFWwindow *window) {

glfwSwapBuffers(window);

if (!full && fps_history_index + 1 == 100) {
if (!full && frame_number + 1 == 100) {
full = true;
logging::info("FPS history full");
logging::verbose("FPS history full");
}
fps_history[fps_history_index] = 1.0f / deltaTime;
fps_history_index = (fps_history_index + 1) % 100;
fps_history[frame_number] = 1.0f / deltaTime;
frame_number = (frame_number + 1) % 100;

const float fps = 30.0f;
auto currentFrameDuation = glfwGetTime() - currentFrame;
if (vsync) {
glfwSwapInterval(1);
if (currentFrameDuation < 1.0 / fps) {
std::this_thread::sleep_for(std::chrono::milliseconds((int)((1.0 / fps - currentFrameDuation) * 1000)));
std::this_thread::sleep_for(std::chrono::milliseconds((int) ((1.0 / fps - currentFrameDuation) * 1000)));
}
auto newFrameDuration = glfwGetTime() - currentFrame;

// If lagging
if (newFrameDuration > 1.0) {
logging::error("Lagging behind: " + std::to_string(1/newFrameDuration));
logging::error("Lagging behind: " + std::to_string(1 / newFrameDuration));
}
} else
glfwSwapInterval(0);
Expand Down Expand Up @@ -193,19 +193,18 @@ void Window::handle_input(GLFWwindow *window) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
toggle = !toggle;
ready = false;
}
else if(glfwGetKey(window, GLFW_KEY_C) == GLFW_RELEASE) {
} else if (glfwGetKey(window, GLFW_KEY_C) == GLFW_RELEASE) {
ready = true;
}

if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera->HandleKeyboard(Camera::CameraMovement::FORWARD, deltaTime, sprinting);
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera->HandleKeyboard(Camera::CameraMovement::BACKWARD, deltaTime);

if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera->HandleKeyboard(Camera::CameraMovement::LEFT, deltaTime);
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera->HandleKeyboard(Camera::CameraMovement::RIGHT, deltaTime);


Expand Down Expand Up @@ -233,7 +232,7 @@ void Window::handle_input(GLFWwindow *window) {
last_click = current_time;
}

if(glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
if (glfwGetKey(window, GLFW_KEY_P) == GLFW_PRESS) {
logging::info("Wireframe: " + std::to_string(wireframe));

if (wireframe)
Expand All @@ -245,12 +244,12 @@ void Window::handle_input(GLFWwindow *window) {
}

// -/+ for FOV
// std::cout << "FOV: " << camera->fov << std::endl;
if(glfwGetKey(window, GLFW_KEY_EQUAL) == GLFW_PRESS) {
// std::cout << "FOV: " << camera->fov << std::endl;
if (glfwGetKey(window, GLFW_KEY_EQUAL) == GLFW_PRESS) {
camera->HandleMouseScroll(10);
last_click = current_time;
}
if(glfwGetKey(window, GLFW_KEY_MINUS) == GLFW_PRESS) {
if (glfwGetKey(window, GLFW_KEY_MINUS) == GLFW_PRESS) {
camera->HandleMouseScroll(-10);
last_click = current_time;
}
Expand Down

0 comments on commit a7db0a1

Please sign in to comment.