#ifndef SHADERS_CPP #define SHADERS_CPP #include #include #include "shaders.hpp" #include "constants.hpp" #include "misc.hpp" unsigned int linkShaderObjectsShaderProgram(unsigned int vertexShaders, unsigned int fragmentShader) { // link shader objects into shader program // will store shader program id unsigned int shaderProgram; // creates program shaderProgram = glCreateProgram(); // attachShaders glAttachShader(shaderProgram, vertexShaders); glAttachShader(shaderProgram, fragmentShader); // link shaders glLinkProgram(shaderProgram); if(!shaderProgramLinkingSuccessful(shaderProgram)) 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"); return shaderProgram; } std::pair compileShaders() { 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) { print("Fragment Shader Compilation Failed"); return std::make_pair(0, 0); } return std::make_pair(vertexShader, fragmentShader); } 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; } 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; } #endif