1 | /* -*-c++-*- osgVisual - Copyright (C) 2009-2010 Torben Dannhauer |
---|
2 | * |
---|
3 | * This library is based on OpenSceneGraph, open source and may be redistributed and/or modified under |
---|
4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
---|
5 | * (at your option) any later version. The full license is in LICENSE file |
---|
6 | * included with this distribution, and on the openscenegraph.org website. |
---|
7 | * |
---|
8 | * osgVisual requires for some proprietary modules a license from the correspondig manufacturer. |
---|
9 | * You have to aquire licenses for all used proprietary modules. |
---|
10 | * |
---|
11 | * This library is distributed in the hope that it will be useful, |
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
14 | * OpenSceneGraph Public License for more details. |
---|
15 | */ |
---|
16 | |
---|
17 | #include "visual_dataIO.h" |
---|
18 | |
---|
19 | using namespace osgVisual; |
---|
20 | |
---|
21 | visual_dataIO::visual_dataIO() |
---|
22 | { |
---|
23 | OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO constructed" << std::endl; |
---|
24 | |
---|
25 | initialized = false; |
---|
26 | } |
---|
27 | |
---|
28 | visual_dataIO::~visual_dataIO() |
---|
29 | { |
---|
30 | OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO destructed" << std::endl; |
---|
31 | } |
---|
32 | |
---|
33 | visual_dataIO* visual_dataIO::getInstance() |
---|
34 | { |
---|
35 | static visual_dataIO instance; |
---|
36 | return &instance; |
---|
37 | }; |
---|
38 | |
---|
39 | void visual_dataIO::init(osgViewer::Viewer* viewer_, osg::ArgumentParser& arguments_) |
---|
40 | { |
---|
41 | OSG_NOTIFY( osg::ALWAYS ) << "visual_dataIO init()" << std::endl; |
---|
42 | |
---|
43 | // Init variables |
---|
44 | viewer = viewer_; |
---|
45 | |
---|
46 | // Parse operating Mode |
---|
47 | if (arguments_.read("-m")) |
---|
48 | { |
---|
49 | OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as MASTER" << std::endl; |
---|
50 | clusterMode = osgVisual::dataIO_cluster::MASTER; |
---|
51 | // Create Container: |
---|
52 | slotContainer = new osgVisual::dataIO_transportContainer(); |
---|
53 | } |
---|
54 | else if (arguments_.read("-s")) |
---|
55 | { |
---|
56 | OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as SLAVE" << std::endl; |
---|
57 | clusterMode = osgVisual::dataIO_cluster::SLAVE; |
---|
58 | slotContainer = NULL; // Slave only recieves container, therefor set this Pointer null. |
---|
59 | } |
---|
60 | else |
---|
61 | { |
---|
62 | OSG_NOTIFY( osg::ALWAYS ) << "Configure osgVisual as STANDALONE" << std::endl; |
---|
63 | clusterMode = osgVisual::dataIO_cluster::STANDALONE; |
---|
64 | // Create Container: |
---|
65 | slotContainer = new osgVisual::dataIO_transportContainer(); |
---|
66 | } |
---|
67 | |
---|
68 | // Create Cluster. |
---|
69 | #ifdef USE_CLUSTER_ASIO_TCP_IOSTREAM |
---|
70 | cluster = new dataIO_clusterAsioTcpIostream(); |
---|
71 | #endif |
---|
72 | #ifdef USE_CLUSTER_ENET |
---|
73 | cluster = new dataIO_clusterENet(); |
---|
74 | cluster->enableHardSync( false ); /** \todo : rebuild this structure in cluster.h and move it this way to a general implementation. */ |
---|
75 | #endif |
---|
76 | #ifdef USE_CLUSTER_DUMMY |
---|
77 | cluster = new dataIO_clusterDummy(); |
---|
78 | #endif |
---|
79 | if(cluster.valid()) |
---|
80 | //cluster->init(arguments_, clusterMode, slotContainer, true, false); |
---|
81 | cluster->init(arguments_, viewer_, clusterMode, slotContainer, false, false); |
---|
82 | |
---|
83 | // Create extLink. |
---|
84 | #ifdef USE_EXTLINK_DUMMY |
---|
85 | extLink = new dataIO_extLinkDummy( dataSlots ); |
---|
86 | #endif |
---|
87 | #ifdef USE_EXTLINK_VCL |
---|
88 | extLink = new dataIO_extLinkVCL( dataSlots ); |
---|
89 | #endif |
---|
90 | extLink->init(); |
---|
91 | |
---|
92 | |
---|
93 | |
---|
94 | // Install callbacks to perform DataIO activities every frame: |
---|
95 | //// EventCallback at the absolute beginning of the frame |
---|
96 | eventCallback = new dataIO_eventCallback(this); |
---|
97 | viewer->getCamera()->setEventCallback( eventCallback ); |
---|
98 | //// FinalDrawCallback at the end of event and update handling, but BEFORE rendering the frame |
---|
99 | finalDrawCallback = new dataIO_finalDrawCallback(this); |
---|
100 | viewer->getCamera()->setFinalDrawCallback( finalDrawCallback ); |
---|
101 | |
---|
102 | initialized = true; |
---|
103 | } |
---|
104 | |
---|
105 | void visual_dataIO::shutdown() |
---|
106 | { |
---|
107 | if(initialized) |
---|
108 | { |
---|
109 | OSG_NOTIFY( osg::ALWAYS ) << "Shutdown visual_dataIO..." << std::endl; |
---|
110 | |
---|
111 | viewer->getCamera()->removeEventCallback( eventCallback ); |
---|
112 | eventCallback = NULL; |
---|
113 | viewer->getCamera()->setFinalDrawCallback( NULL ); |
---|
114 | finalDrawCallback = NULL; |
---|
115 | |
---|
116 | viewer = NULL; |
---|
117 | |
---|
118 | |
---|
119 | if(cluster.valid()) |
---|
120 | cluster->shutdown(); |
---|
121 | if(extLink.valid()) |
---|
122 | extLink->shutdown(); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | void visual_dataIO::dataIO_eventCallback::operator()(osg::Node* node, osg::NodeVisitor* nv) |
---|
127 | { |
---|
128 | // perform all actions for the eventDrawCallback. |
---|
129 | OSG_NOTIFY( osg::INFO ) << "---- Executing EventCallback.." << std::endl; |
---|
130 | |
---|
131 | switch( dataIO->clusterMode ) |
---|
132 | { |
---|
133 | case osgVisual::dataIO_cluster::MASTER : |
---|
134 | { |
---|
135 | dataIO->extLink->readTO_OBJvalues(); |
---|
136 | dataIO->cluster->sendTO_OBJvaluesToSlaves(dataIO->calcViewMatrix()); |
---|
137 | } |
---|
138 | break; |
---|
139 | case osgVisual::dataIO_cluster::SLAVE : |
---|
140 | { |
---|
141 | dataIO->cluster->readTO_OBJvaluesFromMaster(); |
---|
142 | } |
---|
143 | break; |
---|
144 | case osgVisual::dataIO_cluster::STANDALONE : |
---|
145 | { |
---|
146 | dataIO->extLink->readTO_OBJvalues(); |
---|
147 | } |
---|
148 | break; |
---|
149 | default: |
---|
150 | OSG_NOTIFY( osg::FATAL ) << "ERROR: Unkown clustermode!" << std::endl; |
---|
151 | break; |
---|
152 | }; |
---|
153 | traverse(node, nv); |
---|
154 | } |
---|
155 | |
---|
156 | void visual_dataIO::dataIO_finalDrawCallback::operator() (const osg::Camera& camera) const |
---|
157 | { |
---|
158 | // perform all actions for the initialDrawCallback. |
---|
159 | OSG_NOTIFY( osg::INFO ) << "---- Executing InitialDrawCallback.." << std::endl; |
---|
160 | |
---|
161 | switch( dataIO->clusterMode ) |
---|
162 | { |
---|
163 | case osgVisual::dataIO_cluster::MASTER : |
---|
164 | { |
---|
165 | dataIO->extLink->writebackFROM_OBJvalues(); |
---|
166 | dataIO->cluster->waitForAllReadyToSwap(); |
---|
167 | dataIO->cluster->sendSwapCommand(); |
---|
168 | } |
---|
169 | break; |
---|
170 | case osgVisual::dataIO_cluster::SLAVE : |
---|
171 | { |
---|
172 | dataIO->cluster->reportAsReadyToSwap(); |
---|
173 | dataIO->cluster->waitForSwap(); |
---|
174 | } |
---|
175 | break; |
---|
176 | case osgVisual::dataIO_cluster::STANDALONE : |
---|
177 | { |
---|
178 | dataIO->extLink->writebackFROM_OBJvalues(); |
---|
179 | } |
---|
180 | break; |
---|
181 | default: |
---|
182 | OSG_NOTIFY( osg::FATAL ) << "ERROR: visual_dataIO::dataIO_finalDrawCallback::operator() - Unkown clustermode!" << std::endl; |
---|
183 | break; |
---|
184 | }; |
---|
185 | } |
---|
186 | |
---|
187 | void* visual_dataIO::getSlotPointer(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, osgVisual::dataIO_slot::varType variableTyp_ ) |
---|
188 | { |
---|
189 | // iterate through slotlist. If found, return pointer, else add slot to list and return pointer |
---|
190 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
191 | { |
---|
192 | // Check if this variable name&-type already exists |
---|
193 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == variableTyp_) |
---|
194 | { |
---|
195 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot found at position " << i << std::endl; |
---|
196 | // Return pointer to the value |
---|
197 | return dataSlots[i]; |
---|
198 | } |
---|
199 | } |
---|
200 | |
---|
201 | // Slot does not exist -> add it to slot list |
---|
202 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot not found, will add as new slot " << std::endl; |
---|
203 | dataIO_slot* newSlot = new dataIO_slot(); |
---|
204 | newSlot->variableName = variableName_; |
---|
205 | newSlot->variableType = variableTyp_; |
---|
206 | newSlot->value = 0; |
---|
207 | newSlot->sValue = ""; |
---|
208 | dataSlots.push_back( newSlot ); |
---|
209 | return dataSlots.back(); |
---|
210 | } |
---|
211 | |
---|
212 | double visual_dataIO::getSlotDataAsDouble(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ ) |
---|
213 | { |
---|
214 | // iterate through slotlist. If found, return value |
---|
215 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
216 | { |
---|
217 | // Check if this variable name&-type already exists |
---|
218 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::DOUBLE ) |
---|
219 | { |
---|
220 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl; |
---|
221 | return dataSlots[i]->value; |
---|
222 | } |
---|
223 | } |
---|
224 | return 0; |
---|
225 | } |
---|
226 | |
---|
227 | std::string visual_dataIO::getSlotDataAsString(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ ) |
---|
228 | { |
---|
229 | // iterate through slotlist. If found, return value |
---|
230 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
231 | { |
---|
232 | // Check if this variable name&-type already exists |
---|
233 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::STRING ) |
---|
234 | { |
---|
235 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl; |
---|
236 | return dataSlots[i]->sValue; |
---|
237 | } |
---|
238 | } |
---|
239 | return ""; |
---|
240 | } |
---|
241 | |
---|
242 | osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, std::string sValue_ ) |
---|
243 | { |
---|
244 | bool slotFound = false; |
---|
245 | // iterate through slotlist. If found, return pointer, else add slot to list |
---|
246 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
247 | { |
---|
248 | // Check if this variable name&-type already exists |
---|
249 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::STRING) |
---|
250 | { |
---|
251 | // Update value |
---|
252 | dataSlots[i]->sValue = sValue_; |
---|
253 | slotFound = true; |
---|
254 | return dataSlots[i]; |
---|
255 | } |
---|
256 | |
---|
257 | } |
---|
258 | |
---|
259 | if (!slotFound) |
---|
260 | { |
---|
261 | // Slot does not exist -> add it to slot list |
---|
262 | dataIO_slot* newSlot = new dataIO_slot(); |
---|
263 | newSlot->variableName = variableName_; |
---|
264 | newSlot->direction = direction_; |
---|
265 | newSlot->variableType = osgVisual::dataIO_slot::STRING; |
---|
266 | newSlot->value = 0; |
---|
267 | newSlot->sValue = sValue_; |
---|
268 | dataSlots.push_back( newSlot ); |
---|
269 | return dataSlots.back(); |
---|
270 | } |
---|
271 | |
---|
272 | return NULL; |
---|
273 | } |
---|
274 | |
---|
275 | osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, double value_ ) |
---|
276 | { |
---|
277 | // iterate through slotlist. If found, return pointer, else add slot to list |
---|
278 | bool slotFound = false; |
---|
279 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
280 | { |
---|
281 | // Check if this variableName & -type already exists |
---|
282 | if( dataSlots[i]->variableName == variableName_ && dataSlots[i]->direction == direction_ && dataSlots[i]->variableType == osgVisual::dataIO_slot::DOUBLE) |
---|
283 | { |
---|
284 | // Update value |
---|
285 | //OSG_NOTIFY( osg::ALWAYS ) << "setSlotData: " << variableName_ << " - value: " << value_ << std::endl; |
---|
286 | dataSlots[i]->value = value_; |
---|
287 | slotFound = true; |
---|
288 | return dataSlots[i]; |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | if (!slotFound) |
---|
293 | { |
---|
294 | // Slot does not exist -> add it to slot list |
---|
295 | dataIO_slot* newSlot = new dataIO_slot(); |
---|
296 | newSlot->variableName = variableName_; |
---|
297 | newSlot->direction = direction_; |
---|
298 | newSlot->variableType = osgVisual::dataIO_slot::DOUBLE; |
---|
299 | newSlot->value = value_; |
---|
300 | newSlot->sValue = ""; |
---|
301 | dataSlots.push_back( newSlot ); |
---|
302 | return dataSlots.back(); |
---|
303 | } |
---|
304 | |
---|
305 | return NULL; |
---|
306 | } |
---|
307 | |
---|
308 | osg::Matrixd visual_dataIO::calcViewMatrix() |
---|
309 | { |
---|
310 | return viewer->getCameraManipulator()->getInverseMatrix(); |
---|
311 | } |
---|