void ShaderFileLoader::parseFiles() { for (std::size_t i = 0; i < _files.size(); ++i) { const std::string& fullPath = _files[i]; if (_currentOperation) { _currentOperation->setMessage((boost::format(_("Parsing material file %s")) % fullPath).str()); float progress = static_cast<float>(i) / _files.size(); _currentOperation->setProgress(progress); } // Open the file ArchiveTextFilePtr file = GlobalFileSystem().openTextFile(fullPath); if (file != NULL) { std::istream is(&(file->getInputStream())); parseShaderFile(is, fullPath); } else { throw std::runtime_error("Unable to read shaderfile: " + fullPath); } } }
shaderRefMaps AaMaterialFileParser::parseAllShaderFiles(std::string directory, bool subFolders) { shaderRefMaps shaders; if (!boost::filesystem::exists(directory)) return shaders; boost::filesystem::directory_iterator end_itr; // default construction yields past-the-end for ( boost::filesystem::directory_iterator itr(directory); itr != end_itr; ++itr ) { if ( is_directory(itr->path()) && subFolders) { shaderRefMaps subInfos=parseAllShaderFiles(itr->path().generic_string(),subFolders); shaders.vertexShaderRefs.insert(subInfos.vertexShaderRefs.begin(),subInfos.vertexShaderRefs.end()); shaders.pixelShaderRefs.insert(subInfos.pixelShaderRefs.begin(),subInfos.pixelShaderRefs.end()); } else if ( boost::filesystem::is_regular_file(itr->path()) && itr->path().generic_string().find(".program")!=std::string::npos) // see below { AaLogger::getLogger()->writeMessage(itr->path().generic_string()); shaderRefMaps localShaders=parseShaderFile(itr->path().generic_string()); shaders.vertexShaderRefs.insert(localShaders.vertexShaderRefs.begin(),localShaders.vertexShaderRefs.end()); shaders.pixelShaderRefs.insert(localShaders.pixelShaderRefs.begin(),localShaders.pixelShaderRefs.end()); AaLogger::getLogger()->writeMessage("Loaded shader references: "+boost::lexical_cast<std::string,int>(localShaders.pixelShaderRefs.size()+localShaders.vertexShaderRefs.size())); } } AaLogger::getLogger()->writeMessage( "Total shader references here: "+boost::lexical_cast<std::string,int>(shaders.pixelShaderRefs.size()+shaders.vertexShaderRefs.size())); return shaders; }
GPUuint Shader::createShader(const std::string &filename) { //compare file's extension name std::string shaderName(filename); size_t found = shaderName.rfind('.'); GLuint shaderId = 0; GLenum shaderType; if (shaderName.compare(found + 1, 4, "vert") == 0) { shaderType = GL_VERTEX_SHADER; shaderId = glCreateShader(GL_VERTEX_SHADER); } else if (shaderName.compare(found + 1, 4, "frag") == 0) { shaderType = GL_FRAGMENT_SHADER; shaderId = glCreateShader(GL_FRAGMENT_SHADER); } else if (shaderName.compare(found + 1, 4, "geom") == 0) { shaderType = GL_GEOMETRY_SHADER; shaderId = glCreateShader(GL_GEOMETRY_SHADER); } else if (shaderName.compare(found + 1, 4, "comp") == 0) { shaderType = GL_COMPUTE_SHADER; shaderId = glCreateShader(GL_COMPUTE_SHADER); } else { return 0; } const std::string shaderCode = parseShaderFile(filename); const char *code = shaderCode.c_str(); glShaderSource(shaderId, 1, &code, NULL); glCompileShader(shaderId); GLint compileStatus;//whether compilation succeeds glGetShaderiv(shaderId, GL_COMPILE_STATUS, &compileStatus); if(GL_FALSE == compileStatus) { GLint infoLogLength; glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &infoLogLength); GLchar *infoLog = new GLchar[infoLogLength + 1]; glGetShaderInfoLog(shaderId, infoLogLength, NULL, infoLog); std::cerr << "Compile failure in " << shaderName << ": " << std::endl << infoLog << std::endl << std::endl; delete [] infoLog; } return shaderId; }
void ShaderFileLoader::visit(const std::string& filename) { // Construct the full VFS path std::string fullPath = _basePath + filename; // Open the file ArchiveTextFilePtr file = GlobalFileSystem().openTextFile(fullPath); if (file != NULL) { std::istream is(&(file->getInputStream())); parseShaderFile(is, fullPath); } else { throw std::runtime_error("Unable to read shaderfile: " + fullPath); } }