int ShaderProgram::loadShader(const char* shader_filepath, GLuint &shader) { // Read shader code std::string shaderCode = ""; std::ifstream shaderStream(shader_filepath, std::ios::in); if(shaderStream.is_open()) { std::string Line = ""; while(getline(shaderStream, Line)) shaderCode += "\n" + Line; shaderStream.close(); } else { printf("Failed to open %s.\n", shader_filepath); getchar(); return -1;} // Compile shader //printf("Compiling shader: %s\n", shader_filepath); char const* source = shaderCode.c_str(); glShaderSource(shader, 1, &source , NULL); glCompileShader(shader); // Check shader GLint result = GL_FALSE; int infoLogLength; glGetShaderiv(shader, GL_COMPILE_STATUS, &result); glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); if (infoLogLength > 0) { std::vector<char> shaderErrorMessage(infoLogLength+1); glGetShaderInfoLog(shader, infoLogLength, NULL, &shaderErrorMessage[0]); printf("%s\n", &shaderErrorMessage[0]); return -2; } return 0; }
void Shader::LoadShader( const std::string &filename, int type ) { GLuint shaderID = glCreateShader( type ); std::string shaderCode; std::ifstream shaderStream( filename, std::ios::in ); if ( shaderStream.is_open() ) { std::string line = ""; while ( getline( shaderStream, line ) ) { shaderCode += "\n" + line; } shaderStream.close(); } else { fprintf( stderr, "Error loading shader." ); return; } GLint result = GL_FALSE; int infoLogLength; char const *sourcePoint = shaderCode.c_str(); glShaderSource( shaderID, 1, &sourcePoint, NULL ); glCompileShader( shaderID ); glGetShaderiv( shaderID, GL_COMPILE_STATUS, &result ); glGetShaderiv( shaderID, GL_INFO_LOG_LENGTH, &infoLogLength ); std::vector<char> shaderErrorMessage( infoLogLength ); glGetShaderInfoLog( shaderID, infoLogLength, NULL, &shaderErrorMessage[ 0 ] ); // fprintf( stdout, "%s\n", &shaderErrorMessage[ 0 ] ); AttachShader( shaderID ); }
void Shader::buildShader(GLuint shaderLoc, const char* fileName) { // Read the Vertex Shader code from the file std::string shaderCode; std::ifstream shaderStream(fileName, std::ios::in); if (shaderStream.is_open()) { std::string Line = ""; while (getline(shaderStream, Line)) shaderCode += "\n" + Line; shaderStream.close(); } else { LOG_FATAL(logger) << "Could not open file!"; } // Compile Shader char const * sourcePointer = shaderCode.c_str(); glShaderSource(shaderLoc, 1, &sourcePointer , NULL); glCompileShader(shaderLoc); // Check Vertex Shader GLint Result = GL_FALSE; int InfoLogLength; glGetShaderiv(shaderLoc, GL_COMPILE_STATUS, &Result); glGetShaderiv(shaderLoc, GL_INFO_LOG_LENGTH, &InfoLogLength); std::vector<char> shaderErrorMessage(InfoLogLength); glGetShaderInfoLog(shaderLoc, InfoLogLength, NULL, &shaderErrorMessage[0]); if (!Result) LOG_ERROR(logger) << &shaderErrorMessage[0]; }
void Shaders::compileShader(GLuint shaderID, const std::string& sourceCode, GLint* result, int* infoLogLength) { char const * cstringSourceCode = sourceCode.c_str(); glShaderSource(shaderID, 1, &cstringSourceCode , NULL); glCompileShader(shaderID); // Check Vertex Shader glGetShaderiv(shaderID, GL_COMPILE_STATUS, result); glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, infoLogLength); if (*infoLogLength > 0 ){ std::vector<char> shaderErrorMessage(*infoLogLength+1); glGetShaderInfoLog(shaderID, *infoLogLength, NULL, &shaderErrorMessage[0]); printf("%s\n", &shaderErrorMessage[0]); } }
void ShaderProgram::compileShader(GLuint shader, std::string shaderCode) { GLint compileResult = GL_FALSE; int infoLogLength; char const *sourcePointer = shaderCode.c_str(); glShaderSource(shader, 1, &sourcePointer, NULL); glCompileShader(shader); glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult); if(compileResult == GL_FALSE) { glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength); std::vector<char> shaderErrorMessage(infoLogLength); glGetShaderInfoLog(shader, infoLogLength, NULL, &shaderErrorMessage[0]); throw ShaderException(std::string("Error compiling shader: ") + &shaderErrorMessage[0]); } }
GLboolean Program::attachShader(std::string filename, GLenum type) { GLuint shaderId = glCreateShader(type); std::string shaderCode; std::ifstream shaderStream(filename, std::ios::in); if(shaderStream.is_open()) { std::string line = ""; while(getline(shaderStream, line)) shaderCode += "\n" + line; shaderStream.close(); } else { std::cerr << clog::err << "Cannot open " << filename << "." << std::endl; std::cin.get(); return GL_FALSE; } GLint result = GL_FALSE; GLint infoLogLength; //compile vertex shader std::cout << clog::inf << "Compiling shader (" << filename << ")..." << std::endl; char const* sourcePointer = shaderCode.c_str(); glShaderSource(shaderId, 1, &sourcePointer , NULL); glCompileShader(shaderId); //check vertex shader glGetShaderiv(shaderId, GL_COMPILE_STATUS, &result); glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &infoLogLength); if(infoLogLength > 0) { std::vector<char> shaderErrorMessage(infoLogLength + 1); glGetShaderInfoLog(shaderId, infoLogLength, NULL, &shaderErrorMessage[0]); std::cerr << clog::err << &shaderErrorMessage[0] << std::endl; } glAttachShader(instance, shaderId); //glDeleteShader(vertexShaderId); return GL_TRUE; }
/// <summary> /// Loads the shader. /// </summary> /// <param name="filename">The filename.</param> /// <param name="type">The type.</param> /// <returns></returns> GLuint ShaderProgram::LoadShader( const std::string& filename, GLenum type ) { // Create the shader GLuint shaderID = glCreateShader( type ); // Read the Shader code from the file std::string shaderCode; std::ifstream shaderStream( filename, std::ios::in ); if( shaderStream.is_open() ) { std::string line = ""; while( getline( shaderStream, line ) ) shaderCode += "\n" + line; shaderStream.close(); } else { Debug::Log( "Could not find shader with path: " + filename ); return 0; } GLint result = GL_FALSE; int infoLogLength; // Compile Shader Debug::Log( "Compiling shader: " + filename ); char const * sourcePointer = shaderCode.c_str(); glShaderSource( shaderID, 1, &sourcePointer, NULL ); glCompileShader( shaderID ); // Check Shader glGetShaderiv( shaderID, GL_COMPILE_STATUS, &result ); glGetShaderiv( shaderID, GL_INFO_LOG_LENGTH, &infoLogLength ); std::vector<char> shaderErrorMessage( infoLogLength ); glGetShaderInfoLog( shaderID, infoLogLength, NULL, &shaderErrorMessage[0] ); Debug::Log( trim( std::string( shaderErrorMessage.begin(), shaderErrorMessage.end() ) ), LogType::Error ); CHECK_GL_ERROR(); return shaderID; }
GLuint shaderhelper::compileShader(const std::string & shaderFilename, GLenum shaderType) { std::string shaderCode = loadShader(shaderFilename); GLint compilationSuccess = GL_FALSE; int infoLogLength; GLuint shaderId = glCreateShader(shaderType); char const * sourcePointer = shaderCode.c_str(); glShaderSource(shaderId, 1, &sourcePointer, nullptr); glCompileShader(shaderId); glGetShaderiv(shaderId, GL_COMPILE_STATUS, &compilationSuccess); glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &infoLogLength); if (infoLogLength > 0) { std::vector<char> shaderErrorMessage((unsigned long) (infoLogLength + 1)); glGetShaderInfoLog(shaderId, infoLogLength, nullptr, &shaderErrorMessage[0]); throw std::runtime_error(&shaderErrorMessage[0]); } return shaderId; }
GLuint loadShader(string file_path, GLuint shader) { GLuint shaderID = glCreateShader(shader); string shaderCode; ifstream shaderStream(file_path, std::ios::in); if (shaderStream.is_open()) { string Line = ""; while (getline(shaderStream, Line)) shaderCode += "\n" + Line; shaderStream.close(); } else { cerr << "Cannot open: " << file_path << endl; return 0; } cout << "Compiling shader: " << file_path << endl; char const* sourcePointer = shaderCode.c_str(); glShaderSource(shaderID, 1, &sourcePointer, NULL); glCompileShader(shaderID); GLint result = 0; glGetShaderiv(shaderID, GL_COMPILE_STATUS, &result); if (result == GL_FALSE) { GLint infoLogLength = 0; glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &infoLogLength); vector<char> shaderErrorMessage(infoLogLength); glGetShaderInfoLog(shaderID, infoLogLength, NULL, &shaderErrorMessage[0]); cerr << &shaderErrorMessage[0] << endl; return 0; } return shaderID; }
GLuint Shaders::compileShaderCode(const std::string &code, GLenum type) { GLuint shaderHandle = glCreateShader(type); char const *shaderCodeRaw = code.c_str(); glShaderSource(shaderHandle, 1, &shaderCodeRaw, NULL); glCompileShader(shaderHandle); GLint result = GL_FALSE; int infoLength; glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &result); glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &infoLength); if (infoLength > 0) { std::vector<char> shaderErrorMessage(infoLength + 1); glGetShaderInfoLog(shaderHandle, infoLength, NULL, &shaderErrorMessage[0]); std::cout << "Error compiling shader: " << &shaderErrorMessage[0] << std::endl; } else { std::cout << "Shader compilation successful" << std::endl; } return shaderHandle; }