Skip to content

Commit

Permalink
Added support for material loader in osgearth_bakefeaturetiles so tha…
Browse files Browse the repository at this point in the history
…t support material files are copied over as well if they exist.

Added --image-format flag to control what format images are written out as.
  • Loading branch information
jasonbeverage committed Nov 13, 2023
1 parent 6000887 commit a1da15c
Showing 1 changed file with 46 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <osgEarth/TileEstimator>
#include <osgEarth/SimplePager>
#include <osgEarth/NodeUtils>
#include <osgEarth/MaterialLoader>
#include <osgDB/WriteFile>
#include <osgDB/FileUtils>
#include <osg/TextureBuffer>
Expand Down Expand Up @@ -159,10 +160,12 @@ struct PrepareForWriting : public osg::NodeVisitor
struct WriteExternalImages : public osgEarth::TextureAndImageVisitor
{
std::string _destinationPath;
std::string _imageFormat;

WriteExternalImages(const std::string& destinationPath)
WriteExternalImages(const std::string& destinationPath, std::string& imageFormat)
: TextureAndImageVisitor(),
_destinationPath(destinationPath)
_destinationPath(destinationPath),
_imageFormat(imageFormat)
{
setTraversalMode(TRAVERSE_ALL_CHILDREN);
setNodeMaskOverride(~0L);
Expand Down Expand Up @@ -193,7 +196,16 @@ struct WriteExternalImages : public osgEarth::TextureAndImageVisitor

if (image.getWriteHint() != osg::Image::EXTERNAL_FILE)
{
std::string format = "dds";
auto getNormalMapFileName = MaterialUtils::getDefaultNormalMapNameMangler();
auto getPBRFileName = MaterialUtils::getDefaultPBRMapNameMangler();

URI normalMapURI(getNormalMapFileName(path));
osg::ref_ptr< osg::Image > normals = normalMapURI.getImage();

URI pbrURI(getPBRFileName(path));
osg::ref_ptr< osg::Image > pbr = pbrURI.getImage();

std::string format = _imageFormat;
unsigned int hash = osgEarth::hashString(path);

std::string relativeName = Stringify() << "../../images/" << hash << "." << format;
Expand All @@ -203,23 +215,45 @@ struct WriteExternalImages : public osgEarth::TextureAndImageVisitor
image.setFileName(relativeName);
image.setWriteHint(osg::Image::EXTERNAL_FILE);
osg::ref_ptr < osgDB::Options > options = new osgDB::Options;
options->setOptionString("ddsNoAutoFlipWrite");
if (format == "dds")
{
options->setOptionString("ddsNoAutoFlipWrite");
}
osgDB::makeDirectoryForFile(filename);
if (!osgDB::fileExists(filename))
{
osgDB::writeImageFile(image, filename, options.get());
}

if (normals.valid())
{
std::string normalFilename = getNormalMapFileName(filename);
if (!osgDB::fileExists(normalFilename))
{
osgDB::writeImageFile(*normals, normalFilename, options.get());
}
}

if (pbr.valid())
{
std::string pbrFilename = getPBRFileName(filename);
if (!osgDB::fileExists(pbrFilename))
{
osgDB::writeImageFile(*pbr, pbrFilename, options.get());
}
}
}
}
};

struct CreateTileHandler : public TileHandler
{
CreateTileHandler(SimplePager* simplePager, bool overwrite, std::string& path, std::string& ext)
CreateTileHandler(SimplePager* simplePager, bool overwrite, std::string& path, std::string& ext, std::string& imageFormat)
:_simplePager(simplePager),
_overwrite(overwrite),
_path(path),
_ext(ext)
_ext(ext),
_imageFormat(imageFormat)
{
if (::getenv(OSGEARTH_ENV_DEFAULT_COMPRESSOR) != 0L)
{
Expand Down Expand Up @@ -250,7 +284,7 @@ struct CreateTileHandler : public TileHandler
std::string path = osgDB::getFilePath(filename);

// Maybe make this optional
WriteExternalImages write(path);
WriteExternalImages write(path, _imageFormat);
node->accept(write);

osg::ref_ptr< osgDB::Options > options = new osgDB::Options;
Expand Down Expand Up @@ -290,6 +324,7 @@ struct CreateTileHandler : public TileHandler
std::string _compressorName;
std::string _path;
std::string _ext;
std::string _imageFormat;
};


Expand Down Expand Up @@ -518,7 +553,10 @@ main(int argc, char** argv)
std::string ext = "osgb";
args.read("--ext", ext);

visitor->setTileHandler(new CreateTileHandler(simplePager, overwrite, path, ext));
std::string imageFormat = "dds";
args.read("--image-format", imageFormat);

visitor->setTileHandler(new CreateTileHandler(simplePager, overwrite, path, ext, imageFormat));

// set the manual extents, if specified:
double minlat, minlon, maxlat, maxlon;
Expand Down

0 comments on commit a1da15c

Please sign in to comment.