diff --git a/Engine/engine/beforeRender.cpp b/Engine/engine/beforeRender.cpp index 321ac67..24309d8 100644 --- a/Engine/engine/beforeRender.cpp +++ b/Engine/engine/beforeRender.cpp @@ -53,7 +53,7 @@ int initializeGLAD() } // resizes viewport when user resizes window -void framebuffer_size_callback(GLFWwindow *window, int width, int height) +void framebuffer_size_callback(GLFWwindow *window, const int width, const int height) { glViewport(0, 0, width, height); } diff --git a/Engine/engine/beforeRender.hpp b/Engine/engine/beforeRender.hpp index bfcdbc0..2a42f84 100644 --- a/Engine/engine/beforeRender.hpp +++ b/Engine/engine/beforeRender.hpp @@ -6,7 +6,7 @@ void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion); void instantiateGLFWwindow(); GLFWwindow *createWindowObject(); int initializeGLAD(); -void framebuffer_size_callback(GLFWwindow *window, int width, 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/draw.cpp b/Engine/engine/draw.cpp index 573dc03..5a3d0a6 100644 --- a/Engine/engine/draw.cpp +++ b/Engine/engine/draw.cpp @@ -11,16 +11,17 @@ int drawFigure(const bool whatToDraw) { - if(whatToDraw) + if (whatToDraw) { - if(drawTriangle() == -1) + if (drawTriangle() == -1) { print("Error with drawing triangle! "); return -1; } - }else + } + else { - if(drawSquare() == -1) + if (drawSquare() == -1) { print("Error with drawing square! "); return -1; @@ -31,18 +32,20 @@ int drawFigure(const bool whatToDraw) int drawSquare() { - std::pair shaders = compileShaders(); - if(shaders.first == 0 || shaders.second == 0) return -1; + const std::pair shaders = compileShaders(); + if (shaders.first == 0 || shaders.second == 0) + return -1; - unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second); - if(shaderProgram == 0) return -1; + const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second); + if (shaderProgram == 0) + return -1; - unsigned int VAO = generateBindVAO(); + const unsigned int VAO = generateBindVAO(); copyVerticesMemory(constants::SQUARE_VERTICES, constants::SQUARE_VERTICES_SIZE, GL_ARRAY_BUFFER); copyVerticesMemory(constants::SQUARE_INDICES, constants::SQUARE_INDICES_SIZE, GL_ELEMENT_ARRAY_BUFFER); - // set vertex attribute pointers - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + // set vertex attribute pointers + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); doDrawElements(shaderProgram, VAO, GL_TRIANGLES, GL_UNSIGNED_INT, std::size(constants::SQUARE_INDICES)); @@ -55,39 +58,41 @@ void doDrawElements(const unsigned int shaderProgram, const unsigned int vertexA glBindVertexArray(vertexArrayObject); glDrawElements(drawArrayMode, numberOfElementsToDraw, drawType, 0); glBindVertexArray(0); -} +} int drawTriangle() -{ - unsigned int vertexBufferObject = copyVerticesMemory(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER); - - std::pair shaders = compileShaders(); - if(shaders.first == 0 || shaders.second == 0) return -1; +{ + const unsigned int vertexBufferObject = copyVerticesMemory(constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER); - unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second); - if(shaderProgram == 0) return -1; + const std::pair shaders = compileShaders(); + if (shaders.first == 0 || shaders.second == 0) + return -1; + + const unsigned int shaderProgram = linkShaderObjectsShaderProgram(shaders.first, shaders.second); + if (shaderProgram == 0) + return -1; configureVertexAttribute(); - unsigned int vertexArrayObject = generateBindVAO(); + const unsigned int vertexArrayObject = generateBindVAO(); copyVerticesArray(vertexBufferObject, constants::TRIANGLE_VERTICES, constants::TRIANGLE_VERTICES_SIZE, GL_ARRAY_BUFFER); - // set vertex attribute pointers - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + // set vertex attribute pointers + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); glEnableVertexAttribArray(0); doDrawArrays(shaderProgram, vertexArrayObject, GL_TRIANGLES, 0, 3); return 0; } -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) { // use shader program to render an object glUseProgram(shaderProgram); glBindVertexArray(vertexArrayObject); // From left: // primitive type we want to draw - // starting index of vertex array + // starting index of vertex array // how many vertices we want to draw glDrawArrays(drawArrayMode, firstIndex, numberOfIndicesToBeRendered); } -#endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/Engine/engine/match b/Engine/engine/match index 2db30ce..a44b145 100755 Binary files a/Engine/engine/match and b/Engine/engine/match differ diff --git a/Engine/engine/renderLoop.cpp b/Engine/engine/renderLoop.cpp index 9a368f7..5ae41f2 100644 --- a/Engine/engine/renderLoop.cpp +++ b/Engine/engine/renderLoop.cpp @@ -9,21 +9,19 @@ #include "constants.hpp" #include "misc.hpp" -bool processInput(GLFWwindow *window, bool whatToDraw) +bool processInput(GLFWwindow *window, const bool whatToDraw) { - // glfwGetKey takes window and key as an input and checks is currently being pressed + // 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) + if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - if(glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) + if (glfwGetKey(window, GLFW_KEY_C) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); - if(glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS ) return !whatToDraw; + if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS || glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) + return !whatToDraw; return whatToDraw; } - - - // https://stackoverflow.com/a/25680092 unsigned int copyVerticesMemory(const float vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget) { @@ -75,7 +73,7 @@ void configureVertexAttribute() // 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 - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); + glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void *)0); // enable vertex attribute glEnableVertexAttribArray(0); } @@ -86,7 +84,7 @@ unsigned int generateBindVAO() // generate vao unsigned int vertexArrayObject; glGenVertexArrays(1, &vertexArrayObject); - // bind vao + // bind vao glBindVertexArray(vertexArrayObject); return vertexArrayObject; } @@ -98,40 +96,40 @@ void copyVerticesArray(unsigned int vertexBufferObject, const float vertices[], glBufferData(boundBufferTarget, sizeOfVertices, vertices, GL_STATIC_DRAW); } -bool renderLoopInside(GLFWwindow* window, bool whatToDraw) +bool renderLoopInside(GLFWwindow *window, bool 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); - - if(drawFigure(whatToDraw) == -1) - { - print("error with drawing!"); - glfwSetWindowShouldClose(window, true); - }; + // 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); - // 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); + if (drawFigure(whatToDraw) == -1) + { + print("error with drawing!"); + glfwSetWindowShouldClose(window, true); + }; - // glfwPollEvents checks if any event (like mouse/keyboard input was triggered), updates window state and calls functions (which we register via callback methods) - glfwPollEvents(); - return whatToDraw; + // 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(); + return whatToDraw; } -void renderLoop(GLFWwindow* window) +void renderLoop(GLFWwindow *window) { bool whatToDraw = true; // glfwWindowShouldClose checks if GLFW was instructed to close - while(!glfwWindowShouldClose(window)) + while (!glfwWindowShouldClose(window)) { whatToDraw = renderLoopInside(window, whatToDraw); } } -#endif \ No newline at end of file +#endif \ No newline at end of file diff --git a/Engine/engine/shaders.cpp b/Engine/engine/shaders.cpp index 5c86d44..47eddb4 100644 --- a/Engine/engine/shaders.cpp +++ b/Engine/engine/shaders.cpp @@ -6,13 +6,12 @@ #include "constants.hpp" #include "misc.hpp" - unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned int fragmentShader) { - // link shader objects into shader program + // link shader objects into shader program // will store shader program id unsigned int shaderProgram; - // creates program + // creates program shaderProgram = glCreateProgram(); // attachShaders @@ -21,45 +20,47 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned // link shaders glLinkProgram(shaderProgram); - if(!shaderSuccessful(shaderProgram, false)) return 0; + if (!shaderSuccessful(shaderProgram, false)) + return 0; // 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"); + if (shaderProgram == 0) + print("Shader Program Linking Failed"); return shaderProgram; } -unsigned int compileShader(const GLenum shaderType, const char * shaderSource) +unsigned int compileShader(const GLenum shaderType, const char *shaderSource) { // we create vertex shader and assign its id to shader variable - unsigned int shaderID; - shaderID = glCreateShader(shaderType); + 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 NULL) glShaderSource(shaderID, 1, &shaderSource, NULL); // compile shader glCompileShader(shaderID); - if(!shaderSuccessful(shaderID, true)) return 0; + if (!shaderSuccessful(shaderID, true)) + return 0; return shaderID; } std::pair compileShaders() { - unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource); - if(vertexShader == 0) + const unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource); + if (vertexShader == 0) { print("Vertex Shader Compilation Failed"); return std::make_pair(0, 0); } - unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource); - if(fragmentShader == 0) + const unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource); + if (fragmentShader == 0) { print("Fragment Shader Compilation Failed"); return std::make_pair(0, 0); @@ -67,28 +68,27 @@ std::pair compileShaders() return std::make_pair(vertexShader, fragmentShader); } -void shaderFailedMessage(const unsigned int shader, bool compilation) +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, NULL, infoLog) - : glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog); - std::cout << "ERROR vertex shader compilation failed \n" << infoLog << std::endl; + compilation ? glGetShaderInfoLog(shader, sizeOfInfoLog, NULL, infoLog) + : glGetProgramInfoLog(shader, sizeOfInfoLog, NULL, infoLog); + std::cout << "ERROR vertex shader compilation failed \n" + << infoLog << std::endl; } -int shaderSuccessful(const unsigned int shader, const bool compilation) +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); + // 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) + if (!success) { shaderFailedMessage(shader, compilation); } diff --git a/Engine/engine/shaders.hpp b/Engine/engine/shaders.hpp index 8a1add9..238fb31 100644 --- a/Engine/engine/shaders.hpp +++ b/Engine/engine/shaders.hpp @@ -6,11 +6,10 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned int fragmentShader); std::pair compileShaders(); -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); -void shaderFailedMessage(const unsigned int shader, bool compilation); - +void shaderFailedMessage(const unsigned int shader, const bool compilation); #endif \ No newline at end of file