/*************************************************************************** * Copyright (C) 2007 by Thomas Webernorfer * * thomas_weberndorfer@hotmail.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program 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 * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "SceneGraph.h" #include #include "OSGNameAttachment.h" #include "OSGVoidPAttachment.h" #include #include #include #include #include #include #include #include #include #include #include "Config.h" OSG_USING_NAMESPACE; SceneGraph::SceneGraph() { // create the root-node containing a transform-core // and a transformed-node containing a group-core transform = Transform::create(); root = Node::create(); transformednode = Node::create(); transformednode->setCore ( Group::create() ); root->setCore ( transform ); root->addChild ( transformednode ); } SceneGraph::~SceneGraph() { // remove the root-node from its parent NodeRecPtr parent = root->getParent(); if ( parent != nullptr ) { // Parent found parent->subChild ( root ); } } void SceneGraph::loadModel ( const char* filename ) { if ( filename != NULL ) { GraphOpSeq *graphop = GraphOpSeq::create().get(); graphop->addGraphOp(MergeGraphOp::create().get()); graphop->addGraphOp(StripeGraphOp::create().get()); graphop->addGraphOp(MaterialMergeGraphOp::create().get()); addNodePtr ( SceneFileHandler::the()->read(filename, graphop) ); } } void SceneGraph::addNodePtr ( OSG::NodeRecPtr child ) { if ( child != nullptr ) { transformednode->addChild ( child ); BoxVolume &vol = transformednode->editVolume(true); vol.getCenter(center); } } NodeRecPtr SceneGraph::getNodePtr() { return root; } void SceneGraph::setPickingPointer ( void *p ) { if ( p != nullptr ) { VoidPAttachmentRecPtr pointer = VoidPAttachment::create(); pointer->editFieldPtr()->setValue ( p ); transformednode->addAttachment ( pointer ); } } void* SceneGraph::getPickingPointer ( OSG::Line &ray ) { // Create intersection action IntersectActionRefPtr act = IntersectAction::create(); act->setLine ( ray ); act->apply ( root ); void *result = NULL; // Intersect if ( act->didHit() ) { NodeRecPtr hitnode = act->getHitObject(); AttachmentRecPtr attachement; do { // Climb up in the OpenSG-tree until a noOSG_USING_NAMESPACEde with a fitting // attachement has been found. attachement = hitnode->findAttachment ( VoidPAttachment::getClassType() ); hitnode = hitnode->getParent(); } while ( attachement == nullptr ) /* busy wait */ ; // Attachement with pointer found result = dynamic_pointer_cast( attachement )->getField().getValue(); } return result; } void SceneGraph::multMatrix ( OSG::Matrix &matrix ) { transform->editMatrix().mult ( matrix ); } void SceneGraph::setScale ( float x, float y, float z ) { Matrix matrix; matrix.setScale ( x, y, z ); multMatrix ( matrix ); } void SceneGraph::setTranslate ( float x, float y, float z ) { Matrix matrix; matrix.setTranslate ( x, y, z ); multMatrix ( matrix ); } void SceneGraph::setRotate ( float x, float y, float z, float angle ) { setTranslate( center[0] , center[1] , center[2] ); Quaternion q; q.setValueAsAxisDeg ( x, y, z, angle ); Matrix matrix; matrix.setRotate ( q ); multMatrix ( matrix ); setTranslate( -center[0] , -center[1] , -center[2] ); } void SceneGraph::mergeScene ( SceneGraph *scene ) { if ( scene != NULL ) addNodePtr ( scene->getNodePtr() ); } void SceneGraph::setVisibility ( bool vis ) { // 2015-03-05 ZaJ: taken directly from compat implementation of root->setActive in OSGNode.inl: root->setTravMask( vis ? TypeTraits::getMax() : TypeTraits::getZeroElement() ); } void SceneGraph::setTransparency ( float alpha ) { MakeTransparentGraphOpTransitPtr op = MakeTransparentGraphOp::create(); std::ostringstream str; str << "transparency=" << (alpha*100); op->setParams ( str.str() ); op->traverse ( root ); }