Created
December 15, 2015 20:52
-
-
Save jasonbeverage/936c644916ebcee0d6c4 to your computer and use it in GitHub Desktop.
Simple application to load an osg model and take a screenshot of it.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2010 Robert Osfield | |
* | |
* This application is open source and may be redistributed and/or modified | |
* freely and without restriction, both in commercial and non commercial applications, | |
* as long as this copyright notice is maintained. | |
* | |
* This application 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. | |
*/ | |
#include <osgDB/ReadFile> | |
#include <osgDB/WriteFile> | |
#include <osgUtil/Optimizer> | |
#include <osgDB/FileNameUtils> | |
#include <osgViewer/Viewer> | |
#include <osgViewer/ViewerEventHandlers> | |
#include <osgGA/TrackballManipulator> | |
#include <iostream> | |
#include <stdint.h> | |
class WriteToFileCaptureOperation : public osgViewer::ScreenCaptureHandler::CaptureOperation | |
{ | |
public: | |
WriteToFileCaptureOperation(const std::string& filename): | |
_filename( filename ) | |
{ | |
} | |
virtual void operator()(const osg::Image& image, const unsigned int context_id) | |
{ | |
osgDB::writeImageFile(image, _filename ); | |
} | |
std::string _filename; | |
}; | |
int main(int argc, char** argv) | |
{ | |
// use an ArgumentParser object to manage the program arguments. | |
osg::ArgumentParser arguments(&argc,argv); | |
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName()); | |
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the standard OpenSceneGraph example which loads and visualises 3d models."); | |
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ..."); | |
arguments.getApplicationUsage()->addCommandLineOption("--image <filename>","Load an image and render it on a quad"); | |
arguments.getApplicationUsage()->addCommandLineOption("--dem <filename>","Load an image/DEM and render it on a HeightField"); | |
arguments.getApplicationUsage()->addCommandLineOption("--login <url> <username> <password>","Provide authentication information for http file access."); | |
arguments.getApplicationUsage()->addCommandLineOption("-p <filename>","Play specified camera path animation file, previously saved with 'z' key."); | |
arguments.getApplicationUsage()->addCommandLineOption("--speed <factor>","Speed factor for animation playing (1 == normal speed)."); | |
arguments.getApplicationUsage()->addCommandLineOption("--device <device-name>","add named device to the viewer"); | |
osgViewer::Viewer viewer(arguments); | |
unsigned int helpType = 0; | |
if ((helpType = arguments.readHelpType())) | |
{ | |
arguments.getApplicationUsage()->write(std::cout, helpType); | |
return 1; | |
} | |
// report any errors if they have occurred when parsing the program arguments. | |
if (arguments.errors()) | |
{ | |
arguments.writeErrorMessages(std::cout); | |
return 1; | |
} | |
if (arguments.argc()<=1) | |
{ | |
arguments.getApplicationUsage()->write(std::cout,osg::ApplicationUsage::COMMAND_LINE_OPTION); | |
return 1; | |
} | |
viewer.setCameraManipulator(new osgGA::TrackballManipulator()); | |
std::string screenCaptureFilename = "screen_shot.jpg"; | |
while(arguments.read("--screenshot", screenCaptureFilename)) {} | |
osg::ref_ptr<osgViewer::ScreenCaptureHandler::CaptureOperation> writeFile = new WriteToFileCaptureOperation(screenCaptureFilename); | |
osg::ref_ptr<osgViewer::ScreenCaptureHandler> screenCaptureHandler = new osgViewer::ScreenCaptureHandler(writeFile.get()); | |
// add the screen capture handler | |
viewer.addEventHandler(screenCaptureHandler); | |
// load the data | |
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments); | |
if (!loadedModel) | |
{ | |
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl; | |
return 1; | |
} | |
// any option left unread are converted into errors to write out later. | |
arguments.reportRemainingOptionsAsUnrecognized(); | |
// report any errors if they have occurred when parsing the program arguments. | |
if (arguments.errors()) | |
{ | |
arguments.writeErrorMessages(std::cout); | |
return 1; | |
} | |
viewer.setSceneData( loadedModel.get() ); | |
viewer.realize(); | |
while (!viewer.done()) | |
{ | |
screenCaptureHandler->setFramesToCapture(1); | |
screenCaptureHandler->captureNextFrame(viewer); | |
viewer.frame(); | |
screenCaptureHandler->stopCapture(); | |
return 0; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment