diff --git a/.vscode/extensions.json b/.vscode/extensions.json index eafb6d5..ec4c01f 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,24 +1,20 @@ { "recommendations": [ "aaron-bond.better-comments", - "jbenden.c-cpp-flylint", + "ms-vscode.cpptools", "ms-vscode.cpptools-themes", - "twxs.cmake", - "ms-vscode.cmake-tools", - "stackbreak.comment-divider", - "donjayamanne.githistory", - "james-yu.latex-workshop", - "ms-vscode.makefile-tools", - "pkief.material-icon-theme", - "ms-vscode-remote.vscode-remote-extensionpack", - "henriiik.vscode-sort", - "hbenl.test-adapter-converter", - "streetsidesoftware.code-spell-checker", "wmaurer.change-case", + "xaver.clang-format", + "streetsidesoftware.code-spell-checker", + "vadimcn.vscode-lldb", "usernamehw.errorlens", + "donjayamanne.githistory", + "dtoplak.vscode-glsllint", "kisstkondoros.vscode-gutter-preview", + "james-yu.latex-workshop", "davidanson.vscode-markdownlint", + "pkief.material-icon-theme", "streetsidesoftware.code-spell-checker-polish", - "tyriar.sort-lines" + "slevesque.shader" ] } \ No newline at end of file diff --git a/Engine/engine/.clang-format b/Engine/engine/.clang-format new file mode 100644 index 0000000..2593ef5 --- /dev/null +++ b/Engine/engine/.clang-format @@ -0,0 +1 @@ +BasedOnStyle: Google \ No newline at end of file diff --git a/Engine/engine/beforeRender.cpp b/Engine/engine/beforeRender.cpp index 7eef896..9fe1da2 100644 --- a/Engine/engine/beforeRender.cpp +++ b/Engine/engine/beforeRender.cpp @@ -1,101 +1,95 @@ // "Copyright [2023] " #ifndef BEFORE_RENDER_CPP #define BEFORE_RENDER_CPP -#include +#include "./beforeRender.hpp" + #include +#include #include -#include "./beforeRender.hpp" + #include "./constants.hpp" #include "./misc.hpp" void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion) { - // first argument tells us what option to configure - // second is to what we set the value of this option - // see: https://www.glfw.org/docs/latest/window.html#window_hints - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GLFWMajorVersion); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GLFWMinorVersion); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); + // first argument tells us what option to configure + // second is to what we set the value of this option + // see: https://www.glfw.org/docs/latest/window.html#window_hints + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, GLFWMajorVersion); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, GLFWMinorVersion); + glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // we set GLFW to 3.3 CORE // core profile gives us access to smaller subset of // OGL without backwards compatible features // if we are on Mac OS X we need this for our code to work #ifdef __APPLE__ - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #endif } void instantiateGLFWwindow() { - // Initialize GLFW - glfwInit(); - configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION); + // Initialize GLFW + glfwInit(); + configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION); } GLFWwindow *createWindowObject() { - // First two arguments are width and height - // Third is the name of the window - // We ignore last two - GLFWwindow *window = glfwCreateWindow( - constants::MAIN_WINDOW_WIDTH, - constants::MAIN_WINDOW_HEIGHT, - constants::MAIN_WINDOW_NAME, - nullptr, - nullptr); - return window; + // First two arguments are width and height + // Third is the name of the window + // We ignore last two + GLFWwindow *window = glfwCreateWindow( + constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, + constants::MAIN_WINDOW_NAME, nullptr, nullptr); + return window; } int initializeGLAD() { - // we load address of OGL OS-specific function pointers - if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { - print("Failed to initialize GLAD"); - return -1; - } - return 0; + // we load address of OGL OS-specific function pointers + if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { + print("Failed to initialize GLAD"); + return -1; + } + return 0; } // resizes viewport when user resizes window -void framebuffer_size_callback(GLFWwindow *window, - const int width, - const int height) { - glViewport(0, 0, width, height); +void framebuffer_size_callback(GLFWwindow *window, const int width, + const int height) { + glViewport(0, 0, width, height); } void viewPort(GLFWwindow *window) { - // We tell OGL size of rendering window - // First two define left corner of window - // 3th and 4th width and height of rendering window - // we could set them to be smaller than window dimension, - // ogl rendering will be then displayed in smaller window - glViewport(0, - 0, - constants::MAIN_WINDOW_WIDTH, - constants::MAIN_WINDOW_HEIGHT); - // processed coordinates are between -1 and 1 so here we map: - // (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH) - // and (0, constants::MAIN_WINDOW_HEIGHT) - glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); - // we call framebuffer_size_callback on every window resize + // We tell OGL size of rendering window + // First two define left corner of window + // 3th and 4th width and height of rendering window + // we could set them to be smaller than window dimension, + // ogl rendering will be then displayed in smaller window + glViewport(0, 0, constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT); + // processed coordinates are between -1 and 1 so here we map: + // (-1 to 1) to (0, constants::MAIN_WINDOW_WIDTH) + // and (0, constants::MAIN_WINDOW_HEIGHT) + glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); + // we call framebuffer_size_callback on every window resize } GLFWwindow *prepareForRender() { - instantiateGLFWwindow(); + instantiateGLFWwindow(); - GLFWwindow *window = createWindowObject(); - // function returns GLFWWindow object + GLFWwindow *window = createWindowObject(); + // function returns GLFWWindow object - if (window == nullptr) { - print("Failed to create GLFW window"); - glfwTerminate(); - return window; - } - // we make context of this window main context of current thread - glfwMakeContextCurrent(window); - - if (initializeGLAD() == -1) - return nullptr; - viewPort(window); + if (window == nullptr) { + print("Failed to create GLFW window"); + glfwTerminate(); return window; + } + // we make context of this window main context of current thread + glfwMakeContextCurrent(window); + + if (initializeGLAD() == -1) return nullptr; + viewPort(window); + return window; } #endif diff --git a/Engine/engine/beforeRender.hpp b/Engine/engine/beforeRender.hpp index 0863d12..9ac85fb 100644 --- a/Engine/engine/beforeRender.hpp +++ b/Engine/engine/beforeRender.hpp @@ -7,10 +7,8 @@ void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion); void instantiateGLFWwindow(); GLFWwindow *createWindowObject(); int initializeGLAD(); -void framebuffer_size_callback( - GLFWwindow *window, - const int width, - const int height); +void framebuffer_size_callback(GLFWwindow *window, const int width, + const int height); void viewPort(GLFWwindow *window); GLFWwindow *prepareForRender(); diff --git a/Engine/engine/constants.hpp b/Engine/engine/constants.hpp index 23ee1b1..ad5e39d 100644 --- a/Engine/engine/constants.hpp +++ b/Engine/engine/constants.hpp @@ -2,217 +2,195 @@ #ifndef ENGINE_ENGINE_CONSTANTS_HPP_ #define ENGINE_ENGINE_CONSTANTS_HPP_ #include + #include #include - namespace constants { - inline constexpr int GLFW_MAJOR_VERSION { 3 }; - inline constexpr int GLFW_MINOR_VERSION { 3 }; - // best practice is to use inline constexpr - // std::string_view but glfwCreateWindow takes only char* as input - inline const char* MAIN_WINDOW_NAME { "Match" }; - inline constexpr int MAIN_WINDOW_WIDTH { 800 }; - inline constexpr int MAIN_WINDOW_HEIGHT { 600 }; - inline constexpr struct { - GLfloat red = 1.0f; - GLfloat green = 0.0f; - GLfloat blue = 0.0f; - GLfloat alpha = 1.0f; - } RED; +inline constexpr int GLFW_MAJOR_VERSION{3}; +inline constexpr int GLFW_MINOR_VERSION{3}; +// best practice is to use inline constexpr +// std::string_view but glfwCreateWindow takes only char* as input +inline const char* MAIN_WINDOW_NAME{"Match"}; +inline constexpr int MAIN_WINDOW_WIDTH{800}; +inline constexpr int MAIN_WINDOW_HEIGHT{600}; +inline constexpr struct { + GLfloat red = 1.0f; + GLfloat green = 0.0f; + GLfloat blue = 0.0f; + GLfloat alpha = 1.0f; +} RED; - inline constexpr struct { - GLfloat red = 1.0f; - GLfloat green = 1.0f; - GLfloat blue = 1.0f; - GLfloat alpha = 1.0f; - } WHITE; +inline constexpr struct { + GLfloat red = 1.0f; + GLfloat green = 1.0f; + GLfloat blue = 1.0f; + GLfloat alpha = 1.0f; +} WHITE; - inline constexpr struct { - GLfloat red = 0.2f; - GLfloat green = 0.3f; - GLfloat blue = 0.3f; - GLfloat alpha = 1.0f; - } LEARN_OPEN_GL_COLOR; +inline constexpr struct { + GLfloat red = 0.2f; + GLfloat green = 0.3f; + GLfloat blue = 0.3f; + GLfloat alpha = 1.0f; +} LEARN_OPEN_GL_COLOR; - inline const char* VERTEX_SHADER_COLOR_FILENAME { - "./Shaders/vertexShaderColor.vs" - }; +inline const char* VERTEX_SHADER_COLOR_FILENAME{ + "./Shaders/vertexShaderColor.vs"}; - inline const char* VERTEX_SHADER_UPSIDE_DOWN_FILENAME { - "./Shaders/vertexShaderUpsideDown.vs" - }; +inline const char* VERTEX_SHADER_UPSIDE_DOWN_FILENAME{ + "./Shaders/vertexShaderUpsideDown.vs"}; - inline const char* VERTEX_SHADER_OFFSET_FILENAME { - "./Shaders/vertexShaderOffset.vs" - }; - // we write vertex shader - // version of glsl (since ogl 3.3 same as ogl so we pick 330) - // in this shader we just forward input data to shader output - // DO NOT REMOVE UNTIL CAN DRAW SQUARE USING CLASS - inline const char *VERTEX_SHADER_SOURCE { "#version 330 core\n" +inline const char* VERTEX_SHADER_OFFSET_FILENAME{ + "./Shaders/vertexShaderOffset.vs"}; +// we write vertex shader +// version of glsl (since ogl 3.3 same as ogl so we pick 330) +// in this shader we just forward input data to shader output +// DO NOT REMOVE UNTIL CAN DRAW SQUARE USING CLASS +inline const char* VERTEX_SHADER_SOURCE{ + "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "void main()\n" "{\n" " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" - "}\0" }; - inline const char* VERTEX_SHADER_SOURCE_FILENAME { - "./Shaders/vertexShaderSource.vs" - }; + "}\0"}; +inline const char* VERTEX_SHADER_SOURCE_FILENAME{ + "./Shaders/vertexShaderSource.vs"}; - inline const char* VERTEX_SHADER_VERTICE_COLOR_FILENAME { - "./Shaders/vertexShaderVerticeColor.vs" - }; +inline const char* VERTEX_SHADER_VERTICE_COLOR_FILENAME{ + "./Shaders/vertexShaderVerticeColor.vs"}; - inline const char* VERTEX_SHADER_TASK_THREE_FILENAME { - "./Shaders/vertexShaderTaskThree.vs" - }; +inline const char* VERTEX_SHADER_TASK_THREE_FILENAME{ + "./Shaders/vertexShaderTaskThree.vs"}; - inline const char* VERTEX_SHADER_TEXTURE_FILENAME { - "./Shaders/vertexShaderTexture.vs" - }; +inline const char* VERTEX_SHADER_TEXTURE_FILENAME{ + "./Shaders/vertexShaderTexture.vs"}; - inline const char* FRAGMENT_SHADER_TASK_THREE_FILENAME { - "./Shaders/fragmentShaderTaskThree.fs" - }; +inline const char* FRAGMENT_SHADER_TASK_THREE_FILENAME{ + "./Shaders/fragmentShaderTaskThree.fs"}; - inline const char* FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME { - "./Shaders/fragmentShaderColorFromVertex.fs" - }; +inline const char* FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME{ + "./Shaders/fragmentShaderColorFromVertex.fs"}; - inline const char* FRAGMENT_SHADER_UNIFORMS_FILENAME { - "./Shaders/fragmentShaderUniforms.fs" - }; +inline const char* FRAGMENT_SHADER_UNIFORMS_FILENAME{ + "./Shaders/fragmentShaderUniforms.fs"}; - // write fragment shader - // we set the color of each pixel to be orange - // DO NOT REMOVE UNTIL CAN DRAW SQUARE USING CLASS - inline const char *FRAGMENT_SHADER_SOURCE { - "#version 330 core\n" - "out vec4 FragColor;\n" - "void main()\n" - "{\n" - "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" - "}\0" - }; +// write fragment shader +// we set the color of each pixel to be orange +// DO NOT REMOVE UNTIL CAN DRAW SQUARE USING CLASS +inline const char* FRAGMENT_SHADER_SOURCE{ + "#version 330 core\n" + "out vec4 FragColor;\n" + "void main()\n" + "{\n" + "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" + "}\0"}; - inline const char* FRAGMENT_SHADER_SOURCE_FILENAME { - "./Shaders/fragmentShaderSource.fs" - }; - inline const char* FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME { - "./Shaders/fragmentShaderSourceYellow.fs" - }; +inline const char* FRAGMENT_SHADER_SOURCE_FILENAME{ + "./Shaders/fragmentShaderSource.fs"}; +inline const char* FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME{ + "./Shaders/fragmentShaderSourceYellow.fs"}; - inline const char* FRAGMENT_SHADER_TEXTURE_FILENAME { - "./Shaders/fragmentShaderTexture.fs" - }; +inline const char* FRAGMENT_SHADER_TEXTURE_FILENAME{ + "./Shaders/fragmentShaderTexture.fs"}; - // we specify three vertices - // each of them with position in 3d space - // x y z - inline constexpr float TRIANGLE_VERTICES[] { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f - }; +// we specify three vertices +// each of them with position in 3d space +// x y z +inline constexpr float TRIANGLE_VERTICES[]{-0.5f, -0.5f, 0.0f, 0.5f, -0.5f, + 0.0f, 0.0f, 0.5f, 0.0f}; - inline constexpr size_t TRIANGLE_VERTICES_SIZE = { - sizeof(TRIANGLE_VERTICES) - }; +inline constexpr size_t TRIANGLE_VERTICES_SIZE = {sizeof(TRIANGLE_VERTICES)}; - inline constexpr float TRIANGLES_VERTICES[] { - // first triangle - -0.9f, -0.5f, 0.0f, // left - -0.0f, -0.5f, 0.0f, // right - -0.45f, 0.5f, 0.0f, // top - // second triangle - 0.0f, -0.5f, 0.0f, // left - 0.9f, -0.5f, 0.0f, // right - 0.45f, 0.5f, 0.0f // top - }; - - inline constexpr size_t TRIANGLES_VERTICES_SIZE = { - sizeof(TRIANGLES_VERTICES) }; - - inline constexpr float TRIANGLE_ONE[] { - // first triangle - -0.9f, -0.5f, 0.0f, // left - -0.0f, -0.5f, 0.0f, // right - -0.45f, 0.5f, 0.0f, // top - }; - - inline constexpr size_t TRIANGLE_ONE_SIZE = { sizeof(TRIANGLE_ONE) }; - - inline constexpr float TRIANGLE_TWO[] { - // second triangle - 0.0f, -0.5f, 0.0f, // left - 0.9f, -0.5f, 0.0f, // right - 0.45f, 0.5f, 0.0f // top - }; - - inline constexpr size_t TRIANGLE_TWO_SIZE = { sizeof(TRIANGLE_TWO) }; - - inline constexpr float TRIANGLE_COLORS[] { - // positions // colors - 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left - 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top - }; - - inline constexpr size_t TRIANGLE_COLORS_SIZE = { sizeof(TRIANGLE_COLORS) }; - - // compare with square done with only vertices: - /* - float vertices[] = { +inline constexpr float TRIANGLES_VERTICES[]{ // first triangle - 0.5f, 0.5f, 0.0f, // top right - 0.5f, -0.5f, 0.0f, // bottom right - -0.5f, 0.5f, 0.0f, // top left + -0.9f, -0.5f, 0.0f, // left + -0.0f, -0.5f, 0.0f, // right + -0.45f, 0.5f, 0.0f, // top + // second triangle + 0.0f, -0.5f, 0.0f, // left + 0.9f, -0.5f, 0.0f, // right + 0.45f, 0.5f, 0.0f // top +}; + +inline constexpr size_t TRIANGLES_VERTICES_SIZE = {sizeof(TRIANGLES_VERTICES)}; + +inline constexpr float TRIANGLE_ONE[]{ + // first triangle + -0.9f, -0.5f, 0.0f, // left + -0.0f, -0.5f, 0.0f, // right + -0.45f, 0.5f, 0.0f, // top +}; + +inline constexpr size_t TRIANGLE_ONE_SIZE = {sizeof(TRIANGLE_ONE)}; + +inline constexpr float TRIANGLE_TWO[]{ // second triangle - 0.5f, -0.5f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f // top left - }; - bottom right and top left is specified twice ! - */ - inline constexpr float SQUARE_VERTICES[] { - 0.5f, 0.5f, 0.0f, // top right - 0.5f, -0.5f, 0.0f, // bottom right - -0.25f, -0.5f, 0.0f, // bottom left - -0.25f, 0.5f, 0.0f // top left - }; + 0.0f, -0.5f, 0.0f, // left + 0.9f, -0.5f, 0.0f, // right + 0.45f, 0.5f, 0.0f // top +}; - inline constexpr unsigned int SQUARE_INDICES[] { - 0, 1, 3, // first triangle - 1, 2, 3, // second triangle - }; +inline constexpr size_t TRIANGLE_TWO_SIZE = {sizeof(TRIANGLE_TWO)}; - inline constexpr size_t SQUARE_INDICES_SIZE = { sizeof(SQUARE_INDICES) }; +inline constexpr float TRIANGLE_COLORS[]{ + // positions // colors + 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left + 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f // top +}; - inline constexpr size_t SQUARE_VERTICES_SIZE = { sizeof(SQUARE_VERTICES) }; +inline constexpr size_t TRIANGLE_COLORS_SIZE = {sizeof(TRIANGLE_COLORS)}; - inline constexpr int MAX_DRAW_CALL = { 11 }; +// compare with square done with only vertices: +/* +float vertices[] = { +// first triangle + 0.5f, 0.5f, 0.0f, // top right + 0.5f, -0.5f, 0.0f, // bottom right +-0.5f, 0.5f, 0.0f, // top left +// second triangle + 0.5f, -0.5f, 0.0f, // bottom right +-0.5f, -0.5f, 0.0f, // bottom left +-0.5f, 0.5f, 0.0f // top left +}; +bottom right and top left is specified twice ! +*/ +inline constexpr float SQUARE_VERTICES[]{ + 0.5f, 0.5f, 0.0f, // top right + 0.5f, -0.5f, 0.0f, // bottom right + -0.25f, -0.5f, 0.0f, // bottom left + -0.25f, 0.5f, 0.0f // top left +}; - // https://learnopengl.com/img/getting-started/tex_coords.png - inline constexpr float TEXTURE_VERTICES[] { - // positions // colors // texture coords - 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right - 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right - -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left - -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left - }; +inline constexpr unsigned int SQUARE_INDICES[]{ + 0, 1, 3, // first triangle + 1, 2, 3, // second triangle +}; - inline constexpr size_t TEXTURE_VERTICES_SIZE = { - sizeof(TEXTURE_VERTICES) - }; +inline constexpr size_t SQUARE_INDICES_SIZE = {sizeof(SQUARE_INDICES)}; +inline constexpr size_t SQUARE_VERTICES_SIZE = {sizeof(SQUARE_VERTICES)}; - inline constexpr unsigned int TEXTURE_INDICES[] { - 0, 1, 3, // first triangle - 1, 2, 3 // second triangle - }; +inline constexpr int MAX_DRAW_CALL = {11}; - inline constexpr size_t TEXTURE_INDICES_SIZE = { sizeof(TEXTURE_INDICES) }; +// https://learnopengl.com/img/getting-started/tex_coords.png +inline constexpr float TEXTURE_VERTICES[]{ + // positions // colors // texture coords + 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right + 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right + -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left + -0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left +}; + +inline constexpr size_t TEXTURE_VERTICES_SIZE = {sizeof(TEXTURE_VERTICES)}; + +inline constexpr unsigned int TEXTURE_INDICES[]{ + 0, 1, 3, // first triangle + 1, 2, 3 // second triangle +}; + +inline constexpr size_t TEXTURE_INDICES_SIZE = {sizeof(TEXTURE_INDICES)}; } // namespace constants #endif // ENGINE_ENGINE_CONSTANTS_HPP_ diff --git a/Engine/engine/draw.cpp b/Engine/engine/draw.cpp index 7990d51..db7346d 100644 --- a/Engine/engine/draw.cpp +++ b/Engine/engine/draw.cpp @@ -1,332 +1,281 @@ // "Copyright [2023] " #ifndef DRAW_CPP #define DRAW_CPP -#include +#include "./draw.hpp" + #include -#include +#include + #include #include +#include #include -#include "./draw.hpp" -#include "./renderLoop.hpp" -#include "./shaders.hpp" + #include "./constants.hpp" #include "./misc.hpp" +#include "./renderLoop.hpp" #include "./shader.hpp" -#include "./textures.hpp" +#include "./shaders.hpp" #include "./stb_image.h" +#include "./textures.hpp" drawFigureReturn drawFigure(const int whatToDraw) { - switch (whatToDraw) { + switch (whatToDraw) { case 0: - return drawTriangleClass( - constants::TRIANGLE_VERTICES, - constants::TRIANGLE_VERTICES_SIZE, - constants::VERTEX_SHADER_SOURCE_FILENAME, - constants::FRAGMENT_SHADER_SOURCE_FILENAME); + return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + constants::FRAGMENT_SHADER_SOURCE_FILENAME, + constants::TRIANGLE_VERTICES, + constants::TRIANGLE_VERTICES_SIZE); case 1: - return drawSquare( - constants::SQUARE_VERTICES, - constants::SQUARE_VERTICES_SIZE, - constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, - constants::VERTEX_SHADER_SOURCE, - constants::FRAGMENT_SHADER_SOURCE); + return drawSquare( + constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, + constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, + constants::VERTEX_SHADER_SOURCE, constants::FRAGMENT_SHADER_SOURCE); case 2: - // Try to draw 2 triangles next to each other - // using glDrawArrays by adding more vertices to your data. - return drawTriangleClass( - constants::TRIANGLES_VERTICES, - constants::TRIANGLES_VERTICES_SIZE, - constants::VERTEX_SHADER_SOURCE_FILENAME, - constants::FRAGMENT_SHADER_SOURCE_FILENAME); + // Try to draw 2 triangles next to each other + // using glDrawArrays by adding more vertices to your data. + return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + constants::FRAGMENT_SHADER_SOURCE_FILENAME, + constants::TRIANGLES_VERTICES, + constants::TRIANGLES_VERTICES_SIZE); case 3: - drawTriangleClass(constants::TRIANGLE_ONE, - constants::TRIANGLE_ONE_SIZE, - constants::VERTEX_SHADER_SOURCE_FILENAME, - constants::FRAGMENT_SHADER_SOURCE_FILENAME); - // Now create the same 2 triangles - // using two different VAOs and VBOs for their data - return drawTriangleClass( - constants::TRIANGLE_TWO, - constants::TRIANGLE_TWO_SIZE, - constants::VERTEX_SHADER_SOURCE_FILENAME, - constants::FRAGMENT_SHADER_SOURCE_FILENAME); + drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + constants::FRAGMENT_SHADER_SOURCE_FILENAME, + constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE); + // Now create the same 2 triangles + // using two different VAOs and VBOs for their data + return drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + constants::FRAGMENT_SHADER_SOURCE_FILENAME, + constants::TRIANGLE_TWO, + constants::TRIANGLE_TWO_SIZE); case 4: - drawTriangleClass(constants::TRIANGLE_ONE, - constants::TRIANGLE_ONE_SIZE, + drawTriangleClass(constants::VERTEX_SHADER_SOURCE_FILENAME, + constants::FRAGMENT_SHADER_SOURCE_FILENAME, + constants::TRIANGLE_ONE, constants::TRIANGLE_ONE_SIZE); + /* + Create two shader programs where + the second program uses a different + fragment shader that outputs the color yellow; + draw both triangles again where one outputs the color yellow + */ + return drawTriangleClass( constants::VERTEX_SHADER_SOURCE_FILENAME, - constants::FRAGMENT_SHADER_SOURCE_FILENAME); - /* - Create two shader programs where - the second program uses a different - fragment shader that outputs the color yellow; - draw both triangles again where one outputs the color yellow - */ - return drawTriangleClass( - constants::TRIANGLE_TWO, - constants::TRIANGLE_TWO_SIZE, - constants::VERTEX_SHADER_SOURCE_FILENAME, - constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME); + constants::FRAGMENT_SHADER_SOURCE_YELLOW_FILENAME, + constants::TRIANGLE_TWO, constants::TRIANGLE_TWO_SIZE); case 5: - // Get color from vertex shader to fragment shader - return drawTriangleClass(constants::TRIANGLE_VERTICES, - constants::TRIANGLE_VERTICES_SIZE, + // Get color from vertex shader to fragment shader + return drawTriangleClass( constants::VERTEX_SHADER_COLOR_FILENAME, - constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME); + constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, + constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE); case 6: - // set color from opengl code to uniform value in fragment shader - return drawTriangleClass(constants::TRIANGLE_VERTICES, - constants::TRIANGLE_VERTICES_SIZE, - constants::VERTEX_SHADER_COLOR_FILENAME, - constants::FRAGMENT_SHADER_UNIFORMS_FILENAME); + // set color from opengl code to uniform value in fragment shader + return drawTriangleClass(constants::VERTEX_SHADER_COLOR_FILENAME, + constants::FRAGMENT_SHADER_UNIFORMS_FILENAME, + constants::TRIANGLE_VERTICES, + constants::TRIANGLE_VERTICES_SIZE); case 7: - // set color from opengl code to uniform value in fragment shader - return drawTriangleClass(constants::TRIANGLE_COLORS, - constants::TRIANGLE_COLORS_SIZE, - constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME, - constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, - true); + // set color from opengl code to uniform value in fragment shader + return drawTriangleClass( + constants::VERTEX_SHADER_VERTICE_COLOR_FILENAME, + constants::FRAGMENT_SHADER_COLOR_FROM_VERTEX_FILENAME, + constants::TRIANGLE_COLORS, constants::TRIANGLE_COLORS_SIZE, true); case 8: - // upside down triangle - return drawTriangleClass(constants::TRIANGLE_VERTICES, - constants::TRIANGLE_VERTICES_SIZE, - constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME, - constants::FRAGMENT_SHADER_SOURCE_FILENAME); - case 9: - { - // offset triangle - offsetsStruct offsets = offsetsStruct(); - offsets.xOffset = 0.5f; - return drawTriangleClass(constants::TRIANGLE_VERTICES, - constants::TRIANGLE_VERTICES_SIZE, - constants::VERTEX_SHADER_OFFSET_FILENAME, - constants::FRAGMENT_SHADER_SOURCE_FILENAME, - false, - false, - offsets); + // upside down triangle + return drawTriangleClass(constants::VERTEX_SHADER_UPSIDE_DOWN_FILENAME, + constants::FRAGMENT_SHADER_SOURCE_FILENAME, + constants::TRIANGLE_VERTICES, + constants::TRIANGLE_VERTICES_SIZE); + case 9: { + // offset triangle + offsetsStruct offsets = offsetsStruct(); + offsets.xOffset = 0.5f; + return drawTriangleClass(constants::VERTEX_SHADER_OFFSET_FILENAME, + constants::FRAGMENT_SHADER_SOURCE_FILENAME, + constants::TRIANGLE_VERTICES, + constants::TRIANGLE_VERTICES_SIZE, false, false, + offsets); } case 10: - return drawTriangleClass(constants::TRIANGLE_VERTICES, - constants::TRIANGLE_VERTICES_SIZE, - constants::VERTEX_SHADER_TASK_THREE_FILENAME, - constants::FRAGMENT_SHADER_TASK_THREE_FILENAME); + return drawTriangleClass(constants::VERTEX_SHADER_TASK_THREE_FILENAME, + constants::FRAGMENT_SHADER_TASK_THREE_FILENAME, + constants::TRIANGLE_VERTICES, + constants::TRIANGLE_VERTICES_SIZE); case constants::MAX_DRAW_CALL: - return drawDebilMode( - constants::VERTEX_SHADER_TEXTURE_FILENAME, - constants::FRAGMENT_SHADER_TEXTURE_FILENAME, - constants::TEXTURE_VERTICES, - constants::TEXTURE_VERTICES_SIZE, - constants::TEXTURE_INDICES, - constants::TEXTURE_INDICES_SIZE); + return drawDebilMode( + constants::VERTEX_SHADER_TEXTURE_FILENAME, + constants::FRAGMENT_SHADER_TEXTURE_FILENAME, + constants::TEXTURE_VERTICES, constants::TEXTURE_VERTICES_SIZE, + constants::TEXTURE_INDICES, constants::TEXTURE_INDICES_SIZE); default: - throw "No function for this draw call"; - } + throw "No function for this draw call"; + } } -unsigned int getShaderProgram( - const char* vertexShaderSource, - const char* fragmentShaderSource) { - const std::pair shaders = - compileShaders(vertexShaderSource, fragmentShaderSource); - if (shaders.first == 0 || shaders.second == 0) - return 0; +unsigned int getShaderProgram(const char* vertexShaderSource, + const char* fragmentShaderSource) { + const std::pair shaders = + compileShaders(vertexShaderSource, fragmentShaderSource); + if (shaders.first == 0 || shaders.second == 0) return 0; - const unsigned int shaderProgram = - linkShaderObjectsShaderProgram(shaders.first, shaders.second); + const unsigned int shaderProgram = + linkShaderObjectsShaderProgram(shaders.first, shaders.second); - if (shaderProgram == 0) - return 0; - return shaderProgram; + if (shaderProgram == 0) return 0; + return shaderProgram; } void setOffsets(Shader shader, const offsetsStruct offsets) { - shader.setFloat("xOffset", offsets.xOffset); - shader.setFloat("yOffset", offsets.yOffset); - shader.setFloat("zOffset", offsets.zOffset); + shader.setFloat("xOffset", offsets.xOffset); + shader.setFloat("yOffset", offsets.yOffset); + shader.setFloat("zOffset", offsets.zOffset); } -drawFigureReturn drawTriangleClass( - const float triangleVertices[], - const size_t triangleVerticesSize, - const char* vertexPath, - const char* fragmentPath, - const bool colorIncluded, - const bool textureIncluded, - const offsetsStruct offsets) { - // In your CPP file: - // ====================== - // float offset = 0.5f; +drawFigureReturn drawTriangleClass(const char* vertexPath, + const char* fragmentPath, + const float triangleVertices[], + const size_t triangleVerticesSize, + const bool colorIncluded, + const bool textureIncluded, + const offsetsStruct offsets) { + // In your CPP file: + // ====================== + // float offset = 0.5f; - Shader ourShader(vertexPath, fragmentPath); - ourShader.use(); - setOffsets(ourShader, offsets); + Shader ourShader(vertexPath, fragmentPath); + ourShader.use(); + setOffsets(ourShader, offsets); - const unsigned int vertexBufferObject = - copyVerticesMemory( - triangleVertices, - triangleVerticesSize, - GL_ARRAY_BUFFER); - const unsigned int vertexArrayObject = generateBindVAO(); - copyVerticesArray( - vertexBufferObject, - triangleVertices, - triangleVerticesSize, - GL_ARRAY_BUFFER); - // set vertex attribute pointers - configureVertexAttribute(colorIncluded, textureIncluded); - doDrawArrays( - ourShader.ID, - vertexArrayObject, - GL_TRIANGLES, - 0, - triangleVerticesSize); - drawFigureReturn newReturn; - newReturn.success = 0; - newReturn.VAO = vertexArrayObject; - newReturn.VBO = vertexBufferObject; - newReturn.EBO = 0; - return newReturn; + const unsigned int vertexBufferObject = copyVerticesMemory( + triangleVertices, triangleVerticesSize, GL_ARRAY_BUFFER); + const unsigned int vertexArrayObject = generateBindVAO(); + copyVerticesArray(vertexBufferObject, triangleVertices, triangleVerticesSize, + GL_ARRAY_BUFFER); + // set vertex attribute pointers + configureVertexAttribute(colorIncluded, textureIncluded); + doDrawArrays(ourShader.ID, vertexArrayObject, GL_TRIANGLES, 0, + triangleVerticesSize); + drawFigureReturn newReturn; + newReturn.success = 0; + newReturn.VAO = vertexArrayObject; + newReturn.VBO = vertexBufferObject; + newReturn.EBO = 0; + return newReturn; } drawFigureReturn drawSquare( - const float squareVertices[], - const size_t squareVerticesSize, - const unsigned int squareIndices[], - const size_t squareIndicesSize, - const char* vertexShaderSource, - const char* fragmentShaderSource, - const bool colorIncluded, - const bool textureIncluded, + const float squareVertices[], const size_t squareVerticesSize, + const unsigned int squareIndices[], const size_t squareIndicesSize, + const char* vertexShaderSource, const char* fragmentShaderSource, + const bool colorIncluded, const bool textureIncluded, const offsetsStruct offsets) { - const unsigned int shaderProgram = - getShaderProgram(vertexShaderSource, fragmentShaderSource); - drawFigureReturn newReturn = drawFigureReturn(); - if (shaderProgram == 0) { - newReturn.success = -1; - return newReturn; - } - const unsigned int VAO = generateBindVAO(); - copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER); - copyVerticesMemory( - squareIndices, - squareIndicesSize, - GL_ELEMENT_ARRAY_BUFFER); - - // set vertex attribute pointers - configureVertexAttribute(colorIncluded, textureIncluded); - - doDrawElements( - shaderProgram, - VAO, - GL_TRIANGLES, - GL_UNSIGNED_INT, - squareIndicesSize); - newReturn.success = 0; - newReturn.VAO = VAO; + const unsigned int shaderProgram = + getShaderProgram(vertexShaderSource, fragmentShaderSource); + drawFigureReturn newReturn = drawFigureReturn(); + if (shaderProgram == 0) { + newReturn.success = -1; return newReturn; + } + const unsigned int VAO = generateBindVAO(); + copyVerticesMemory(squareVertices, squareVerticesSize, GL_ARRAY_BUFFER); + copyVerticesMemory(squareIndices, squareIndicesSize, GL_ELEMENT_ARRAY_BUFFER); + + // set vertex attribute pointers + configureVertexAttribute(colorIncluded, textureIncluded); + + doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, + squareIndicesSize); + newReturn.success = 0; + newReturn.VAO = VAO; + return newReturn; } -drawFigureReturn drawDebilMode( - const char* vertexPath, - const char* fragmentPath, - const float vertices[], - const size_t verticesSize, - const unsigned int indices[], - const size_t indicesSize) { - // https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader - Shader ourShader( - constants::VERTEX_SHADER_TEXTURE_FILENAME, - constants::FRAGMENT_SHADER_TEXTURE_FILENAME); +drawFigureReturn drawDebilMode(const char* vertexPath, const char* fragmentPath, + const float vertices[], + const size_t verticesSize, + const unsigned int indices[], + const size_t indicesSize) { + // https://stackoverflow.com/questions/33883609/opengl-linker-error-linking-with-uncompiled-shader + Shader ourShader(constants::VERTEX_SHADER_TEXTURE_FILENAME, + constants::FRAGMENT_SHADER_TEXTURE_FILENAME); - unsigned int VAO = generateBindVAO(); - unsigned int VBO = copyVerticesMemory( - vertices, - verticesSize, - GL_ARRAY_BUFFER); - unsigned int EBO = copyVerticesMemory(indices, - indicesSize, - GL_ELEMENT_ARRAY_BUFFER); + unsigned int VAO = generateBindVAO(); + unsigned int VBO = + copyVerticesMemory(vertices, verticesSize, GL_ARRAY_BUFFER); + unsigned int EBO = + copyVerticesMemory(indices, indicesSize, GL_ELEMENT_ARRAY_BUFFER); - configureVertexAttribute(true, true); + configureVertexAttribute(true, true); - unsigned int texture1 = loadAndCreateTexture("assets/container.png", true); - unsigned int texture2 = loadAndCreateTexture( - "assets/awesomeface.png", - false); + unsigned int texture1 = loadAndCreateTexture("assets/container.png", true); + unsigned int texture2 = loadAndCreateTexture("assets/awesomeface.png", false); - ourShader.use(); // don't forget to activate/use - // the shader before setting uniforms! - // set it via the texture class - ourShader.setInt("texture1", 0); - ourShader.setInt("texture2", 1); + ourShader.use(); // don't forget to activate/use + // the shader before setting uniforms! + // set it via the texture class + ourShader.setInt("texture1", 0); + ourShader.setInt("texture2", 1); + activateAndBindTextures(texture1, GL_TEXTURE0); + activateAndBindTextures(texture2, GL_TEXTURE1); - activateAndBindTextures(texture1, GL_TEXTURE0); - activateAndBindTextures(texture2, GL_TEXTURE1); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - - drawFigureReturn newReturn; - newReturn.success = 0; - newReturn.VAO = VAO; - newReturn.VBO = VBO; - newReturn.EBO = EBO; - return newReturn; + drawFigureReturn newReturn; + newReturn.success = 0; + newReturn.VAO = VAO; + newReturn.VBO = VBO; + newReturn.EBO = EBO; + return newReturn; } -void doDrawElements( - const unsigned int shaderProgram, - const unsigned int vertexArrayObject, - const GLenum drawArrayMode, - const GLenum drawType, - const int numberOfElementsToDraw) { - glUseProgram(shaderProgram); - glBindVertexArray(vertexArrayObject); - glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0); - glBindVertexArray(0); +void doDrawElements(const unsigned int shaderProgram, + const unsigned int vertexArrayObject, + const GLenum drawArrayMode, const GLenum drawType, + const int numberOfElementsToDraw) { + glUseProgram(shaderProgram); + glBindVertexArray(vertexArrayObject); + glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0); + glBindVertexArray(0); } -void updateUniformColor( - const unsigned int shaderProgram, - const GLchar* uniformName) { - // update the uniform color - const float timeValue = glfwGetTime(); // retrieve running time - const float greenValue = - sin(timeValue) / 2.0f + 0.5f; - // vary the color from 0.0 to 1.0 using sin - // query the location of our uniform - const int vertexColorLocation = - glGetUniformLocation(shaderProgram, uniformName); - // if glGetUniformLocation returns -1 it could not find the location - if (vertexColorLocation != -1) { - glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); - /* we set the uniform value using glUniform4f - 4f means that it expects 4 floats - few of possible postfixes: - f: the function expects a float as its value. - i: the function expects an int as its value. - ui: the function expects an unsigned int as its value. - 3f: the function expects 3 floats as its value. - fv: the function expects a float vector/array as its value. - */ - } +void updateUniformColor(const unsigned int shaderProgram, + const GLchar* uniformName) { + // update the uniform color + const float timeValue = glfwGetTime(); // retrieve running time + const float greenValue = sin(timeValue) / 2.0f + 0.5f; + // vary the color from 0.0 to 1.0 using sin + // query the location of our uniform + const int vertexColorLocation = + glGetUniformLocation(shaderProgram, uniformName); + // if glGetUniformLocation returns -1 it could not find the location + if (vertexColorLocation != -1) { + glUniform4f(vertexColorLocation, 0.0f, greenValue, 0.0f, 1.0f); + /* we set the uniform value using glUniform4f + 4f means that it expects 4 floats + few of possible postfixes: + f: the function expects a float as its value. + i: the function expects an int as its value. + ui: the function expects an unsigned int as its value. + 3f: the function expects 3 floats as its value. + fv: the function expects a float vector/array as its value. + */ + } } -void doDrawArrays( - const unsigned int shaderProgram, - const unsigned int vertexArrayObject, - const GLenum drawArrayMode, - const int firstIndex, - const unsigned int numberOfIndicesToBeRendered) { - // use shader program to render an object - glUseProgram(shaderProgram); - updateUniformColor(shaderProgram, "ourColor"); - glBindVertexArray(vertexArrayObject); - // From left: - // primitive type we want to draw - // starting index of vertex array - // how many vertices we want to draw - glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered); +void doDrawArrays(const unsigned int shaderProgram, + const unsigned int vertexArrayObject, + const GLenum drawArrayMode, const int firstIndex, + const unsigned int numberOfIndicesToBeRendered) { + // use shader program to render an object + glUseProgram(shaderProgram); + updateUniformColor(shaderProgram, "ourColor"); + glBindVertexArray(vertexArrayObject); + // From left: + // primitive type we want to draw + // starting index of vertex array + // how many vertices we want to draw + glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered); } #endif // #ifndef DRAW_CPP diff --git a/Engine/engine/draw.hpp b/Engine/engine/draw.hpp index 95dc656..f50b282 100644 --- a/Engine/engine/draw.hpp +++ b/Engine/engine/draw.hpp @@ -1,22 +1,24 @@ // "Copyright [2023] " #ifndef ENGINE_ENGINE_DRAW_HPP_ #define ENGINE_ENGINE_DRAW_HPP_ -#include #include +#include + #include + #include "./constants.hpp" struct offsetsStruct { - offsetsStruct(): xOffset(0), yOffset(0), zOffset(0) { } - float xOffset, yOffset, zOffset; + offsetsStruct() : xOffset(0), yOffset(0), zOffset(0) {} + float xOffset, yOffset, zOffset; }; struct drawFigureReturn { - drawFigureReturn(): success(-1), VAO(0), VBO(0), EBO(0) { } - int success; - unsigned int VAO; - unsigned int VBO; - unsigned int EBO; + drawFigureReturn() : success(-1), VAO(0), VBO(0), EBO(0) {} + int success; + unsigned int VAO; + unsigned int VBO; + unsigned int EBO; }; drawFigureReturn drawFigure(const int whatToDraw); @@ -28,43 +30,34 @@ drawFigureReturn drawSquare( const size_t squareIndicesSize = constants::SQUARE_INDICES_SIZE, const char* vertexShaderSource = constants::VERTEX_SHADER_SOURCE, const char* fragmentShaderSource = constants::FRAGMENT_SHADER_SOURCE, - const bool colorIncluded = false, - const bool textureIncluded = false, + const bool colorIncluded = false, const bool textureIncluded = false, const offsetsStruct offsets = offsetsStruct()); -void doDrawElements( - const unsigned int shaderProgram, - const unsigned int vertexArrayObject, - const GLenum drawArrayMode, - const GLenum drawType, - const int numberOfElementsToDraw); +void doDrawElements(const unsigned int shaderProgram, + const unsigned int vertexArrayObject, + const GLenum drawArrayMode, const GLenum drawType, + const int numberOfElementsToDraw); drawFigureReturn drawTriangleClass( - const float triangleVertices[], - const size_t triangleVerticesSize, - const char* vertexPath, - const char* fragmentPath, - const bool colorIncluded = false, - const bool textureIncluded = false, + const char* vertexPath, const char* fragmentPath, + const float triangleVertices[], const size_t triangleVerticesSize, + const bool colorIncluded = false, const bool textureIncluded = false, const offsetsStruct offsets = offsetsStruct()); -void updateUniformColor( - const unsigned int shaderProgram, - const GLchar* uniformName); +void updateUniformColor(const unsigned int shaderProgram, + const GLchar* uniformName); -void doDrawArrays( - const unsigned int shaderProgram, - const unsigned int vertexArrayObject, - const GLenum drawArrayMode, - const int firstIndex, - const unsigned int numberOfIndicesToBeRendered); +void doDrawArrays(const unsigned int shaderProgram, + const unsigned int vertexArrayObject, + const GLenum drawArrayMode, const int firstIndex, + const unsigned int numberOfIndicesToBeRendered); drawFigureReturn drawDebilMode( - const char* vertexPath, - const char* fragmentPath, - const float vertices[], - const size_t verticesSize, - const unsigned int indices[], - const size_t indicesSize); + const char* vertexPath = constants::VERTEX_SHADER_SOURCE_FILENAME, + const char* fragmentPath = constants::FRAGMENT_SHADER_SOURCE_FILENAME, + const float vertices[] = constants::TRIANGLE_VERTICES, + const size_t verticesSize = constants::TRIANGLE_VERTICES_SIZE, + const unsigned int indices[] = constants::TEXTURE_INDICES, + const size_t indicesSize = constants::TEXTURE_INDICES_SIZE); #endif // ENGINE_ENGINE_DRAW_HPP_ diff --git a/Engine/engine/match b/Engine/engine/match index 9976563..556d71c 100755 Binary files a/Engine/engine/match and b/Engine/engine/match differ diff --git a/Engine/engine/match.cpp b/Engine/engine/match.cpp index 695e1cd..85ae73d 100644 --- a/Engine/engine/match.cpp +++ b/Engine/engine/match.cpp @@ -1,8 +1,8 @@ // "Copyright [2023] " #ifndef MAIN_CPP #define MAIN_CPP -#include #include +#include #include #include @@ -11,21 +11,20 @@ // https://arvid.io/2018/06/30/on-cxx-random-number-generator-quality/ // whenever, if ever I feel like it -#include "./constants.hpp" #include "./beforeRender.hpp" +#include "./constants.hpp" #include "./misc.hpp" #include "./renderLoop.hpp" int main() { - // changed in glfwWindowShouldClose - GLFWwindow *window = prepareForRender(); - if (window == nullptr) - return -1; - renderLoop(window); + // changed in glfwWindowShouldClose + GLFWwindow *window = prepareForRender(); + if (window == nullptr) return -1; + renderLoop(window); - // clean GLFW resources - glfwTerminate(); - return 0; + // clean GLFW resources + glfwTerminate(); + return 0; } #endif // #ifndef MAIN_CPP diff --git a/Engine/engine/misc.cpp b/Engine/engine/misc.cpp index 7d9996e..a372e44 100644 --- a/Engine/engine/misc.cpp +++ b/Engine/engine/misc.cpp @@ -1,10 +1,9 @@ // "Copyright [2023] " #ifndef MISC_CPP #define MISC_CPP -#include #include "./misc.hpp" -void print(const std::string s) { - std::cout << s << std::endl; -} + +#include +void print(const std::string s) { std::cout << s << std::endl; } #endif diff --git a/Engine/engine/renderLoop.cpp b/Engine/engine/renderLoop.cpp index 1d0174a..baaa7d2 100644 --- a/Engine/engine/renderLoop.cpp +++ b/Engine/engine/renderLoop.cpp @@ -1,229 +1,215 @@ // "Copyright [2023] " #ifndef RENDER_LOOP_CPP #define RENDER_LOOP_CPP -#include -#include -#include #include "./renderLoop.hpp" -#include "./draw.hpp" -#include "./shaders.hpp" + +#include +#include + +#include + #include "./constants.hpp" +#include "./draw.hpp" #include "./misc.hpp" +#include "./shaders.hpp" int processInput(GLFWwindow *window, const int whatToDraw) { - static bool lockedLeft = false; - static bool lockedRight = false; - const bool PRESSED_CHANGE_LEFT = - (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS); - const bool PRESSED_CHANGE_RIGHT = - (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS); + static bool lockedLeft = false; + static bool lockedRight = false; + const bool PRESSED_CHANGE_LEFT = + (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS); + const bool PRESSED_CHANGE_RIGHT = + (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS); - // glfwGetKey takes window and key as - // an input and checks is currently being pressed - // if the user pressed escape we close window - if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) - glfwSetWindowShouldClose(window, true); - if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) - glfwSetWindowShouldClose(window, true); - if ( !PRESSED_CHANGE_LEFT ) - lockedLeft = 0; - if ( !PRESSED_CHANGE_RIGHT ) - lockedRight = 0; - if ( PRESSED_CHANGE_RIGHT && lockedRight == 0 ) { - lockedRight = 1; - return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1); - } - if ( PRESSED_CHANGE_LEFT && lockedLeft == 0 ) { - lockedLeft = 1; - return (whatToDraw == 0 ? constants::MAX_DRAW_CALL : whatToDraw - 1); - } - return whatToDraw; + // glfwGetKey takes window and key as + // an input and checks is currently being pressed + // if the user pressed escape we close window + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); + if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) + glfwSetWindowShouldClose(window, true); + if (!PRESSED_CHANGE_LEFT) lockedLeft = 0; + if (!PRESSED_CHANGE_RIGHT) lockedRight = 0; + if (PRESSED_CHANGE_RIGHT && lockedRight == 0) { + lockedRight = 1; + return (whatToDraw == constants::MAX_DRAW_CALL ? 0 : whatToDraw + 1); + } + if (PRESSED_CHANGE_LEFT && lockedLeft == 0) { + lockedLeft = 1; + return (whatToDraw == 0 ? constants::MAX_DRAW_CALL : whatToDraw - 1); + } + return whatToDraw; } // https://stackoverflow.com/a/25680092 -unsigned int copyVerticesMemory( - const float vertices[], - const size_t sizeOfVertices, - const GLenum boundBufferTarget) { - // stores vertices in gpu memory - unsigned int vertexBufferObject; - // this is open gl object so we refer - // it by its ID generated here and stored in vertexBufferObject variable - glGenBuffers(1, &vertexBufferObject); - // buffer type of vertex buffer object is GL_ARRAY_BUFFER - glBindBuffer(boundBufferTarget, vertexBufferObject); - // now whenever we change GL_ARRAY_BUFFER - // we change bound buffer vertexBufferObject +unsigned int copyVerticesMemory(const float vertices[], + const size_t sizeOfVertices, + const GLenum boundBufferTarget) { + // stores vertices in gpu memory + unsigned int vertexBufferObject; + // this is open gl object so we refer + // it by its ID generated here and stored in vertexBufferObject variable + glGenBuffers(1, &vertexBufferObject); + // buffer type of vertex buffer object is GL_ARRAY_BUFFER + glBindBuffer(boundBufferTarget, vertexBufferObject); + // now whenever we change GL_ARRAY_BUFFER + // we change bound buffer vertexBufferObject - /* we copy vertex data into buffer memory - GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few times. - GL_STATIC_DRAW: the data is set only once and used many times. - GL_DYNAMIC_DRAW: the data is changed a lot and used many times. - */ - glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); - return vertexBufferObject; + /* we copy vertex data into buffer memory + GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few + times. GL_STATIC_DRAW: the data is set only once and used many times. + GL_DYNAMIC_DRAW: the data is changed a lot and used many times. + */ + glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); + return vertexBufferObject; } -unsigned int copyVerticesMemory( - const unsigned int vertices[], - const size_t sizeOfVertices, - const GLenum boundBufferTarget) { - // stores vertices in gpu memory - unsigned int vertexBufferObject; - // this is open gl object so we refer it - // by its ID generated here and stored in vertexBufferObject variable - glGenBuffers(1, &vertexBufferObject); - // buffer type of vertex buffer object is GL_ARRAY_BUFFER - glBindBuffer( - boundBufferTarget, - vertexBufferObject); - // now whenever we change GL_ARRAY_BUFFER - // we change bound buffer vertexBufferObject +unsigned int copyVerticesMemory(const unsigned int vertices[], + const size_t sizeOfVertices, + const GLenum boundBufferTarget) { + // stores vertices in gpu memory + unsigned int vertexBufferObject; + // this is open gl object so we refer it + // by its ID generated here and stored in vertexBufferObject variable + glGenBuffers(1, &vertexBufferObject); + // buffer type of vertex buffer object is GL_ARRAY_BUFFER + glBindBuffer(boundBufferTarget, vertexBufferObject); + // now whenever we change GL_ARRAY_BUFFER + // we change bound buffer vertexBufferObject - /* we copy vertex data into buffer memory - GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few times. - GL_STATIC_DRAW: the data is set only once and used many times. - GL_DYNAMIC_DRAW: the data is changed a lot and used many times. - */ - glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); - return vertexBufferObject; + /* we copy vertex data into buffer memory + GL_STREAM_DRAW: the data is set only once and used by the GPU at most a few + times. GL_STATIC_DRAW: the data is set only once and used many times. + GL_DYNAMIC_DRAW: the data is changed a lot and used many times. + */ + glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); + return vertexBufferObject; } -void configureVertexAttribute( - const bool colorIncluded, - const bool textureIncluded) { - /* specify how OGL interprets vertex data - From left: - which vertex attribute we configure (from shader source code layout (location = 0)) - size of vertex attribute (we use vec3 so it contains 3 values) - type of data of which vec consists of - should data be normalized, (useful when we use integer data) - space between vertex attributes, each position data is 3 times the size of float - offset of where position data begins in buffer - see: https://learnopengl.com/img/getting-started/vertex_attribute_pointer.png - vertex attribute data take data from memory managed by VBO bound to GL_ARRAY_BUFFER */ - if (!colorIncluded) { - glVertexAttribPointer( - 0, 3, GL_FLOAT, GL_FALSE, - 3 * sizeof(float), reinterpret_cast(0)); - // enable vertex attribute - glEnableVertexAttribArray(0); - return; - } +void configureVertexAttribute(const bool colorIncluded, + const bool textureIncluded) { + /* specify how OGL interprets vertex data + From left: + which vertex attribute we configure (from shader source code layout (location + = 0)) size of vertex attribute (we use vec3 so it contains 3 values) type of + data of which vec consists of should data be normalized, (useful when we use + integer data) space between vertex attributes, each position data is 3 times + the size of float offset of where position data begins in buffer see: + https://learnopengl.com/img/getting-started/vertex_attribute_pointer.png + vertex attribute data take data from memory managed by VBO bound to + GL_ARRAY_BUFFER */ + if (!colorIncluded) { + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), + reinterpret_cast(0)); + // enable vertex attribute + glEnableVertexAttribArray(0); + return; + } + /* we change attribute location, color values have size of 3 floats + we do not normalize values, in order to get the next attribute value in data + array we need to move 6 floats, (3 for position and 3 for color), we also need + to specify an offset, first we have position then after 3 floats we have color + https://learnopengl.com/img/getting-started/vertex_attribute_pointer_interleaved.png +*/ + if (colorIncluded && !textureIncluded) { + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), + reinterpret_cast(0)); + // enable vertex attribute + glEnableVertexAttribArray(0); + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), + reinterpret_cast(3 * sizeof(float))); + // enable vertex attribute + glEnableVertexAttribArray(1); + return; + } - /* we change attribute location, color values have size of 3 floats - we do not normalize values, in order to get the next attribute value in data array we need to move 6 floats, (3 for position and 3 for color), we also need to specify an offset, first we have position then after 3 floats we have color - https://learnopengl.com/img/getting-started/vertex_attribute_pointer_interleaved.png */ - if (colorIncluded && !textureIncluded) { - glVertexAttribPointer( - 0, 3, GL_FLOAT, GL_FALSE, - 6 * sizeof(float), reinterpret_cast(0)); - // enable vertex attribute - glEnableVertexAttribArray(0); + // Since we've added an extra vertex + // attribute we again have to notify OpenGL of the new vertex format + // this time for textures we add just + // 2 more attributes and not 3 as with color + if (textureIncluded) { + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), + reinterpret_cast(0)); + // enable vertex attribute + glEnableVertexAttribArray(0); - glVertexAttribPointer( - 1, 3, GL_FLOAT, GL_FALSE, - 6 * sizeof(float), reinterpret_cast(3* sizeof(float))); - // enable vertex attribute - glEnableVertexAttribArray(1); - return; - } + glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), + reinterpret_cast(3 * sizeof(float))); + // enable vertex attribute + glEnableVertexAttribArray(1); - // Since we've added an extra vertex - // attribute we again have to notify OpenGL of the new vertex format - // this time for textures we add just - // 2 more attributes and not 3 as with color - if (textureIncluded) { - glVertexAttribPointer( - 0, 3, GL_FLOAT, GL_FALSE, - 8 * sizeof(float), reinterpret_cast(0)); - // enable vertex attribute - glEnableVertexAttribArray(0); - - glVertexAttribPointer( - 1, 3, GL_FLOAT, GL_FALSE, - 8 * sizeof(float), reinterpret_cast(3* sizeof(float))); - // enable vertex attribute - glEnableVertexAttribArray(1); - - glVertexAttribPointer( - 2, 2, GL_FLOAT, GL_FALSE, - 8 * sizeof(float), reinterpret_cast(6 * sizeof(float))); - glEnableVertexAttribArray(2); - return; - } + glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), + reinterpret_cast(6 * sizeof(float))); + glEnableVertexAttribArray(2); + return; + } } unsigned int generateBindVAO() { - // vertex array object is used to draw objects by binding them to vao - // generate vao - unsigned int vertexArrayObject; - glGenVertexArrays(1, &vertexArrayObject); - // bind vao - glBindVertexArray(vertexArrayObject); - return vertexArrayObject; + // vertex array object is used to draw objects by binding them to vao + // generate vao + unsigned int vertexArrayObject; + glGenVertexArrays(1, &vertexArrayObject); + // bind vao + glBindVertexArray(vertexArrayObject); + return vertexArrayObject; } -void copyVerticesArray( - const unsigned int vertexBufferObject, - const float vertices[], - const size_t sizeOfVertices, - const GLenum boundBufferTarget) { - // copy vertices array in array useful for OGL - glBindBuffer(boundBufferTarget, vertexBufferObject); - glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); +void copyVerticesArray(const unsigned int vertexBufferObject, + const float vertices[], const size_t sizeOfVertices, + const GLenum boundBufferTarget) { + // copy vertices array in array useful for OGL + glBindBuffer(boundBufferTarget, vertexBufferObject); + glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); } int renderLoopInside(GLFWwindow *window, int whatToDraw) { - // input - whatToDraw = processInput(window, whatToDraw); - // We specify the color to clear the screen with - // RGB and alpha value - glClearColor( - constants::LEARN_OPEN_GL_COLOR.red, - constants::LEARN_OPEN_GL_COLOR.green, - constants::LEARN_OPEN_GL_COLOR.blue, - constants::LEARN_OPEN_GL_COLOR.alpha); - // There is - // GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT - glClear(GL_COLOR_BUFFER_BIT); + // input + whatToDraw = processInput(window, whatToDraw); + // We specify the color to clear the screen with + // RGB and alpha value + glClearColor(constants::LEARN_OPEN_GL_COLOR.red, + constants::LEARN_OPEN_GL_COLOR.green, + constants::LEARN_OPEN_GL_COLOR.blue, + constants::LEARN_OPEN_GL_COLOR.alpha); + // There is + // GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT and GL_STENCIL_BUFFER_BIT + glClear(GL_COLOR_BUFFER_BIT); + drawFigureReturn successAndVao = drawFigure(whatToDraw); + if (successAndVao.success == -1) { + print("error with drawing!"); + glfwSetWindowShouldClose(window, true); + } - drawFigureReturn successAndVao = drawFigure(whatToDraw); - if (successAndVao.success == -1) { - print("error with drawing!"); - glfwSetWindowShouldClose(window, true); - } + // swaps buffer containing color values of each pixel in window + // there is front buffer (final image) + // and back buffer (where all rendering commands draw to) + // when back buffer is ready we swap it + // with front buffer to eliminate flickering + glfwSwapBuffers(window); - // swaps buffer containing color values of each pixel in window - // there is front buffer (final image) - // and back buffer (where all rendering commands draw to) - // when back buffer is ready we swap it - // with front buffer to eliminate flickering - glfwSwapBuffers(window); + // glfwPollEvents checks if any event + // (like mouse/keyboard input was triggered), + // updates window state and calls functions + // (which we register via callback methods) + glfwPollEvents(); - // glfwPollEvents checks if any event - // (like mouse/keyboard input was triggered), - // updates window state and calls functions - // (which we register via callback methods) - glfwPollEvents(); + glDeleteVertexArrays(1, &successAndVao.VAO); + glDeleteBuffers(1, &successAndVao.VBO); + glDeleteBuffers(1, &successAndVao.EBO); - glDeleteVertexArrays(1, &successAndVao.VAO); - glDeleteBuffers(1, &successAndVao.VBO); - glDeleteBuffers(1, &successAndVao.EBO); - - - return whatToDraw; + return whatToDraw; } void renderLoop(GLFWwindow *window) { - int whatToDraw = 0; - // glfwWindowShouldClose checks if GLFW was instructed to close - while (!glfwWindowShouldClose(window)) { - whatToDraw = renderLoopInside(window, whatToDraw); - } + int whatToDraw = 0; + // glfwWindowShouldClose checks if GLFW was instructed to close + while (!glfwWindowShouldClose(window)) { + whatToDraw = renderLoopInside(window, whatToDraw); + } } #endif diff --git a/Engine/engine/renderLoop.hpp b/Engine/engine/renderLoop.hpp index ee5adab..abc2932 100644 --- a/Engine/engine/renderLoop.hpp +++ b/Engine/engine/renderLoop.hpp @@ -2,33 +2,29 @@ #ifndef ENGINE_ENGINE_RENDERLOOP_HPP_ #define ENGINE_ENGINE_RENDERLOOP_HPP_ #include + #include void renderLoop(GLFWwindow *window); int renderLoopInside(GLFWwindow *window, int whatToDraw); -void copyVerticesArray( - unsigned int vertexBufferObject, - const float vertices[], - const size_t sizeOfVertices, - const GLenum boundBufferTarget); +void copyVerticesArray(unsigned int vertexBufferObject, const float vertices[], + const size_t sizeOfVertices, + const GLenum boundBufferTarget); unsigned int generateBindVAO(); -void configureVertexAttribute( - const bool colorIncluded, - const bool textureIncluded); +void configureVertexAttribute(const bool colorIncluded, + const bool textureIncluded); int processInput(GLFWwindow *window, int whatToDraw); -unsigned int copyVerticesMemory( - const float vertices[], - const size_t sizeOfVertices, - const GLenum boundBufferTarget); +unsigned int copyVerticesMemory(const float vertices[], + const size_t sizeOfVertices, + const GLenum boundBufferTarget); -unsigned int copyVerticesMemory( - const unsigned int vertices[], - const size_t sizeOfVertices, - const GLenum boundBufferTarget); +unsigned int copyVerticesMemory(const unsigned int vertices[], + const size_t sizeOfVertices, + const GLenum boundBufferTarget); #endif // ENGINE_ENGINE_RENDERLOOP_HPP_ diff --git a/Engine/engine/shader.cpp b/Engine/engine/shader.cpp index 7fd9f87..a506214 100644 --- a/Engine/engine/shader.cpp +++ b/Engine/engine/shader.cpp @@ -2,113 +2,106 @@ #ifndef SHADER_CPP #define SHADER_CPP -#include - -#include -#include -#include -#include #include "./shader.hpp" +#include + +#include +#include +#include +#include + const std::string Shader::fileToShader(const std::string shaderPath) { - // 1. retrieve the vertex/fragment source code from filePath - std::string shaderCode; - std::ifstream shaderFile; - // ensure ifstream objects can throw exceptions: - shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); - try { - // open files - shaderFile.open(shaderPath); - std::stringstream shaderStream; - // read file's buffer contents into streams - shaderStream << shaderFile.rdbuf(); - // close file handlers - shaderFile.close(); - // convert stream into string - shaderCode = shaderStream.str(); - } - catch (std::ifstream::failure& e) { - std::cout << - "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " - << e.what() << std::endl; - } - return shaderCode; + // 1. retrieve the vertex/fragment source code from filePath + std::string shaderCode; + std::ifstream shaderFile; + // ensure ifstream objects can throw exceptions: + shaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + try { + // open files + shaderFile.open(shaderPath); + std::stringstream shaderStream; + // read file's buffer contents into streams + shaderStream << shaderFile.rdbuf(); + // close file handlers + shaderFile.close(); + // convert stream into string + shaderCode = shaderStream.str(); + } catch (std::ifstream::failure &e) { + std::cout << "ERROR::SHADER::FILE_NOT_SUCCESSFULLY_READ: " << e.what() + << std::endl; + } + return shaderCode; } -Shader::Shader(const std::string vertexPath, const std::string fragmentPath) { - const std::string vshaderCodeString = this -> fileToShader(vertexPath); - - const std::string fShaderCodeString = - this -> fileToShader(fragmentPath); +Shader::Shader(const std::string vertexPath, const std::string fragmentPath) { + const std::string vshaderCodeString = this->fileToShader(vertexPath); + const std::string fShaderCodeString = this->fileToShader(fragmentPath); - const char* vShaderCode = vshaderCodeString.c_str(); - const char * fShaderCode = fShaderCodeString.c_str(); - // 2. compile shaders - unsigned int vertex, fragment; - // vertex shader - vertex = glCreateShader(GL_VERTEX_SHADER); - glShaderSource(vertex, 1, &vShaderCode, NULL); - glCompileShader(vertex); - checkCompileErrors(vertex, "VERTEX"); - // fragment Shader - fragment = glCreateShader(GL_FRAGMENT_SHADER); - glShaderSource(fragment, 1, &fShaderCode, NULL); - glCompileShader(fragment); - checkCompileErrors(fragment, "FRAGMENT"); - // shader Program - ID = glCreateProgram(); - glAttachShader(ID, vertex); - glAttachShader(ID, fragment); - glLinkProgram(ID); - checkCompileErrors(ID, "PROGRAM"); - // delete the shaders as they're linked - // into our program now and no longer necessary - glDeleteShader(vertex); - glDeleteShader(fragment); + const char *vShaderCode = vshaderCodeString.c_str(); + const char *fShaderCode = fShaderCodeString.c_str(); + // 2. compile shaders + unsigned int vertex, fragment; + // vertex shader + vertex = glCreateShader(GL_VERTEX_SHADER); + glShaderSource(vertex, 1, &vShaderCode, NULL); + glCompileShader(vertex); + checkCompileErrors(vertex, "VERTEX"); + // fragment Shader + fragment = glCreateShader(GL_FRAGMENT_SHADER); + glShaderSource(fragment, 1, &fShaderCode, NULL); + glCompileShader(fragment); + checkCompileErrors(fragment, "FRAGMENT"); + // shader Program + ID = glCreateProgram(); + glAttachShader(ID, vertex); + glAttachShader(ID, fragment); + glLinkProgram(ID); + checkCompileErrors(ID, "PROGRAM"); + // delete the shaders as they're linked + // into our program now and no longer necessary + glDeleteShader(vertex); + glDeleteShader(fragment); } -void Shader::use() { - glUseProgram(ID); -} +void Shader::use() { glUseProgram(ID); } void Shader::setBool(const std::string &name, bool value) const { - glUniform1i( - glGetUniformLocation(ID, name.c_str()), - reinterpret_cast(value)); + glUniform1i(glGetUniformLocation(ID, name.c_str()), int(value)); } void Shader::setInt(const std::string &name, int value) const { - glUniform1i(glGetUniformLocation(ID, name.c_str()), value); + glUniform1i(glGetUniformLocation(ID, name.c_str()), value); } void Shader::setFloat(const std::string &name, float value) const { - glUniform1f(glGetUniformLocation(ID, name.c_str()), value); + glUniform1f(glGetUniformLocation(ID, name.c_str()), value); } void Shader::checkCompileErrors(unsigned int shader, std::string type) { - int success; - char infoLog[1024]; - if (type != "PROGRAM") { - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (!success) { - glGetShaderInfoLog(shader, 1024, NULL, infoLog); - std::cout << - "ERROR::SHADER_COMPILATION_ERROR of type: " - << type << "\n" << infoLog << - "\n -- --------------------------------------------------- -- " - << std::endl; - } - } else { - glGetProgramiv(shader, GL_LINK_STATUS, &success); - if (!success) { - glGetProgramInfoLog(shader, 1024, NULL, infoLog); - std::cout << - "ERROR::PROGRAM_LINKING_ERROR of type: " - << type << "\n" << infoLog << - "\n -- --------------------------------------------------- -- " - << std::endl; - } + int success; + char infoLog[1024]; + if (type != "PROGRAM") { + glGetShaderiv(shader, GL_COMPILE_STATUS, &success); + if (!success) { + glGetShaderInfoLog(shader, 1024, NULL, infoLog); + std::cout + << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" + << infoLog + << "\n -- --------------------------------------------------- -- " + << std::endl; } + } else { + glGetProgramiv(shader, GL_LINK_STATUS, &success); + if (!success) { + glGetProgramInfoLog(shader, 1024, NULL, infoLog); + std::cout + << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" + << infoLog + << "\n -- --------------------------------------------------- -- " + << std::endl; + } + } } -#endif // SHADER_CPP \ No newline at end of file +#endif // SHADER_CPP diff --git a/Engine/engine/shader.hpp b/Engine/engine/shader.hpp index 122315d..41bfc82 100644 --- a/Engine/engine/shader.hpp +++ b/Engine/engine/shader.hpp @@ -4,38 +4,36 @@ #include -#include #include -#include #include +#include +#include class Shader { public: - unsigned int ID; - // constructor generates the shader on the fly - // ------------------------------------------------------------------------ - Shader(const std::string vertexPath, const std::string fragmentPath); - const std::string fileToShader(const std::string shaderPath); - // activate the shader - // ------------------------------------------------------------------------ - void use(); - // utility uniform functions - // ------------------------------------------------------------------------ - void setBool(const std::string &name, bool value) const; - // ------------------------------------------------------------------------ - void setInt(const std::string &name, int value) const; - // ------------------------------------------------------------------------ - void setFloat(const std::string &name, float value) const; + unsigned int ID; + // constructor generates the shader on the fly + // ------------------------------------------------------------------------ + Shader(const std::string vertexPath, const std::string fragmentPath); + const std::string fileToShader(const std::string shaderPath); + // activate the shader + // ------------------------------------------------------------------------ + void use(); + // utility uniform functions + // ------------------------------------------------------------------------ + void setBool(const std::string &name, bool value) const; + // ------------------------------------------------------------------------ + void setInt(const std::string &name, int value) const; + // ------------------------------------------------------------------------ + void setFloat(const std::string &name, float value) const; private: - // utility function for checking shader compilation/linking errors. - // ------------------------------------------------------------------------ - void checkCompileErrors(unsigned int shader, std::string type); + // utility function for checking shader compilation/linking errors. + // ------------------------------------------------------------------------ + void checkCompileErrors(unsigned int shader, std::string type); - void compileErrorsMessage( - const unsigned int shader, - const bool compilation, - const std::string type, - const std::string errorMessage); + void compileErrorsMessage(const unsigned int shader, const bool compilation, + const std::string type, + const std::string errorMessage); }; #endif // ENGINE_ENGINE_SHADER_HPP_ diff --git a/Engine/engine/shaders.cpp b/Engine/engine/shaders.cpp index 23a2af5..a69fe2d 100644 --- a/Engine/engine/shaders.cpp +++ b/Engine/engine/shaders.cpp @@ -1,103 +1,98 @@ // "Copyright [2023] " #ifndef SHADERS_CPP #define SHADERS_CPP -#include -#include #include "./shaders.hpp" + +#include +#include + #include "./constants.hpp" #include "./misc.hpp" -unsigned int linkShaderObjectsShaderProgram( - const unsigned int vertexShaders, - const unsigned int fragmentShader) { - // link shader objects into shader program - // will store shader program id - // creates program - const unsigned int shaderProgram = glCreateProgram(); +unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, + const unsigned int fragmentShader) { + // link shader objects into shader program + // will store shader program id + // creates program + const unsigned int shaderProgram = glCreateProgram(); - // attachShaders - glAttachShader(shaderProgram, vertexShaders); - glAttachShader(shaderProgram, fragmentShader); + // attachShaders + glAttachShader(shaderProgram, vertexShaders); + glAttachShader(shaderProgram, fragmentShader); - // link shaders - glLinkProgram(shaderProgram); - if (!shaderSuccessful(shaderProgram, false)) - return 0; + // link shaders + glLinkProgram(shaderProgram); + if (!shaderSuccessful(shaderProgram, false)) return 0; - // activate program - // after that every shader and rendering call will use this program object - glUseProgram(shaderProgram); + // activate program + // after that every shader and rendering call will use this program object + glUseProgram(shaderProgram); - // delete shaders - // (they are linked into shaderProgram and we do not need them anymore) - glDeleteShader(vertexShaders); - glDeleteShader(fragmentShader); - if (shaderProgram == 0) - print("Shader Program Linking Failed"); - return shaderProgram; + // delete shaders + // (they are linked into shaderProgram and we do not need them anymore) + glDeleteShader(vertexShaders); + glDeleteShader(fragmentShader); + if (shaderProgram == 0) print("Shader Program Linking Failed"); + return shaderProgram; } -unsigned int compileShader( - const GLenum shaderType, - const char *shaderSource) { - // we create vertex shader and assign its id to shader variable - const unsigned int shaderID = glCreateShader(shaderType); +unsigned int compileShader(const GLenum shaderType, const char* shaderSource) { + // we create vertex shader and assign its id to shader variable + const unsigned int shaderID = glCreateShader(shaderType); - // attach shader source code to shader object - // from left: shader object to compile, - // how many strings as source code, actual source code - // (we leave the 4th as nullptr) - glShaderSource(shaderID, 1, &shaderSource, nullptr); - // compile shader - glCompileShader(shaderID); - if (!shaderSuccessful(shaderID, true)) - return 0; - return shaderID; + // attach shader source code to shader object + // from left: shader object to compile, + // how many strings as source code, actual source code + // (we leave the 4th as nullptr) + glShaderSource(shaderID, 1, &shaderSource, nullptr); + // compile shader + glCompileShader(shaderID); + if (!shaderSuccessful(shaderID, true)) return 0; + return shaderID; } std::pair compileShaders( - const char* vertexShaderSource, - const char* fragmentShaderSource) { - const unsigned int vertexShader = - compileShader(GL_VERTEX_SHADER, vertexShaderSource); + const char* vertexShaderSource, const char* fragmentShaderSource) { + const unsigned int vertexShader = + compileShader(GL_VERTEX_SHADER, vertexShaderSource); - if (vertexShader == 0) { - print("Vertex Shader Compilation Failed"); - return std::make_pair(0, 0); - } + if (vertexShader == 0) { + print("Vertex Shader Compilation Failed"); + return std::make_pair(0, 0); + } - const unsigned int fragmentShader = - compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource); + const unsigned int fragmentShader = + compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource); - if (fragmentShader == 0) { - print("Fragment Shader Compilation Failed"); - return std::make_pair(0, 0); - } - return std::make_pair(vertexShader, fragmentShader); + if (fragmentShader == 0) { + print("Fragment Shader Compilation Failed"); + return std::make_pair(0, 0); + } + return std::make_pair(vertexShader, fragmentShader); } void shaderFailedMessage(const unsigned int shader, const bool compilation) { - char infoLog[512]; - const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]); - compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, nullptr, infoLog) - : glGetProgramInfoLog(shader, sizeOfInfoLog, nullptr, infoLog); - std::cout << "ERROR vertex shader compilation failed \n" - << infoLog << std::endl; + char infoLog[512]; + const size_t sizeOfInfoLog = sizeof(infoLog) / sizeof(infoLog[0]); + compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, nullptr, infoLog) + : glGetProgramInfoLog(shader, sizeOfInfoLog, nullptr, infoLog); + std::cout << "ERROR vertex shader compilation failed \n" + << infoLog << std::endl; } int shaderSuccessful(const unsigned int shader, const bool compilation) { - // check if compilation was successful - // int because glGetShaderiv requires int - int success; - // here we store info about compilation - // check if compilation was successful - compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success) - : glGetProgramiv(shader, GL_LINK_STATUS, &success); - // if not display compilation log - if (!success) { - shaderFailedMessage(shader, compilation); - } - return success; + // check if compilation was successful + // int because glGetShaderiv requires int + int success; + // here we store info about compilation + // check if compilation was successful + compilation ? glGetShaderiv(shader, GL_COMPILE_STATUS, &success) + : glGetProgramiv(shader, GL_LINK_STATUS, &success); + // if not display compilation log + if (!success) { + shaderFailedMessage(shader, compilation); + } + return success; } #endif diff --git a/Engine/engine/shaders.hpp b/Engine/engine/shaders.hpp index 3a95690..e90fc83 100644 --- a/Engine/engine/shaders.hpp +++ b/Engine/engine/shaders.hpp @@ -1,20 +1,19 @@ // "Copyright [2023] " #ifndef ENGINE_ENGINE_SHADERS_HPP_ #define ENGINE_ENGINE_SHADERS_HPP_ -#include #include +#include + #include #include -unsigned int linkShaderObjectsShaderProgram( - const unsigned int vertexShaders, - const unsigned int fragmentShader); +unsigned int linkShaderObjectsShaderProgram(const unsigned int vertexShaders, + const unsigned int fragmentShader); std::pair compileShaders( - const char* vertexShaderSource, - const char* fragmentShaderSource); + const char* vertexShaderSource, const char* fragmentShaderSource); -unsigned int compileShader(const GLenum shaderType, const char *shaderSource); +unsigned int compileShader(const GLenum shaderType, const char* shaderSource); int shaderCompilationSuccessful(const unsigned int shader); int shaderProgramLinkingSuccessful(const unsigned int shaderProgram); int shaderSuccessful(const unsigned int shader, const bool compilation); diff --git a/Engine/engine/textures.cpp b/Engine/engine/textures.cpp index 274ef7c..099385a 100644 --- a/Engine/engine/textures.cpp +++ b/Engine/engine/textures.cpp @@ -1,138 +1,158 @@ // "Copyright [2023] " #ifndef TEXTURES_CPP #define TEXTURES_CPP -#include -#include -#include "stb_image.h" #include "textures.hpp" -void setTextureSCoordinate(const GLenum textureTarget, const GLint sCoordinateOption, const float* borderColor) -{ - // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml - // first argument is texture target (by default we work with 2D textures so it will be GL_TEXTURE_2D) - // second argument is what option we want to set and for which texture axis (s, t or r) - // Third argument is texture wrapping mode - glTexParameteri(textureTarget, GL_TEXTURE_WRAP_S, sCoordinateOption); - if(sCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) - { - // if we use GL_CLAMP_TO_BORDER all of the space that is not occupied by texture will be occupied by color - // we pass the color we want to use in float array (RGB + transparency) - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); +#include +#include + +#include "stb_image.h" + +void setTextureSCoordinate(const GLenum textureTarget, + const GLint sCoordinateOption, + const float* borderColor) { + // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml + // first argument is texture target (by default we work with 2D textures so it + // will be GL_TEXTURE_2D) second argument is what option we want to set and + // for which texture axis (s, t or r) Third argument is texture wrapping mode + glTexParameteri(textureTarget, GL_TEXTURE_WRAP_S, sCoordinateOption); + if (sCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) { + // if we use GL_CLAMP_TO_BORDER all of the space that is not occupied by + // texture will be occupied by color we pass the color we want to use in + // float array (RGB + transparency) + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); + } +} + +void setTextureTCoordinate(const GLenum textureTarget, + const GLint tCoordinateOption, + const float* borderColor) { + // for comments concerning glTexParameteri and glTexParameterfv go to + // setTextureSCoordinate function + if (textureTarget == GL_TEXTURE_2D || textureTarget == GL_TEXTURE_3D) { + glTexParameteri(textureTarget, GL_TEXTURE_WRAP_T, tCoordinateOption); + if (tCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) { + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); } + } } -void setTextureTCoordinate(const GLenum textureTarget, const GLint tCoordinateOption, const float* borderColor) -{ - // for comments concerning glTexParameteri and glTexParameterfv go to setTextureSCoordinate function - if(textureTarget == GL_TEXTURE_2D || textureTarget == GL_TEXTURE_3D) - { - glTexParameteri(textureTarget, GL_TEXTURE_WRAP_T, tCoordinateOption); - if(tCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) - { - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); - } +void setTextureRCoordinate(const GLenum textureTarget, + const GLint rCoordinateOption, + const float* borderColor) { + // for comments concerning glTexParameteri and glTexParameterfv go to + // setTextureSCoordinate function + if (textureTarget == GL_TEXTURE_3D) { + glTexParameteri(textureTarget, GL_TEXTURE_WRAP_R, rCoordinateOption); + if (rCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) { + glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); } + } } -void setTextureRCoordinate(const GLenum textureTarget, const GLint rCoordinateOption, const float* borderColor) -{ - // for comments concerning glTexParameteri and glTexParameterfv go to setTextureSCoordinate function - if(textureTarget == GL_TEXTURE_3D) - { - glTexParameteri(textureTarget, GL_TEXTURE_WRAP_R, rCoordinateOption); - if(rCoordinateOption == GL_CLAMP_TO_BORDER && borderColor != NULL) - { - glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor); - } - } +// Coordinate options can be: GL_REPEAT, GL_MIRRORED_REPEAT, GL_CLAMP_TO_EDGE, +// GL_CLAMP_TO_BORDER s t r are equivalent to x y z +void setTextureParametersINT(const GLenum textureTarget, + const GLint sCoordinateOption, + const GLint tCoordinateOption, + const GLint rCoordinateOption, + const float* borderColor) { + setTextureSCoordinate(textureTarget, sCoordinateOption, borderColor); + setTextureTCoordinate(textureTarget, tCoordinateOption, borderColor); + setTextureRCoordinate(textureTarget, rCoordinateOption, borderColor); } -// Coordinate options can be: GL_REPEAT, GL_MIRRORED_REPEAT, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_BORDER -// s t r are equivalent to x y z -void setTextureParametersINT(const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor) -{ - setTextureSCoordinate(textureTarget, sCoordinateOption, borderColor); - setTextureTCoordinate(textureTarget, tCoordinateOption, borderColor); - setTextureRCoordinate(textureTarget, rCoordinateOption, borderColor); +void setTextureFilteringAndMipMap(const GLenum textureTarget, + const GLenum filterType, + const GLint textureFilteringMethod, + const GLint mipMapFilteringMethod) { + // mipMapFilteringMethod can be equal to: GL_NEAREST_MIPMAP_NEAREST, + // GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR + // NEAREST_MIPMAP - take nearest mipmap to match pixel size + // LINEAR_MIPMAP - interpolate between two mipmaps that match the size of a + // pixel NEAREST (at the end) - samples texture based on nearest neighbor + // interpolation LINEAR (at the end) - samples texture using linear + // interpolation setting anything else than GL_TEXTURE_MIN_FILTER as a second + // parameter results in GL_INVALID_ENUM error + glTexParameteri(textureTarget, GL_TEXTURE_MIN_FILTER, mipMapFilteringMethod); + // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml + // first argument is texture target (by default we work with 2D textures so it + // will be GL_TEXTURE_2D) second argument is specifying whether this filter + // works for magnifying textures GL_TEXTURE_MAG_FILTER or minifying + // GL_TEXTURE_MIN_FILTER third argument is how we should magnify/minify + // textures, whether we should choose the color of pixel closest to the + // coordinates GL_NEAREST (default) or should we blend the neighboring pixels + // and choose blended color GL_LINEAR + glTexParameteri(textureTarget, filterType, textureFilteringMethod); } -void setTextureFilteringAndMipMap(const GLenum textureTarget, const GLenum filterType, const GLint textureFilteringMethod, const GLint mipMapFilteringMethod) -{ - // mipMapFilteringMethod can be equal to: GL_NEAREST_MIPMAP_NEAREST, GL_LINEAR_MIPMAP_NEAREST, GL_NEAREST_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR - // NEAREST_MIPMAP - take nearest mipmap to match pixel size - // LINEAR_MIPMAP - interpolate between two mipmaps that match the size of a pixel - // NEAREST (at the end) - samples texture based on nearest neighbor interpolation - // LINEAR (at the end) - samples texture using linear interpolation - // setting anything else than GL_TEXTURE_MIN_FILTER as a second parameter results in GL_INVALID_ENUM error - glTexParameteri(textureTarget, GL_TEXTURE_MIN_FILTER, mipMapFilteringMethod); - // https://registry.khronos.org/OpenGL-Refpages/gl4/html/glTexParameter.xhtml - // first argument is texture target (by default we work with 2D textures so it will be GL_TEXTURE_2D) - // second argument is specifying whether this filter works for magnifying textures GL_TEXTURE_MAG_FILTER or minifying GL_TEXTURE_MIN_FILTER - // third argument is how we should magnify/minify textures, whether we should choose the color of pixel closest to the coordinates GL_NEAREST (default) - // or should we blend the neighboring pixels and choose blended color GL_LINEAR - glTexParameteri(textureTarget, filterType, textureFilteringMethod); +unsigned int generateAndBindTexture(const GLenum textureTarget, + const GLsizei numberOfTextures) { + unsigned int texture; + // first argument is how many textures we want to generate + // second is an array of unsigned int in which we will store id of those + // textures + glGenTextures(numberOfTextures, &texture); + // then we bind texture + glBindTexture(textureTarget, texture); + return texture; } - -unsigned int generateAndBindTexture(const GLenum textureTarget, const GLsizei numberOfTextures ) { - unsigned int texture; - // first argument is how many textures we want to generate - // second is an array of unsigned int in which we will store id of those textures - glGenTextures(numberOfTextures, &texture); - // then we bind texture - glBindTexture(textureTarget, texture); +unsigned int loadAndBindTextureFile(unsigned int texture, + const char* texturePath, bool flipImage) { + int textureWidth, textureHeight, colorChannels; + stbi_set_flip_vertically_on_load(flipImage); + unsigned char* data = stbi_load(texturePath, &textureWidth, &textureHeight, + &colorChannels, STBI_rgb_alpha); + if (data) { + std::cout << "textureWidth " << textureWidth << " textureHeight " + << textureHeight << " colorChannels " << colorChannels + << std::endl; + // generate texture + /* + first argument is texture target, setting it to GL_TEXTURE_2D will only + affect 2D targets and not 1D or 3D 2nd is mipmap level for which to + create texture, base level is 0 3rd is in what format we want to store + texture (our image has only rgb values so rgb) 4th and 5th are the width + and height of texture 6th argument is always 0 (legacy, its called border + parameter) 7th and 8th are the format and datatype of source image, we + store the image data as chars (bytes) so we pass that 9th last argument + is actual data of the texture + */ + glPixelStorei(GL_UNPACK_ALIGNMENT, 1); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, + GL_RGBA, GL_UNSIGNED_BYTE, data); + glGenerateMipmap(GL_TEXTURE_2D); + // after generating texture, lets free the memory from image + stbi_image_free(data); return texture; + } else { + std::cout << "Failed to load texture! " << std::endl; + return -1; + } } -unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePath, bool flipImage) { - int textureWidth, textureHeight, colorChannels; - stbi_set_flip_vertically_on_load(flipImage); - unsigned char *data = stbi_load(texturePath, &textureWidth, &textureHeight, &colorChannels, STBI_rgb_alpha); - if(data) { - std::cout << "textureWidth " << textureWidth << " textureHeight " << textureHeight << " colorChannels " << colorChannels << std::endl; - // generate texture - /* - first argument is texture target, setting it to GL_TEXTURE_2D will only affect 2D targets and not 1D or 3D - 2nd is mipmap level for which to create texture, base level is 0 - 3rd is in what format we want to store texture (our image has only rgb values so rgb) - 4th and 5th are the width and height of texture - 6th argument is always 0 (legacy, its called border parameter) - 7th and 8th are the format and datatype of source image, we store the image data as chars (bytes) so we pass that - 9th last argument is actual data of the texture - */ - glPixelStorei(GL_UNPACK_ALIGNMENT, 1); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, textureWidth, textureHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); - glGenerateMipmap(GL_TEXTURE_2D); - // after generating texture, lets free the memory from image - stbi_image_free(data); - return texture; - } - else - { - std::cout << "Failed to load texture! " << std::endl; - return -1; - } -} - -// const char* texturePath, int textureWidth, int textureHeight, int colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor +// const char* texturePath, int textureWidth, int textureHeight, int +// colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, +// const GLint tCoordinateOption, const GLint rCoordinateOption, const float* +// borderColor unsigned int loadAndCreateTexture(const char* texturePath, bool flipImage) { - // load and create a texture - // ------------------------- - unsigned int texture = generateAndBindTexture(GL_TEXTURE_2D, 1); - setTextureParametersINT(GL_TEXTURE_2D, GL_REPEAT, GL_REPEAT); + // load and create a texture + // ------------------------- + unsigned int texture = generateAndBindTexture(GL_TEXTURE_2D, 1); + setTextureParametersINT(GL_TEXTURE_2D, GL_REPEAT, GL_REPEAT); - // set texture filtering parameters - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - // load image, create texture and generate mipmaps - texture = loadAndBindTextureFile(texture, texturePath, flipImage); - return texture; + // set texture filtering parameters + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + // load image, create texture and generate mipmaps + texture = loadAndBindTextureFile(texture, texturePath, flipImage); + return texture; } -void activateAndBindTextures(unsigned int texture, GLenum textureNumber) -{ - glActiveTexture(textureNumber); - glBindTexture(GL_TEXTURE_2D, texture); +void activateAndBindTextures(unsigned int texture, GLenum textureNumber) { + glActiveTexture(textureNumber); + glBindTexture(GL_TEXTURE_2D, texture); } -#endif // TEXTURES_CPP +#endif // TEXTURES_CPP diff --git a/Engine/engine/textures.hpp b/Engine/engine/textures.hpp index 2c6339e..0f0be80 100644 --- a/Engine/engine/textures.hpp +++ b/Engine/engine/textures.hpp @@ -1,20 +1,39 @@ // "Copyright [2023] " #ifndef TEXTURES_HPP #define TEXTURES_HPP -#include -#include #include +#include -void setTextureParametersINT(const GLenum textureTarget = GL_TEXTURE_2D, const GLint sCoordinateOption = GL_REPEAT, const GLint tCoordinateOption = GL_REPEAT, const GLint rCoordinateOption = GL_REPEAT, const float* borderColor = NULL); -void setTextureSCoordinate(const GLenum textureTarget = GL_TEXTURE_2D, const GLint sCoordinateOption = GL_REPEAT, const float* borderColor = NULL); -void setTextureTCoordinate(const GLenum textureTarget = GL_TEXTURE_2D, const GLint tCoordinateOption = GL_REPEAT, const float* borderColor = NULL); -void setTextureRCoordinate(const GLenum textureTarget = GL_TEXTURE_3D, const GLint rCoordinateOption = GL_REPEAT, const float* borderColor = NULL); -void setTextureFilteringAndMipMap(const GLenum textureTarget = GL_TEXTURE_2D, const GLenum filterType = GL_TEXTURE_MAG_FILTER, const GLint textureFilteringMethod = GL_NEAREST, const GLint mipMapFilteringMethod = GL_NEAREST_MIPMAP_NEAREST); +#include + +void setTextureParametersINT(const GLenum textureTarget = GL_TEXTURE_2D, + const GLint sCoordinateOption = GL_REPEAT, + const GLint tCoordinateOption = GL_REPEAT, + const GLint rCoordinateOption = GL_REPEAT, + const float* borderColor = NULL); +void setTextureSCoordinate(const GLenum textureTarget = GL_TEXTURE_2D, + const GLint sCoordinateOption = GL_REPEAT, + const float* borderColor = NULL); +void setTextureTCoordinate(const GLenum textureTarget = GL_TEXTURE_2D, + const GLint tCoordinateOption = GL_REPEAT, + const float* borderColor = NULL); +void setTextureRCoordinate(const GLenum textureTarget = GL_TEXTURE_3D, + const GLint rCoordinateOption = GL_REPEAT, + const float* borderColor = NULL); +void setTextureFilteringAndMipMap( + const GLenum textureTarget = GL_TEXTURE_2D, + const GLenum filterType = GL_TEXTURE_MAG_FILTER, + const GLint textureFilteringMethod = GL_NEAREST, + const GLint mipMapFilteringMethod = GL_NEAREST_MIPMAP_NEAREST); unsigned int generateAndBindTexture(const GLenum textureTarget); -// const char* texturePath, int textureWidth, int textureHeight, int colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, const GLint tCoordinateOption, const GLint rCoordinateOption, const float* borderColor +// const char* texturePath, int textureWidth, int textureHeight, int +// colorChannels, const GLenum textureTarget, const GLint sCoordinateOption, +// const GLint tCoordinateOption, const GLint rCoordinateOption, const float* +// borderColor unsigned int loadAndCreateTexture(const char* texturePath, bool flipImage); void activateAndBindTextures(unsigned int texture, GLenum textureNumber); -unsigned int loadAndBindTextureFile(unsigned int texture, const char* texturePath, bool flipImage); +unsigned int loadAndBindTextureFile(unsigned int texture, + const char* texturePath, bool flipImage); -#endif // TEXTURES_HPP \ No newline at end of file +#endif // TEXTURES_HPP \ No newline at end of file