#include #include #include #include #include #include double round_to_presicion( double value, int precision ) { const int adjustment = pow(10,precision); return floor( value*(adjustment) + 0.5 )/adjustment; } std::string readFile(std::string path) { std::ifstream file(path); if (!file.is_open()) std::cerr << "File " << path << " was not found." << std::endl; std::string content = ""; std::string buffer = ""; while(getline(file, buffer)) { content += buffer + "\n"; } content += '\0'; file.close(); return content; } void gracefulExit(int code) { glfwTerminate(); exit(code); } unsigned int prepareShaders() { unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); std::string vertexShaderSourceString = readFile("../shaders/shader.vert"); std::string fragmentShaderSourceString = readFile("../shaders/shader.frag"); const char *vertexShaderSource = vertexShaderSourceString.c_str(); const char *fragmentShaderSource = fragmentShaderSourceString.c_str(); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); int success; char log[512]; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertexShader, 512, NULL, log); std::cerr << "Could not compile vertex shader: " << log << std::endl; gracefulExit(-1); } glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragmentShader, 512, NULL, log); std::cerr << "Could not compile fragment shader: " << log << std::endl; gracefulExit(-1); } unsigned int shaderProgram; shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if(!success) { glGetProgramInfoLog(shaderProgram, 512, NULL, log); std::cerr << "Could not link program: " << log << std::endl; gracefulExit(-1); } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); return shaderProgram; }