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 | if (variableTyp_ == osgVisual::dataIO_slot::STRING ) |
---|
198 | return &(dataSlots[i].sValue); |
---|
199 | else |
---|
200 | return &(dataSlots[i].value); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | // Slot does not exist -> add it to slot list |
---|
205 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotPointer() - Slot not found, will add as new slot " << std::endl; |
---|
206 | dataIO_slot newSlot; |
---|
207 | newSlot.variableName = variableName_; |
---|
208 | newSlot.variableType = variableTyp_; |
---|
209 | newSlot.value = 0; |
---|
210 | newSlot.sValue = ""; |
---|
211 | dataSlots.push_back( newSlot ); |
---|
212 | if (variableTyp_ == osgVisual::dataIO_slot::STRING ) |
---|
213 | return &(dataSlots.back().sValue); |
---|
214 | else |
---|
215 | { |
---|
216 | return &(dataSlots.back().value); |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | double visual_dataIO::getSlotDataAsDouble(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ ) |
---|
221 | { |
---|
222 | // iterate through slotlist. If found, return value |
---|
223 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
224 | { |
---|
225 | // Check if this variable name&-type already exists |
---|
226 | if( dataSlots[i].variableName == variableName_ && dataSlots[i].direction == direction_ && dataSlots[i].variableType == osgVisual::dataIO_slot::DOUBLE ) |
---|
227 | { |
---|
228 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl; |
---|
229 | return dataSlots[i].value; |
---|
230 | } |
---|
231 | } |
---|
232 | return 0; |
---|
233 | } |
---|
234 | |
---|
235 | std::string visual_dataIO::getSlotDataAsString(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_ ) |
---|
236 | { |
---|
237 | // iterate through slotlist. If found, return value |
---|
238 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
239 | { |
---|
240 | // Check if this variable name&-type already exists |
---|
241 | if( dataSlots[i].variableName == variableName_ && dataSlots[i].direction == direction_ && dataSlots[i].variableType == osgVisual::dataIO_slot::STRING ) |
---|
242 | { |
---|
243 | //OSG_NOTIFY( osg::INFO ) << "visual_dataIO::getSlotDataAsDouble() - Slot found at position " << i << std::endl; |
---|
244 | return dataSlots[i].sValue; |
---|
245 | } |
---|
246 | } |
---|
247 | return ""; |
---|
248 | } |
---|
249 | |
---|
250 | osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, std::string sValue_ ) |
---|
251 | { |
---|
252 | bool slotFound = false; |
---|
253 | // iterate through slotlist. If found, return pointer, else add slot to list |
---|
254 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
255 | { |
---|
256 | // Check if this variable name&-type already exists |
---|
257 | if( dataSlots[i].variableName == variableName_ && dataSlots[i].direction == direction_ && dataSlots[i].variableType == osgVisual::dataIO_slot::STRING) |
---|
258 | { |
---|
259 | // Update value |
---|
260 | dataSlots[i].sValue = sValue_; |
---|
261 | slotFound = true; |
---|
262 | return &(dataSlots[i]); |
---|
263 | } |
---|
264 | |
---|
265 | } |
---|
266 | |
---|
267 | if (!slotFound) |
---|
268 | { |
---|
269 | // Slot does not exist -> add it to slot list |
---|
270 | dataIO_slot newSlot; |
---|
271 | newSlot.variableName = variableName_; |
---|
272 | newSlot.direction = direction_; |
---|
273 | newSlot.variableType = osgVisual::dataIO_slot::STRING; |
---|
274 | newSlot.value = 0; |
---|
275 | newSlot.sValue = sValue_; |
---|
276 | dataSlots.push_back( newSlot ); |
---|
277 | } |
---|
278 | |
---|
279 | return NULL; |
---|
280 | } |
---|
281 | |
---|
282 | osgVisual::dataIO_slot* visual_dataIO::setSlotData(std::string variableName_, osgVisual::dataIO_slot::dataDirection direction_, double value_ ) |
---|
283 | { |
---|
284 | // iterate through slotlist. If found, return pointer, else add slot to list |
---|
285 | bool slotFound = false; |
---|
286 | for (unsigned int i=0; i<dataSlots.size(); i++) |
---|
287 | { |
---|
288 | // Check if this variableName & -type already exists |
---|
289 | if( dataSlots[i].variableName == variableName_ && dataSlots[i].direction == direction_ && dataSlots[i].variableType == osgVisual::dataIO_slot::DOUBLE) |
---|
290 | { |
---|
291 | // Update value |
---|
292 | //OSG_NOTIFY( osg::ALWAYS ) << "setSlotData: " << variableName_ << " - value: " << value_ << std::endl; |
---|
293 | dataSlots[i].value = value_; |
---|
294 | slotFound = true; |
---|
295 | return &(dataSlots[i]); |
---|
296 | } |
---|
297 | } |
---|
298 | |
---|
299 | if (!slotFound) |
---|
300 | { |
---|
301 | // Slot does not exist -> add it to slot list |
---|
302 | dataIO_slot newSlot; |
---|
303 | newSlot.variableName = variableName_; |
---|
304 | newSlot.direction = direction_; |
---|
305 | newSlot.variableType = osgVisual::dataIO_slot::DOUBLE; |
---|
306 | newSlot.value = value_; |
---|
307 | newSlot.sValue = ""; |
---|
308 | dataSlots.push_back( newSlot ); |
---|
309 | } |
---|
310 | |
---|
311 | return NULL; |
---|
312 | } |
---|
313 | |
---|
314 | osg::Matrixd visual_dataIO::calcViewMatrix() |
---|
315 | { |
---|
316 | return viewer->getCameraManipulator()->getInverseMatrix(); |
---|
317 | } |
---|