| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | #include <osgDB/Output> |
|---|
| 14 | #include <osgDB/Registry> |
|---|
| 15 | #include <osgDB/FileNameUtils> |
|---|
| 16 | |
|---|
| 17 | #include <osg/Notify> |
|---|
| 18 | |
|---|
| 19 | #include <sstream> |
|---|
| 20 | #include <stdio.h> |
|---|
| 21 | #include <string.h> |
|---|
| 22 | |
|---|
| 23 | using namespace std; |
|---|
| 24 | using namespace osgDB; |
|---|
| 25 | |
|---|
| 26 | static osg::ApplicationUsageProxy Output_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_WRITE_OUT_DEFAULT_VALUES", "ON | OFF"); |
|---|
| 27 | |
|---|
| 28 | Output::Output() |
|---|
| 29 | { |
|---|
| 30 | init(); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | Output::Output(const char* name) : osgDB::ofstream(name) |
|---|
| 34 | { |
|---|
| 35 | init(); |
|---|
| 36 | _filename = name; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | Output::~Output() |
|---|
| 40 | { |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | void Output::init() |
|---|
| 45 | { |
|---|
| 46 | _indent = 0; |
|---|
| 47 | _indentStep = 2; |
|---|
| 48 | _numIndicesPerLine = 10; |
|---|
| 49 | _pathNameHint = AS_IS; |
|---|
| 50 | |
|---|
| 51 | _outputTextureFiles = false; |
|---|
| 52 | _textureFileNameNumber = 0; |
|---|
| 53 | |
|---|
| 54 | _outputShaderFiles = false; |
|---|
| 55 | _shaderFileNameNumber = 0; |
|---|
| 56 | |
|---|
| 57 | _writeOutDefaultValues = false; |
|---|
| 58 | |
|---|
| 59 | const char* env = getenv("OSG_WRITE_OUT_DEFAULT_VALUES"); |
|---|
| 60 | if (env) |
|---|
| 61 | { |
|---|
| 62 | _writeOutDefaultValues = strcmp(env,"ON")==0; |
|---|
| 63 | } |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | void Output::setOptions(const Options* options) |
|---|
| 67 | { |
|---|
| 68 | _options = options; |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | void Output::open(const char *name) |
|---|
| 72 | { |
|---|
| 73 | init(); |
|---|
| 74 | osgDB::ofstream::open(name); |
|---|
| 75 | _filename = name; |
|---|
| 76 | } |
|---|
| 77 | |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | Output& Output::indent() |
|---|
| 90 | { |
|---|
| 91 | for(int i=0;i<_indent;++i) *this<<' '; |
|---|
| 92 | return *this; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | void Output::moveIn() |
|---|
| 98 | { |
|---|
| 99 | _indent += _indentStep; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | |
|---|
| 103 | void Output::moveOut() |
|---|
| 104 | { |
|---|
| 105 | _indent -= _indentStep; |
|---|
| 106 | if (_indent<0) _indent=0; |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | std::string Output::wrapString(const char* str) |
|---|
| 110 | { |
|---|
| 111 | if (!str) return std::string("\"\""); |
|---|
| 112 | return wrapString(std::string(str)); |
|---|
| 113 | } |
|---|
| 114 | |
|---|
| 115 | std::string Output::wrapString(const std::string& str) |
|---|
| 116 | { |
|---|
| 117 | std::string newstring; |
|---|
| 118 | newstring += '"'; |
|---|
| 119 | for(unsigned int i=0;i<str.size();++i) |
|---|
| 120 | { |
|---|
| 121 | if (str[i]=='\\') |
|---|
| 122 | { |
|---|
| 123 | newstring += '\\'; |
|---|
| 124 | newstring += '\\'; |
|---|
| 125 | } |
|---|
| 126 | else if (str[i]=='"') |
|---|
| 127 | { |
|---|
| 128 | newstring += '\\'; |
|---|
| 129 | newstring += '"'; |
|---|
| 130 | } |
|---|
| 131 | else newstring += (str[i]); |
|---|
| 132 | } |
|---|
| 133 | newstring += '"'; |
|---|
| 134 | return newstring; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | bool Output::writeObject(const osg::Object& obj) |
|---|
| 138 | { |
|---|
| 139 | return Registry::instance()->getDeprecatedDotOsgObjectWrapperManager()->writeObject(obj,*this); |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | |
|---|
| 143 | void Output::writeBeginObject(const std::string& name) |
|---|
| 144 | { |
|---|
| 145 | indent() << name << " {" << std::endl; |
|---|
| 146 | } |
|---|
| 147 | |
|---|
| 148 | void Output::writeEndObject() |
|---|
| 149 | { |
|---|
| 150 | indent() << "}" << std::endl; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | void Output::writeUseID(const std::string& id) |
|---|
| 154 | { |
|---|
| 155 | indent() << "Use " << id << std::endl; |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | void Output::writeUniqueID(const std::string& id) |
|---|
| 159 | { |
|---|
| 160 | indent() << "UniqueID " << id << std::endl; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | bool Output::getUniqueIDForObject(const osg::Object* obj,std::string& uniqueID) |
|---|
| 164 | { |
|---|
| 165 | UniqueIDToLabelMapping::iterator fitr = _objectToUniqueIDMap.find(obj); |
|---|
| 166 | if (fitr != _objectToUniqueIDMap.end()) |
|---|
| 167 | { |
|---|
| 168 | uniqueID = (*fitr).second; |
|---|
| 169 | return true; |
|---|
| 170 | } |
|---|
| 171 | else return false; |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | bool Output::createUniqueIDForObject(const osg::Object* obj,std::string& uniqueID) |
|---|
| 176 | { |
|---|
| 177 | char str[256]; |
|---|
| 178 | sprintf(str,"%s_%i",obj->className(),(unsigned int)_objectToUniqueIDMap.size()); |
|---|
| 179 | uniqueID = str; |
|---|
| 180 | return true; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | |
|---|
| 184 | bool Output::registerUniqueIDForObject(const osg::Object* obj,std::string& uniqueID) |
|---|
| 185 | { |
|---|
| 186 | _objectToUniqueIDMap[obj] = uniqueID; |
|---|
| 187 | return true; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | std::string Output::getFileNameForOutput(const std::string& filename) const |
|---|
| 191 | { |
|---|
| 192 | switch(_pathNameHint) |
|---|
| 193 | { |
|---|
| 194 | case(FULL_PATH): |
|---|
| 195 | { |
|---|
| 196 | |
|---|
| 197 | OSG_WARN<<"Warning: Output::getFileNameForOutput() does not support FULL_PATH yet."<< std::endl; |
|---|
| 198 | return filename; |
|---|
| 199 | } |
|---|
| 200 | case(RELATIVE_PATH): |
|---|
| 201 | { |
|---|
| 202 | |
|---|
| 203 | OSG_WARN<<"Warning: Output::getFileNameForOutput() does not support RELATIVE_PATH yet."<< std::endl; |
|---|
| 204 | return filename; |
|---|
| 205 | } |
|---|
| 206 | case(FILENAME_ONLY): |
|---|
| 207 | |
|---|
| 208 | return getSimpleFileName(filename); |
|---|
| 209 | case(AS_IS): |
|---|
| 210 | default: |
|---|
| 211 | |
|---|
| 212 | return filename; |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | |
|---|
| 216 | std::string Output::getTextureFileNameForOutput() |
|---|
| 217 | { |
|---|
| 218 | std::string fileName = osgDB::getNameLessExtension(_filename); |
|---|
| 219 | if (_textureFileNameNumber>0) |
|---|
| 220 | { |
|---|
| 221 | std::ostringstream o; |
|---|
| 222 | o << '_' << _textureFileNameNumber; |
|---|
| 223 | fileName += o.str(); |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | fileName += ".dds"; |
|---|
| 227 | ++_textureFileNameNumber; |
|---|
| 228 | |
|---|
| 229 | return fileName; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | std::string Output::getShaderFileNameForOutput() |
|---|
| 233 | { |
|---|
| 234 | std::string fileName = osgDB::getNameLessExtension(_filename); |
|---|
| 235 | if (_shaderFileNameNumber>0) |
|---|
| 236 | { |
|---|
| 237 | std::ostringstream o; |
|---|
| 238 | o << '_' << _shaderFileNameNumber; |
|---|
| 239 | fileName += o.str(); |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | fileName += ".glsl"; |
|---|
| 243 | ++_shaderFileNameNumber; |
|---|
| 244 | |
|---|
| 245 | return fileName; |
|---|
| 246 | } |
|---|
| 247 | |
|---|
| 248 | void Output::setExternalFileWritten(const std::string& filename, bool hasBeenWritten) |
|---|
| 249 | { |
|---|
| 250 | _externalFileWritten[filename] = hasBeenWritten; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | bool Output::getExternalFileWritten(const std::string& filename) const |
|---|
| 254 | { |
|---|
| 255 | ExternalFileWrittenMap::const_iterator itr = _externalFileWritten.find(filename); |
|---|
| 256 | if (itr != _externalFileWritten.end()) return itr->second; |
|---|
| 257 | return false; |
|---|
| 258 | } |
|---|