diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 388b933..e4600fd 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -6,7 +6,7 @@ "command": "/usr/bin/g++", "args": [ "-g", - "-std=c++17", + "-std=c++2a", "-I${workspaceFolder}/dependencies/include", "-L${workspaceFolder}/dependencies/library", "-Wall", diff --git a/Engine/engine/beforeRender.cpp b/Engine/engine/beforeRender.cpp index 106cefc..dc3cb8b 100644 --- a/Engine/engine/beforeRender.cpp +++ b/Engine/engine/beforeRender.cpp @@ -30,11 +30,11 @@ void instantiateGLFWwindow() { configureGLFW(constants::GLFW_MAJOR_VERSION, constants::GLFW_MINOR_VERSION); } -GLFWwindow* createWindowObject() { +const 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, NULL, NULL); + const GLFWwindow* window = glfwCreateWindow(constants::MAIN_WINDOW_WIDTH, constants::MAIN_WINDOW_HEIGHT, constants::MAIN_WINDOW_NAME, NULL, NULL); return window; } diff --git a/Engine/engine/beforeRender.hpp b/Engine/engine/beforeRender.hpp index 51627d8..b551090 100644 --- a/Engine/engine/beforeRender.hpp +++ b/Engine/engine/beforeRender.hpp @@ -4,7 +4,7 @@ void configureGLFW(const int GLFWMajorVersion, const int GLFWMinorVersion); void instantiateGLFWwindow(); -GLFWwindow* createWindowObject(); +const GLFWwindow* createWindowObject(); int initializeGLAD(); void framebuffer_size_callback(GLFWwindow* window, int width, int height); void viewPort(GLFWwindow* window); diff --git a/Engine/engine/draw.cpp b/Engine/engine/draw.cpp index f3c84a8..573dc03 100644 --- a/Engine/engine/draw.cpp +++ b/Engine/engine/draw.cpp @@ -38,8 +38,8 @@ int drawSquare() if(shaderProgram == 0) return -1; 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); + 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); diff --git a/Engine/engine/match b/Engine/engine/match old mode 100644 new mode 100755 index 8cada05..f8966b3 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 3c57f97..67d85a0 100644 --- a/Engine/engine/match.cpp +++ b/Engine/engine/match.cpp @@ -15,7 +15,7 @@ int main() { - + // changed in glfwWindowShouldClose GLFWwindow* window = prepareForRender(); if(window == NULL) return -1; renderLoop(window); diff --git a/Engine/engine/renderLoop.cpp b/Engine/engine/renderLoop.cpp index 2dceb21..9a368f7 100644 --- a/Engine/engine/renderLoop.cpp +++ b/Engine/engine/renderLoop.cpp @@ -21,24 +21,30 @@ bool processInput(GLFWwindow *window, bool whatToDraw) return whatToDraw; } -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); - // 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(!shaderCompilationSuccessful(shaderID)) return 0; - return shaderID; + + +// 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 + + /* 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; } -template -// https://stackoverflow.com/a/25680092 -unsigned int copyVerticesMemory(const T vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget) +unsigned int copyVerticesMemory(const unsigned int vertices[], const size_t sizeOfVertices, const GLenum boundBufferTarget) { // stores vertices in gpu memory unsigned int vertexBufferObject; diff --git a/Engine/engine/renderLoop.hpp b/Engine/engine/renderLoop.hpp index 09aafa9..c17cd3b 100644 --- a/Engine/engine/renderLoop.hpp +++ b/Engine/engine/renderLoop.hpp @@ -12,7 +12,9 @@ unsigned int generateBindVAO(); void configureVertexAttribute(); bool processInput(GLFWwindow *window, bool whatToDraw); -template unsigned int copyVerticesMemory(const T 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); #endif \ No newline at end of file diff --git a/Engine/engine/shaders.cpp b/Engine/engine/shaders.cpp index 8078f56..5c86d44 100644 --- a/Engine/engine/shaders.cpp +++ b/Engine/engine/shaders.cpp @@ -21,7 +21,7 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned // link shaders glLinkProgram(shaderProgram); - if(!shaderProgramLinkingSuccessful(shaderProgram)) return 0; + if(!shaderSuccessful(shaderProgram, false)) return 0; // activate program // after that every shader and rendering call will use this program object @@ -34,6 +34,21 @@ unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned return shaderProgram; } +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); + + // 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; + return shaderID; +} + std::pair compileShaders() { unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource); @@ -52,40 +67,32 @@ std::pair compileShaders() return std::make_pair(vertexShader, fragmentShader); } -int shaderCompilationSuccessful(const unsigned int shader) +void shaderFailedMessage(const unsigned int shader, bool compilation) { - // check if compilation was successful - // int because glGetShaderiv requires int - int successfulCompilation; - // here we store info about compilation char infoLog[512]; - // check if compilation was successful - glGetShaderiv(shader, GL_COMPILE_STATUS, &successfulCompilation); - // if not display compilation log - if(!successfulCompilation) - { - glGetShaderInfoLog(shader, sizeof(infoLog) / sizeof(infoLog[0]), NULL, infoLog); - std::cout << "ERROR vertex shader compilation failed \n" << infoLog << std::endl; - } - return successfulCompilation; + 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; } -int shaderProgramLinkingSuccessful(const unsigned int shaderProgram) +int shaderSuccessful(const unsigned int shader, const bool compilation) { // check if compilation was successful // int because glGetShaderiv requires int - int successfulLinking; + int success; // here we store info about compilation - char infoLog[512]; // check if compilation was successful - glGetProgramiv(shaderProgram, GL_LINK_STATUS, &successfulLinking); + compilation ? + glGetShaderiv(shader, GL_COMPILE_STATUS, &success) + : glGetProgramiv(shader, GL_LINK_STATUS, &success); // if not display compilation log - if(!successfulLinking) + if(!success) { - glGetProgramInfoLog(shaderProgram, sizeof(infoLog) / sizeof(infoLog[0]), NULL, infoLog); - std::cout << "ERROR shaderProgram compilation failed \n" << infoLog << std::endl; + shaderFailedMessage(shader, compilation); } - return successfulLinking; + return success; } #endif \ No newline at end of file diff --git a/Engine/engine/shaders.hpp b/Engine/engine/shaders.hpp index 899a5f1..8a1add9 100644 --- a/Engine/engine/shaders.hpp +++ b/Engine/engine/shaders.hpp @@ -9,6 +9,8 @@ std::pair compileShaders(); 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); #endif \ No newline at end of file