-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMiscOSG.cpp
107 lines (80 loc) · 2.8 KB
/
MiscOSG.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "MiscOSG.h"
// ================================================
// reparentNode
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
MPVCMNOSG_SPEC void reparentNode( osg::Node *node, osg::Group *newParent )
{
if( !node ) return;
node->ref();
// first, remove node from all parents
while( node->getNumParents() > 0 ) {
node->getParent(0)->removeChild( node );
}
// next, add node to the newParent
if( newParent ) {
newParent->addChild( node );
}
node->unref();
}
// ================================================
// searchForNodeByName
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
MPVCMNOSG_SPEC osg::Node * searchForNodeByName( osg::Node *node, const std::string &name )
{
if( node == NULL ) return NULL;
if( node->getName() == name ) return node;
osg::Group *group = node->asGroup();
if( group == NULL ) return NULL;
for( unsigned int i = 0; i < group->getNumChildren(); i++ )
{
osg::Node *child = group->getChild(i);
osg::Node *result = searchForNodeByName( child, name );
if( result )
return result;
}
// if no children contained the requested node, return NULL
return NULL;
}
// ================================================
// searchForNodeByClass
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
MPVCMNOSG_SPEC void searchForNodeByClass( osg::Node *node, const std::string &name, std::list<osg::Node *> &result )
{
if( node == NULL ) return;
if( node->className() == name )
result.push_back( node );
osg::Group *group = node->asGroup();
if( group == NULL ) return;
for( unsigned int i = 0; i < group->getNumChildren(); i++ )
{
osg::Node *child = group->getChild(i);
searchForNodeByClass( child, name, result );
}
return;
}
// ================================================
// convertMPVMatrixToOSGMatrix
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
MPVCMNOSG_SPEC osg::Matrix convertMPVMatrixToOSGMatrix( const mpv::Mtx4 &mpvMtx )
{
osg::Matrix result;
// OSG matrices are stored "sideways", like OpenGL.
// MPV matrices are stored like you learned in math class (except the subscript is
// backwards; math class convention for refering to an element is Mcol,row).
// They need to be transposed to convert between the two.
result.set( mpvMtx.transpose().GetM() );
return result;
}
// ================================================
// convertOSGMatrixToMPVMatrix
// vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
MPVCMNOSG_SPEC mpv::Mtx4 convertOSGMatrixToMPVMatrix( const osg::Matrix &osgMtx )
{
mpv::Mtx4 result;
// OSG matrices are stored "sideways", like OpenGL.
// MPV matrices are stored like you learned in math class (except the subscript is
// backwards; math class convention for refering to an element is Mcol,row).
// They need to be transposed to convert between the two.
result.pcset( osgMtx.ptr() );
return result;
}