diff --git a/README.md b/README.md index 9b15d264..7ddbe8e4 100644 --- a/README.md +++ b/README.md @@ -65,8 +65,8 @@ Pull requests are welcome. For major changes, please open an issue first to disc - [x] 3D Cube - [x] 3D Transforms - [x] WASD - - [x] basic input handling - - [x] Mouse controlled camers + - [x] Basic input handling + - [x] Mouse controlled camera - [x] Window Class - [x] Resize context on window resize - [x] Time Class @@ -92,4 +92,4 @@ Pull requests are welcome. For major changes, please open an issue first to disc - [ ] Scene Graph - [ ] XML based scene graph import - [ ] Test ON Windows - - [ ] Port to Emscripten + - [ ] Port to Emscripten \ No newline at end of file diff --git a/engine/ce_assets.h b/engine/ce_assets.h index 78753747..3b33619d 100644 --- a/engine/ce_assets.h +++ b/engine/ce_assets.h @@ -1,5 +1,4 @@ -#ifndef _CE_ASSETS_H_ -#define _CE_ASSETS_H_ +#pragma once #include #include @@ -31,6 +30,4 @@ namespace ce { specularTex = "missing.png", shader = ""; }; -} - -#endif // !_CE_ASSETS_H_ +} \ No newline at end of file diff --git a/engine/core/time.cpp b/engine/core/time.cpp index a2a8e747..3627a0a0 100644 --- a/engine/core/time.cpp +++ b/engine/core/time.cpp @@ -5,7 +5,8 @@ float ce::Time::getTime() { return SDL_GetTicks() / 1000.0f; } -ce::Time::Time() : m_last(getTime()), m_now(0), m_dt(0), m_fps(0) {} +ce::Time::Time() + : m_last(getTime()), m_now(0), m_dt(0), m_fps(0) {} ce::Time::~Time() {} @@ -14,4 +15,4 @@ void ce::Time::update() { m_dt = m_now - m_last; m_fps = 1 / m_dt; m_last = m_now; -} +} \ No newline at end of file diff --git a/engine/core/tpnt_log.h b/engine/core/tpnt_log.h index b455a6f8..bcefc5b5 100644 --- a/engine/core/tpnt_log.h +++ b/engine/core/tpnt_log.h @@ -24,7 +24,7 @@ #define TPNT_LOGGER_FORMAT "[%s][%s:%d|%s]: %s\n" #define TPNT_LOGGER_FILE strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__ #define TPNT_LOGGER_CONTEXT TPNT_LOGGER_FILE, __LINE__, __FUNCTION__ -#define TPNT_LOG(L, X) printf(TPNT_LOGGER_FORMAT, TPNT_LOGGER_STR(L), TPNT_LOGGER_CONTEXT,TPNT_LOGGER_STR(X)) +#define TPNT_LOG(L, X) printf(TPNT_LOGGER_FORMAT, TPNT_LOGGER_STR(L), TPNT_LOGGER_CONTEXT, TPNT_LOGGER_STR(X)) #define TPNT_LOG_RESET "\033[0m" #define TPNT_LOG_SUCCESS "\033[1;32m" #define TPNT_LOG_WARN "\033[1;33m" @@ -33,4 +33,4 @@ #define LOG_INFO(X) TPNT_LOG("INFO", X) #define LOG_WARN(X) TPNT_LOG(TPNT_LOG_WARN + "WARN" + TPNT_LOG_RESET, X) #define LOG_ERROR(X) TPNT_LOG(TPNT_LOG_ERROR + "ERROR" + TPNT_LOG_RESET, X) -#define LOG_SUCCESS(X) TPNT_LOG(TPNT_LOG_SUCCESS + "SUCCESS" + TPNT_LOG_RESET, X) +#define LOG_SUCCESS(X) TPNT_LOG(TPNT_LOG_SUCCESS + "SUCCESS" + TPNT_LOG_RESET, X) \ No newline at end of file diff --git a/engine/main.cpp b/engine/main.cpp index 4a43b146..212c312e 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -91,18 +91,18 @@ int main(int argc, char* argv[]) { ce::RenderingEngine* renderingEngine = new ce::RenderingEngine(); renderingEngine->setFOV(75.0f); renderingEngine->setSize(window->getWindowSize()); - renderingEngine->setClipRange(0.1f,100.0f); + renderingEngine->setClipRange(0.1f, 100.0f); ce::Transform* transform = new ce::Transform(); ce::Mesh* mesh = new ce::Mesh(vertices, vertexCount, indices, indexCount); ce::Shader* shader = new ce::Shader("basic"); ce::Material* material = new ce::Material(shader); - ce::Material* planeMaterial = new ce::Material(shader); material->setTexture(new ce::Texture("uv-map.png")); - - ce::Mesh* plane = new ce::Mesh(planeVertices,planeIndexCount,planeIndices,planeVertexCount); + + ce::Material* planeMaterial = new ce::Material(shader); + ce::Mesh* plane = new ce::Mesh(planeVertices, planeIndexCount, planeIndices, planeVertexCount); ce::Transform* planeTransform = new ce::Transform(); - + float mouseSensitivity = 0.1f; ce::Camera* camera = new ce::Camera(); // Seperate so i can put in a player class later @@ -159,10 +159,8 @@ int main(int argc, char* argv[]) { if ((event.key.keysym.sym == SDLK_a && cameraVelocity.x < 0) || (event.key.keysym.sym == SDLK_d && cameraVelocity.x > 0)) cameraVelocity.x = 0; - if ((event.key.keysym.sym == SDLK_SPACE && - cameraVelocity.y > 0) || - (event.key.keysym.sym == SDLK_LSHIFT && - cameraVelocity.y < 0)) + if ((event.key.keysym.sym == SDLK_SPACE && cameraVelocity.y > 0) || + (event.key.keysym.sym == SDLK_LSHIFT && cameraVelocity.y < 0)) cameraVelocity.y = 0; break; } @@ -180,7 +178,7 @@ int main(int argc, char* argv[]) { transform->roll(25.0f * time->getDeltaTime()); transform->yaw(50.0f * time->getDeltaTime()); transform->pitch(100.0f * time->getDeltaTime()); - + // Camera glm::vec3 cameraFront = camera->getTransform()->getForward(), @@ -190,10 +188,10 @@ int main(int argc, char* argv[]) { (cameraFront * cameraVelocity.z) + (cameraRight * cameraVelocity.x) + (cameraUp * cameraVelocity.y)); - + /* Render */ renderingEngine->registerCommand({transform, material, mesh, mesh->GetIndexCount()}); - renderingEngine->registerCommand({planeTransform,planeMaterial,plane,plane->GetIndexCount()}); + renderingEngine->registerCommand({planeTransform, planeMaterial, plane, plane->GetIndexCount()}); renderingEngine->render(); window->swapBuffers(); diff --git a/engine/managers/asset_manager.cpp b/engine/managers/asset_manager.cpp index 52db06ab..69f8bdeb 100644 --- a/engine/managers/asset_manager.cpp +++ b/engine/managers/asset_manager.cpp @@ -25,8 +25,7 @@ std::string ce::AssetManager::load_text_file(std::string path) { text = filestream.str(); LOG_SUCCESS("LOADED_FILE: " + path); } catch (std::fstream::failure e) { - LOG_ERROR( - "FILE_NOT_SUCCESSFULLY_READ: (" + path + ") " + std::string(e.what())); + LOG_ERROR("FILE_NOT_SUCCESSFULLY_READ: (" + path + ") " + std::string(e.what())); } return text; } @@ -65,4 +64,4 @@ ce::TextureFile ce::AssetManager::getTextureFile(std::string filename) { void ce::AssetManager::freeTextureFile(ce::TextureFile textureFile) { stbi_image_free(textureFile.data); -} +} \ No newline at end of file diff --git a/engine/managers/asset_manager.h b/engine/managers/asset_manager.h index 5dd47861..0638cfb2 100644 --- a/engine/managers/asset_manager.h +++ b/engine/managers/asset_manager.h @@ -7,8 +7,9 @@ namespace ce { class AssetManager { private: - inline static const std::string SHADER_FOLDER = "shaders", - TEXTURE_FOLDER = "textures"; + inline static const std::string + SHADER_FOLDER = "shaders", + TEXTURE_FOLDER = "textures"; static std::string load_text_file(std::string file); diff --git a/engine/math/transform.cpp b/engine/math/transform.cpp index ca6ee491..51fa07ef 100644 --- a/engine/math/transform.cpp +++ b/engine/math/transform.cpp @@ -31,15 +31,13 @@ void ce::Transform::sendToShader(ce::Shader* shader) { shader->setMat4("transform.model", getMatrix()); } -glm::vec3 ce::Transform::getForward() -{ +glm::vec3 ce::Transform::getForward() { float yaw = glm::radians(getYaw()), pitch = glm::radians(getPitch()); glm::vec3 dir( - cos(yaw)*cos(pitch), + cos(yaw) * cos(pitch), sin(pitch), - sin(yaw)*cos(pitch) - ); + sin(yaw) * cos(pitch)); return glm::normalize(dir); } diff --git a/engine/math/transform.h b/engine/math/transform.h index 5ff49605..6237039a 100644 --- a/engine/math/transform.h +++ b/engine/math/transform.h @@ -15,19 +15,15 @@ namespace ce { glm::vec3 m_pos, m_rot, m_scale; public: - static glm::vec3 GetGlobalUp(){return glm::vec3(0.0f,1.0f,0.0f);} - + static glm::vec3 GetGlobalUp() { return glm::vec3(0.0f, 1.0f, 0.0f); } + Transform(); ~Transform(); glm::vec3 getPosition() { return m_pos; } void setPosition(glm::vec3 pos) { m_pos = pos; } - void setPosition(float x, float y, float z) { - setPosition(glm::vec3(x, y, z)); - } + void setPosition(float x, float y, float z) { setPosition(glm::vec3(x, y, z)); } void translate(glm::vec3 delta) { m_pos += delta; } - void translate(float x, float y, float z) { - translate(glm::vec3(x, y, z)); - } + void translate(float x, float y, float z) { translate(glm::vec3(x, y, z)); } void setPitch(float pitch) { m_rot.x = pitch; } void pitch(float delta) { m_rot.x += delta; } @@ -43,9 +39,7 @@ namespace ce { glm::vec3 getRotation() { return m_rot; } void setRotation(glm::vec3 rot) { m_rot = rot; } - void setRotation(float x, float y, float z) { - setRotation(glm::vec3(x, y, z)); - } + void setRotation(float x, float y, float z) { setRotation(glm::vec3(x, y, z)); } void rotate(glm::vec3 delta) { m_rot += delta; } void rotate(float x, float y, float z) { rotate(glm::vec3(x, y, z)); } diff --git a/engine/rendering/camera.cpp b/engine/rendering/camera.cpp index 20c77678..88b89a27 100644 --- a/engine/rendering/camera.cpp +++ b/engine/rendering/camera.cpp @@ -1,44 +1,37 @@ #include "camera.h" ce::Camera::Camera() -:m_transform(NULL) -{ + : m_transform(NULL) { m_transform = new Transform(); } -ce::Camera::~Camera() -{ +ce::Camera::~Camera() { delete m_transform; } -glm::mat4 ce::Camera::getView() -{ +glm::mat4 ce::Camera::getView() { glm::vec3 pos = m_transform->getPosition(), forward = m_transform->getForward(); - return glm::lookAt(pos,pos+forward,ce::Transform::GetGlobalUp()); + return glm::lookAt(pos, pos + forward, ce::Transform::GetGlobalUp()); } -glm::vec3 ce::Camera::getRight() -{ +glm::vec3 ce::Camera::getRight() { glm::vec3 forward = m_transform->getForward(), up = ce::Transform::GetGlobalUp(); - return glm::normalize(glm::cross(forward,up)); + return glm::normalize(glm::cross(forward, up)); } -void ce::Camera::sendToShader(ce::Shader* shader) -{ - shader->setMat4("transform.view",getView()); +void ce::Camera::sendToShader(ce::Shader* shader) { + shader->setMat4("transform.view", getView()); } -void ce::Camera::boundPitch() -{ +void ce::Camera::boundPitch() { float cameraPitch = m_transform->getPitch(); if (cameraPitch > 89.9f) // TODO: fix broken 90 degree pitch cameraPitch = 89.9f; if (cameraPitch < -89.9f) cameraPitch = -89.9f; m_transform->setPitch(cameraPitch); -} - +} \ No newline at end of file diff --git a/engine/rendering/camera.h b/engine/rendering/camera.h index cb7b3fa3..23c6437c 100644 --- a/engine/rendering/camera.h +++ b/engine/rendering/camera.h @@ -5,18 +5,20 @@ namespace ce { class Camera { - private: + private: ce::Transform* m_transform; - float speed, - fov; - public: + float + speed, + fov; + + public: Camera(); ~Camera(); - + glm::mat4 getView(); glm::vec3 getRight(); void sendToShader(ce::Shader* shader); - Transform* getTransform(){return m_transform;} + Transform* getTransform() { return m_transform; } void boundPitch(); }; } diff --git a/engine/rendering/material.cpp b/engine/rendering/material.cpp index 7c69a3c2..33aed684 100644 --- a/engine/rendering/material.cpp +++ b/engine/rendering/material.cpp @@ -6,8 +6,7 @@ ce::Material::Material(Shader* shader) update(); } -ce::Material::~Material() -{ +ce::Material::~Material() { delete m_shader; delete m_texture; } diff --git a/engine/rendering/mesh.cpp b/engine/rendering/mesh.cpp index 0270ab77..ece7d05f 100644 --- a/engine/rendering/mesh.cpp +++ b/engine/rendering/mesh.cpp @@ -7,46 +7,51 @@ ce::Mesh::Mesh(Vertex* vertexArray, const unsigned vertexCount, GLuint* indexArray, const unsigned indexCount) : vertexCount(vertexCount), indexCount(indexCount), VAO(0), VBO(0), EBO(0) { glGenVertexArrays(1, &VAO); - bind(); - this->initVAO(vertexArray, indexArray); - unbind(); + glBindVertexArray(VAO); + initVAO(vertexArray, indexArray); + glBindVertexArray(0); } - ce::Mesh::~Mesh() { glDeleteVertexArrays(1, &VAO); - glDeleteBuffers(1, &this->VBO); - glDeleteBuffers(1, &this->EBO); + glDeleteBuffers(1, &VBO); + glDeleteBuffers(1, &EBO); } void ce::Mesh::bind() { glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); } - void ce::Mesh::unbind() { glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -void ce::Mesh::sendToShader(ce::Shader* shader) { - bind(); - shader->vertexAttribPointer("aPos", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), - (GLvoid*)offsetof(Vertex, position)); - shader->vertexAttribPointer("aColor", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), - (GLvoid*)offsetof(Vertex, color)); - shader->vertexAttribPointer("aTexCoord", 2, GL_FLOAT, GL_FALSE, - sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoord)); - unbind(); +void ce::Mesh::sendToShader(ce::Shader* shader, bool bind) { + if (bind) { + glBindVertexArray(VAO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + } + shader->vertexAttribPointer("aPos", 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, position)); + shader->vertexAttribPointer("aColor", 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, color)); + shader->vertexAttribPointer("aTexCoord", 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)offsetof(Vertex, texCoord)); + if (bind) { + glBindVertexArray(0); + glBindBuffer(GL_ARRAY_BUFFER, 0); + } } void ce::Mesh::initVAO(Vertex* vertexArray, GLuint* indexArray) { - glGenBuffers(1, &this->VBO); - glBindBuffer(GL_ARRAY_BUFFER, this->VBO); - glBufferData(GL_ARRAY_BUFFER, this->vertexCount * sizeof(Vertex), - vertexArray, GL_STATIC_DRAW); + glGenBuffers(1, &VBO); + glBindBuffer(GL_ARRAY_BUFFER, VBO); + glBufferData(GL_ARRAY_BUFFER, vertexCount * sizeof(Vertex), vertexArray, GL_STATIC_DRAW); + glBindBuffer(GL_ARRAY_BUFFER, 0); if (indexCount > 0) { - glGenBuffers(1, &this->EBO); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->EBO); - glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indexCount * sizeof(GLuint), - indexArray, GL_STATIC_DRAW); + glGenBuffers(1, &EBO); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexCount * sizeof(GLuint), indexArray, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } -} +} \ No newline at end of file diff --git a/engine/rendering/mesh.h b/engine/rendering/mesh.h index 9c928741..cbb42367 100644 --- a/engine/rendering/mesh.h +++ b/engine/rendering/mesh.h @@ -6,7 +6,6 @@ namespace ce { class Mesh { - private: unsigned vertexCount, indexCount; GLuint VAO, VBO, EBO; @@ -17,7 +16,7 @@ namespace ce { Mesh(Vertex* vertexArray, const unsigned vertexCount, GLuint* indexArray = NULL, const unsigned indexCount = 0); ~Mesh(); - void sendToShader(class Shader* shader); + void sendToShader(class Shader* shader, bool bind = true); unsigned GetIndexCount() { return indexCount; }; void bind(), unbind(); diff --git a/engine/rendering/rendering_engine.cpp b/engine/rendering/rendering_engine.cpp index fa8c2866..2a2a76cc 100644 --- a/engine/rendering/rendering_engine.cpp +++ b/engine/rendering/rendering_engine.cpp @@ -1,44 +1,40 @@ #include "rendering_engine.h" #include -void ce::RenderingEngine::clear() -{ +void ce::RenderingEngine::clear() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } -void ce::RenderingEngine::render(unsigned count) -{ - glDrawElements(GL_TRIANGLES,count,GL_UNSIGNED_INT,0); +void ce::RenderingEngine::render(unsigned count) { + glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_INT, 0); } -void ce::RenderingEngine::bind(RenderCommand command) -{ - // Update Shader Values +void ce::RenderingEngine::bind(RenderCommand command) { + // Update Shader Values TODO: shouldn't this be somewhere else instead of the bind command? command.material->update(); - + + // TODO: get rid of unneccecary binding command.mesh->sendToShader(command.material->getShader()); command.transform->sendToShader(command.material->getShader()); - command.material->getShader()->setMat4("transform.proj",getProjection()); + command.material->getShader()->setMat4("transform.proj", getProjection()); m_camera->sendToShader(command.material->getShader()); - + // Bind Things - command.material->bind(); command.mesh->bind(); + command.material->bind(); } -void ce::RenderingEngine::unbind(RenderCommand command) -{ +void ce::RenderingEngine::unbind(RenderCommand command) { command.mesh->unbind(); command.material->unbind(); } ce::RenderingEngine::RenderingEngine() -:m_aspectRatio(0), -m_fov(0), -m_near(0), -m_far(0), -m_camera(NULL) -{ + : m_aspectRatio(0), + m_fov(0), + m_near(0), + m_far(0), + m_camera(NULL) { /* * GLEW */ @@ -57,43 +53,32 @@ m_camera(NULL) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - + setClearColor(glm::vec4()); } -ce::RenderingEngine::~RenderingEngine() -{ - -} +ce::RenderingEngine::~RenderingEngine() {} -void ce::RenderingEngine::setClearColor(glm::vec4 color) -{ - glClearColor(color.r,color.g,color.b,color.a); +void ce::RenderingEngine::setClearColor(glm::vec4 color) { + glClearColor(color.r, color.g, color.b, color.a); } -void ce::RenderingEngine::setSize(glm::vec2 size) -{ +void ce::RenderingEngine::setSize(glm::vec2 size) { glViewport(0, 0, size.x, size.y); - m_aspectRatio = size.x/size.y; + m_aspectRatio = size.x / size.y; } -glm::mat4 ce::RenderingEngine::getProjection() -{ - return glm::perspective(m_fov,m_aspectRatio, m_near, m_far); +glm::mat4 ce::RenderingEngine::getProjection() { + return glm::perspective(m_fov, m_aspectRatio, m_near, m_far); } - - -void ce::RenderingEngine::render() -{ - +void ce::RenderingEngine::render() { clear(); - for(int i=0;i #include #include #include -#include "camera.h" namespace ce { struct RenderCommand { @@ -19,7 +18,7 @@ namespace ce { class RenderingEngine { private: float m_aspectRatio, m_fov, m_near, m_far; - + Camera* m_camera; std::vector m_commands = {}; @@ -40,7 +39,7 @@ namespace ce { } void setSize(glm::vec2 size); void setClearColor(glm::vec4 color); - void setCamera(Camera* camera){m_camera = camera;} + void setCamera(Camera* camera) { m_camera = camera; } glm::mat4 getProjection(); @@ -49,6 +48,4 @@ namespace ce { } void render(); }; -} - -#endif // _RENDER_ENGINE_H_ +} \ No newline at end of file diff --git a/engine/rendering/shader.cpp b/engine/rendering/shader.cpp index 025c49b3..cae405a8 100644 --- a/engine/rendering/shader.cpp +++ b/engine/rendering/shader.cpp @@ -85,7 +85,8 @@ int ce::Shader::registerUniform(std::string name) { return location; } -ce::Shader::Shader(const char* name) : program(glCreateProgram()) { +ce::Shader::Shader(const char* name) + : program(glCreateProgram()) { ShaderFile shaderFile = ce::AssetManager::getShaderFile(name); int vertexShader = 0; diff --git a/engine/rendering/shader.h b/engine/rendering/shader.h index 9d4080ba..6ef522e1 100644 --- a/engine/rendering/shader.h +++ b/engine/rendering/shader.h @@ -11,7 +11,6 @@ using namespace glm; namespace ce { - class Shader { private: inline static int MIN_LOC = 0; diff --git a/engine/rendering/texture.cpp b/engine/rendering/texture.cpp index 6f0d13e2..e34903e0 100644 --- a/engine/rendering/texture.cpp +++ b/engine/rendering/texture.cpp @@ -47,4 +47,4 @@ void ce::Texture::unbind() { glDisable(type); glActiveTexture(0); glBindTexture(this->type, 0); -} +} \ No newline at end of file diff --git a/engine/rendering/texture.h b/engine/rendering/texture.h index 60e965d0..6daa61a1 100644 --- a/engine/rendering/texture.h +++ b/engine/rendering/texture.h @@ -5,7 +5,6 @@ #include namespace ce { - class Texture { private: GLuint texture;