Removed slideshow3D example source code, since it being moved to a seperate

distribution.
This commit is contained in:
Robert Osfield
2004-02-04 13:19:32 +00:00
parent 77840e62e7
commit 9ca8d6a88e
10 changed files with 0 additions and 2167 deletions

View File

@@ -1,22 +0,0 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
SlideShowConstructor.cpp\
SlideEventHandler.cpp\
PointsEventHandler.cpp\
ReaderWriterXML.cpp\
slideshow3D.cpp\
LIBS += -lxml2 -losgProducer -lProducer -losgText -losgGA -losgDB -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
INSTFILES = \
$(CXXFILES)\
GNUmakefile.inst=GNUmakefile
EXEC = slideshow3D
INC += $(X_INC) $(LIBXML2_INCLUDE)
include $(TOPDIR)/Make/makerules

View File

@@ -1,18 +0,0 @@
TOPDIR = ../..
include $(TOPDIR)/Make/makedefs
CXXFILES =\
SlideShowConstructor.cpp\
SlideEventHandler.cpp\
DefaultPresentation.cpp\
ReaderWriterXML.cpp\
slideshow3D.cpp\
LIBS += -lxml2 -losgProducer -lProducer -losgDB -losgText -losgUtil -losg $(GL_LIBS) $(X_LIBS) $(OTHER_LIBS)
EXEC = slideshow3D
INC += $(PRODUCER_INCLUDE_DIR) $(X_INC) $(LIBXML2_INCLUDE)
LDFLAGS += $(PRODUCER_LIB_DIR)
include $(TOPDIR)/Make/makerules

View File

@@ -1,79 +0,0 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the GNU Public License (GPL) version 1.0 or
* (at your option) any later version.
*
* 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.
*/
#include "PointsEventHandler.h"
PointsEventHandler::PointsEventHandler()
{
_point = new osg::Point;
//_point->setDistanceAttenuation(osg::Vec3(0.0,0.0005,0.0f));
_point->setDistanceAttenuation(osg::Vec3(0.0,0.0000,0.000005f));
}
bool PointsEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if (ea.getKey()=='+' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Add)
{
changePointSize(1.0f);
return true;
}
else if (ea.getKey()=='-' || ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Subtract)
{
changePointSize(-1.0f);
return true;
}
break;
}
default:
break;
}
return false;
}
void PointsEventHandler::accept(osgGA::GUIEventHandlerVisitor& v)
{
v.visit(*this);
}
void PointsEventHandler::getUsage(osg::ApplicationUsage& usage) const
{
usage.addKeyboardMouseBinding("+","Increase point size");
usage.addKeyboardMouseBinding("-","Reduce point size");
}
float PointsEventHandler::getPointSize() const
{
return _point->getSize();
}
void PointsEventHandler::setPointSize(float psize)
{
if (psize>0.0)
{
_point->setSize(psize);
_stateset->setAttribute(_point.get());
}
else
{
_stateset->setAttribute(_point.get(),osg::StateAttribute::INHERIT);
}
std::cout<<"Point size "<<psize<<std::endl;
}
void PointsEventHandler::changePointSize(float delta)
{
setPointSize(getPointSize()+delta);
}

View File

@@ -1,49 +0,0 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the GNU Public License (GPL) version 1.0 or
* (at your option) any later version.
*
* 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.
*/
#ifndef POINTSEVENTHANDLER
#define POINTSEVENTHANDLER 1
#include <osg/StateSet>
#include <osg/Point>
#include <osgGA/GUIEventHandler>
class PointsEventHandler : public osgGA::GUIEventHandler
{
public:
PointsEventHandler();
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
virtual void accept(osgGA::GUIEventHandlerVisitor& v);
void getUsage(osg::ApplicationUsage& usage) const;
void setStateSet(osg::StateSet* stateset) { _stateset=stateset; }
osg::StateSet* getStateSet() { return _stateset.get(); }
const osg::StateSet* getStateSet() const { return _stateset.get(); }
void setPointSize(float psize);
float getPointSize() const;
void changePointSize(float delta);
osg::ref_ptr<osg::StateSet> _stateset;
osg::ref_ptr<osg::Point> _point;
};
#endif

View File

@@ -1,530 +0,0 @@
#include <osgDB/ReaderWriter>
#include <osgDB/FileNameUtils>
#include <osgDB/FileUtils>
#include <osgDB/Registry>
#include "SlideShowConstructor.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
#include <sstream>
/**
* OpenSceneGraph plugin wrapper/converter.
*/
class ReaderWriterSS3D : public osgDB::ReaderWriter
{
public:
ReaderWriterSS3D()
{
_colorMap["WHITE"] .set(1.0f,1.0f,1.0f,1.0f);
_colorMap["BLACK"] .set(0.0f,0.0f,0.0f,1.0f);
_colorMap["PURPLE"] .set(1.0f,0.0f,1.0f,1.0f);
_colorMap["BLUE"] .set(0.0f,0.0f,1.0f,1.0f);
_colorMap["RED"] .set(1.0f,0.0f,0.0f,1.0f);
_colorMap["CYAN"] .set(0.0f,1.0f,1.0f,1.0f);
_colorMap["YELLOW"] .set(1.0f,1.0f,0.0f,1.0f);
_colorMap["GREEN"] .set(0.0f,1.0f,0.0f,1.0f);
}
virtual const char* className()
{
return "slideshow3D XML Reader/Writer";
}
virtual bool acceptsExtension(const std::string& extension)
{
return osgDB::equalCaseInsensitive(extension,"ss3d") ||
osgDB::equalCaseInsensitive(extension,"xml") ;
}
virtual ReadResult readNode(const std::string& fileName,
const osgDB::ReaderWriter::Options* options);
void parseModel(SlideShowConstructor& constructor, xmlDocPtr doc, xmlNodePtr cur);
void parseStereoPair(SlideShowConstructor& constructor, xmlDocPtr doc, xmlNodePtr cur);
void parseLayer(SlideShowConstructor& constructor, xmlDocPtr doc, xmlNodePtr cur);
void parseSlide (SlideShowConstructor& constructor, xmlDocPtr doc, xmlNodePtr cur);
osg::Vec4 mapStringToColor(const std::string& str)
{
return _colorMap[str];
}
inline bool read(const char* str, float& value) const;
inline bool read(const char* str, osg::Vec2& value) const;
inline bool read(const char* str, osg::Vec3& value) const;
inline bool read(const char* str, osg::Vec4& value) const;
inline bool read(const std::string& str, float& value) const;
inline bool read(const std::string& str, osg::Vec2& value) const;
inline bool read(const std::string& str, osg::Vec3& value) const;
inline bool read(const std::string& str, osg::Vec4& value) const;
bool getProperty(xmlNodePtr cur, const char* token) const;
bool getProperty(xmlNodePtr cur, const char* token, float& value) const;
bool getProperty(xmlNodePtr cur, const char* token, osg::Vec2& value) const;
bool getProperty(xmlNodePtr cur, const char* token, osg::Vec3& value) const;
bool getProperty(xmlNodePtr cur, const char* token, osg::Vec4& value) const;
bool getProperty(xmlNodePtr cur, const char* token, std::string& value) const;
std::map<std::string,osg::Vec4> _colorMap;
};
// Register with Registry to instantiate the above reader/writer.
osgDB::RegisterReaderWriterProxy<ReaderWriterSS3D> g_readerWriter_SS3D_Proxy;
bool ReaderWriterSS3D::read(const char* str, float& value) const
{
if (!str) return false;
std::istringstream iss((const char*)str);
iss >> value;
return !iss.fail();
}
bool ReaderWriterSS3D::read(const char* str, osg::Vec2& value) const
{
if (!str) return false;
std::istringstream iss((const char*)str);
iss >> value.x() >> value.y();
return !iss.fail();
}
bool ReaderWriterSS3D::read(const char* str, osg::Vec3& value) const
{
if (!str) return false;
std::istringstream iss((const char*)str);
iss >> value.x() >> value.y() >> value.z();
return !iss.fail();
}
bool ReaderWriterSS3D::read(const char* str, osg::Vec4& value) const
{
if (!str) return false;
std::istringstream iss((const char*)str);
iss >> value.x() >> value.y() >> value.z() >> value.w();
return !iss.fail();
}
bool ReaderWriterSS3D::read(const std::string& str, float& value) const
{
std::istringstream iss(str);
iss >> value;
return !iss.fail();
}
bool ReaderWriterSS3D::read(const std::string& str, osg::Vec2& value) const
{
std::istringstream iss(str);
iss >> value.x() >> value.y();
return !iss.fail();
}
bool ReaderWriterSS3D::read(const std::string& str, osg::Vec3& value) const
{
std::istringstream iss(str);
iss >> value.x() >> value.y() >> value.z();
return !iss.fail();
}
bool ReaderWriterSS3D::read(const std::string& str, osg::Vec4& value) const
{
std::istringstream iss(str);
iss >> value.x() >> value.y() >> value.z() >> value.w();
return !iss.fail();
}
bool ReaderWriterSS3D::getProperty(xmlNodePtr cur, const char* token) const
{
bool success = false;
xmlChar *key;
key = xmlGetProp (cur, (const xmlChar *)token);
if (key) success=true;
xmlFree(key);
return success;
}
bool ReaderWriterSS3D::getProperty(xmlNodePtr cur, const char* token, float& value) const
{
xmlChar *key;
key = xmlGetProp (cur, (const xmlChar *)token);
bool success = read((const char*)key,value);
xmlFree(key);
return success;
}
bool ReaderWriterSS3D::getProperty(xmlNodePtr cur, const char* token, osg::Vec2& value) const
{
xmlChar *key;
key = xmlGetProp (cur, (const xmlChar *)token);
bool success = read((const char*)key,value);
xmlFree(key);
return success;
}
bool ReaderWriterSS3D::getProperty(xmlNodePtr cur, const char* token, osg::Vec3& value) const
{
xmlChar *key;
key = xmlGetProp (cur, (const xmlChar *)token);
bool success = read((const char*)key,value);
xmlFree(key);
return success;
}
bool ReaderWriterSS3D::getProperty(xmlNodePtr cur, const char* token, osg::Vec4& value) const
{
xmlChar *key;
key = xmlGetProp (cur, (const xmlChar *)token);
bool success = read((const char*)key,value);
xmlFree(key);
return success;
}
bool ReaderWriterSS3D::getProperty(xmlNodePtr cur, const char* token, std::string& value) const
{
bool success = false;
xmlChar *key;
key = xmlGetProp (cur, (const xmlChar *)token);
if (key)
{
success = true;
value = (const char*)key;
}
xmlFree(key);
return success;
}
void ReaderWriterSS3D::parseModel(SlideShowConstructor& constructor, xmlDocPtr doc, xmlNodePtr cur)
{
std::string filename;
SlideShowConstructor::CoordinateFrame coordinate_frame = SlideShowConstructor::SLIDE;
osg::Vec3 position(0.0f,1.0f,0.0f);
osg::Vec4 rotate(0.0f,0.0f,0.0f,1.0f);
float scale = 1.0f;
osg::Vec4 rotation(0.0f,0.0f,0.0f,1.0f);
std::string animation_path;
std::string camera_path;
// temporary
std::string str;
if (getProperty(cur, "coordinate_frame", str))
{
if (str=="model") coordinate_frame = SlideShowConstructor::MODEL;
else if (str=="slide") coordinate_frame = SlideShowConstructor::SLIDE;
else std::cout<<"Parser error - coordinate_frame=\""<<str<<"\" unrecongonized value"<<std::endl;
}
if (coordinate_frame==SlideShowConstructor::SLIDE)
{
position.set(0.5,0.5,0.0);
if (getProperty(cur, "position", str))
{
osg::Vec2 vec2;
osg::Vec3 vec3;
bool fail = false;
if (str=="center") position.set(0.5f,.5f,0.0f);
else if (str=="eye") position.set(0.0f,0.0f,1.0f);
else if (read(str,vec3)) position = vec3;
else if (read(str,vec2)) position.set(vec3.x(),vec3.y(),0.0f);
else fail = true;
if (fail) std::cout<<"Parser error - position=\""<<str<<"\" unrecongonized value"<<std::endl;
else std::cout<<"Read position="<<position<<std::endl;
}
}
else // coordinate_frame==SlideShowConstructor::MODEL
{
position.set(0.0,0.0,0.0);
if (getProperty(cur, "position", str))
{
bool fail = false;
if (str=="center") position.set(0.0f,1.0f,0.0f);
else if (str=="eye") position.set(0.0f,0.0f,0.0f);
else if (!read(str,position)) fail = true;
if (fail) std::cout<<"Parser error - position=\""<<str<<"\" unrecongonized value"<<std::endl;
else std::cout<<"Read position="<<position<<std::endl;
}
}
if (getProperty(cur, "scale", scale))
{
std::cout<<"scale read "<<scale<<std::endl;
}
if (getProperty(cur, "rotate", rotate))
{
std::cout<<"rotate read "<<rotate<<std::endl;
}
if (getProperty(cur, "rotation", rotation))
{
std::cout<<"rotation read "<<rotation<<std::endl;
}
if (getProperty(cur, "path", animation_path))
{
std::cout<<"path read "<<animation_path<<std::endl;
}
if (getProperty(cur, "camera_path", camera_path))
{
std::cout<<"camera path read "<<camera_path<<std::endl;
}
xmlChar *key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) filename = (const char*)key;
xmlFree(key);
if (!filename.empty())
{
if (!camera_path.empty())
{
constructor.addModelWithCameraPath(filename,coordinate_frame,position,scale,rotate,camera_path);
}
else if (!animation_path.empty())
{
constructor.addModelWithPath(filename,coordinate_frame,position,scale,rotate,animation_path);
}
else
{
constructor.addModel(filename,coordinate_frame,position,scale,rotate,rotation);
}
}
}
void ReaderWriterSS3D::parseStereoPair(SlideShowConstructor& constructor, xmlDocPtr doc, xmlNodePtr cur)
{
std::string filenameLeft;
std::string filenameRight;
float height = 1.0f;
xmlChar *key;
key = xmlGetProp (cur, (const xmlChar *)"height");
if (key) height = atof((const char*)key);
xmlFree(key);
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
if ((!xmlStrcmp(cur->name, (const xmlChar *)"image_left")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) filenameLeft = (const char*)key;
xmlFree(key);
}
if ((!xmlStrcmp(cur->name, (const xmlChar *)"image_right")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) filenameRight = (const char*)key;
xmlFree(key);
}
cur = cur->next;
}
if (!filenameLeft.empty() && !filenameRight.empty())
constructor.addStereoImagePair(filenameLeft,filenameRight,height);
}
void ReaderWriterSS3D::parseLayer(SlideShowConstructor& constructor, xmlDocPtr doc, xmlNodePtr cur)
{
constructor.addLayer();
xmlChar *key;
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
if ((!xmlStrcmp(cur->name, (const xmlChar *)"bullet")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.addBullet((const char*)key);
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"paragraph")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.addParagraph((const char*)key);
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"image")))
{
std::string filename;
float height = 1.0f;
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) filename = (const char*)key;
xmlFree(key);
key = xmlGetProp (cur, (const xmlChar *)"height");
if (key) height = atoi((const char*)key);
xmlFree(key);
if (!filename.empty()) constructor.addImage(filename,height);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"stereo_pair")))
{
parseStereoPair(constructor, doc,cur);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"model")))
{
parseModel(constructor, doc,cur);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"duration")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.setLayerDuration(atof((const char*)key));
xmlFree(key);
}
cur = cur->next;
}
}
void ReaderWriterSS3D::parseSlide (SlideShowConstructor& constructor, xmlDocPtr doc, xmlNodePtr cur)
{
constructor.addSlide();
xmlChar *key;
cur = cur->xmlChildrenNode;
while (cur != NULL)
{
if ((!xmlStrcmp(cur->name, (const xmlChar *)"title")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.setSlideTitle((const char*)key);
else constructor.setSlideTitle("");
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"background")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.setSlideBackground((const char*)key);
else constructor.setSlideBackground("");
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"layer")))
{
parseLayer (constructor, doc, cur);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"duration")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.setSlideDuration(atof((const char*)key));
xmlFree(key);
}
cur = cur->next;
}
return;
}
osgDB::ReaderWriter::ReadResult ReaderWriterSS3D::readNode(const std::string& file,
const osgDB::ReaderWriter::Options*)
{
std::string ext = osgDB::getLowerCaseFileExtension(file);
if (!acceptsExtension(ext)) return ReadResult::FILE_NOT_HANDLED;
std::string fileName = osgDB::findDataFile( file );
if (fileName.empty()) return ReadResult::FILE_NOT_FOUND;
xmlDocPtr doc;
xmlNodePtr cur;
doc = xmlParseFile(fileName.c_str());
if (doc == NULL ) {
fprintf(stderr,"Document not parsed successfully. \n");
return ReadResult::FILE_NOT_HANDLED;
}
cur = xmlDocGetRootElement(doc);
if (cur == NULL) {
fprintf(stderr,"empty document\n");
xmlFreeDoc(doc);
return ReadResult::FILE_NOT_HANDLED;
}
if (xmlStrcmp(cur->name, (const xmlChar *) "presentation")) {
fprintf(stderr,"document of the wrong type, root node != story");
xmlFreeDoc(doc);
return ReadResult::FILE_NOT_HANDLED;
}
SlideShowConstructor constructor;
xmlChar *key;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
if ((!xmlStrcmp(cur->name, (const xmlChar *)"name")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.setPresentationName((const char*)key);
else constructor.setPresentationName("");
xmlFree(key);
}
/*else if ((!xmlStrcmp(cur->name, (const xmlChar *)"ratio")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.setPresentationAspectRatio((const char*)key);
xmlFree(key);
}*/
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"bgcolor")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.setBackgroundColor(mapStringToColor((const char*)key));
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"textcolor")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.setTextColor(mapStringToColor((const char*)key));
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"duration")))
{
key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
if (key) constructor.setPresentationDuration(atof((const char*)key));
xmlFree(key);
}
else if ((!xmlStrcmp(cur->name, (const xmlChar *)"slide")))
{
parseSlide (constructor, doc, cur);
}
cur = cur->next;
}
xmlFreeDoc(doc);
return constructor.takePresentation();
}

View File

@@ -1,375 +0,0 @@
#include "SlideEventHandler.h"
#include "SlideShowConstructor.h"
#include <osg/AnimationPath>
#include <osgUtil/TransformCallback>
class FindNamedSwitchVisitor : public osg::NodeVisitor
{
public:
FindNamedSwitchVisitor(const std::string& name):
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
_name(name),
_switch(0) {}
void apply(osg::Switch& sw)
{
if (sw.getName().find(_name)!=std::string::npos)
{
_switch = &sw;
return; // note, no need to do traverse now we've located the relevant switch
}
traverse(sw);
}
std::string _name;
osg::Switch* _switch;
};
SlideEventHandler::SlideEventHandler():
_presentationSwitch(0),
_activeSlide(0),
_slideSwitch(0),
_activeLayer(0),
_firstTraversal(true),
_previousTime(-1.0f),
_timePerSlide(1.0),
_autoSteppingActive(false),
_loopPresentation(true),
_pause(false)
{
}
void SlideEventHandler::set(osg::Node* model)
{
FindNamedSwitchVisitor findPresentation("Presentation");
model->accept(findPresentation);
if (findPresentation._switch)
{
std::cout<<"Presentation '"<<model->getName()<<"'"<<std::endl;
_presentationSwitch = findPresentation._switch;
SlideShowConstructor::Duration* durationData = dynamic_cast<SlideShowConstructor::Duration*>(_presentationSwitch->getUserData());
if (durationData)
{
std::cout<<"Presentation time set to "<<durationData->duration<<std::endl;
_timePerSlide = durationData->duration;
}
selectSlide(0);
}
else
{
std::cout<<"No presentation present in scene."<<std::endl;
_presentationSwitch = 0;
_activeSlide = 0;
FindNamedSwitchVisitor findSlide("Slide");
model->accept(findSlide);
if (findSlide._switch)
{
std::cout<<"Found presentation slide"<<findSlide._switch->getName()<<std::endl;
_slideSwitch = findSlide._switch;
selectLayer(0);
}
else
{
std::cout<<"No slides present in scene, unable to operate as a slideshow."<<std::endl;
}
}
}
double SlideEventHandler::getCurrentTimeDelayBetweenSlides() const
{
const SlideShowConstructor::Duration* durationData = dynamic_cast<const SlideShowConstructor::Duration*>(_slideSwitch->getChild(_activeLayer)->getUserData());
if (durationData)
{
return durationData->duration;
}
durationData = dynamic_cast<const SlideShowConstructor::Duration*>(_slideSwitch->getUserData());
if (durationData)
{
return durationData->duration;
}
return _timePerSlide;
}
bool SlideEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&)
{
switch(ea.getEventType())
{
case(osgGA::GUIEventAdapter::FRAME):
{
if (_autoSteppingActive && !_pause)
{
double time = ea.time();
if (_firstTraversal)
{
_firstTraversal = false;
_previousTime = time;
}
else if (time-_previousTime>getCurrentTimeDelayBetweenSlides())
{
_previousTime = time;
nextLayerOrSlide();
}
}
}
case(osgGA::GUIEventAdapter::KEYDOWN):
{
if (ea.getKey()=='a')
{
_autoSteppingActive = !_autoSteppingActive;
_previousTime = ea.time();
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Home ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Home)
{
_autoSteppingActive = false;
selectSlide(0);
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_End ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_End)
{
_autoSteppingActive = false;
selectSlide(LAST_POSITION,LAST_POSITION);
return true;
}
else if (ea.getKey()=='n' ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Down ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Down)
{
_autoSteppingActive = false;
nextLayerOrSlide();
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Up ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Up)
{
_autoSteppingActive = false;
previousLayerOrSlide();
return true;
}
else if (ea.getKey()=='N' ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Right ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Right ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Page_Down ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Page_Down)
{
_autoSteppingActive = false;
nextSlide();
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Left ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Left ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_Page_Up ||
ea.getKey()==osgGA::GUIEventAdapter::KEY_KP_Page_Up)
{
_autoSteppingActive = false;
previousSlide();
return true;
}
else if (ea.getKey()==osgGA::GUIEventAdapter::KEY_Pause ||
ea.getKey()=='p')
{
_pause = !_pause;
resetUpdateCallbackActivity();
}
else if (ea.getKey()=='r')
{
resetUpdateCallbacks();
}
return false;
}
default:
return false;
}
}
void SlideEventHandler::getUsage(osg::ApplicationUsage& usage) const
{
usage.addKeyboardMouseBinding("a","Toggle on/off the automatic advancement for image to image");
usage.addKeyboardMouseBinding("n","Advance to next layer or slide");
usage.addKeyboardMouseBinding("p","Move to previous layer or slide");
}
bool SlideEventHandler::selectSlide(unsigned int slideNum,unsigned int layerNum)
{
if (!_presentationSwitch) return false;
if (slideNum==LAST_POSITION && _presentationSwitch->getNumChildren()>0)
{
slideNum = _presentationSwitch->getNumChildren()-1;
}
if (slideNum>=_presentationSwitch->getNumChildren()) return false;
_activeSlide = slideNum;
_presentationSwitch->setSingleChildOn(_activeSlide);
//std::cout<<"Selected slide '"<<_presentationSwitch->getChild(_activeSlide)->getName()<<"'"<<std::endl;
FindNamedSwitchVisitor findSlide("Slide");
_presentationSwitch->getChild(_activeSlide)->accept(findSlide);
if (findSlide._switch)
{
//std::cout<<"Found slide '"<<findSlide._switch->getName()<<"'"<<std::endl;
_slideSwitch = findSlide._switch;
return selectLayer(layerNum);
}
else
{
//std::cout<<"Not found slide"<<std::endl;
}
return false;
}
bool SlideEventHandler::selectLayer(unsigned int layerNum)
{
if (!_slideSwitch) return false;
if (layerNum==LAST_POSITION && _slideSwitch->getNumChildren()>0)
{
layerNum = _slideSwitch->getNumChildren()-1;
}
if (layerNum>=_slideSwitch->getNumChildren()) return false;
_activeLayer = layerNum;
_slideSwitch->setSingleChildOn(_activeLayer);
//std::cout<<"Selected layer '"<<_slideSwitch->getChild(_activeLayer)->getName()<<"' num="<<_activeLayer<< std::endl;
return true;
}
bool SlideEventHandler::nextLayerOrSlide()
{
if (nextLayer()) return true;
else return nextSlide();
}
bool SlideEventHandler::previousLayerOrSlide()
{
if (previousLayer()) return true;
else return previousSlide();
}
bool SlideEventHandler::nextSlide()
{
if (selectSlide(_activeSlide+1)) return true;
else if (_loopPresentation) return selectSlide(0);
else return false;
}
bool SlideEventHandler::previousSlide()
{
if (_activeSlide>0) return selectSlide(_activeSlide-1,LAST_POSITION);
else if (_loopPresentation && _presentationSwitch.valid()) return selectSlide(_presentationSwitch->getNumChildren()-1,LAST_POSITION);
else return false;
}
bool SlideEventHandler::nextLayer()
{
return selectLayer(_activeLayer+1);
}
bool SlideEventHandler::previousLayer()
{
if (_activeLayer>0) return selectLayer(_activeLayer-1);
else return false;
}
class ResetUpdateCallbacksVisitor : public osg::NodeVisitor
{
public:
ResetUpdateCallbacksVisitor(bool pause):
// osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
_pause(pause) {}
void apply(osg::Node& node)
{
osg::AnimationPathCallback* apc = dynamic_cast<osg::AnimationPathCallback*>(node.getUpdateCallback());
osgUtil::TransformCallback* tc = dynamic_cast<osgUtil::TransformCallback*>(node.getUpdateCallback());
if (apc)
{
apc->reset();
apc->update(node);
}
if (tc)
{
//tc->reset();
}
traverse(node);
}
bool _pause;
};
void SlideEventHandler::resetUpdateCallbacks()
{
ResetUpdateCallbacksVisitor rucv(_pause);
_presentationSwitch->accept(rucv);
}
class ActivityUpdateCallbacksVisitor : public osg::NodeVisitor
{
public:
ActivityUpdateCallbacksVisitor(bool pause):
// osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN),
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN),
_pause(pause) {}
void apply(osg::Node& node)
{
osg::AnimationPathCallback* apc = dynamic_cast<osg::AnimationPathCallback*>(node.getUpdateCallback());
osgUtil::TransformCallback* tc = dynamic_cast<osgUtil::TransformCallback*>(node.getUpdateCallback());
if (apc)
{
apc->setPause(_pause);
}
if (tc)
{
tc->setPause(_pause);
}
traverse(node);
}
bool _pause;
};
void SlideEventHandler::resetUpdateCallbackActivity()
{
ActivityUpdateCallbacksVisitor aucv(_pause);
_presentationSwitch->accept(aucv);
}

View File

@@ -1,86 +0,0 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the GNU Public License (GPL) version 1.0 or
* (at your option) any later version.
*
* 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.
*/
#ifndef SLIDEEVENTHANDLER
#define SLIDEEVENTHANDLER 1
#include <osg/Switch>
#include <osgGA/GUIEventHandler>
class SlideEventHandler : public osgGA::GUIEventHandler
{
public:
SlideEventHandler();
META_Object(osgslideshowApp,SlideEventHandler);
void set(osg::Node* model);
virtual void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); }
virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
virtual void getUsage(osg::ApplicationUsage& usage) const;
enum WhichPosition
{
FIRST_POSITION = 0,
LAST_POSITION = 0xffffffff,
};
bool selectSlide(unsigned int slideNum,unsigned int layerNum=FIRST_POSITION);
bool selectLayer(unsigned int layerNum);
bool nextLayerOrSlide();
bool previousLayerOrSlide();
bool nextSlide();
bool previousSlide();
bool nextLayer();
bool previousLayer();
void setAutoSteppingActive(bool flag = true) { _autoSteppingActive = flag; }
bool getAutoSteppingActive() const { return _autoSteppingActive; }
void setTimeDelayBetweenSlides(double dt) { _timePerSlide = dt; }
double getTimeDelayBetweenSlides() const { return _timePerSlide; }
double getCurrentTimeDelayBetweenSlides() const;
protected:
~SlideEventHandler() {}
SlideEventHandler(const SlideEventHandler&,const osg::CopyOp&) {}
osg::ref_ptr<osg::Switch> _presentationSwitch;
unsigned int _activeSlide;
osg::ref_ptr<osg::Switch> _slideSwitch;
unsigned int _activeLayer;
bool _firstTraversal;
double _previousTime;
double _timePerSlide;
bool _autoSteppingActive;
bool _loopPresentation;
bool _pause;
void resetUpdateCallbacks();
void resetUpdateCallbackActivity();
};
#endif

View File

@@ -1,634 +0,0 @@
#include "SlideShowConstructor.h"
#include <osg/Geometry>
#include <osg/PolygonOffset>
#include <osg/Geode>
#include <osg/Texture2D>
#include <osg/MatrixTransform>
#include <osgUtil/TransformCallback>
#include <osgDB/ReadFile>
#include <osgDB/FileUtils>
#include <osgText/Text>
SlideShowConstructor::SlideShowConstructor()
{
_slideDistance = osg::DisplaySettings::instance()->getScreenDistance();
_slideHeight = osg::DisplaySettings::instance()->getScreenHeight();
_slideWidth = osg::DisplaySettings::instance()->getScreenWidth();
_backgroundColor.set(0.0f,0.0f,0.0f,1.0f);
_textColor.set(1.0f,1.0f,1.0f,1.0f);
_textFont = "fonts/arial.ttf";
_titlePositionRatios = osg::Vec3(0.5f,0.0f,0.92f);
_titleAlignment = osgText::Text::CENTER_BASE_LINE;
_titlePositionRatios = osg::Vec3(0.1f,0.0f,0.92f);
_titleAlignment = osgText::Text::LEFT_BASE_LINE;
_presentationDuration = 0.0;
}
void SlideShowConstructor::setPresentationAspectRatio(float aspectRatio)
{
_slideWidth = _slideHeight*aspectRatio;
}
void SlideShowConstructor::setPresentationAspectRatio(const std::string& str)
{
if (str=="Reality Theatre") setPresentationAspectRatio(3.0f);
else if (str=="Desktop") setPresentationAspectRatio(1280.0f/1024.0f);
else
{
float ratio = (float)atof(str.c_str());
if (ratio!=0.0) setPresentationAspectRatio(1280.0f/1024.0f);
else
{
std::cout<<"Error: presentation aspect ratio incorrect type"<<std::endl;
std::cout<<" valid types are \"Reality Theatre\", \"Desktop\" or a numerical value."<<std::endl;
}
}
}
void SlideShowConstructor::createPresentation()
{
_slideOrigin.set(-_slideWidth*0.5f,_slideDistance,-_slideHeight*0.5f);
_titleHeight = _slideHeight*0.06f;
_titleWidth = _slideWidth*0.8f;
_titleOrigin = _slideOrigin + osg::Vec3(_titlePositionRatios.x()*_slideWidth,_titlePositionRatios.y()*1.0f,_titlePositionRatios.z()*_slideHeight);
_textHeight = _slideHeight*0.04f;
_textWidth = _slideWidth*0.8f;
_textOrigin = _slideOrigin + osg::Vec3(_slideWidth*0.1f,0.0f,_titleOrigin.z()-_slideOrigin.z()-2*_textHeight);
_imageOrigin = _slideOrigin + osg::Vec3(_slideWidth*0.7f,0.0f,_slideHeight*0.45f);
_root = new osg::ClearNode;
_root->setClearColor(_backgroundColor);
_presentationSwitch = new osg::Switch;
_presentationSwitch->setName(std::string("Presentation_")+_presentationName);
_root->addChild(_presentationSwitch.get());
osg::Vec3 slideCenter = _slideOrigin + osg::Vec3(_slideWidth*0.5f,0.0f,_slideHeight*0.5f);
HomePosition* hp = new HomePosition;
hp->eye.set(0.0f,0.0f,0.0f);
hp->center = slideCenter;
hp->up.set(0.0f,0.0f,1.0f);
std::cout<<" slideCenter "<<slideCenter<<std::endl;
if (_presentationDuration!=0.0)
{
_presentationSwitch->setUserData(new Duration(_presentationDuration));
}
_root->setUserData(hp);
}
void SlideShowConstructor::setBackgroundColor(const osg::Vec4& color)
{
_backgroundColor = color;
if (_root.valid()) _root->setClearColor(_backgroundColor);
}
void SlideShowConstructor::setTextColor(const osg::Vec4& color)
{
_textColor = color;
}
void SlideShowConstructor::setPresentationName(const std::string& name)
{
_presentationName = name;
if (_presentationSwitch.valid()) _presentationSwitch->setName(std::string("Presentation_")+_presentationName);
}
void SlideShowConstructor::setPresentationDuration(double duration)
{
_presentationDuration = duration;
if (_presentationDuration!=0.0 && _presentationSwitch.valid())
{
_presentationSwitch->setUserData(new Duration(_presentationDuration));
}
}
void SlideShowConstructor::addSlide()
{
if (!_presentationSwitch) createPresentation();
// reset cursors
_textCursor = _textOrigin;
_imageCursor = _imageOrigin;
_slide = new osg::Switch;
_slide->setName(std::string("Slide_")+_slideTitle);
_presentationSwitch->addChild(_slide.get());
_previousLayer = 0;
_currentLayer = 0;
}
void SlideShowConstructor::setSlideDuration(double duration)
{
if (!_slide) addSlide();
if (_slide.valid())
{
_slide->setUserData(new Duration(duration));
}
}
void SlideShowConstructor::addLayer()
{
if (!_slide) addSlide();
_previousLayer = _currentLayer;
_currentLayer = new osg::Group;
_slide->addChild(_currentLayer.get());
if (!_previousLayer)
{
// create the background and title..
if (!_slideBackgroundImageFileName.empty())
{
osg::Geometry* backgroundQuad = osg::createTexturedQuadGeometry(_slideOrigin,
osg::Vec3(_slideWidth,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,_slideHeight));
osg::Geode* background = new osg::Geode;
osg::StateSet* backgroundStateSet = background->getOrCreateStateSet();
backgroundStateSet->setAttributeAndModes(
new osg::PolygonOffset(1.0f,2.0f),
osg::StateAttribute::ON);
backgroundStateSet->setTextureAttributeAndModes(0,
new osg::Texture2D(osgDB::readImageFile(_slideBackgroundImageFileName)),
osg::StateAttribute::ON);
background->addDrawable(backgroundQuad);
_currentLayer->addChild(background);
}
if (!_slideTitle.empty())
{
osg::Geode* geode = new osg::Geode;
osgText::Text* text = new osgText::Text;
text->setFont(_textFont);
text->setColor(_textColor);
text->setCharacterSize(_titleHeight);
text->setMaximumWidth(_titleWidth);
text->setAxisAlignment(osgText::Text::XZ_PLANE);
text->setAlignment(_titleAlignment);
text->setPosition(_titleOrigin);
text->setText(_slideTitle);
geode->addDrawable(text);
_currentLayer->addChild(geode);
}
}
else
{
// copy previous layer's children across into new layer.
for(unsigned int i=0;i<_previousLayer->getNumChildren();++i)
{
_currentLayer->addChild(_previousLayer->getChild(i));
}
}
}
void SlideShowConstructor::setLayerDuration(double duration)
{
if (!_currentLayer) addLayer();
if (_currentLayer.valid())
{
_currentLayer->setUserData(new Duration(duration));
}
}
void SlideShowConstructor::addBullet(const std::string& bullet)
{
if (!_currentLayer) addLayer();
osg::Geode* geode = new osg::Geode;
osgText::Text* text = new osgText::Text;
text->setFont(_textFont);
text->setColor(_textColor);
text->setCharacterSize(_textHeight);
text->setMaximumWidth(_textWidth);
text->setAxisAlignment(osgText::Text::XZ_PLANE);
text->setAlignment(osgText::Text::BASE_LINE);
text->setPosition(_textCursor);
text->setText(bullet);
osg::BoundingBox bb = text->getBound();
_textCursor.z() = bb.zMin()-_textHeight*1.5;
geode->addDrawable(text);
_currentLayer->addChild(geode);
}
void SlideShowConstructor::addParagraph(const std::string& paragraph)
{
if (!_currentLayer) addLayer();
osg::Geode* geode = new osg::Geode;
osgText::Text* text = new osgText::Text;
text->setFont(_textFont);
text->setColor(_textColor);
text->setCharacterSize(_textHeight);
text->setMaximumWidth(_textWidth);
text->setAxisAlignment(osgText::Text::XZ_PLANE);
text->setAlignment(osgText::Text::BASE_LINE);
text->setPosition(_textCursor);
text->setText(paragraph);
osg::BoundingBox bb = text->getBound();
_textCursor.z() = bb.zMin()-_textHeight*1.5;
geode->addDrawable(text);
_currentLayer->addChild(geode);
}
void SlideShowConstructor::addImage(const std::string& filename,float height)
{
if (!_currentLayer) addLayer();
osg::Image* image = osgDB::readImageFile(filename);
if (!image) return;
float s = image->s();
float t = image->t();
float image_height = _slideHeight*0.6f;
float image_width = image_height*s/t;
float offset = height*image_height*0.1f;
osg::Vec3 pos = _imageCursor + osg::Vec3(-image_width*0.5f+offset,-offset,-image_height*0.5f-offset);
osg::Geometry* pictureQuad = osg::createTexturedQuadGeometry(pos,
osg::Vec3(image_width,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,image_height));
osg::Geode* picture = new osg::Geode;
osg::StateSet* pictureStateSet = picture->getOrCreateStateSet();
pictureStateSet->setTextureAttributeAndModes(0,
new osg::Texture2D(image),
osg::StateAttribute::ON);
picture->addDrawable(pictureQuad);
_currentLayer->addChild(picture);
}
void SlideShowConstructor::addStereoImagePair(const std::string& filenameLeft,const std::string& filenameRight,float height)
{
if (!_currentLayer) addLayer();
osg::ref_ptr<osg::Image> imageLeft = osgDB::readImageFile(filenameLeft);
osg::ref_ptr<osg::Image> imageRight = osgDB::readImageFile(filenameRight);
if (!imageLeft && !imageRight) return;
float s = imageLeft->s();
float t = imageLeft->t();
float image_height = _slideHeight*0.6f;
float image_width = image_height*s/t;
float offset = height*image_height*0.1f;
osg::Vec3 pos = _imageCursor + osg::Vec3(-image_width*0.5f+offset,-offset,-image_height*0.5f-offset);
osg::Geode* pictureLeft = new osg::Geode;
{
pictureLeft->setNodeMask(0x01);
osg::StateSet* pictureLeftStateSet = pictureLeft->getOrCreateStateSet();
pictureLeftStateSet->setTextureAttributeAndModes(0,
new osg::Texture2D(imageLeft.get()),
osg::StateAttribute::ON);
osg::Geometry* pictureLeftQuad = osg::createTexturedQuadGeometry(pos,
osg::Vec3(image_width,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,image_height));
pictureLeft->addDrawable(pictureLeftQuad);
}
osg::Geode* pictureRight = new osg::Geode;
{
pictureRight->setNodeMask(0x02);
osg::StateSet* pictureRightStateSet = pictureRight->getOrCreateStateSet();
pictureRightStateSet->setTextureAttributeAndModes(0,
new osg::Texture2D(imageRight.get()),
osg::StateAttribute::ON);
osg::Geometry* pictureRightQuad = osg::createTexturedQuadGeometry(pos,
osg::Vec3(image_width,0.0f,0.0f),
osg::Vec3(0.0f,0.0f,image_height));
pictureRight->addDrawable(pictureRightQuad);
}
osg::Group* stereopair = new osg::Group;
stereopair->addChild(pictureLeft);
stereopair->addChild(pictureRight);
_currentLayer->addChild(stereopair);
}
osg::StateSet* createTransformStateSet()
{
osg::StateSet* stateset = new osg::StateSet;
stateset->setMode(GL_NORMALIZE,osg::StateAttribute::ON);
return stateset;
}
void SlideShowConstructor::addModel(const std::string& filename, CoordinateFrame coordinate_frame, const osg::Vec3& position, float scale, const osg::Vec4& rotate, const osg::Vec4& rotation)
{
if (!_currentLayer) addLayer();
osg::Node* model = osgDB::readNodeFile(filename);
if (!model) return;
osg::Object::DataVariance defaultMatrixDataVariance = osg::Object::DYNAMIC; // STATIC
if (coordinate_frame==SLIDE)
{
osg::Vec3 pos = (_slideOrigin+osg::Vec3(_slideWidth*position.x(),0.0f,_slideHeight*position.y()))*(1.0f-position.z());
const osg::BoundingSphere& bs = model->getBound();
float model_scale = scale*_slideHeight*(1.0f-position.z())*0.7f/bs.radius();
osg::MatrixTransform* transform = new osg::MatrixTransform;
transform->setDataVariance(defaultMatrixDataVariance);
transform->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(model_scale,model_scale,model_scale)*
osg::Matrix::rotate(osg::DegreesToRadians(rotate[0]),rotate[1],rotate[2],rotate[3])*
osg::Matrix::translate(pos));
transform->setStateSet(createTransformStateSet());
transform->addChild(model);
if (rotation[0]!=0.0)
{
osg::MatrixTransform* animation_transform = new osg::MatrixTransform;
animation_transform->setDataVariance(osg::Object::DYNAMIC);
animation_transform->setUpdateCallback(new osgUtil::TransformCallback(pos,osg::Vec3(rotation[1],rotation[2],rotation[3]),osg::DegreesToRadians(rotation[0])));
animation_transform->addChild(transform);
_currentLayer->addChild(animation_transform);
}
else
{
_currentLayer->addChild(transform);
}
}
else
{
osg::Matrix matrix(osg::Matrix::scale(1.0f/scale,1.0f/scale,1.0f/scale)*
osg::Matrix::rotate(osg::DegreesToRadians(rotate[0]),rotate[1],rotate[2],rotate[3])*
osg::Matrix::translate(position));
osg::MatrixTransform* transform = new osg::MatrixTransform;
transform->setDataVariance(defaultMatrixDataVariance);
transform->setMatrix(osg::Matrix::inverse(matrix));
transform->addChild(model);
if (rotation[0]!=0.0)
{
osg::MatrixTransform* animation_transform = new osg::MatrixTransform;
animation_transform->setDataVariance(osg::Object::DYNAMIC);
animation_transform->setUpdateCallback(new osgUtil::TransformCallback(osg::Vec3(0.0f,0.0f,0.0f),osg::Vec3(rotation[1],rotation[2],rotation[3]),osg::DegreesToRadians(rotation[0])));
animation_transform->addChild(transform);
_currentLayer->addChild(animation_transform);
}
else
{
_currentLayer->addChild(transform);
}
}
}
void SlideShowConstructor::addModelWithPath(const std::string& filename, CoordinateFrame coordinate_frame, const osg::Vec3& position, float scale, const osg::Vec4& rotate, const std::string& animation_path)
{
if (!_currentLayer) addLayer();
osg::Node* model = osgDB::readNodeFile(filename);
if (!model) return;
osg::Object::DataVariance defaultMatrixDataVariance = osg::Object::DYNAMIC; // STATIC
osg::AnimationPath* animation = 0;
if (!animation_path.empty())
{
std::string absolute_animation_file_path = osgDB::findDataFile(animation_path);
if (!absolute_animation_file_path.empty())
{
std::ifstream animation_filestream(absolute_animation_file_path.c_str());
if (!animation_filestream.eof())
{
animation = new osg::AnimationPath;
animation->read(animation_filestream);
}
}
}
if (coordinate_frame==SLIDE)
{
osg::Vec3 pos(_slideWidth*position.x(),
_slideDistance*position.y(),
_slideHeight*position.z());
const osg::BoundingSphere& bs = model->getBound();
float model_scale = scale*_slideHeight*0.7f/bs.radius();
osg::MatrixTransform* transform = new osg::MatrixTransform;
transform->setDataVariance(defaultMatrixDataVariance);
transform->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(model_scale,model_scale,model_scale)*
osg::Matrix::rotate(osg::DegreesToRadians(rotate[0]),rotate[1],rotate[2],rotate[3])*
osg::Matrix::translate(pos));
transform->addChild(model);
_currentLayer->addChild(transform);
}
else
{
osg::MatrixTransform* transform = new osg::MatrixTransform;
transform->setDataVariance(defaultMatrixDataVariance);
transform->setMatrix(osg::Matrix::translate(-position)*
osg::Matrix::scale(scale,scale,scale)*
osg::Matrix::rotate(osg::DegreesToRadians(rotate[0]),rotate[1],rotate[2],rotate[3]));
transform->addChild(model);
if (animation)
{
osg::MatrixTransform* animation_transform = new osg::MatrixTransform;
animation_transform->setDataVariance(osg::Object::DYNAMIC);
osg::AnimationPathCallback* apc = new osg::AnimationPathCallback(animation);
animation_transform->setUpdateCallback(apc);
animation_transform->addChild(transform);
_currentLayer->addChild(animation_transform);
//
// osg::MatrixTransform* orientation_transform = new osg::MatrixTransform;
// orientation_transform->setDataVariance(osg::Object::STATIC);
// orientation_transform->addChild(animation_transform);
//
// //orientation_transform->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(-00.0f),0.0f,1.0f,0.0f));
// //orientation_transform->setMatrix(osg::Matrix::inverse(osg::Matrix::lookAt(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,1.0,0.0),osg::Vec3(0.0,0.0,1.0))));
//
// _currentLayer->addChild(orientation_transform);
}
else
{
_currentLayer->addChild(transform);
}
}
}
void SlideShowConstructor::addModelWithCameraPath(const std::string& filename, CoordinateFrame coordinate_frame, const osg::Vec3& position, float scale, const osg::Vec4& rotate, const std::string& animation_path)
{
if (!_currentLayer) addLayer();
osg::Node* model = osgDB::readNodeFile(filename);
if (!model) return;
osg::Object::DataVariance defaultMatrixDataVariance = osg::Object::DYNAMIC; // STATIC
osg::AnimationPath* animation = 0;
if (!animation_path.empty())
{
std::string absolute_animation_file_path = osgDB::findDataFile(animation_path);
if (!absolute_animation_file_path.empty())
{
std::ifstream animation_filestream(absolute_animation_file_path.c_str());
if (!animation_filestream.eof())
{
animation = new osg::AnimationPath;
animation->read(animation_filestream);
}
}
}
if (coordinate_frame==SLIDE)
{
osg::Vec3 pos(_slideWidth*position.x(),
_slideDistance*position.y(),
_slideHeight*position.z());
const osg::BoundingSphere& bs = model->getBound();
float model_scale = scale*_slideHeight*0.7f/bs.radius();
osg::MatrixTransform* transform = new osg::MatrixTransform;
transform->setDataVariance(defaultMatrixDataVariance);
transform->setMatrix(osg::Matrix::translate(-bs.center())*
osg::Matrix::scale(model_scale,model_scale,model_scale)*
osg::Matrix::rotate(osg::DegreesToRadians(rotate[0]),rotate[1],rotate[2],rotate[3])*
osg::Matrix::translate(pos));
transform->addChild(model);
_currentLayer->addChild(transform);
}
else
{
osg::MatrixTransform* transform = new osg::MatrixTransform;
transform->setDataVariance(osg::Object::STATIC);
transform->setMatrix(osg::Matrix::translate(-position)*
osg::Matrix::scale(scale,scale,scale)*
osg::Matrix::rotate(osg::DegreesToRadians(rotate[0]),rotate[1],rotate[2],rotate[3]));
transform->addChild(model);
if (animation)
{
osg::MatrixTransform* animation_transform = new osg::MatrixTransform;
animation_transform->setDataVariance(osg::Object::DYNAMIC);
animation_transform->setReferenceFrame(osg::Transform::RELATIVE_TO_ABSOLUTE);
osg::AnimationPathCallback* apc = new osg::AnimationPathCallback(animation);
apc->setUseInverseMatrix(true);
animation_transform->setUpdateCallback(apc);
animation_transform->addChild(transform);
_currentLayer->addChild(animation_transform);
// osg::MatrixTransform* orientation_transform = new osg::MatrixTransform;
// orientation_transform->setDataVariance(osg::Object::STATIC);
// orientation_transform->addChild(animation_transform);
//
// //orientation_transform->setMatrix(osg::Matrix::rotate(osg::DegreesToRadians(-00.0f),0.0f,1.0f,0.0f));
// orientation_transform->setMatrix(osg::Matrix::inverse(osg::Matrix::lookAt(osg::Vec3(0.0,0.0,0.0),osg::Vec3(0.0,1.0,0.0),osg::Vec3(0.0,0.0,1.0))));
//
// _currentLayer->addChild(orientation_transform);
}
else
{
_currentLayer->addChild(transform);
}
}
}

View File

@@ -1,131 +0,0 @@
#ifndef SLIDESHOWCONSTUCTOR
#define SLIDESHOWCONSTRUCTOR
#include <osg/Vec3>
#include <osg/Vec4>
#include <osg/Group>
#include <osg/ClearNode>
#include <osg/Switch>
#include <osgText/Text>
class SlideShowConstructor
{
public:
SlideShowConstructor();
struct HomePosition : public osg::Referenced
{
HomePosition() {}
HomePosition(const osg::Vec3& in_eye, const osg::Vec3& in_center, const osg::Vec3& in_up):
eye(in_eye),
center(in_center),
up(in_up) {}
osg::Vec3 eye;
osg::Vec3 center;
osg::Vec3 up;
};
struct Duration : public osg::Referenced
{
Duration(double in_duration):duration(in_duration) {}
double duration;
};
void createPresentation();
void setBackgroundColor(const osg::Vec4& color);
void setTextColor(const osg::Vec4& color);
void setPresentationName(const std::string& name);
void setPresentationAspectRatio(float aspectRatio);
void setPresentationAspectRatio(const std::string& str);
void setPresentationDuration(double duration);
void addSlide();
void setSlideTitle(const std::string& name) { _slideTitle = name; }
void setSlideBackground(const std::string& name) { _slideBackgroundImageFileName = name; }
void setSlideDuration(double duration);
void addLayer();
void setLayerDuration(double duration);
void addBullet(const std::string& bullet);
void addParagraph(const std::string& paragraph);
void addImage(const std::string& filename,float height);
void addStereoImagePair(const std::string& filenameLeft,const std::string& filenameRight,float height);
enum CoordinateFrame { SLIDE, MODEL };
void addModel(const std::string& filename, CoordinateFrame coordinate_frame, const osg::Vec3& position, float scale, const osg::Vec4& rotate, const osg::Vec4& rotation);
void addModelWithPath(const std::string& filename, CoordinateFrame coordinate_frame, const osg::Vec3& position, float scale, const osg::Vec4& rotate, const std::string& animation_path);
void addModelWithCameraPath(const std::string& filename, CoordinateFrame coordinate_frame, const osg::Vec3& position, float scale, const osg::Vec4& rotate, const std::string& animation_path);
osg::ClearNode* takePresentation() { return _root.release(); }
osg::ClearNode* getPresentation() { return _root.get(); }
osg::Switch* getPresentationSwitch() { return _presentationSwitch.get(); }
osg::Switch* getCurrentSlide() { return _slide.get(); }
osg::Group* getCurrentLayer() { return _currentLayer.get(); }
protected:
osg::Vec3 _slideOrigin;
osg::Vec3 _eyeOrigin;
float _slideWidth;
float _slideHeight;
float _slideDistance;
osg::Vec4 _backgroundColor;
osg::Vec4 _textColor;
std::string _textFont;
float _titleHeight;
float _titleWidth;
float _textHeight;
float _textWidth;
std::string _presentationName;
double _presentationDuration;
osg::Vec3 _titlePositionRatios;
osgText::Text::AlignmentType _titleAlignment;
osg::Vec3 _titleOrigin;
osg::Vec3 _textOrigin;
osg::Vec3 _imageOrigin;
osg::Vec3 _textCursor;
osg::Vec3 _imageCursor;
osg::Vec3 _modelCursor;
osg::ref_ptr<osg::ClearNode> _root;
osg::ref_ptr<osg::Switch> _presentationSwitch;
osg::ref_ptr<osg::Switch> _slide;
std::string _slideTitle;
std::string _slideBackgroundImageFileName;
osg::ref_ptr<osg::Group> _previousLayer;
osg::ref_ptr<osg::Group> _currentLayer;
};
#endif

View File

@@ -1,243 +0,0 @@
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2003 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the GNU Public License (GPL) version 1.0 or
* (at your option) any later version.
*
* 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.
*/
#include <osgDB/ReadFile>
#include <osgUtil/Optimizer>
#include <osgProducer/Viewer>
#include <osgGA/TrackballManipulator>
#include "SlideEventHandler.h"
#include "PointsEventHandler.h"
#include "SlideShowConstructor.h"
class FindHomePositionVisitor : public osg::NodeVisitor
{
public:
FindHomePositionVisitor():
osg::NodeVisitor(osg::NodeVisitor::TRAVERSE_ALL_CHILDREN) {}
void apply(osg::Node& node)
{
SlideShowConstructor::HomePosition* homePosition = dynamic_cast<SlideShowConstructor::HomePosition*>(node.getUserData());
if (homePosition)
{
_homePosition = homePosition;
}
traverse(node);
}
osg::ref_ptr<SlideShowConstructor::HomePosition> _homePosition;
};
// add in because MipsPro can't handle no osgGA infront of TrackballManipulator
// but VisualStudio6.0 can't handle the osgGA...
using namespace osgGA;
class SlideShowTrackballManipulator : public osgGA::TrackballManipulator
{
public:
SlideShowTrackballManipulator()
{
}
virtual void home(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us)
{
FindHomePositionVisitor fhpv;
if (_node.valid()) _node->accept(fhpv);
if (fhpv._homePosition.valid())
{
computePosition(fhpv._homePosition->eye,
fhpv._homePosition->center,
fhpv._homePosition->up);
us.requestRedraw();
}
else
{
TrackballManipulator::home(ea,us);
}
}
};
int main( int argc, char **argv )
{
// use an ArgumentParser object to manage the program arguments.
osg::ArgumentParser arguments(&argc,argv);
// set up the usage document, in case we need to print out how to use this program.
arguments.getApplicationUsage()->setApplicationName(arguments.getApplicationName());
arguments.getApplicationUsage()->setDescription(arguments.getApplicationName()+" is the application for presenting 3D interactive slide shows.");
arguments.getApplicationUsage()->setCommandLineUsage(arguments.getApplicationName()+" [options] filename ...");
arguments.getApplicationUsage()->addCommandLineOption("-h or --help","Display this information");
arguments.getApplicationUsage()->addCommandLineOption("-a","Turn auto stepping on by default");
arguments.getApplicationUsage()->addCommandLineOption("-d <float>","Time duration in seconds between layers/slides");
arguments.getApplicationUsage()->addCommandLineOption("-s <float> <float> <float>","width, height, distance and of the screen away from the viewer");
// construct the viewer.
osgProducer::Viewer viewer(arguments);
SlideShowTrackballManipulator* sstbm = new SlideShowTrackballManipulator;
viewer.addCameraManipulator(sstbm);
// set up the value with sensible default event handler.
viewer.setUpViewer(osgProducer::Viewer::DRIVE_MANIPULATOR |
osgProducer::Viewer::FLIGHT_MANIPULATOR |
osgProducer::Viewer::STATE_MANIPULATOR |
osgProducer::Viewer::HEAD_LIGHT_SOURCE |
osgProducer::Viewer::STATS_MANIPULATOR |
osgProducer::Viewer::VIEWER_MANIPULATOR |
osgProducer::Viewer::ESCAPE_SETS_DONE);
// read any time delay argument.
float timeDelayBetweenSlides = 1.5f;
while (arguments.read("-d",timeDelayBetweenSlides)) {}
bool autoSteppingActive = false;
while (arguments.read("-a")) autoSteppingActive = true;
// register the slide event handler - which moves the presentation from slide to slide, layer to layer.
SlideEventHandler* seh = new SlideEventHandler;
viewer.getEventHandlerList().push_front(seh);
seh->setAutoSteppingActive(autoSteppingActive);
seh->setTimeDelayBetweenSlides(timeDelayBetweenSlides);
// register the handler for modifying the point size
PointsEventHandler* peh = new PointsEventHandler;
viewer.getEventHandlerList().push_front(peh);
osg::DisplaySettings::instance()->setSplitStereoAutoAjustAspectRatio(false);
float width = osg::DisplaySettings::instance()->getScreenWidth();
float height = osg::DisplaySettings::instance()->getScreenHeight();
float distance = osg::DisplaySettings::instance()->getScreenDistance();
bool sizesSpecified = false;
while (arguments.read("-s", width, height, distance))
{
sizesSpecified = true;
osg::DisplaySettings::instance()->setScreenDistance(distance);
osg::DisplaySettings::instance()->setScreenHeight(height);
osg::DisplaySettings::instance()->setScreenWidth(width);
}
// get details on keyboard and mouse bindings used by the viewer.
viewer.getUsage(*arguments.getApplicationUsage());
// if user request help write it out to cout.
if (arguments.read("-h") || arguments.read("--help"))
{
arguments.getApplicationUsage()->write(std::cout);
return 1;
}
// any option left unread are converted into errors to write out later.
//arguments.reportRemainingOptionsAsUnrecognized();
// report any errors if they have occured when parsing the program aguments.
if (arguments.errors())
{
arguments.writeErrorMessages(std::cout);
return 1;
}
osg::Timer timer;
osg::Timer_t start_tick = timer.tick();
// allow sharing of models.
osgDB::Registry::instance()->setUseObjectCacheHint(true);
// read the scene from the list of file specified commandline args.
osg::ref_ptr<osg::Node> loadedModel = osgDB::readNodeFiles(arguments);
// switch sharing of loaded models back off.
osgDB::Registry::instance()->setUseObjectCacheHint(false);
// if no model has been successfully loaded report failure.
if (!loadedModel)
{
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
return 1;
}
osg::Timer_t end_tick = timer.tick();
std::cout << "Time to load = "<<timer.delta_s(start_tick,end_tick)<<std::endl;
// optimize the scene graph, remove rendundent nodes and state etc.
osgUtil::Optimizer optimizer;
optimizer.optimize(loadedModel.get());
// pass the model to the slide event handler so it knows which to manipulate.
seh->set(loadedModel.get());
// set the scene to render
viewer.setSceneData(loadedModel.get());
// pass the global stateset to the point event handler so that it can
// alter the point size of all points in the scene.
peh->setStateSet(viewer.getGlobalStateSet());
// create the windows and run the threads.
viewer.realize();
double vfov = osg::RadiansToDegrees(atan2(height/2.0f,distance)*2.0);
double hfov = osg::RadiansToDegrees(atan2(width/2.0f,distance)*2.0);
viewer.setLensPerspective( hfov, vfov, 0.1, 1000.0);
// set all the sceneview's up so that their left and right add cull masks are set up.
for(osgProducer::OsgCameraGroup::SceneHandlerList::iterator itr=viewer.getSceneHandlerList().begin();
itr!=viewer.getSceneHandlerList().end();
++itr)
{
osgUtil::SceneView* sceneview = (*itr)->getSceneView();
sceneview->setCullMask(0xffffffff);
sceneview->setCullMaskLeft(0x00000001);
sceneview->setCullMaskRight(0x00000002);
// sceneview->setFusionDistance(osgUtil::SceneView::USE_FUSION_DISTANCE_VALUE,radius);
}
while( !viewer.done() )
{
// wait for all cull and draw threads to complete.
viewer.sync();
// update the scene by traversing it with the the update visitor which will
// call all node update callbacks and animations.
viewer.update();
// fire off the cull and draw traversals of the scene.
viewer.frame();
}
// wait for all cull and draw threads to complete before exit.
viewer.sync();
return 0;
}