diff options
Diffstat (limited to 'src/uscxml/plugins/invoker/graphics')
4 files changed, 442 insertions, 441 deletions
diff --git a/src/uscxml/plugins/invoker/graphics/openscenegraph/CompositeDisplay.cpp b/src/uscxml/plugins/invoker/graphics/openscenegraph/CompositeDisplay.cpp index d7e247b..6d2a8e2 100644 --- a/src/uscxml/plugins/invoker/graphics/openscenegraph/CompositeDisplay.cpp +++ b/src/uscxml/plugins/invoker/graphics/openscenegraph/CompositeDisplay.cpp @@ -11,117 +11,116 @@ CompositeDisplay::CompositeDisplay(unsigned int x, unsigned int y, unsigned int width, unsigned int height, - int screenId) -{ - _waitForViewOp = false; - unsigned int tWidth = 0; - unsigned int tHeight = 0; - getResolution(tWidth, tHeight, screenId); - - osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; - traits->doubleBuffer = true; - traits->sharedContext = 0; - traits->screenNum = screenId; - - if (width == 0 || height == 0 || (width == tWidth && height == tHeight)) { - // fullscreen - traits->windowDecoration = false; - traits->width = tWidth; - traits->height = tHeight; - traits->x = 0; - traits->y = 0; - } else { - // Start with given resolution - traits->windowDecoration = true; - traits->x = x; - traits->y = y; - traits->width = width; - traits->height = height; - } - - _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); - if (_gc.valid()) { - _gc->setClearColor(osg::Vec4f(1.0f,1.0f,1.0f,1.0f)); - _gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - } - - _width = traits->width; - _height = traits->height; - - setRunMaxFrameRate(30); + int screenId) { + _waitForViewOp = false; + unsigned int tWidth = 0; + unsigned int tHeight = 0; + getResolution(tWidth, tHeight, screenId); + + osg::ref_ptr<osg::GraphicsContext::Traits> traits = new osg::GraphicsContext::Traits; + traits->doubleBuffer = true; + traits->sharedContext = 0; + traits->screenNum = screenId; + + if (width == 0 || height == 0 || (width == tWidth && height == tHeight)) { + // fullscreen + traits->windowDecoration = false; + traits->width = tWidth; + traits->height = tHeight; + traits->x = 0; + traits->y = 0; + } else { + // Start with given resolution + traits->windowDecoration = true; + traits->x = x; + traits->y = y; + traits->width = width; + traits->height = height; + } + + _gc = osg::GraphicsContext::createGraphicsContext(traits.get()); + if (_gc.valid()) { + _gc->setClearColor(osg::Vec4f(1.0f,1.0f,1.0f,1.0f)); + _gc->setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + + _width = traits->width; + _height = traits->height; + + setRunMaxFrameRate(30); // setRunFrameScheme(osgViewer::ViewerBase::ON_DEMAND); - setThreadingModel(osgViewer::Viewer::AutomaticSelection); + setThreadingModel(osgViewer::Viewer::AutomaticSelection); } CompositeDisplay::~CompositeDisplay() {} void CompositeDisplay::frame(double simulationTime) { - tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); - CompositeViewer::frame(); + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + CompositeViewer::frame(); } bool CompositeDisplay::checkNeedToDoFrame() { - return CompositeViewer::checkNeedToDoFrame(); + return CompositeViewer::checkNeedToDoFrame(); } void CompositeDisplay::addView(const std::string& name, osg::Viewport* v, osgViewer::View* view) { - tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); _viewports[name] = v; - + _views[name] = view; _views[name]->setName(name); - _views[name]->getCamera()->setName(name); - _views[name]->setCameraManipulator(new osgGA::TrackballManipulator); - - // add the state manipulator - osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator; - statesetManipulator->setStateSet(_views[name]->getCamera()->getOrCreateStateSet()); - _views[name]->addEventHandler( statesetManipulator.get() ); + _views[name]->getCamera()->setName(name); + _views[name]->setCameraManipulator(new osgGA::TrackballManipulator); + + // add the state manipulator + osg::ref_ptr<osgGA::StateSetManipulator> statesetManipulator = new osgGA::StateSetManipulator; + statesetManipulator->setStateSet(_views[name]->getCamera()->getOrCreateStateSet()); + _views[name]->addEventHandler( statesetManipulator.get() ); _views[name]->addEventHandler( new osgViewer::StatsHandler ); _views[name]->addEventHandler( new osgViewer::HelpHandler ); _views[name]->addEventHandler( new osgViewer::WindowSizeHandler ); _views[name]->addEventHandler( new osgViewer::ThreadingHandler ); - - _views[name]->getCamera()->setViewport(v); - - // set graphic context - _views[name]->getCamera()->setGraphicsContext(_gc.get()); - CompositeViewer::addView(_views[name]); + + _views[name]->getCamera()->setViewport(v); + + // set graphic context + _views[name]->getCamera()->setGraphicsContext(_gc.get()); + CompositeViewer::addView(_views[name]); } -void CompositeDisplay::moveView(const std::string& name, osg::Viewport* v) { - tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); - const osg::GraphicsContext::Traits* traits = _gc->getTraits(); - osg::Viewport* absoluteVp = new osg::Viewport(v->x() * (traits->width/100.0), - v->y() * (traits->height/100.0), - v->width() * (traits->width/100.0), - v->height() * (traits->height/100.0)); - _views[name]->getCamera()->setViewport(absoluteVp); +void CompositeDisplay::moveView(const std::string& name, osg::Viewport* v) { + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + const osg::GraphicsContext::Traits* traits = _gc->getTraits(); + osg::Viewport* absoluteVp = new osg::Viewport(v->x() * (traits->width/100.0), + v->y() * (traits->height/100.0), + v->width() * (traits->width/100.0), + v->height() * (traits->height/100.0)); + _views[name]->getCamera()->setViewport(absoluteVp); } void CompositeDisplay::removeView(const std::string& name) { - tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); - - _views[name]->getCamera()->setGraphicsContext(NULL); - _views[name]->getCamera()->setViewport(NULL); + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + + _views[name]->getCamera()->setGraphicsContext(NULL); + _views[name]->getCamera()->setViewport(NULL); CompositeViewer::removeView(_views[name]); - + if (_views.find(name) != _views.end()) { _views.erase(name); - } + } if (_viewports.find(name) != _viewports.end()) _viewports.erase(name); } osg::GraphicsContext::WindowingSystemInterface* CompositeDisplay::wsi = NULL; void CompositeDisplay::getResolution(unsigned int& width, unsigned int& height, int screenId) { - if (!wsi) - wsi = osg::GraphicsContext::getWindowingSystemInterface(); + if (!wsi) + wsi = osg::GraphicsContext::getWindowingSystemInterface(); if (wsi) { - wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(screenId), width, height); + wsi->getScreenResolution(osg::GraphicsContext::ScreenIdentifier(screenId), width, height); } else { width = 800; height = 600; diff --git a/src/uscxml/plugins/invoker/graphics/openscenegraph/CompositeDisplay.h b/src/uscxml/plugins/invoker/graphics/openscenegraph/CompositeDisplay.h index 0d08c7f..b524777 100644 --- a/src/uscxml/plugins/invoker/graphics/openscenegraph/CompositeDisplay.h +++ b/src/uscxml/plugins/invoker/graphics/openscenegraph/CompositeDisplay.h @@ -11,36 +11,40 @@ #include <iostream> class CompositeDisplay : public osgViewer::CompositeViewer { -public: - CompositeDisplay(unsigned int x, - unsigned int y, - unsigned int width, - unsigned int height, - int screenId); +public: + CompositeDisplay(unsigned int x, + unsigned int y, + unsigned int width, + unsigned int height, + int screenId); virtual ~CompositeDisplay(); - + virtual void addView(const std::string& name, osg::Viewport* v, osgViewer::View* view); - virtual void moveView(const std::string& name, osg::Viewport* v); + virtual void moveView(const std::string& name, osg::Viewport* v); virtual void removeView(const std::string& name); - - virtual void frame(double simulationTime); - virtual bool checkNeedToDoFrame(); - - int getWidth() { return _width; } - int getHeight() { return _height; } - - static void getResolution(unsigned int& width, unsigned int& height, int screenId); - + + virtual void frame(double simulationTime); + virtual bool checkNeedToDoFrame(); + + int getWidth() { + return _width; + } + int getHeight() { + return _height; + } + + static void getResolution(unsigned int& width, unsigned int& height, int screenId); + protected: - tthread::recursive_mutex _mutex; - tthread::condition_variable _monitor; - bool _waitForViewOp; + tthread::recursive_mutex _mutex; + tthread::condition_variable _monitor; + bool _waitForViewOp; std::map<std::string, osgViewer::View*> _views; std::map<std::string, osg::Viewport*> _viewports; - osg::ref_ptr<osg::GraphicsContext> _gc; + osg::ref_ptr<osg::GraphicsContext> _gc; - static osg::GraphicsContext::WindowingSystemInterface* wsi; - int _width, _height; + static osg::GraphicsContext::WindowingSystemInterface* wsi; + int _width, _height; }; diff --git a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGInvoker.cpp b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGInvoker.cpp index 08fc1f3..9e584c3 100644 --- a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGInvoker.cpp +++ b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGInvoker.cpp @@ -7,7 +7,7 @@ #endif namespace uscxml { - + #ifdef BUILD_AS_PLUGINS PLUMA_CONNECTOR bool connect(pluma::Host& host) { @@ -15,7 +15,7 @@ bool connect(pluma::Host& host) { return true; } #endif - + OSGInvoker::OSGInvoker() { } @@ -43,288 +43,288 @@ void OSGInvoker::sendToParent(const SendRequest& req) { } void OSGInvoker::invoke(const InvokeRequest& req) { - tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); - - // register default event handlers - Arabica::DOM::Events::EventTarget<std::string> evTarget = Arabica::DOM::Events::EventTarget<std::string>(req.dom); - evTarget.addEventListener("DOMSubtreeModified", *this, false); - evTarget.addEventListener("DOMNodeInserted", *this, false); - evTarget.addEventListener("DOMNodeRemoved", *this, false); - evTarget.addEventListener("DOMAttrModified", *this, false); - - Arabica::XPath::NodeSet<std::string> content = Interpreter::filterChildElements("content", req.dom); - - std::set<std::string> validChilds; - validChilds.insert("display"); - processChildren(validChilds, content[0]); + tthread::lock_guard<tthread::recursive_mutex> lock(_mutex); + + // register default event handlers + Arabica::DOM::Events::EventTarget<std::string> evTarget = Arabica::DOM::Events::EventTarget<std::string>(req.dom); + evTarget.addEventListener("DOMSubtreeModified", *this, false); + evTarget.addEventListener("DOMNodeInserted", *this, false); + evTarget.addEventListener("DOMNodeRemoved", *this, false); + evTarget.addEventListener("DOMAttrModified", *this, false); + + Arabica::XPath::NodeSet<std::string> content = Interpreter::filterChildElements("content", req.dom); + + std::set<std::string> validChilds; + validChilds.insert("display"); + processChildren(validChilds, content[0]); } void OSGInvoker::runOnMainThread() { - _displays_t::iterator dispIter = _displays.begin(); - if (_mutex.try_lock()) { - while(dispIter != _displays.end()) { - dispIter->second->osgViewer::ViewerBase::frame(); - dispIter++; - } - _mutex.unlock(); - } + _displays_t::iterator dispIter = _displays.begin(); + if (_mutex.try_lock()) { + while(dispIter != _displays.end()) { + dispIter->second->osgViewer::ViewerBase::frame(); + dispIter++; + } + _mutex.unlock(); + } } void OSGInvoker::handleEvent(Arabica::DOM::Events::Event<std::string>& event) { // std::cout << "Handling Event!" << std::endl; - Arabica::DOM::Node<std::string> node(event.getTarget()); - if (_nodes.find(node) != _nodes.end()) { - osg::Node* osgNode = _nodes[node]; - if (false) { - } else if (boost::iequals(LOCALNAME(node), "rotation")) { - updateRotation(osgNode, event); - } - } + Arabica::DOM::Node<std::string> node(event.getTarget()); + if (_nodes.find(node) != _nodes.end()) { + osg::Node* osgNode = _nodes[node]; + if (false) { + } else if (boost::iequals(LOCALNAME(node), "rotation")) { + updateRotation(osgNode, event); + } + } } void OSGInvoker::processDisplay(const Arabica::DOM::Node<std::string>& element) { // std::cout << element << std::endl; - if (_displays.find(element) == _displays.end()) { - - int screenId = 0; - unsigned int actualX = 0; - unsigned int actualY = 0; - unsigned int actualWidth = 0; - unsigned int actualHeight = 0; - getViewport(element, actualX, actualY, actualWidth, actualHeight, screenId); - - CompositeDisplay* compDisp = new CompositeDisplay(actualX, actualY, actualWidth, actualHeight, screenId); - _displays[element] = compDisp; - - std::set<std::string> validChilds; - validChilds.insert("viewport"); - processChildren(validChilds, element); - } + if (_displays.find(element) == _displays.end()) { + + int screenId = 0; + unsigned int actualX = 0; + unsigned int actualY = 0; + unsigned int actualWidth = 0; + unsigned int actualHeight = 0; + getViewport(element, actualX, actualY, actualWidth, actualHeight, screenId); + + CompositeDisplay* compDisp = new CompositeDisplay(actualX, actualY, actualWidth, actualHeight, screenId); + _displays[element] = compDisp; + + std::set<std::string> validChilds; + validChilds.insert("viewport"); + processChildren(validChilds, element); + } } void OSGInvoker::processViewport(const Arabica::DOM::Node<std::string>& element) { - if (_displays.find(element.getParentNode()) == _displays.end()) - return; - - CompositeDisplay* compDisp = _displays[element.getParentNode()]; - osgViewer::View* sceneView = new osgViewer::View(); - _views[element] = sceneView; - - osg::Group* group = new osg::Group(); - _nodes[element] = group; - sceneView->setSceneData(group); - - std::string name = (HAS_ATTR(element, "id") ? ATTR(element, "id") : Interpreter::getUUID()); - - unsigned int actualX = 0; - unsigned int actualY = 0; - unsigned int actualWidth = 0; - unsigned int actualHeight = 0; - getViewport(element, actualX, actualY, actualWidth, actualHeight, compDisp); - - osg::Viewport* viewPort = new osg::Viewport(actualX, actualY, actualWidth, actualHeight); - compDisp->addView(name, viewPort, sceneView); - - std::set<std::string> validChilds; - validChilds.insert("camera"); - validChilds.insert("translation"); - validChilds.insert("rotation"); - validChilds.insert("scale"); - validChilds.insert("node"); - processChildren(validChilds, element); + if (_displays.find(element.getParentNode()) == _displays.end()) + return; + + CompositeDisplay* compDisp = _displays[element.getParentNode()]; + osgViewer::View* sceneView = new osgViewer::View(); + _views[element] = sceneView; + + osg::Group* group = new osg::Group(); + _nodes[element] = group; + sceneView->setSceneData(group); + + std::string name = (HAS_ATTR(element, "id") ? ATTR(element, "id") : Interpreter::getUUID()); + + unsigned int actualX = 0; + unsigned int actualY = 0; + unsigned int actualWidth = 0; + unsigned int actualHeight = 0; + getViewport(element, actualX, actualY, actualWidth, actualHeight, compDisp); + + osg::Viewport* viewPort = new osg::Viewport(actualX, actualY, actualWidth, actualHeight); + compDisp->addView(name, viewPort, sceneView); + + std::set<std::string> validChilds; + validChilds.insert("camera"); + validChilds.insert("translation"); + validChilds.insert("rotation"); + validChilds.insert("scale"); + validChilds.insert("node"); + processChildren(validChilds, element); } void OSGInvoker::processCamera(const Arabica::DOM::Node<std::string>& element) {} void OSGInvoker::updateCamera(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event) {} void OSGInvoker::processTranslation(const Arabica::DOM::Node<std::string>& element) { - assert(_nodes.find(element.getParentNode()) != _nodes.end()); - osg::Node* node = _nodes[element.getParentNode()]; - - double x = 0, y = 0, z = 0; - if (HAS_ATTR(element, "x")) - x = strTo<float>(ATTR(element, "x")); - if (HAS_ATTR(element, "y")) - y = strTo<float>(ATTR(element, "y")); - if (HAS_ATTR(element, "z")) - z = strTo<float>(ATTR(element, "z")); - - osg::Matrix translate; - translate.makeTranslate(x, y, z); - - osg::MatrixTransform* transform = new osg::MatrixTransform(); - transform->setMatrix(translate); - node->asGroup()->addChild(transform); - _nodes[element] = transform; - - std::set<std::string> validChilds; - validChilds.insert("translation"); - validChilds.insert("rotation"); - validChilds.insert("scale"); - validChilds.insert("node"); - processChildren(validChilds, element); + assert(_nodes.find(element.getParentNode()) != _nodes.end()); + osg::Node* node = _nodes[element.getParentNode()]; + + double x = 0, y = 0, z = 0; + if (HAS_ATTR(element, "x")) + x = strTo<float>(ATTR(element, "x")); + if (HAS_ATTR(element, "y")) + y = strTo<float>(ATTR(element, "y")); + if (HAS_ATTR(element, "z")) + z = strTo<float>(ATTR(element, "z")); + + osg::Matrix translate; + translate.makeTranslate(x, y, z); + + osg::MatrixTransform* transform = new osg::MatrixTransform(); + transform->setMatrix(translate); + node->asGroup()->addChild(transform); + _nodes[element] = transform; + + std::set<std::string> validChilds; + validChilds.insert("translation"); + validChilds.insert("rotation"); + validChilds.insert("scale"); + validChilds.insert("node"); + processChildren(validChilds, element); } void OSGInvoker::processRotation(const Arabica::DOM::Node<std::string>& element) { - assert(_nodes.find(element.getParentNode()) != _nodes.end()); - osg::Node* node = _nodes[element.getParentNode()]; - - osg::Matrix rotation = rotationFromElement(element); - osg::MatrixTransform* transform = new osg::MatrixTransform(); - transform->setMatrix(rotation); - node->asGroup()->addChild(transform); - _nodes[element] = transform; - - std::set<std::string> validChilds; - validChilds.insert("translation"); - validChilds.insert("rotation"); - validChilds.insert("scale"); - validChilds.insert("node"); - processChildren(validChilds, element); + assert(_nodes.find(element.getParentNode()) != _nodes.end()); + osg::Node* node = _nodes[element.getParentNode()]; + + osg::Matrix rotation = rotationFromElement(element); + osg::MatrixTransform* transform = new osg::MatrixTransform(); + transform->setMatrix(rotation); + node->asGroup()->addChild(transform); + _nodes[element] = transform; + + std::set<std::string> validChilds; + validChilds.insert("translation"); + validChilds.insert("rotation"); + validChilds.insert("scale"); + validChilds.insert("node"); + processChildren(validChilds, element); } void OSGInvoker::updateRotation(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event) { - osg::MatrixTransform* transform = static_cast<osg::MatrixTransform*>(node); - if (false) { - } else if (boost::iequals(event.getType(), "DOMAttrModified")) { - osg::Matrix rotation = rotationFromElement(Arabica::DOM::Node<std::string>(event.getTarget())); - transform->setMatrix(rotation); - } + osg::MatrixTransform* transform = static_cast<osg::MatrixTransform*>(node); + if (false) { + } else if (boost::iequals(event.getType(), "DOMAttrModified")) { + osg::Matrix rotation = rotationFromElement(Arabica::DOM::Node<std::string>(event.getTarget())); + transform->setMatrix(rotation); + } } osg::Matrix OSGInvoker::rotationFromElement(const Arabica::DOM::Node<std::string>& element) { - double pitch = 0, roll = 0, yaw = 0; - if (HAS_ATTR(element, "pitch")) { - NumAttr pitchAttr = NumAttr(ATTR(element, "pitch")); - if (boost::iequals(pitchAttr.unit, "deg")) { - pitch = osg::DegreesToRadians(strTo<float>(pitchAttr.value)); - } else if (boost::iequals(pitchAttr.unit, "%")) { - pitch = osg::DegreesToRadians((strTo<float>(pitchAttr.value) * 360) / 100); - } else { - pitch = strTo<float>(pitchAttr.value); - } - } - if (HAS_ATTR(element, "roll")) { - NumAttr rollAttr = NumAttr(ATTR(element, "roll")); - if (boost::iequals(rollAttr.unit, "deg")) { - roll = osg::DegreesToRadians(strTo<float>(rollAttr.value)); - } else if (boost::iequals(rollAttr.unit, "%")) { - roll = osg::DegreesToRadians((strTo<float>(rollAttr.value) * 360) / 100); - } else { - roll = strTo<float>(rollAttr.value); - } - } - if (HAS_ATTR(element, "yaw")) { - NumAttr yawAttr = NumAttr(ATTR(element, "yaw")); - if (boost::iequals(yawAttr.unit, "deg")) { - yaw = osg::DegreesToRadians(strTo<float>(yawAttr.value)); - } else if (boost::iequals(yawAttr.unit, "%")) { - yaw = osg::DegreesToRadians((strTo<float>(yawAttr.value) * 360) / 100); - } else { - yaw = strTo<float>(yawAttr.value); - } - } - - osg::Matrix rotation; - rotation.makeRotate(roll, osg::Vec3(0,1,0), // roll - pitch, osg::Vec3(1,0,0) , // pitch - yaw, osg::Vec3(0,0,1) ); // heading - - return rotation; + double pitch = 0, roll = 0, yaw = 0; + if (HAS_ATTR(element, "pitch")) { + NumAttr pitchAttr = NumAttr(ATTR(element, "pitch")); + if (boost::iequals(pitchAttr.unit, "deg")) { + pitch = osg::DegreesToRadians(strTo<float>(pitchAttr.value)); + } else if (boost::iequals(pitchAttr.unit, "%")) { + pitch = osg::DegreesToRadians((strTo<float>(pitchAttr.value) * 360) / 100); + } else { + pitch = strTo<float>(pitchAttr.value); + } + } + if (HAS_ATTR(element, "roll")) { + NumAttr rollAttr = NumAttr(ATTR(element, "roll")); + if (boost::iequals(rollAttr.unit, "deg")) { + roll = osg::DegreesToRadians(strTo<float>(rollAttr.value)); + } else if (boost::iequals(rollAttr.unit, "%")) { + roll = osg::DegreesToRadians((strTo<float>(rollAttr.value) * 360) / 100); + } else { + roll = strTo<float>(rollAttr.value); + } + } + if (HAS_ATTR(element, "yaw")) { + NumAttr yawAttr = NumAttr(ATTR(element, "yaw")); + if (boost::iequals(yawAttr.unit, "deg")) { + yaw = osg::DegreesToRadians(strTo<float>(yawAttr.value)); + } else if (boost::iequals(yawAttr.unit, "%")) { + yaw = osg::DegreesToRadians((strTo<float>(yawAttr.value) * 360) / 100); + } else { + yaw = strTo<float>(yawAttr.value); + } + } + + osg::Matrix rotation; + rotation.makeRotate(roll, osg::Vec3(0,1,0), // roll + pitch, osg::Vec3(1,0,0) , // pitch + yaw, osg::Vec3(0,0,1) ); // heading + + return rotation; } void OSGInvoker::processScale(const Arabica::DOM::Node<std::string>& element) { - assert(_nodes.find(element.getParentNode()) != _nodes.end()); - osg::Node* node = _nodes[element.getParentNode()]; - - double x = 1, y = 1, z = 1; - if (HAS_ATTR(element, "x")) - x = strTo<float>(ATTR(element, "x")); - if (HAS_ATTR(element, "y")) - y = strTo<float>(ATTR(element, "y")); - if (HAS_ATTR(element, "z")) - z = strTo<float>(ATTR(element, "z")); - - osg::Matrix scale; - scale.makeScale(x, y, z); - - osg::MatrixTransform* transform = new osg::MatrixTransform(); - transform->setMatrix(scale); - node->asGroup()->addChild(transform); - _nodes[element] = transform; - - std::set<std::string> validChilds; - validChilds.insert("translation"); - validChilds.insert("rotation"); - validChilds.insert("scale"); - validChilds.insert("node"); - processChildren(validChilds, element); + assert(_nodes.find(element.getParentNode()) != _nodes.end()); + osg::Node* node = _nodes[element.getParentNode()]; + + double x = 1, y = 1, z = 1; + if (HAS_ATTR(element, "x")) + x = strTo<float>(ATTR(element, "x")); + if (HAS_ATTR(element, "y")) + y = strTo<float>(ATTR(element, "y")); + if (HAS_ATTR(element, "z")) + z = strTo<float>(ATTR(element, "z")); + + osg::Matrix scale; + scale.makeScale(x, y, z); + + osg::MatrixTransform* transform = new osg::MatrixTransform(); + transform->setMatrix(scale); + node->asGroup()->addChild(transform); + _nodes[element] = transform; + + std::set<std::string> validChilds; + validChilds.insert("translation"); + validChilds.insert("rotation"); + validChilds.insert("scale"); + validChilds.insert("node"); + processChildren(validChilds, element); } void OSGInvoker::processNode(const Arabica::DOM::Node<std::string>& element) { - _nodes_t::iterator nodeIter = _nodes.find(element.getParentNode()); - assert(nodeIter != _nodes.end()); - - assert(_nodes.find(element.getParentNode()) != _nodes.end()); - osg::Node* parent = _nodes[element.getParentNode()]; - - std::string filename; - if (HAS_ATTR(element, "src")) { - filename = ATTR(element, "src"); - - if (filename.length() > 0) { - std::string extension; - size_t extensionStart = filename.find_last_of("."); - if (extensionStart != std::string::npos) { - extension = filename.substr(extensionStart); - } - - URL srcURI(filename); - if (!srcURI.toAbsolute(_interpreter->getBaseURI())) { - LOG(ERROR) << "invoke element has relative src URI with no baseURI set."; - return; - } - filename = srcURI.asLocalFile(extension); - osg::ref_ptr<osg::Node> model = osgDB::readNodeFile(filename); - if (model.get()) - parent->asGroup()->addChild(model); - - } - } + _nodes_t::iterator nodeIter = _nodes.find(element.getParentNode()); + assert(nodeIter != _nodes.end()); + + assert(_nodes.find(element.getParentNode()) != _nodes.end()); + osg::Node* parent = _nodes[element.getParentNode()]; + + std::string filename; + if (HAS_ATTR(element, "src")) { + filename = ATTR(element, "src"); + + if (filename.length() > 0) { + std::string extension; + size_t extensionStart = filename.find_last_of("."); + if (extensionStart != std::string::npos) { + extension = filename.substr(extensionStart); + } + + URL srcURI(filename); + if (!srcURI.toAbsolute(_interpreter->getBaseURI())) { + LOG(ERROR) << "invoke element has relative src URI with no baseURI set."; + return; + } + filename = srcURI.asLocalFile(extension); + osg::ref_ptr<osg::Node> model = osgDB::readNodeFile(filename); + if (model.get()) + parent->asGroup()->addChild(model); + + } + } } void OSGInvoker::processChildren(const std::set<std::string>& validChildren, const Arabica::DOM::Node<std::string>& element) { - Arabica::DOM::NodeList<std::string> childs = element.getChildNodes(); - for (int i = 0; i < childs.getLength(); ++i) { - if (childs.item(i).getNodeType() != Arabica::DOM::Node_base::ELEMENT_NODE) - continue; - if (false) { - } else if (boost::iequals(LOCALNAME(childs.item(i)), "node") && - validChildren.find("node") != validChildren.end()) { - processNode(childs.item(i)); - } else if (boost::iequals(LOCALNAME(childs.item(i)), "translation") && - validChildren.find("translation") != validChildren.end()) { - processTranslation(childs.item(i)); - } else if (boost::iequals(LOCALNAME(childs.item(i)), "rotation") && - validChildren.find("rotation") != validChildren.end()) { - processRotation(childs.item(i)); - } else if (boost::iequals(LOCALNAME(childs.item(i)), "scale") && - validChildren.find("scale") != validChildren.end()) { - processScale(childs.item(i)); - } else if (boost::iequals(LOCALNAME(childs.item(i)), "viewport") && - validChildren.find("viewport") != validChildren.end()) { - processViewport(childs.item(i)); - } else if (boost::iequals(LOCALNAME(childs.item(i)), "camera") && - validChildren.find("camera") != validChildren.end()) { - processCamera(childs.item(i)); - } else if (boost::iequals(LOCALNAME(childs.item(i)), "display") && - validChildren.find("display") != validChildren.end()) { - processDisplay(childs.item(i)); - } else { - LOG(INFO) << "Unknown XML element " << TAGNAME(childs.item(i)); - } - } + Arabica::DOM::NodeList<std::string> childs = element.getChildNodes(); + for (int i = 0; i < childs.getLength(); ++i) { + if (childs.item(i).getNodeType() != Arabica::DOM::Node_base::ELEMENT_NODE) + continue; + if (false) { + } else if (boost::iequals(LOCALNAME(childs.item(i)), "node") && + validChildren.find("node") != validChildren.end()) { + processNode(childs.item(i)); + } else if (boost::iequals(LOCALNAME(childs.item(i)), "translation") && + validChildren.find("translation") != validChildren.end()) { + processTranslation(childs.item(i)); + } else if (boost::iequals(LOCALNAME(childs.item(i)), "rotation") && + validChildren.find("rotation") != validChildren.end()) { + processRotation(childs.item(i)); + } else if (boost::iequals(LOCALNAME(childs.item(i)), "scale") && + validChildren.find("scale") != validChildren.end()) { + processScale(childs.item(i)); + } else if (boost::iequals(LOCALNAME(childs.item(i)), "viewport") && + validChildren.find("viewport") != validChildren.end()) { + processViewport(childs.item(i)); + } else if (boost::iequals(LOCALNAME(childs.item(i)), "camera") && + validChildren.find("camera") != validChildren.end()) { + processCamera(childs.item(i)); + } else if (boost::iequals(LOCALNAME(childs.item(i)), "display") && + validChildren.find("display") != validChildren.end()) { + processDisplay(childs.item(i)); + } else { + LOG(INFO) << "Unknown XML element " << TAGNAME(childs.item(i)); + } + } } void OSGInvoker::getViewport(const Arabica::DOM::Node<std::string>& element, @@ -333,7 +333,7 @@ void OSGInvoker::getViewport(const Arabica::DOM::Node<std::string>& element, unsigned int& width, unsigned int& height, CompositeDisplay* display) { - getViewport(element, x, y, width, height, display->getWidth(), display->getHeight()); + getViewport(element, x, y, width, height, display->getWidth(), display->getHeight()); } @@ -342,15 +342,14 @@ void OSGInvoker::getViewport(const Arabica::DOM::Node<std::string>& element, unsigned int& y, unsigned int& width, unsigned int& height, - int& screenId) -{ - - screenId = (HAS_ATTR(element, "screenId") ? strTo<int>(ATTR(element, "screenId")) : 0); - - unsigned int fullWidth = 0; - unsigned int fullHeight = 0; - CompositeDisplay::getResolution(fullWidth, fullHeight, screenId); - getViewport(element, x, y, width, height, fullWidth, fullHeight); + int& screenId) { + + screenId = (HAS_ATTR(element, "screenId") ? strTo<int>(ATTR(element, "screenId")) : 0); + + unsigned int fullWidth = 0; + unsigned int fullHeight = 0; + CompositeDisplay::getResolution(fullWidth, fullHeight, screenId); + getViewport(element, x, y, width, height, fullWidth, fullHeight); } void OSGInvoker::getViewport(const Arabica::DOM::Node<std::string>& element, @@ -359,43 +358,42 @@ void OSGInvoker::getViewport(const Arabica::DOM::Node<std::string>& element, unsigned int& width, unsigned int& height, unsigned int fullWidth, - unsigned int fullHeight) -{ - if (HAS_ATTR(element, "x")) { - NumAttr xAttr = NumAttr(ATTR(element, "x")); - x = strTo<float>(xAttr.value); - if (boost::iequals(xAttr.unit, "%")) - x = (x * fullWidth) / 100; - } - if (HAS_ATTR(element, "y")) { - NumAttr yAttr = NumAttr(ATTR(element, "y")); - y = strTo<float>(yAttr.value); - if (boost::iequals(yAttr.unit, "%")) - y = (y * fullHeight) / 100; - } - if (HAS_ATTR(element, "width")) { - NumAttr widthAttr = NumAttr(ATTR(element, "width")); - width = strTo<float>(widthAttr.value); - if (boost::iequals(widthAttr.unit, "%")) - width = (width * fullWidth) / 100; - } - if (HAS_ATTR(element, "height")) { - NumAttr heightAttr = NumAttr(ATTR(element, "height")); - height = strTo<float>(heightAttr.value); - if (boost::iequals(heightAttr.unit, "%")) - height = (height * fullHeight) / 100; - } + unsigned int fullHeight) { + if (HAS_ATTR(element, "x")) { + NumAttr xAttr = NumAttr(ATTR(element, "x")); + x = strTo<float>(xAttr.value); + if (boost::iequals(xAttr.unit, "%")) + x = (x * fullWidth) / 100; + } + if (HAS_ATTR(element, "y")) { + NumAttr yAttr = NumAttr(ATTR(element, "y")); + y = strTo<float>(yAttr.value); + if (boost::iequals(yAttr.unit, "%")) + y = (y * fullHeight) / 100; + } + if (HAS_ATTR(element, "width")) { + NumAttr widthAttr = NumAttr(ATTR(element, "width")); + width = strTo<float>(widthAttr.value); + if (boost::iequals(widthAttr.unit, "%")) + width = (width * fullWidth) / 100; + } + if (HAS_ATTR(element, "height")) { + NumAttr heightAttr = NumAttr(ATTR(element, "height")); + height = strTo<float>(heightAttr.value); + if (boost::iequals(heightAttr.unit, "%")) + height = (height * fullHeight) / 100; + } } osgViewer::View* OSGInvoker::getView(const Arabica::DOM::Node<std::string>& element) { - Arabica::DOM::Node<std::string> curr = element; - while(curr && !boost::iequals(LOCALNAME(curr), "viewport")) { - curr = curr.getParentNode(); - } - if (curr && _views.find(curr) != _views.end()) - return _views[curr]; - return NULL; + Arabica::DOM::Node<std::string> curr = element; + while(curr && !boost::iequals(LOCALNAME(curr), "viewport")) { + curr = curr.getParentNode(); + } + if (curr && _views.find(curr) != _views.end()) + return _views[curr]; + return NULL; } - + }
\ No newline at end of file diff --git a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGInvoker.h b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGInvoker.h index aca54ec..e65bff8 100644 --- a/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGInvoker.h +++ b/src/uscxml/plugins/invoker/graphics/openscenegraph/OSGInvoker.h @@ -35,64 +35,64 @@ public: virtual void cancel(const std::string sendId); virtual void invoke(const InvokeRequest& req); virtual void sendToParent(const SendRequest& req); - virtual void handleEvent(Arabica::DOM::Events::Event<std::string>& event); + virtual void handleEvent(Arabica::DOM::Events::Event<std::string>& event); - virtual void runOnMainThread(); + virtual void runOnMainThread(); protected: - void processDisplay(const Arabica::DOM::Node<std::string>& element); - void updateDisplay(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); - void processViewport(const Arabica::DOM::Node<std::string>& element); - void updateViewport(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); - void processCamera(const Arabica::DOM::Node<std::string>& element); - void updateCamera(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); - - void processTranslation(const Arabica::DOM::Node<std::string>& element); - void updateTranslation(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); - - void processRotation(const Arabica::DOM::Node<std::string>& element); - void updateRotation(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); - static osg::Matrix rotationFromElement(const Arabica::DOM::Node<std::string>& element); - - void processScale(const Arabica::DOM::Node<std::string>& element); - void updateScale(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); - void processNode(const Arabica::DOM::Node<std::string>& element); - void updateNode(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); - - void processChildren(const std::set<std::string>& validChildren, const Arabica::DOM::Node<std::string>& element); - - void getViewport(const Arabica::DOM::Node<std::string>& element, - unsigned int& x, - unsigned int& y, - unsigned int& width, - unsigned int& height, - int& screenId); - - void getViewport(const Arabica::DOM::Node<std::string>& element, - unsigned int& x, - unsigned int& y, - unsigned int& width, - unsigned int& height, - CompositeDisplay* display); - - void getViewport(const Arabica::DOM::Node<std::string>& element, - unsigned int& x, - unsigned int& y, - unsigned int& width, - unsigned int& height, - unsigned int fullWidth, - unsigned int fullHeight); - - osgViewer::View* getView(const Arabica::DOM::Node<std::string>& element); - - std::map<Arabica::DOM::Node<std::string>, CompositeDisplay*> _displays; - typedef std::map<Arabica::DOM::Node<std::string>, CompositeDisplay*> _displays_t; - - std::map<Arabica::DOM::Node<std::string>, osgViewer::View*> _views; - typedef std::map<Arabica::DOM::Node<std::string>, osgViewer::View*> _views_t; - - std::map<Arabica::DOM::Node<std::string>, osg::Node*> _nodes; - typedef std::map<Arabica::DOM::Node<std::string>, osg::Node*> _nodes_t; + void processDisplay(const Arabica::DOM::Node<std::string>& element); + void updateDisplay(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); + void processViewport(const Arabica::DOM::Node<std::string>& element); + void updateViewport(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); + void processCamera(const Arabica::DOM::Node<std::string>& element); + void updateCamera(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); + + void processTranslation(const Arabica::DOM::Node<std::string>& element); + void updateTranslation(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); + + void processRotation(const Arabica::DOM::Node<std::string>& element); + void updateRotation(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); + static osg::Matrix rotationFromElement(const Arabica::DOM::Node<std::string>& element); + + void processScale(const Arabica::DOM::Node<std::string>& element); + void updateScale(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); + void processNode(const Arabica::DOM::Node<std::string>& element); + void updateNode(osg::Node* node, Arabica::DOM::Events::Event<std::string>& event); + + void processChildren(const std::set<std::string>& validChildren, const Arabica::DOM::Node<std::string>& element); + + void getViewport(const Arabica::DOM::Node<std::string>& element, + unsigned int& x, + unsigned int& y, + unsigned int& width, + unsigned int& height, + int& screenId); + + void getViewport(const Arabica::DOM::Node<std::string>& element, + unsigned int& x, + unsigned int& y, + unsigned int& width, + unsigned int& height, + CompositeDisplay* display); + + void getViewport(const Arabica::DOM::Node<std::string>& element, + unsigned int& x, + unsigned int& y, + unsigned int& width, + unsigned int& height, + unsigned int fullWidth, + unsigned int fullHeight); + + osgViewer::View* getView(const Arabica::DOM::Node<std::string>& element); + + std::map<Arabica::DOM::Node<std::string>, CompositeDisplay*> _displays; + typedef std::map<Arabica::DOM::Node<std::string>, CompositeDisplay*> _displays_t; + + std::map<Arabica::DOM::Node<std::string>, osgViewer::View*> _views; + typedef std::map<Arabica::DOM::Node<std::string>, osgViewer::View*> _views_t; + + std::map<Arabica::DOM::Node<std::string>, osg::Node*> _nodes; + typedef std::map<Arabica::DOM::Node<std::string>, osg::Node*> _nodes_t; tthread::recursive_mutex _mutex; std::string _invokeId; |