2006-07-18 23:21:48 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <osgDB/Output>
|
|
|
|
#include <osgDB/Registry>
|
|
|
|
#include <osgDB/FileNameUtils>
|
|
|
|
|
|
|
|
#include <osg/Notify>
|
|
|
|
|
2005-09-19 22:07:44 +08:00
|
|
|
#include <sstream>
|
2001-09-20 05:08:56 +08:00
|
|
|
#include <stdio.h>
|
2008-12-13 02:47:30 +08:00
|
|
|
#include <string.h>
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2001-12-15 07:18:28 +08:00
|
|
|
using namespace std;
|
2001-09-20 05:08:56 +08:00
|
|
|
using namespace osgDB;
|
|
|
|
|
2007-09-20 00:53:40 +08:00
|
|
|
static osg::ApplicationUsageProxy Output_e0(osg::ApplicationUsage::ENVIRONMENTAL_VARIABLE,"OSG_WRITE_OUT_DEFAULT_VALUES", "ON | OFF");
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
Output::Output()
|
|
|
|
{
|
|
|
|
init();
|
|
|
|
}
|
|
|
|
|
2008-11-07 23:08:08 +08:00
|
|
|
Output::Output(const char* name) : osgDB::ofstream(name)
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
init();
|
|
|
|
_filename = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
Output::~Output()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Output::init()
|
|
|
|
{
|
|
|
|
_indent = 0;
|
|
|
|
_indentStep = 2;
|
|
|
|
_numIndicesPerLine = 10;
|
|
|
|
_pathNameHint = AS_IS;
|
2008-03-04 22:04:48 +08:00
|
|
|
|
2005-09-19 22:07:44 +08:00
|
|
|
_outputTextureFiles = false;
|
|
|
|
_textureFileNameNumber = 0;
|
2008-03-04 22:04:48 +08:00
|
|
|
|
|
|
|
_outputShaderFiles = false;
|
|
|
|
_shaderFileNameNumber = 0;
|
|
|
|
|
2007-09-17 01:46:33 +08:00
|
|
|
_writeOutDefaultValues = false;
|
2007-09-20 00:53:40 +08:00
|
|
|
|
|
|
|
const char* env = getenv("OSG_WRITE_OUT_DEFAULT_VALUES");
|
|
|
|
if (env)
|
|
|
|
{
|
|
|
|
_writeOutDefaultValues = strcmp(env,"ON")==0;
|
|
|
|
}
|
2007-09-17 01:46:33 +08:00
|
|
|
}
|
|
|
|
|
2009-05-09 16:49:27 +08:00
|
|
|
void Output::setOptions(const Options* options)
|
2007-09-17 01:46:33 +08:00
|
|
|
{
|
|
|
|
_options = options;
|
2001-09-20 05:08:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Output::open(const char *name)
|
|
|
|
{
|
|
|
|
init();
|
2008-11-07 23:08:08 +08:00
|
|
|
osgDB::ofstream::open(name);
|
2001-09-20 05:08:56 +08:00
|
|
|
_filename = name;
|
|
|
|
}
|
|
|
|
|
2002-01-21 00:24:54 +08:00
|
|
|
// Comment out to avoid compile errors under new compilers, the int mode
|
|
|
|
// is now a replaced by a class to wrap the mode.
|
|
|
|
// This method is not used right now to hopefully nobody will miss it...
|
|
|
|
// Jan 2002.
|
|
|
|
// void Output::open(const char *name,int mode)
|
|
|
|
// {
|
|
|
|
// init();
|
|
|
|
// ofstream::open(name,mode);
|
|
|
|
// _filename = name;
|
|
|
|
// }
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
Output& Output::indent()
|
|
|
|
{
|
|
|
|
for(int i=0;i<_indent;++i) *this<<' ';
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-28 20:55:19 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
void Output::moveIn()
|
|
|
|
{
|
|
|
|
_indent += _indentStep;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Output::moveOut()
|
|
|
|
{
|
|
|
|
_indent -= _indentStep;
|
|
|
|
if (_indent<0) _indent=0;
|
|
|
|
}
|
|
|
|
|
2008-06-19 00:45:21 +08:00
|
|
|
std::string Output::wrapString(const char* str)
|
|
|
|
{
|
|
|
|
if (!str) return std::string("\"\"");
|
|
|
|
return wrapString(std::string(str));
|
|
|
|
}
|
|
|
|
|
2002-10-28 20:55:19 +08:00
|
|
|
std::string Output::wrapString(const std::string& str)
|
|
|
|
{
|
|
|
|
std::string newstring;
|
2002-11-04 23:33:08 +08:00
|
|
|
newstring += '"';
|
2002-10-28 20:55:19 +08:00
|
|
|
for(unsigned int i=0;i<str.size();++i)
|
|
|
|
{
|
2008-05-28 00:32:17 +08:00
|
|
|
if (str[i]=='\\')
|
|
|
|
{
|
|
|
|
newstring += '\\';
|
|
|
|
newstring += '\\';
|
|
|
|
}
|
|
|
|
else if (str[i]=='"')
|
2005-11-17 21:35:53 +08:00
|
|
|
{
|
|
|
|
newstring += '\\';
|
|
|
|
newstring += '"';
|
|
|
|
}
|
|
|
|
else newstring += (str[i]);
|
2002-10-28 20:55:19 +08:00
|
|
|
}
|
2002-11-04 23:33:08 +08:00
|
|
|
newstring += '"';
|
2002-10-28 20:55:19 +08:00
|
|
|
return newstring;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
bool Output::writeObject(const osg::Object& obj)
|
|
|
|
{
|
|
|
|
return Registry::instance()->writeObject(obj,*this);
|
|
|
|
}
|
|
|
|
|
2007-05-09 18:01:15 +08:00
|
|
|
|
|
|
|
void Output::writeBeginObject(const std::string& name)
|
|
|
|
{
|
|
|
|
indent() << name << " {" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::writeEndObject()
|
|
|
|
{
|
|
|
|
indent() << "}" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::writeUseID(const std::string& id)
|
|
|
|
{
|
|
|
|
indent() << "Use " << id << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Output::writeUniqueID(const std::string& id)
|
|
|
|
{
|
|
|
|
indent() << "UniqueID " << id << std::endl;
|
|
|
|
}
|
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
bool Output::getUniqueIDForObject(const osg::Object* obj,std::string& uniqueID)
|
|
|
|
{
|
|
|
|
UniqueIDToLabelMapping::iterator fitr = _objectToUniqueIDMap.find(obj);
|
|
|
|
if (fitr != _objectToUniqueIDMap.end())
|
|
|
|
{
|
|
|
|
uniqueID = (*fitr).second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Output::createUniqueIDForObject(const osg::Object* obj,std::string& uniqueID)
|
|
|
|
{
|
|
|
|
char str[256];
|
2002-07-14 05:17:40 +08:00
|
|
|
sprintf(str,"%s_%i",obj->className(),(unsigned int)_objectToUniqueIDMap.size());
|
2001-09-20 05:08:56 +08:00
|
|
|
uniqueID = str;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool Output::registerUniqueIDForObject(const osg::Object* obj,std::string& uniqueID)
|
|
|
|
{
|
|
|
|
_objectToUniqueIDMap[obj] = uniqueID;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-05-17 21:15:54 +08:00
|
|
|
std::string Output::getFileNameForOutput(const std::string& filename) const
|
2001-09-20 05:08:56 +08:00
|
|
|
{
|
|
|
|
switch(_pathNameHint)
|
|
|
|
{
|
|
|
|
case(FULL_PATH):
|
|
|
|
{
|
|
|
|
// need to think about how best to implement this first...
|
2001-12-15 07:18:28 +08:00
|
|
|
osg::notify(osg::WARN)<<"Warning: Output::getFileNameForOutput() does not support FULL_PATH yet."<< std::endl;
|
2001-09-20 05:08:56 +08:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
case(RELATIVE_PATH):
|
|
|
|
{
|
|
|
|
// need to think about how best to implement this as well...
|
2001-12-15 07:18:28 +08:00
|
|
|
osg::notify(osg::WARN)<<"Warning: Output::getFileNameForOutput() does not support RELATIVE_PATH yet."<< std::endl;
|
2001-09-20 05:08:56 +08:00
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
case(FILENAME_ONLY):
|
|
|
|
// this one is straight forward.
|
|
|
|
return getSimpleFileName(filename);
|
|
|
|
case(AS_IS):
|
|
|
|
default:
|
|
|
|
// and this one is even more trivial.
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
}
|
2005-09-19 22:07:44 +08:00
|
|
|
|
|
|
|
std::string Output::getTextureFileNameForOutput()
|
|
|
|
{
|
|
|
|
std::string fileName = osgDB::getNameLessExtension(_filename);
|
|
|
|
if (_textureFileNameNumber>0)
|
|
|
|
{
|
|
|
|
std::ostringstream o;
|
|
|
|
o << '_' << _textureFileNameNumber;
|
|
|
|
fileName += o.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName += ".dds";
|
|
|
|
++_textureFileNameNumber;
|
|
|
|
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|
2008-03-04 22:04:48 +08:00
|
|
|
std::string Output::getShaderFileNameForOutput()
|
|
|
|
{
|
|
|
|
std::string fileName = osgDB::getNameLessExtension(_filename);
|
|
|
|
if (_shaderFileNameNumber>0)
|
|
|
|
{
|
|
|
|
std::ostringstream o;
|
|
|
|
o << '_' << _shaderFileNameNumber;
|
|
|
|
fileName += o.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
fileName += ".glsl";
|
|
|
|
++_shaderFileNameNumber;
|
|
|
|
|
|
|
|
return fileName;
|
|
|
|
}
|
|
|
|
|