GLint ShaderProgram::loadShader(const std::string &shader_file, GLint type) { // Create shaders auto shader_id = glCreateShader(type); auto result = GL_FALSE; auto info_length = 0; // Load shader code std::ifstream shader_stream(shader_file); std::string shader_code((std::istreambuf_iterator<char>(shader_stream)), std::istreambuf_iterator<char>()); // Compile shader std::cout << "Compiling Shader ..." << shader_file << std::endl; auto shader_code_ptr = shader_code.c_str(); glShaderSource(shader_id, 1, &shader_code_ptr, NULL); glCompileShader(shader_id); // Check vertex shader log glGetShaderiv(shader_id, GL_COMPILE_STATUS, &result); if (result == GL_FALSE) { glGetShaderiv(shader_id, GL_INFO_LOG_LENGTH, &info_length); std::string vertex_shader_log((unsigned int)info_length, ' '); glGetShaderInfoLog(shader_id, info_length, NULL, &vertex_shader_log[0]); std::cout << vertex_shader_log << std::endl; } return shader_id; }
GLuint cShader::LoadShader(const std::string& filename, GLenum shader_type) { GLuint shader_handle = glCreateShader(shader_type); //read shader file std::ifstream shader_file(filename.c_str()); std::cout<< "Compiling shader: " << filename.c_str() << std::endl; std::string shader_code((std::istreambuf_iterator<char>(shader_file)), (std::istreambuf_iterator<char>())); const char* shader_code_str = shader_code.c_str(); glShaderSource(shader_handle, 1, &shader_code_str, NULL); glCompileShader(shader_handle); // Check the compile status GLint compiled; glGetShaderiv(shader_handle, GL_COMPILE_STATUS, &compiled); if (!compiled) { std::cout<< "shader compilation failed: " << filename.c_str() << std::endl; // get log string length GLint max_len = 0; glGetShaderiv(shader_handle, GL_INFO_LOG_LENGTH, &max_len); char* error_log = new char[max_len]; glGetShaderInfoLog(shader_handle, max_len, &max_len, error_log); std::cout << error_log << std::endl; delete[] error_log; glDeleteShader(shader_handle); shader_handle = -1; } return shader_handle; }