/* * Reads the contents of a file and stores them in a c_string. * The c_string is allocated dynamically. If * there is any error at any stage, a null c_string is returned. * Input: * The full path to a file (including the filename) as * a std::string. Note that it is assumed that the file * is assumed to be plaintext. Also, if the filename alone * is specified, it assumes that the file is in the current * directory. * Returns: * A c style string with the contents of the shader's file. * A null string. */ GLchar* file_to_char_pointer(std::string path_to_file) { //TODO: Add exception handling. //TODO: Make this function nicer, so as to use glShaderSource fully. GLchar* shader_source_code = NULL; unsigned int file_size; //Open the file. std::ifstream shader_file(path_to_file.c_str()); //Read in all the lines if the file is open. if(shader_file.is_open()) { //Calculate the file's size in bytes //Move the get pointer to the end shader_file.seekg(0, std::ios::end); //The position of the get pointer gives the number of bytes file_size = shader_file.tellg(); if(file_size != -1) { /* Allocate enough space in the char array to hold * the file's contents. */ //TODO: Check this code. shader_source_code = new GLchar[file_size]; //Send the get pointer back to the beginning. shader_file.seekg(0, std::ios::beg); //Read the file into the source code buffer. int i = 0; //Gives the position in the buffer to write the //next character to. while(!shader_file.eof()) { /* TODO: See if the other ways to read data are * better. */ /* Get the next character and write it to * the buffer. */ shader_file.get(shader_source_code[i]); //Step to te next position in the buffer. i++; } } } //Close the file. shader_file.close(); #ifdef DEBUG std::cout << shader_source_code; #endif //Return the said pointer. return shader_source_code; }
GLuint GameObject::load_shader(GLenum shader_type, const std::string &shader_file_name) { try { std::ifstream shader_file(shader_file_name.c_str()); std::stringstream shader_data; shader_data << shader_file.rdbuf(); return glutil::CompileShader(shader_type, shader_data.str()); } catch (std::exception &e) { std::cerr << e.what() << std::endl; throw; } }
bool GLSLProgram::read_shader_file(const char *file_name, char* &shader_str) const { shader_str = nullptr; // reset string std::ifstream shader_file(file_name); if (!shader_file.is_open()) { cout << "ERROR: Cannot open shader file " << file_name << endl; return false; } string contents((std::istreambuf_iterator<char>(shader_file)), std::istreambuf_iterator<char>()); if (!contents.size()) { cout << "ERROR: The shader file contains nothing in " << file_name << endl; return false; } delete shader_str; shader_str = new char[contents.size() + 1]; memcpy(shader_str, contents.c_str(), contents.size() + 1); return true; }
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; }
const char* loadShaderAsString(const char* file) { std::ifstream shader_file(file, std::ifstream::in); std::string str((std::istreambuf_iterator<char>(shader_file)), std::istreambuf_iterator<char>()); return str.c_str(); }
ImageGlsl::ImageGlsl(const int width, const int height, const std::string& filestem) : mWidth(width) , mHeight(height) , mVertexFilename(shader_file(filestem, "vert")) , mFragmentFilename(shader_file(filestem, "frag")) { }