diff --git a/Engine/engine/constants.hpp b/Engine/engine/constants.hpp index 5037467..0c4fa69 100644 --- a/Engine/engine/constants.hpp +++ b/Engine/engine/constants.hpp @@ -30,6 +30,27 @@ namespace constants GLfloat alpha = 1.0f; } LEARN_OPEN_GL_COLOR; + // 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 + inline const char *vertexShaderSource { "#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" } ; + + // write fragment shader + // we set the color of each pixel to be orange + inline const char *fragmentShaderSource { + "#version 330 core\n" + "out vec4 FragColor;\n" + "void main()\n" + "{\n" + "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" + "}\0" + }; + } #endif \ No newline at end of file diff --git a/Engine/engine/match b/Engine/engine/match index 9319b06..35a2000 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 5a03ee8..46f7c8c 100644 --- a/Engine/engine/match.cpp +++ b/Engine/engine/match.cpp @@ -81,6 +81,60 @@ void processInput(GLFWwindow *window) glfwSetWindowShouldClose(window, true); } +int shaderProgramLinkingSuccessful(const unsigned int shaderProgram) +{ + // check if compilation was successful + // int because glGetShaderiv requires int + int successfulLinking; + // here we store info about compilation + char infoLog[512]; + // check if compilation was successful + glGetProgramiv(shaderProgram, GL_LINK_STATUS, &successfulLinking); + // if not display compilation log + if(!successfulLinking) + { + glGetProgramInfoLog(shaderProgram, sizeof(infoLog) / sizeof(infoLog[0]), NULL, infoLog); + std::cout << "ERROR shaderProgram compilation failed \n" << infoLog << std::endl; + } + return successfulLinking; +} + + +int shaderCompilationSuccessful(const unsigned int shader) +{ + + + // 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; +} + +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(GL_VERTEX_SHADER); + + // 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; +} + void drawTriangle() { // we specify three vertices @@ -108,18 +162,38 @@ void drawTriangle() */ glBufferData(GL_ARRAY_BUFFER, sizeof(triangleVertices), triangleVertices, GL_STATIC_DRAW); - // 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 - const char *vertexShaderSource = "#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"; + unsigned int vertexShader = compileShader(GL_VERTEX_SHADER, constants::vertexShaderSource); + if(vertexShader == 0) return; + + unsigned int fragmentShader = compileShader(GL_FRAGMENT_SHADER, constants::fragmentShaderSource); + if(fragmentShader == 0) return; + + // link shader objects into shader program + // will store shader program id + unsigned int shaderProgram; + // creates program + shaderProgram = glCreateProgram(); + + // attachShaders + glAttachShader(shaderProgram, vertexShader); + glAttachShader(shaderProgram, fragmentShader); + + // link shaders + glLinkProgram(shaderProgram); + if(!shaderProgramLinkingSuccessful(shaderProgram)) return; + + // 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(vertexShader); + glDeleteShader(fragmentShader); } + + void renderLoop(GLFWwindow* window) { // glfwWindowShouldClose checks if GLFW was instructed to close @@ -141,6 +215,7 @@ void renderLoop(GLFWwindow* 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(); + drawTriangle(); } }