]> git.mxchange.org Git - simgear.git/blob - simgear/scene/util/SGSceneUserData.cxx
Work around apparent OSG 3.2.0 normal binding bug.
[simgear.git] / simgear / scene / util / SGSceneUserData.cxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2006-2007 Mathias Froehlich 
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #  include <simgear_config.h>
24 #endif
25
26 #include <osgDB/Registry>
27 #include <osgDB/Input>
28 #include <osgDB/ParameterOutput>
29
30 #include "SGSceneUserData.hxx"
31
32 SGSceneUserData*
33 SGSceneUserData::getSceneUserData(osg::Node* node)
34 {
35   if (!node)
36     return 0;
37   osg::Referenced* referenced = node->getUserData();
38   if (!referenced)
39     return 0;
40   return dynamic_cast<SGSceneUserData*>(referenced);
41 }
42
43 const SGSceneUserData*
44 SGSceneUserData::getSceneUserData(const osg::Node* node)
45 {
46   if (!node)
47     return 0;
48   const osg::Referenced* referenced = node->getUserData();
49   if (!referenced)
50     return 0;
51   return dynamic_cast<const SGSceneUserData*>(referenced);
52 }
53
54 SGSceneUserData*
55 SGSceneUserData::getOrCreateSceneUserData(osg::Node* node)
56 {
57   SGSceneUserData* userData = getSceneUserData(node);
58   if (userData)
59     return userData;
60   userData = new SGSceneUserData;
61   node->setUserData(userData);
62   return userData;
63 }
64
65 unsigned
66 SGSceneUserData::getNumPickCallbacks() const
67 {
68   return _pickCallbacks.size();
69 }
70
71 SGPickCallback*
72 SGSceneUserData::getPickCallback(unsigned i) const
73 {
74   if (_pickCallbacks.size() <= i)
75     return 0;
76   return _pickCallbacks[i];
77 }
78
79 void
80 SGSceneUserData::setPickCallback(SGPickCallback* pickCallback)
81 {
82   _pickCallbacks.clear();
83   addPickCallback(pickCallback);
84 }
85
86 void
87 SGSceneUserData::addPickCallback(SGPickCallback* pickCallback)
88 {
89   if (!pickCallback)
90     return;
91   _pickCallbacks.push_back(pickCallback);
92 }
93
94 bool SGSceneUserData_writeLocalData(const osg::Object& obj, osgDB::Output& fw)
95 {
96     const SGSceneUserData& data = static_cast<const SGSceneUserData&>(obj);
97
98     unsigned numPickCallbacks = data.getNumPickCallbacks();
99     if (numPickCallbacks > 0)
100         fw.indent() << "num_pickCallbacks " << numPickCallbacks << "\n";
101     if (data.getBVHNode())
102         fw.indent() << "hasBVH true\n";
103     const SGSceneUserData::Velocity* vel = data.getVelocity();
104     if (vel) {
105         fw.indent() << "velocity {\n";
106         fw.moveIn();
107         fw.indent() << "linear " << vel->linear << "\n";
108         fw.indent() << "angular " << vel->angular << "\n";
109         fw.indent() << "referenceTime " << vel->referenceTime << "\n";
110         fw.indent() << "id " << static_cast<unsigned>(vel->id) << "\n";
111         fw.moveOut();
112         fw.indent() << "}\n";
113     }
114     return true;
115 }
116
117 namespace
118 {
119 osgDB::RegisterDotOsgWrapperProxy SGSceneUserDataProxy
120 (
121     new SGSceneUserData,
122     "simgear::SGSceneUserData",
123     "Object simgear::SGSceneUserData",
124     0,
125     &SGSceneUserData_writeLocalData
126     );
127 }