void ShaderWithVariables::initProgram(const char* shadername) { glGenVertexArrays(1, &m_vao); std::cout << "Shader [" << shadername << "] " ; std::string vs = shadername; std::string fs = shadername; vs += ".vert"; fs += ".frag"; m_program = makeShaderFromSource(vs.c_str(), fs.c_str()); if (m_program == 0) return; std::cout << " vars: "; const std::string vsrc = GetShaderSource(vs.c_str()); findVariables(vsrc.c_str()); const std::string fsrc = GetShaderSource(fs.c_str()); findVariables(fsrc.c_str()); std::cout << m_unis.size() << " unis, " << m_attrs.size() << " attrs." << std::endl; }
bool Shader::CompileShader(GLuint &shader, string sourcefile) { string source = GetShaderSource(sourcefile); const char *cSource = source.c_str(); int sourceLength = source.length(); glShaderSource(shader, 1, &cSource, &sourceLength); glCompileShader(shader); // Check for errors GLint success = 0; glGetShaderiv(shader, GL_COMPILE_STATUS, &success); if (success == GL_FALSE) { // Report errors GLint logSize = 0; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logSize); char *errLog = new char[logSize]; glGetShaderInfoLog(shader, logSize, &logSize, errLog); printf("Failed to compile shader:\n%s\n", errLog); delete[] errLog; glDeleteShader(shader); shader = 0; return false; } return true; }
bool IShaderObject::ReloadFromDisk() { std::string newsrc = GetShaderSource(srcFile); if (newsrc != curShaderSrc) { curShaderSrc = std::move(newsrc); return true; } return false; }
void LoadShaders(GLenum& program, const std::string& vert_shader_fn, const std::string& frag_shader_fn) { log_file << "===================" << std::endl; log_file << "Building Shader" << std::endl; log_file << "vertex shader: " << vert_shader_fn << std::endl; log_file << "fragment shader: " << frag_shader_fn << std::endl; log_file << "Loading source..."; std::string vertex_shader_source = GetShaderSource(vert_shader_fn); std::string fragment_shader_source = GetShaderSource(frag_shader_fn); log_file << "done." << std::endl; // Create Shader And Program Objects program = glCreateProgramObjectARB(); GLenum vertex_shader = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB); GLenum fragment_shader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB); // Load Shader Sources char* temp = (char*)vertex_shader_source.c_str(); glShaderSourceARB(vertex_shader, 1, (const GLcharARB**)&temp, NULL); temp = (char*)fragment_shader_source.c_str(); glShaderSourceARB(fragment_shader, 1, (const GLcharARB**)&temp, NULL); // Compile The Shaders log_file << "Compiling..."; glCompileShaderARB(vertex_shader); glCompileShaderARB(fragment_shader); log_file << "done." << std::endl; // Attach The Shader Objects To The Program Object glAttachObjectARB(program, vertex_shader); glAttachObjectARB(program, fragment_shader); // Link The Program Object log_file << "Linking..."; glLinkProgramARB(program); log_file << "done." << std::endl; PrintProgramInfoLog(program); // PrintShaderInfoLog(program); log_file << "===================" << std::endl; }
/// Once source is obtained from either file or hard-coded map, compile the /// shader, release the string memory and return the ID. GLuint loadShaderFile(const char* filename, const unsigned long Type) { const GLchar* shaderSource = GetShaderSource(filename); if (shaderSource == NULL) return 0; GLint length = strlen(shaderSource); GLuint shaderId = glCreateShader(Type); glShaderSource(shaderId, 1, &shaderSource, &length); glCompileShader(shaderId); delete [] shaderSource; return shaderId; }
/// Once source is obtained from either file or hard-coded map, compile the /// shader, release the string memory and return the ID. GLuint loadShaderFile(const char* filename, const unsigned long Type) { if (filename == NULL) return 0; const std::string shaderSource = GetShaderSource(filename); std::string sourceString(shaderSource); if (sourceString.empty()) return 0; GLint length = static_cast<GLint>(sourceString.length()); GLuint shaderId = glCreateShader(Type); const GLchar* pSrcStr = sourceString.c_str(); glShaderSource(shaderId, 1, &pSrcStr, &length); glCompileShader(shaderId); return shaderId; }