From e5263bb3a1aadebb445de133e72aba37921af4a4 Mon Sep 17 00:00:00 2001 From: SArpnt <36542932+SArpnt@users.noreply.github.com> Date: Tue, 16 Feb 2021 12:54:43 -0500 Subject: [PATCH] code cleanup --- .clang-format | 30 ++++ CMakeLists.txt | 2 +- dep/CMakeLists.txt | 22 ++- engine/CMakeLists.txt | 20 +-- engine/ce_event_handler.h | 14 +- engine/ce_math.h | 7 +- engine/ce_render_fundementals.h | 6 +- engine/ce_rendering.h | 7 +- engine/core/time.cpp | 19 +-- engine/core/time.h | 33 ++--- engine/core/tpnt_log.h | 35 ++--- engine/core/window.cpp | 53 +++---- engine/core/window.h | 28 ++-- engine/main.cpp | 222 ++++++++++++++---------------- engine/managers/asset_manager.cpp | 45 +++--- engine/managers/asset_manager.h | 17 +-- engine/math/transform.cpp | 44 +++--- engine/math/transform.h | 95 ++++++------- engine/rendering/mesh.cpp | 45 +++--- engine/rendering/mesh.h | 29 ++-- engine/rendering/shader.cpp | 194 +++++++++++++------------- engine/rendering/shader.h | 43 +++--- engine/rendering/texture.cpp | 33 ++--- engine/rendering/texture.h | 21 +-- engine/rendering/vertex.h | 7 +- res/shaders/basic.frag | 7 +- res/shaders/basic.vert | 3 +- 27 files changed, 504 insertions(+), 577 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 00000000..bbdb0f8b --- /dev/null +++ b/.clang-format @@ -0,0 +1,30 @@ +BasedOnStyle: LLVM + +IndentWidth: 3 +TabWidth: 3 +AccessModifierOffset: -2 +ConstructorInitializerIndentWidth: 3 +ContinuationIndentWidth: 3 +ObjCBlockIndentWidth: 3 +UseTab: ForContinuationAndIndentation +IndentCaseLabels: true +IndentPPDirectives: BeforeHash +NamespaceIndentation: All + +DerivePointerAlignment: false +PointerAlignment: Left + +BreakBeforeBraces: Attach +BreakAfterJavaFieldAnnotations: true +BreakBeforeBinaryOperators: None + +AllowShortBlocksOnASingleLine: Never +AllowShortCaseLabelsOnASingleLine: false +#AllowShortEnumsOnASingleLine: true # this breaks things? +AllowShortFunctionsOnASingleLine: Inline +AllowShortIfStatementsOnASingleLine: Never +AllowShortLambdasOnASingleLine: Empty +AllowShortLoopsOnASingleLine: false + +AlignAfterOpenBracket: DontAlign +FixNamespaceComments: false \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index cd8f94a7..f4518b3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,4 +14,4 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/run) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/dep/") add_subdirectory(dep) -add_subdirectory(engine) +add_subdirectory(engine) \ No newline at end of file diff --git a/dep/CMakeLists.txt b/dep/CMakeLists.txt index 3770efde..a96b6cc2 100644 --- a/dep/CMakeLists.txt +++ b/dep/CMakeLists.txt @@ -9,14 +9,12 @@ message(NOTICE "OPENGL_LIBRARY " ${OPENGL_LIBRARY}) message(NOTICE "OPENGL_LIBRARIES " ${OPENGL_LIBRARIES}) message(NOTICE "OPENGL_INCLUDE_DIR " ${OPENGL_INCLUDE_DIR}) - # glew find_package(GLEW REQUIRED) include_directories(${GLEW_INCLUDE_DIRS}) message(NOTICE "GLEW_LIBRARIES " ${GLEW_LIBRARIES}) message(NOTICE "GLEW_INCLUDE_DIRS " ${GLEW_INCLUDE_DIRS}) - # SDL find_package(SDL2 REQUIRED) include_directories(${SDL2_INCLUDE_DIRS}) @@ -33,18 +31,18 @@ message(NOTICE "GLM_INCLUDE_DIRS " ${GLM_INCLUDE_DIRS}) # Libs set(CE_LIBS -${OPENGL_LIBRARY} -${GLEW_LIBRARIES} -${SDL2_LIBRARIES} + ${OPENGL_LIBRARY} + ${GLEW_LIBRARIES} + ${SDL2_LIBRARIES} -PARENT_SCOPE + PARENT_SCOPE ) #Include set(CE_INCLUDES -${OPENGL_INCLUDE_DIR} -${GLEW_INCLUDE_DIRS} -${SDL2_INCLUDE_DIRS} -${GLM_INCLUDE_DIRS} -PARENT_SCOPE -) + ${OPENGL_INCLUDE_DIR} + ${GLEW_INCLUDE_DIRS} + ${SDL2_INCLUDE_DIRS} + ${GLM_INCLUDE_DIRS} + PARENT_SCOPE +) \ No newline at end of file diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index b9052313..89ce7fda 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -1,22 +1,23 @@ set(NAME engine) file(GLOB_RECURSE CE_SRC -"*.hpp" -"*.cpp" -"*.c" + "*.hpp" + "*.cpp" + "*.c" ) add_executable(${NAME} -#WIN32 -${CE_SRC}) + #WIN32 + ${CE_SRC} +) target_link_libraries(${NAME} -${CE_LIBS} + ${CE_LIBS} ) include_directories(${NAME} -${CMAKE_CURRENT_SOURCE_DIR} -${CE_INCLUDES} + ${CMAKE_CURRENT_SOURCE_DIR} + ${CE_INCLUDES} ) @@ -29,9 +30,8 @@ set_target_properties( ${NAME} PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "$" ) - add_test( NAME run COMMAND ${NAME} WORKING_DIRECTORY "$" -) +) \ No newline at end of file diff --git a/engine/ce_event_handler.h b/engine/ce_event_handler.h index 8aa09fc7..243338e5 100644 --- a/engine/ce_event_handler.h +++ b/engine/ce_event_handler.h @@ -1,15 +1,11 @@ - -#ifndef _CE_EVENT_HANDLER_H_ -#define _CE_EVENT_HANDLER_H_ +#pragma once #include namespace ce { class IEventHandler { - private: - public: - virtual void onEvent(SDL_Event& event)=0; + private: + public: + virtual void onEvent(SDL_Event& event) = 0; }; -} - -#endif // _CE_EVENT_HANDLER_H_ +} \ No newline at end of file diff --git a/engine/ce_math.h b/engine/ce_math.h index 29560d07..7feafec2 100644 --- a/engine/ce_math.h +++ b/engine/ce_math.h @@ -1,8 +1,5 @@ -#ifndef _CE_MATH_H_ -#define _CE_MATH_H_ +#pragma once #include #include -#include - -#endif // _CE_MATH_H_ +#include \ No newline at end of file diff --git a/engine/ce_render_fundementals.h b/engine/ce_render_fundementals.h index c08bc652..9ec6d51a 100644 --- a/engine/ce_render_fundementals.h +++ b/engine/ce_render_fundementals.h @@ -1,11 +1,11 @@ -//#include +// #include +#include "rendering/mesh.h" #include "rendering/shader.h" #include "rendering/texture.h" #include "rendering/vertex.h" -#include "rendering/mesh.h" /* #define PTR std::shared_ptr #define PTR_CAST = dynamic_pointer_cast #define OBJMAP(x) std::map> -*/ +*/ \ No newline at end of file diff --git a/engine/ce_rendering.h b/engine/ce_rendering.h index 76e545c6..180ae5ce 100644 --- a/engine/ce_rendering.h +++ b/engine/ce_rendering.h @@ -1,7 +1,4 @@ -#ifndef _CE_GL_H_ -#define _CE_RENDERING_H_ +#pragma once #include -#include - -#endif // !_CE_GL_H_ +#include \ No newline at end of file diff --git a/engine/core/time.cpp b/engine/core/time.cpp index 18cb9f90..a2a8e747 100644 --- a/engine/core/time.cpp +++ b/engine/core/time.cpp @@ -2,23 +2,14 @@ #include -float ce::Time::getTime() -{ - return SDL_GetTicks()/1000.0f; +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() -{ -} +ce::Time::~Time() {} -void ce::Time::update() -{ +void ce::Time::update() { m_now = getTime(); m_dt = m_now - m_last; m_fps = 1 / m_dt; diff --git a/engine/core/time.h b/engine/core/time.h index 1f697a8e..b587fbb2 100644 --- a/engine/core/time.h +++ b/engine/core/time.h @@ -1,26 +1,19 @@ -#ifndef _CE_TIME_H_ -#define _CE_TIME_H_ +#pragma once namespace ce { -class Time -{ -private: - float - m_now, - m_last, - m_dt, - m_fps; + class Time { + private: + float m_now, m_last, m_dt, m_fps; - float getTime(); -public: - Time(); - ~Time(); + float getTime(); - void update(); + public: + Time(); + ~Time(); - float getDeltaTime() {return m_dt;} - float getFPS() {return m_fps;}; -}; -} + void update(); -#endif // !_CE_TIME_H_ + float getDeltaTime() { return m_dt; } + float getFPS() { return m_fps; }; + }; +} \ No newline at end of file diff --git a/engine/core/tpnt_log.h b/engine/core/tpnt_log.h index c2db00ba..1202fdd5 100644 --- a/engine/core/tpnt_log.h +++ b/engine/core/tpnt_log.h @@ -2,38 +2,39 @@ * Tumpnt Logger * Author: Cameron Trow * Source: https://github.com/Tumpnt/TumpntAudio/blob/master/src/core/ta_log.h - * + * * Example Usage - * + * * int main() { * LOG_INFO("Hello World"); * return 0; * } - * + * * Output: * [INFO][main.cpp:3|main]: Hello World - * + * */ -#ifndef _TPNT_LOG_H_ -#define _TPNT_LOG_H_ +#pragma once -#include #include +#include #include -#define TPNT_LOGGER_STR(X) (std::string("")+X).c_str() +#define TPNT_LOGGER_STR(X) (std::string("") + X).c_str() #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_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_RESET "\033[0m" #define TPNT_LOG_SUCCESS "\033[1;32m" #define TPNT_LOG_WARN "\033[1;33m" #define TPNT_LOG_ERROR "\033[1;31m" -#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) - -#endif // _TPNT_LOG_H_ +#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) \ No newline at end of file diff --git a/engine/core/window.cpp b/engine/core/window.cpp index 81eb36c0..57409d26 100644 --- a/engine/core/window.cpp +++ b/engine/core/window.cpp @@ -3,11 +3,8 @@ #include "tpnt_log.h" ce::Window::Window(const char* title) -:m_window(NULL), -m_context(NULL), -m_mouse(true) -{ - + : m_window(NULL), m_context(NULL), m_mouse(true) { + /* * Init SDL */ @@ -19,55 +16,45 @@ m_mouse(true) /* * Window Creation */ - m_window = SDL_CreateWindow( - "Cinnabar", - SDL_WINDOWPOS_UNDEFINED, - SDL_WINDOWPOS_UNDEFINED, - 1280, - 720, - SDL_WINDOW_OPENGL| SDL_WINDOW_RESIZABLE - ); - if(m_window==NULL) - { + m_window = SDL_CreateWindow("Cinnabar", SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, 1280, 720, + SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE); + if (m_window == NULL) { LOG_ERROR("Failed to create GLFW window"); SDL_Quit(); exit(-1); } - //SDL_MaximizeWindow(window); + // SDL_MaximizeWindow(window); /*int w,h; SDL_GetWindowSize(window,&w, &h); WINDOW = glm::vec2(w,h);*/ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - + SDL_GL_SetAttribute( + SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + setMouseVisibility(true); m_context = SDL_GL_CreateContext(m_window); } -ce::Window::~Window() -{ +ce::Window::~Window() { SDL_DestroyWindow(m_window); SDL_Quit(); } -void ce::Window::swapBuffers() -{ +void ce::Window::swapBuffers() { SDL_GL_SwapWindow(m_window); } -glm::vec2 ce::Window::getWindowSize() -{ - int w,h; - SDL_GetWindowSize(m_window,&w, &h); - return glm::vec2((float)w,(float)h); +glm::vec2 ce::Window::getWindowSize() { + int w, h; + SDL_GetWindowSize(m_window, &w, &h); + return glm::vec2((float)w, (float)h); } -float ce::Window::getAspectRatio() -{ +float ce::Window::getAspectRatio() { glm::vec2 size = getWindowSize(); - return size.x/size.y; + return size.x / size.y; } -void ce::Window::setMouseVisibility(bool enabled) -{ +void ce::Window::setMouseVisibility(bool enabled) { m_mouse = enabled; - SDL_SetRelativeMouseMode(enabled?SDL_FALSE:SDL_TRUE); + SDL_SetRelativeMouseMode(enabled ? SDL_FALSE : SDL_TRUE); } diff --git a/engine/core/window.h b/engine/core/window.h index 90f4b8c4..1d1af273 100644 --- a/engine/core/window.h +++ b/engine/core/window.h @@ -1,30 +1,26 @@ -#ifndef _WINDOW_H_ -#define _WINDOW_H_ +#pragma once +#include #include #include -#include namespace ce { class Window { - private: + private: SDL_Window* m_window; SDL_GLContext m_context; - + bool m_mouse; - public: + + public: Window(const char* title); ~Window(); - - void - swapBuffers(), - setMouseVisibility(bool enabled); - - SDL_GLContext getContext() { return m_context;}; - bool mouseVisible(){return m_mouse;}; + + void swapBuffers(), setMouseVisibility(bool enabled); + + SDL_GLContext getContext() { return m_context; }; + bool mouseVisible() { return m_mouse; }; glm::vec2 getWindowSize(); float getAspectRatio(); }; -} - -#endif // _WINDOW_H_ +} \ No newline at end of file diff --git a/engine/main.cpp b/engine/main.cpp index cfbf10ce..386018a6 100644 --- a/engine/main.cpp +++ b/engine/main.cpp @@ -1,83 +1,86 @@ -//Debugging -#include +// Debugging #include +#include -//Core -#include "core/window.h" +// Core #include "core/time.h" +#include "core/window.h" -//Assets -#include "stb_image.h" +// Assets #include "managers/asset_manager.h" +#include "stb_image.h" -//Maths -#include +// Maths #include "math/transform.h" +#include -//Rendering +// Rendering #include "ce_render_fundementals.h" /* -* Vertices -*/ + * Vertices + */ +// clang-format off ce::Vertex vertices[] = { - glm::vec3(0.5f, 0.5f, -0.5f), glm::vec4(0.0f,0.0f,0.0f,0.0f), glm::vec2(1.0f, 1.0f),//0 - glm::vec3(0.5f, -0.5f, -0.5f),glm::vec4(0.0f,0.0f,1.0f,1.0f), glm::vec2(1.0f, 0.0f),//1 - glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec4(0.0f,1.0f,1.0f,1.0f), glm::vec2(0.0f, 0.0f),//2 - glm::vec3(-0.5f, 0.5f, -0.5), glm::vec4(0.0f,1.0f,0.0f,1.0f), glm::vec2(0.0f, 1.0f),//3 + glm::vec3( 0.5f, 0.5f, -0.5f), glm::vec4(0.0f, 0.0f, 0.0f, 0.0f), glm::vec2(1.0f, 1.0f),// 0 + glm::vec3( 0.5f, -0.5f, -0.5f), glm::vec4(0.0f, 0.0f, 1.0f, 1.0f), glm::vec2(1.0f, 0.0f),// 1 + glm::vec3(-0.5f, -0.5f, -0.5f), glm::vec4(0.0f, 1.0f, 1.0f, 1.0f), glm::vec2(0.0f, 0.0f),// 2 + glm::vec3(-0.5f, 0.5f, -0.5f), glm::vec4(0.0f, 1.0f, 0.0f, 1.0f), glm::vec2(0.0f, 1.0f),// 3 - glm::vec3(0.5f, 0.5f, 0.5f), glm::vec4(1.0f,0.0f,0.0f,0.0f), glm::vec2(1.0f, 1.0f),//4 - glm::vec3(0.5f, -0.5f, 0.5f),glm::vec4(1.0f,1.0f,0.0f,1.0f), glm::vec2(1.0f, 0.0f),//5 - glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec4(1.0f,0.0f,1.0f,1.0f), glm::vec2(0.0f, 0.0f),//6 - glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec4(1.0f,1.0f,1.0f,1.0f), glm::vec2(0.0f, 1.0f)//7 + glm::vec3( 0.5f, 0.5f, 0.5f), glm::vec4(1.0f, 0.0f, 0.0f, 0.0f), glm::vec2(1.0f, 1.0f),// 4 + glm::vec3( 0.5f, -0.5f, 0.5f), glm::vec4(1.0f, 1.0f, 0.0f, 1.0f), glm::vec2(1.0f, 0.0f),// 5 + glm::vec3(-0.5f, -0.5f, 0.5f), glm::vec4(1.0f, 0.0f, 1.0f, 1.0f), glm::vec2(0.0f, 0.0f),// 6 + glm::vec3(-0.5f, 0.5f, 0.5f), glm::vec4(1.0f, 1.0f, 1.0f, 1.0f), glm::vec2(0.0f, 1.0f),// 7 }; +// clang-format on /* 7-4 6-5 3-0 2-1 */ -unsigned vertexCount = sizeof(vertices)/sizeof(ce::Vertex); -//7<=>5 +unsigned vertexCount = sizeof(vertices) / sizeof(ce::Vertex); +// 7<=>5 +// clang-format off GLuint indices [] = { - //F + // F 0, 1, 3, 1, 2, 3, - //U - 4,0,7, - 0,3,7, - //L - 3,2,7, - 2,6,7, - //R - 4,5,0, - 5,1,0, - //D - 1,5,2, - 5,6,2, - //B - 7,6,4, - 6,5,4 + // U + 4, 0, 7, + 0, 3, 7, + // L + 3, 2, 7, + 2, 6, 7, + // R + 4, 5, 0, + 5, 1, 0, + // D + 1, 5, 2, + 5, 6, 2, + // B + 7, 6, 4, + 6, 5, 4, }; -unsigned indexCount = sizeof(indices)/sizeof(GLuint); +// clang-format on +unsigned indexCount = sizeof(indices) / sizeof(GLuint); int main(int argc, char* argv[]) { LOG_INFO("Hello World"); - + ce::Window* window = new ce::Window("Cinnabar"); /* * GLEW */ GLenum err = glewInit(); - if (GLEW_OK != err) - { + if (GLEW_OK != err) { /* Problem: glewInit failed, something is seriously wrong. */ LOG_ERROR((const char*)glewGetErrorString(err)); } - LOG_INFO("Status: Using GLEW "+ (const char*)glewGetString(GLEW_VERSION)); - - //OpenGL Setup + LOG_INFO("Status: Using GLEW " + (const char*)glewGetString(GLEW_VERSION)); + + // OpenGL Setup glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); @@ -85,124 +88,108 @@ int main(int argc, char* argv[]) { glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - - //Time + + // Time ce::Time* time = new ce::Time(); - + ce::Transform transform; ce::Shader* shader = new ce::Shader("basic"); - ce::Mesh* mesh = new ce::Mesh( - vertices,vertexCount, - indices,indexCount - ); + ce::Mesh* mesh = new ce::Mesh(vertices, vertexCount, indices, indexCount); mesh->sendToShader(shader); ce::Texture* texture = new ce::Texture("uv-map.png"); - shader->setInt("uTex",0); - float - mouseSensitivity = 0.1f, - cameraPitch = 0.0f, - cameraYaw = -90.0f; - - glm::vec3 - cameraPos(0.0f, 0.0f, 3.0f), - cameraFront(0.0f,0.0f,-1.0f), - cameraUp(0.0f,1.0f,0.0f), + shader->setInt("uTex", 0); + float mouseSensitivity = 0.1f, cameraPitch = 0.0f, cameraYaw = -90.0f; + + glm::vec3 cameraPos(0.0f, 0.0f, 3.0f), cameraFront(0.0f, 0.0f, -1.0f), + cameraUp(0.0f, 1.0f, 0.0f), cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp)); - - + /* * Game Loop */ SDL_Event event; int running = 1; - while (running) - { - //Time + while (running) { time->update(); - - //Events + while (SDL_PollEvent(&event)) { - switch(event.type) { - case SDL_MOUSEMOTION: - { - if(window->mouseVisible())break; - glm::vec2 mouseDelta(event.motion.xrel,event.motion.yrel); + switch (event.type) { + case SDL_MOUSEMOTION: { + if (window->mouseVisible()) + break; + glm::vec2 mouseDelta(event.motion.xrel, event.motion.yrel); mouseDelta *= mouseSensitivity; cameraYaw += mouseDelta.x; cameraPitch -= mouseDelta.y; - if(cameraPitch>89.0f) - cameraPitch = 89.0f; - if(cameraPitch<-89.0f) - cameraPitch = -89.0f; - + if (cameraPitch > 89.9f) // TODO: fix broken 90 degree pitch + cameraPitch = 89.9f; + if (cameraPitch < -89.9f) + cameraPitch = -89.9f; + break; } case SDL_MOUSEBUTTONDOWN: - if(window->mouseVisible()){ + if (window->mouseVisible()) window->setMouseVisibility(false); - } break; - case SDL_KEYDOWN: - { - float cameraSpeed = 2.5f*time->getDeltaTime(); // adjust accordingly + case SDL_KEYDOWN: { + float cameraSpeed = + 2.5f * time->getDeltaTime(); // adjust accordingly if (event.key.keysym.sym == SDLK_w) cameraPos += cameraFront * cameraSpeed; - if (event.key.keysym.sym == SDLK_s) + if (event.key.keysym.sym == SDLK_s) cameraPos -= cameraFront * cameraSpeed; if (event.key.keysym.sym == SDLK_a) cameraPos -= cameraRight * cameraSpeed; if (event.key.keysym.sym == SDLK_d) cameraPos += cameraRight * cameraSpeed; - if(event.key.keysym.sym == SDLK_ESCAPE){ + if (event.key.keysym.sym == SDLK_ESCAPE) window->setMouseVisibility(true); - } break; } - case SDL_QUIT: - { + case SDL_QUIT: { running = 0; break; } - case SDL_WINDOWEVENT: - { + case SDL_WINDOWEVENT: { glm::vec2 size = window->getWindowSize(); - glViewport(0, 0, size.x,size.y); + glViewport(0, 0, size.x, size.y); break; } } } - - glm::mat4 proj = glm::perspective(glm::radians(45.0f), window->getAspectRatio(), 0.1f, 100.0f); - shader->setMat4("transform.proj",proj); - + + glm::mat4 proj = glm::perspective( + glm::radians(45.0f), window->getAspectRatio(), 0.1f, 100.0f); + shader->setMat4("transform.proj", proj); + // Transform - transform.roll(25.0f*time->getDeltaTime()); - transform.yaw(50.0f*time->getDeltaTime()); - transform.pitch(100.0f*time->getDeltaTime()); + transform.roll(25.0f * time->getDeltaTime()); + transform.yaw(50.0f * time->getDeltaTime()); + transform.pitch(100.0f * time->getDeltaTime()); transform.saveToShader(shader); - + // Camera - glm::vec3 - cameraDirection( - cos(glm::radians(cameraYaw))*cos(glm::radians(cameraPitch)), - sin(glm::radians(cameraPitch)), - sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch)) - ); + glm::vec3 cameraDirection( + cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch)), + sin(glm::radians(cameraPitch)), + sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch))); cameraFront = glm::normalize(cameraDirection); cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp)); - glm::mat4 view = glm::lookAt(cameraPos,cameraPos + cameraFront,cameraUp); - shader->setMat4("transform.view",view); - - /* Render */ - glClearColor(0.0f,0.0f,0.0f,1.0f); - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - + glm::mat4 view = + glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); + shader->setMat4("transform.view", view); + + // Render + glClearColor(0.0f, 0.0f, 0.0f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + texture->activate(0); shader->bind(); mesh->bind(); - - glDrawElements(GL_TRIANGLES,mesh->GetIndexCount(),GL_UNSIGNED_INT,0); - + + glDrawElements(GL_TRIANGLES, mesh->GetIndexCount(), GL_UNSIGNED_INT, 0); + mesh->unbind(); shader->unbind(); texture->unbind(); @@ -212,8 +199,7 @@ int main(int argc, char* argv[]) { delete mesh; delete shader; delete texture; - + delete window; return 0; -} - +} \ No newline at end of file diff --git a/engine/managers/asset_manager.cpp b/engine/managers/asset_manager.cpp index 304e8d21..52db06ab 100644 --- a/engine/managers/asset_manager.cpp +++ b/engine/managers/asset_manager.cpp @@ -2,20 +2,18 @@ #include #include -//TEXT FILES +// TEXT FILES #include #include -//TEXTURES +// TEXTURES #include "stb_image.h" -std::string ce::AssetManager::load_text_file(std::string path) -{ +std::string ce::AssetManager::load_text_file(std::string path) { std::fstream file; std::string text = ""; file.exceptions(std::fstream::failbit | std::fstream::badbit); - try - { + try { file.open(path); if (!file.good()) { LOG_WARN("FILE_DOES_NOT_EXIST: " + path); @@ -26,42 +24,45 @@ std::string ce::AssetManager::load_text_file(std::string path) file.close(); text = filestream.str(); LOG_SUCCESS("LOADED_FILE: " + path); - } - catch (std::fstream::failure e) - { - LOG_ERROR("FILE_NOT_SUCCESSFULLY_READ: ("+ path +") " + std::string(e.what())); + } catch (std::fstream::failure e) { + LOG_ERROR( + "FILE_NOT_SUCCESSFULLY_READ: (" + path + ") " + std::string(e.what())); } return text; } -ce::ShaderFile ce::AssetManager::getShaderFile(std::string filename) -{ +ce::ShaderFile ce::AssetManager::getShaderFile(std::string filename) { std::string path = SHADER_FOLDER + "/" + filename; ShaderFile shaderFile; shaderFile.name = filename; + shaderFile.vertex = load_text_file(path + ".vert"); - if (shaderFile.vertex == "") shaderFile.vertex = load_text_file(path + ".vs"); + if (shaderFile.vertex == "") + shaderFile.vertex = load_text_file(path + ".vs"); + shaderFile.fragment = load_text_file(path + ".frag"); - if (shaderFile.fragment == "") shaderFile.fragment = load_text_file(path + ".fs"); + if (shaderFile.fragment == "") + shaderFile.fragment = load_text_file(path + ".fs"); + shaderFile.geometry = load_text_file(path + ".geom"); - if (shaderFile.geometry == "") shaderFile.geometry = load_text_file(path + ".gs"); + if (shaderFile.geometry == "") + shaderFile.geometry = load_text_file(path + ".gs"); + return shaderFile; } - -ce::TextureFile ce::AssetManager::getTextureFile(std::string filename) -{ +ce::TextureFile ce::AssetManager::getTextureFile(std::string filename) { std::string path = TEXTURE_FOLDER + "/" + filename; - //stbi_set_flip_vertically_on_load(1); + // stbi_set_flip_vertically_on_load(1); LOG_SUCCESS("LOADED_TEXTURE " + path); TextureFile textureFile; textureFile.name = filename; - textureFile.data = stbi_load(path.c_str(), &textureFile.width, &textureFile.height, &textureFile.channelCount, 0); + textureFile.data = stbi_load(path.c_str(), &textureFile.width, + &textureFile.height, &textureFile.channelCount, 0); return textureFile; } -void ce::AssetManager::freeTextureFile(ce::TextureFile textureFile) -{ +void ce::AssetManager::freeTextureFile(ce::TextureFile textureFile) { stbi_image_free(textureFile.data); } diff --git a/engine/managers/asset_manager.h b/engine/managers/asset_manager.h index 1adcaf1e..5dd47861 100644 --- a/engine/managers/asset_manager.h +++ b/engine/managers/asset_manager.h @@ -1,5 +1,4 @@ -#ifndef _ASSET_MANAGER_H_ -#define _ASSET_MANAGER_H_ +#pragma once #include @@ -7,17 +6,15 @@ namespace ce { class AssetManager { - private: - inline static const std::string - SHADER_FOLDER = "shaders", - TEXTURE_FOLDER = "textures"; + private: + inline static const std::string SHADER_FOLDER = "shaders", + TEXTURE_FOLDER = "textures"; static std::string load_text_file(std::string file); - public: + + public: static ShaderFile getShaderFile(std::string filename); static ce::TextureFile getTextureFile(std::string filename); static void freeTextureFile(ce::TextureFile textureFile); }; -} - -#endif // !_ASSET_MANAGER_H_ +} \ No newline at end of file diff --git a/engine/math/transform.cpp b/engine/math/transform.cpp index a3462d1d..9b90563c 100644 --- a/engine/math/transform.cpp +++ b/engine/math/transform.cpp @@ -1,36 +1,32 @@ #include "transform.h" ce::Transform::Transform() -:m_pos(0.0f,0.0f,0.0f), -m_rot(0.0f,0.0f,0.0f), -m_scale(1.0f,1.0f,1.0f) -{ -} + : m_pos(0.0f, 0.0f, 0.0f), m_rot(0.0f, 0.0f, 0.0f), + m_scale(1.0f, 1.0f, 1.0f) {} -ce::Transform::~Transform() -{ -} +ce::Transform::~Transform() {} -glm::mat4 ce::Transform::getMatrix() -{ +glm::mat4 ce::Transform::getMatrix() { glm::mat4 transform(1.0f); - + // Scale - transform = glm::scale(transform,m_scale); - + transform = glm::scale(transform, m_scale); + // Rotation - // TODO: Quaterniums - transform = glm::rotate(transform,glm::radians(getPitch()),glm::vec3(1.0f,0.0f,0.0f));//Pitch - transform = glm::rotate(transform,glm::radians(getYaw()),glm::vec3(0.0f,1.0f,0.0f));//Yaw - transform = glm::rotate(transform,glm::radians(getRoll()),glm::vec3(0.0f,0.0f,1.0f));//Roll - - //Translation - transform = glm::translate(transform,m_pos); - + // TODO: Quaternions + transform = glm::rotate(transform, glm::radians(getPitch()), + glm::vec3(1.0f, 0.0f, 0.0f)); // Pitch + transform = glm::rotate( + transform, glm::radians(getYaw()), glm::vec3(0.0f, 1.0f, 0.0f)); // Yaw + transform = glm::rotate( + transform, glm::radians(getRoll()), glm::vec3(0.0f, 0.0f, 1.0f)); // Roll + + // Translation + transform = glm::translate(transform, m_pos); + return transform; } -void ce::Transform::saveToShader(ce::Shader* shader) -{ - shader->setMat4("transform.model",getMatrix()); +void ce::Transform::saveToShader(ce::Shader* shader) { + shader->setMat4("transform.model", getMatrix()); } diff --git a/engine/math/transform.h b/engine/math/transform.h index a2ca7c1b..94b51f62 100644 --- a/engine/math/transform.h +++ b/engine/math/transform.h @@ -1,62 +1,63 @@ -#ifndef _CE_TRANSFORM_H_ -#define _CE_TRANSFORM_H_ +#pragma once #include #include namespace ce { class Transform { - private: + private: /* * Pitch:X * Yaw:Y * Roll: Z - * - */ - glm::vec3 - m_pos, - m_rot, - m_scale; - public: + * + */ + glm::vec3 m_pos, m_rot, m_scale; + + public: 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 translate(glm::vec3 delta){m_pos+=delta;} - 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;} - float getPitch(){return m_rot.x;} - - void setYaw(float yaw){m_rot.y = yaw;} - void yaw(float delta){m_rot.y += delta;} - float getYaw(){return m_rot.y;} - - void setRoll(float roll) {m_rot.z = roll;} - void roll(float delta) {m_rot.z += delta;} - float getRoll(){return m_rot.z;} - - 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 rotate(glm::vec3 delta) {m_rot+=delta;} - void rotate(float x, float y, float z) {rotate(glm::vec3(x,y,z));} - - glm::vec3 getScale() {return m_scale;} - void setScale(glm::vec3 scale){m_scale=scale;} - void setScale(float x,float y,float z){ setScale(glm::vec3(x,y,z));} - void setScale(float a){setScale(a,a,a);} - void scale(glm::vec3 delta) {m_scale*=delta;} - void scale(float x, float y, float z) {scale(glm::vec3(x,y,z));} - void scale(float a){scale(a,a,a);} - + + 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 translate(glm::vec3 delta) { m_pos += delta; } + 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; } + float getPitch() { return m_rot.x; } + + void setYaw(float yaw) { m_rot.y = yaw; } + void yaw(float delta) { m_rot.y += delta; } + float getYaw() { return m_rot.y; } + + void setRoll(float roll) { m_rot.z = roll; } + void roll(float delta) { m_rot.z += delta; } + float getRoll() { return m_rot.z; } + + 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 rotate(glm::vec3 delta) { m_rot += delta; } + void rotate(float x, float y, float z) { rotate(glm::vec3(x, y, z)); } + + glm::vec3 getScale() { return m_scale; } + void setScale(glm::vec3 scale) { m_scale = scale; } + void setScale(float x, float y, float z) { setScale(glm::vec3(x, y, z)); } + void setScale(float a) { setScale(a, a, a); } + void scale(glm::vec3 delta) { m_scale *= delta; } + void scale(float x, float y, float z) { scale(glm::vec3(x, y, z)); } + void scale(float a) { scale(a, a, a); } + glm::mat4 getMatrix(); - + void saveToShader(ce::Shader* shader); }; -} - -#endif // _CE_TRANSFORM_H_ +} \ No newline at end of file diff --git a/engine/rendering/mesh.cpp b/engine/rendering/mesh.cpp index 90325dc0..0270ab77 100644 --- a/engine/rendering/mesh.cpp +++ b/engine/rendering/mesh.cpp @@ -1,59 +1,52 @@ #include "mesh.h" -#include "vertex.h" #include "shader.h" +#include "vertex.h" -ce::Mesh::Mesh( - Vertex* vertexArray, const unsigned vertexCount, - GLuint* indexArray, const unsigned indexCount -) : - vertexCount(vertexCount), - indexCount(indexCount), - VAO(0), - VBO(0), - EBO(0) -{ +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(); } -ce::Mesh::~Mesh() -{ +ce::Mesh::~Mesh() { glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &this->VBO); glDeleteBuffers(1, &this->EBO); } -void ce::Mesh::bind() -{ +void ce::Mesh::bind() { glBindVertexArray(VAO); } -void ce::Mesh::unbind() -{ +void ce::Mesh::unbind() { glBindVertexArray(0); } -void ce::Mesh::sendToShader(ce::Shader* shader) -{ +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)); + 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::initVAO(Vertex* vertexArray, GLuint* indexArray) -{ +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); + glBufferData(GL_ARRAY_BUFFER, this->vertexCount * sizeof(Vertex), + vertexArray, GL_STATIC_DRAW); 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); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, this->indexCount * sizeof(GLuint), + indexArray, GL_STATIC_DRAW); } } diff --git a/engine/rendering/mesh.h b/engine/rendering/mesh.h index 072c7cbb..9c928741 100644 --- a/engine/rendering/mesh.h +++ b/engine/rendering/mesh.h @@ -1,32 +1,25 @@ -#ifndef _MESH_H_ -#define _MESH_H_ +#pragma once #include #include "vertex.h" namespace ce { - class Mesh - { + class Mesh { - private: - unsigned vertexCount,indexCount; + private: + unsigned vertexCount, indexCount; GLuint VAO, VBO, EBO; void initVAO(Vertex* vertexArray, GLuint* indexArray); - public: - Mesh( - Vertex* vertexArray, const unsigned vertexCount, - GLuint* indexArray = NULL, const unsigned indexCount = 0 - ); + + public: + Mesh(Vertex* vertexArray, const unsigned vertexCount, + GLuint* indexArray = NULL, const unsigned indexCount = 0); ~Mesh(); void sendToShader(class Shader* shader); - + unsigned GetIndexCount() { return indexCount; }; - void - bind(), - unbind(); + void bind(), unbind(); }; -} - -#endif // !_MESH_H_ +} \ No newline at end of file diff --git a/engine/rendering/shader.cpp b/engine/rendering/shader.cpp index ace26c63..025c49b3 100644 --- a/engine/rendering/shader.cpp +++ b/engine/rendering/shader.cpp @@ -1,44 +1,46 @@ #include "shader.h" +#include #include #include -#include -void checkCompileErrors(GLuint shader, GLint shaderType) -{ +void checkCompileErrors(GLuint shader, GLint shaderType) { std::string type; - switch (shaderType) - { - case GL_VERTEX_SHADER: type = "VERTEX"; break; - case GL_FRAGMENT_SHADER: type = "FRAGMENT"; break; - case GL_GEOMETRY_SHADER: type = "GEOMETRY"; break; - default: return; + switch (shaderType) { + case GL_VERTEX_SHADER: + type = "VERTEX"; + break; + case GL_FRAGMENT_SHADER: + type = "FRAGMENT"; + break; + case GL_GEOMETRY_SHADER: + type = "GEOMETRY"; + break; + default: + return; } int success; char infoLog[1024]; glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (!success) - { + if (!success) { glGetShaderInfoLog(shader, 1024, NULL, infoLog); - LOG_ERROR("SHADER_COMPILATION_ERROR of type: " + type + "\n" + infoLog+"\n"); + LOG_ERROR( + "SHADER_COMPILATION_ERROR of type: " + type + "\n" + infoLog + "\n"); exit(-1); } } -void checkCompileErrors(GLuint program) -{ +void checkCompileErrors(GLuint program) { int success; char infoLog[1024]; glGetProgramiv(program, GL_LINK_STATUS, &success); - if (!success) - { + if (!success) { glGetProgramInfoLog(program, 1024, NULL, infoLog); LOG_ERROR("PROGRAM_LINKING_ERROR\n" + infoLog + "\n"); exit(-1); } } -int createShader(GLenum type, std::string source) -{ +int createShader(GLenum type, std::string source) { const char* shaderSourceStr = source.c_str(); GLuint shader = glCreateShader(type); @@ -49,16 +51,19 @@ int createShader(GLenum type, std::string source) return shader; } -void ce::Shader::linkProgram(int vertexShader, int fragmentShader, int geometryShader) { - if (vertexShader != 0) glAttachShader(program, vertexShader); - if (fragmentShader != 0) glAttachShader(program, fragmentShader); - if (geometryShader != 0) glAttachShader(program, geometryShader); +void ce::Shader::linkProgram( + int vertexShader, int fragmentShader, int geometryShader) { + if (vertexShader != 0) + glAttachShader(program, vertexShader); + if (fragmentShader != 0) + glAttachShader(program, fragmentShader); + if (geometryShader != 0) + glAttachShader(program, geometryShader); glLinkProgram(program); checkCompileErrors(program); } -int ce::Shader::registerAttribute(std::string name) -{ +int ce::Shader::registerAttribute(std::string name) { int location = glGetAttribLocation(program, name.c_str()); if (location < Shader::MIN_LOC) { LOG_ERROR("Invalid Attribute: " + name); @@ -69,8 +74,7 @@ int ce::Shader::registerAttribute(std::string name) return location; } -int ce::Shader::registerUniform(std::string name) -{ +int ce::Shader::registerUniform(std::string name) { int location = glGetUniformLocation(program, name.c_str()); if (location < Shader::MIN_LOC) { LOG_ERROR("Invalid Uniform: " + name); @@ -81,22 +85,22 @@ 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; int fragmentShader = 0; int geometryShader = 0; - if (shaderFile.vertex != "") vertexShader = createShader(GL_VERTEX_SHADER, shaderFile.vertex); - if (shaderFile.fragment != "") fragmentShader = createShader(GL_FRAGMENT_SHADER, shaderFile.fragment); - if (shaderFile.geometry != "") geometryShader = createShader(GL_GEOMETRY_SHADER, shaderFile.geometry); + if (shaderFile.vertex != "") + vertexShader = createShader(GL_VERTEX_SHADER, shaderFile.vertex); + if (shaderFile.fragment != "") + fragmentShader = createShader(GL_FRAGMENT_SHADER, shaderFile.fragment); + if (shaderFile.geometry != "") + geometryShader = createShader(GL_GEOMETRY_SHADER, shaderFile.geometry); linkProgram(vertexShader, fragmentShader, vertexShader); - - int attrCount=0, uniformCount=0; + + int attrCount = 0, uniformCount = 0; glGetProgramiv(program, GL_ACTIVE_ATTRIBUTES, &attrCount); glGetProgramiv(program, GL_ACTIVE_UNIFORMS, &uniformCount); @@ -104,29 +108,25 @@ ce::Shader::Shader(const char* name ) : uniforms.resize(uniformCount); } -ce::Shader::~Shader() -{ +ce::Shader::~Shader() { glDeleteProgram(program); } -void ce::Shader::bind() -{ +void ce::Shader::bind() { glUseProgram(program); } -void ce::Shader::unbind() -{ +void ce::Shader::unbind() { glUseProgram(0); } -GLuint ce::Shader::getShader() -{ +GLuint ce::Shader::getShader() { return program; } -GLuint ce::Shader::getAttribLocation(const std::string name) -{ - if (attributes.size() < (size_t)Shader::MIN_LOC) return registerAttribute(name.c_str()); +GLuint ce::Shader::getAttribLocation(const std::string name) { + if (attributes.size() < (size_t)Shader::MIN_LOC) + return registerAttribute(name.c_str()); auto location = std::find(attributes.begin(), attributes.end(), name); if (location != attributes.end()) return std::distance(attributes.begin(), location); @@ -134,9 +134,9 @@ GLuint ce::Shader::getAttribLocation(const std::string name) return registerAttribute(name.c_str()); } -GLuint ce::Shader::getUniformLocation(const std::string name) -{ - if (attributes.size() < (size_t)Shader::MIN_LOC) return registerUniform(name.c_str()); +GLuint ce::Shader::getUniformLocation(const std::string name) { + if (attributes.size() < (size_t)Shader::MIN_LOC) + return registerUniform(name.c_str()); auto location = std::find(uniforms.begin(), uniforms.end(), name); if (location != uniforms.end()) return std::distance(uniforms.begin(), location); @@ -144,121 +144,123 @@ GLuint ce::Shader::getUniformLocation(const std::string name) return registerUniform(name.c_str()); } -void ce::Shader::vertexAttribPointer(std::string attrib, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void* pointer) { +void ce::Shader::vertexAttribPointer(std::string attrib, GLint size, + GLenum type, GLboolean normalized, GLsizei stride, const void* pointer) { GLuint location = getAttribLocation(attrib); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glVertexAttribPointer(location, size, type, normalized, stride, pointer); glEnableVertexAttribArray(location); } -void ce::Shader::setBool(const std::string name, bool value) -{ +void ce::Shader::setBool(const std::string name, bool value) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniform1i(location, (int)value); unbind(); } -void ce::Shader::setInt(const std::string name, int value) -{ +void ce::Shader::setInt(const std::string name, int value) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniform1i(location, value); unbind(); } -void ce::Shader::setFloat(const std::string name, float value) -{ +void ce::Shader::setFloat(const std::string name, float value) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniform1f(location, value); unbind(); } // ------------------------------------------------------------------------ -void ce::Shader::setVec2(const std::string name, vec2 value) -{ +void ce::Shader::setVec2(const std::string name, vec2 value) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniform2fv(location, 1, &value[0]); unbind(); } -void ce::Shader::setVec2(const std::string name, float x, float y) -{ +void ce::Shader::setVec2(const std::string name, float x, float y) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniform2f(location, x, y); - //storageLog("uniform", name, &x, &y); + // storageLog("uniform", name, &x, &y); unbind(); } -void ce::Shader::setVec3(const std::string name, vec3 value) -{ +void ce::Shader::setVec3(const std::string name, vec3 value) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniform3fv(location, 1, &value[0]); unbind(); } -void ce::Shader::setVec3(const std::string name, float x, float y, float z) -{ +void ce::Shader::setVec3(const std::string name, float x, float y, float z) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniform3f(location, x, y, z); - //storageLog("uniform", name, &x, &y, &z); + // storageLog("uniform", name, &x, &y, &z); unbind(); } // ------------------------------------------------------------------------ -void ce::Shader::setVec4(const std::string name, vec4 value) -{ +void ce::Shader::setVec4(const std::string name, vec4 value) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniform4fv(location, 1, &value[0]); - //storageLog("uniform", name, &value[0]); + // storageLog("uniform", name, &value[0]); unbind(); } -void ce::Shader::setVec4(const std::string name, float x, float y, float z, float w) -{ +void ce::Shader::setVec4( + const std::string name, float x, float y, float z, float w) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniform4f(location, x, y, z, w); - //storageLog("uniform", name, &x, &y, &z, &w); + // storageLog("uniform", name, &x, &y, &z, &w); unbind(); } // ------------------------------------------------------------------------ -void ce::Shader::setMat2(const std::string name, mat2 mat) -{ +void ce::Shader::setMat2(const std::string name, mat2 mat) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniformMatrix2fv(location, 1, GL_FALSE, &mat[0][0]); - //storageLog("uniform", name, &mat[0][0]); + // storageLog("uniform", name, &mat[0][0]); unbind(); } // ------------------------------------------------------------------------ -void ce::Shader::setMat3(const std::string name, mat3 mat) -{ +void ce::Shader::setMat3(const std::string name, mat3 mat) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniformMatrix3fv(location, 1, GL_FALSE, &mat[0][0]); - //storageLog("uniform", name, &mat[0][0]); + // storageLog("uniform", name, &mat[0][0]); unbind(); } // ------------------------------------------------------------------------ -void ce::Shader::setMat4(const std::string name, mat4 mat) -{ +void ce::Shader::setMat4(const std::string name, mat4 mat) { bind(); GLuint location = getUniformLocation(name); - if (location < (GLuint)Shader::MIN_LOC) return; + if (location < (GLuint)Shader::MIN_LOC) + return; glUniformMatrix4fv(location, 1, GL_FALSE, &mat[0][0]); - //storageLog("uniform", name, &mat[0][0]); + // storageLog("uniform", name, &mat[0][0]); unbind(); -} - +} \ No newline at end of file diff --git a/engine/rendering/shader.h b/engine/rendering/shader.h index bc1c887e..9d4080ba 100644 --- a/engine/rendering/shader.h +++ b/engine/rendering/shader.h @@ -1,46 +1,41 @@ -#ifndef _SHADER_H_ -#define _SHADER_H_ +#pragma once +#include #include #include -#include +#include #include #include -#include using namespace glm; namespace ce { - - class Shader - { - private: + + class Shader { + private: inline static int MIN_LOC = 0; GLuint program; std::vector attributes, uniforms; - void linkProgram(int vertexShader, int fragmentShader, int geometryShader); + void linkProgram( + int vertexShader, int fragmentShader, int geometryShader); - int - registerAttribute(std::string name), + int registerAttribute(std::string name), registerUniform(std::string name); - public: + + public: Shader(const char* name); ~Shader(); - - void - bind(), - unbind(); - - GLuint - getShader(), - getAttribLocation(const std::string name), + + void bind(), unbind(); + + GLuint getShader(), getAttribLocation(const std::string name), getUniformLocation(const std::string name); - void - vertexAttribPointer(std::string attrib, GLint size, GLenum type, GLboolean normalized, GLsizei stride, const void* pointer), + void vertexAttribPointer(std::string attrib, GLint size, GLenum type, + GLboolean normalized, GLsizei stride, const void*pointer), setBool(const std::string name, bool value), setInt(const std::string name, int value), @@ -59,6 +54,4 @@ namespace ce { setMat3(const std::string name, mat3 mat), setMat4(const std::string name, mat4 mat); }; -} - -#endif // !_SHADER_H_ +} \ No newline at end of file diff --git a/engine/rendering/texture.cpp b/engine/rendering/texture.cpp index b5d1e90c..6f0d13e2 100644 --- a/engine/rendering/texture.cpp +++ b/engine/rendering/texture.cpp @@ -1,20 +1,16 @@ #include "texture.h" -#include #include +#include -ce::Texture::Texture(std::string filename, GLenum type): - width(0), - height(0), - channelCount(0), - type(type) -{ +ce::Texture::Texture(std::string filename, GLenum type) + : width(0), height(0), channelCount(0), type(type) { TextureFile textureFile = ce::AssetManager::getTextureFile(filename); - + width = textureFile.width; height = textureFile.height; channelCount = textureFile.channelCount; - + glGenTextures(1, &texture); bind(); glTexParameteri(type, GL_TEXTURE_WRAP_S, GL_REPEAT); @@ -22,33 +18,28 @@ ce::Texture::Texture(std::string filename, GLenum type): glTexParameteri(type, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(type, GL_TEXTURE_MIN_FILTER, GL_REPEAT); - if (textureFile.data) { - glTexImage2D(type, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureFile.data); + glTexImage2D(type, 0, GL_RGBA, width, height, 0, GL_RGBA, + GL_UNSIGNED_BYTE, textureFile.data); glGenerateMipmap(type); LOG_SUCCESS(textureFile.name + " has been loaded."); - } - else { + } else LOG_ERROR("TEXTURE_LOADING_FAILED: " + textureFile.name); - } unbind(); ce::AssetManager::freeTextureFile(textureFile); } -ce::Texture::~Texture() -{; +ce::Texture::~Texture() { glDeleteTextures(1, &texture); } -void ce::Texture::bind() -{ +void ce::Texture::bind() { glBindTexture(type, texture); glEnable(type); } -void ce::Texture::activate(int slot=0) -{ - glActiveTexture(GL_TEXTURE0+slot); +void ce::Texture::activate(int slot = 0) { + glActiveTexture(GL_TEXTURE0 + slot); bind(); } diff --git a/engine/rendering/texture.h b/engine/rendering/texture.h index 2dff3ca9..60e965d0 100644 --- a/engine/rendering/texture.h +++ b/engine/rendering/texture.h @@ -1,28 +1,21 @@ -#ifndef _TEXTURE_H_ -#define _TEXTURE_H_ +#pragma once -#include #include +#include #include namespace ce { - class Texture - { - private: + class Texture { + private: GLuint texture; int width, height, channelCount; unsigned int type; - public: + public: Texture(std::string filename, GLenum type = GL_TEXTURE_2D); ~Texture(); - void - bind(), - unbind(), - activate(int slot); + void bind(), unbind(), activate(int slot); }; -} - -#endif // !_TEXTURE_H_ +} \ No newline at end of file diff --git a/engine/rendering/vertex.h b/engine/rendering/vertex.h index 532a1e70..1f2c6acb 100644 --- a/engine/rendering/vertex.h +++ b/engine/rendering/vertex.h @@ -1,5 +1,4 @@ -#ifndef _VERTEX_H_ -#define _VERTEX_H_ +#pragma once #include @@ -11,6 +10,4 @@ namespace ce { vec4 color; vec2 texCoord; }; -} - -#endif // !_VERTEX_H_ +} \ No newline at end of file diff --git a/res/shaders/basic.frag b/res/shaders/basic.frag index 6c013590..ca6918b0 100644 --- a/res/shaders/basic.frag +++ b/res/shaders/basic.frag @@ -6,7 +6,6 @@ in vec2 vTexCoord; out vec4 fColor; -void main() -{ - fColor = texture(uTex,vTexCoord)*vColor; -} +void main() { + fColor = texture(uTex,vTexCoord) * vColor; +} \ No newline at end of file diff --git a/res/shaders/basic.vert b/res/shaders/basic.vert index 103d401e..4458d251 100644 --- a/res/shaders/basic.vert +++ b/res/shaders/basic.vert @@ -16,8 +16,7 @@ out vec4 vColor; out vec2 vTexCoord; -void main() -{ +void main() { vColor = aColor; vTexCoord = aTexCoord;