| 1 | #include "ConvertFromInventor.h" |
|---|
| 2 | |
|---|
| 3 | #include "PendulumCallback.h" |
|---|
| 4 | #include "ShuttleCallback.h" |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | #include <osg/MatrixTransform> |
|---|
| 8 | #include <osg/Node> |
|---|
| 9 | #include <osg/Geode> |
|---|
| 10 | #include <osg/Notify> |
|---|
| 11 | #include <osg/LineWidth> |
|---|
| 12 | #include <osg/Point> |
|---|
| 13 | #include <osg/TexEnv> |
|---|
| 14 | #include <osg/Texture2D> |
|---|
| 15 | #include <osg/PolygonMode> |
|---|
| 16 | #include <osg/BlendFunc> |
|---|
| 17 | #include <osg/Material> |
|---|
| 18 | #include <osg/CullFace> |
|---|
| 19 | #include <osg/LightModel> |
|---|
| 20 | #include <osg/LightSource> |
|---|
| 21 | #include <osg/ShadeModel> |
|---|
| 22 | #include <osg/LOD> |
|---|
| 23 | #include <osgUtil/TransformCallback> |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | #include <Inventor/SoDB.h> |
|---|
| 27 | #include <Inventor/SoInteraction.h> |
|---|
| 28 | #include <Inventor/nodes/SoSeparator.h> |
|---|
| 29 | #include <Inventor/nodes/SoTransformSeparator.h> |
|---|
| 30 | #include <Inventor/nodes/SoShape.h> |
|---|
| 31 | #include <Inventor/nodes/SoVertexShape.h> |
|---|
| 32 | #include <Inventor/nodes/SoLight.h> |
|---|
| 33 | #include <Inventor/nodes/SoDirectionalLight.h> |
|---|
| 34 | #include <Inventor/nodes/SoSpotLight.h> |
|---|
| 35 | #include <Inventor/nodes/SoPointLight.h> |
|---|
| 36 | #include <Inventor/nodes/SoRotor.h> |
|---|
| 37 | #include <Inventor/nodes/SoPendulum.h> |
|---|
| 38 | #include <Inventor/nodes/SoShuttle.h> |
|---|
| 39 | #include <Inventor/nodes/SoLOD.h> |
|---|
| 40 | #include <Inventor/nodes/SoTexture2.h> |
|---|
| 41 | #include <Inventor/nodes/SoEnvironment.h> |
|---|
| 42 | #include <Inventor/misc/SoChildList.h> |
|---|
| 43 | #include <Inventor/SoPrimitiveVertex.h> |
|---|
| 44 | #include <Inventor/SbLinear.h> |
|---|
| 45 | #include <Inventor/nodes/SoTransform.h> |
|---|
| 46 | #include <Inventor/nodes/SoInfo.h> |
|---|
| 47 | #include <Inventor/actions/SoWriteAction.h> |
|---|
| 48 | #include <Inventor/elements/SoModelMatrixElement.h> |
|---|
| 49 | |
|---|
| 50 | #ifdef __COIN__ |
|---|
| 51 | #include <Inventor/VRMLnodes/SoVRMLImageTexture.h> |
|---|
| 52 | #include <Inventor/VRMLnodes/SoVRMLTransform.h> |
|---|
| 53 | #include <Inventor/VRMLnodes/SoVRMLAppearance.h> |
|---|
| 54 | #include <Inventor/VRMLnodes/SoVRMLMaterial.h> |
|---|
| 55 | #endif |
|---|
| 56 | |
|---|
| 57 | #if defined(__COIN__) && (COIN_MAJOR_VERSION >= 3 || \ |
|---|
| 58 | (COIN_MAJOR_VERSION == 2 && COIN_MINOR_VERSION>=5)) |
|---|
| 59 | #define INVENTOR_SHADERS_AVAILABLE |
|---|
| 60 | #endif |
|---|
| 61 | |
|---|
| 62 | #ifdef INVENTOR_SHADERS_AVAILABLE |
|---|
| 63 | #include <Inventor/nodes/SoShaderProgram.h> |
|---|
| 64 | #include <Inventor/nodes/SoVertexShader.h> |
|---|
| 65 | #include <Inventor/nodes/SoGeometryShader.h> |
|---|
| 66 | #include <Inventor/nodes/SoFragmentShader.h> |
|---|
| 67 | #endif |
|---|
| 68 | |
|---|
| 69 | #include <map> |
|---|
| 70 | #include <assert.h> |
|---|
| 71 | #include <math.h> |
|---|
| 72 | #include <string.h> |
|---|
| 73 | #ifdef __linux |
|---|
| 74 | #include <values.h> |
|---|
| 75 | #endif |
|---|
| 76 | #ifdef __APPLE__ |
|---|
| 77 | #include <float.h> |
|---|
| 78 | #endif |
|---|
| 79 | |
|---|
| 80 | #define DEBUG_IV_PLUGIN |
|---|
| 81 | #define NOTIFY_HEADER "Inventor Plugin (reader): " |
|---|
| 82 | |
|---|
| 83 | |
|---|
| 84 | ConvertFromInventor::ConvertFromInventor() |
|---|
| 85 | { |
|---|
| 86 | numPrimitives = 0; |
|---|
| 87 | transformInfoName = ""; |
|---|
| 88 | appearanceName = ""; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | ConvertFromInventor::~ConvertFromInventor() |
|---|
| 92 | { |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | static bool |
|---|
| 96 | nodePreservesState(const SoNode *node) |
|---|
| 97 | { |
|---|
| 98 | return node->isOfType(SoSeparator::getClassTypeId()) || |
|---|
| 99 | (node->getChildren() != NULL && node->affectsState() == FALSE); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | SoCallbackAction::Response |
|---|
| 103 | ConvertFromInventor::restructure(void* data, SoCallbackAction* action, |
|---|
| 104 | const SoNode* node) |
|---|
| 105 | { |
|---|
| 106 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 107 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "restructure() " |
|---|
| 108 | << node->getTypeId().getName().getString(); |
|---|
| 109 | #endif |
|---|
| 110 | |
|---|
| 111 | int childrenTotal = 0; |
|---|
| 112 | int numModifiedChildren = 0; |
|---|
| 113 | int numRemovedNodes = 0; |
|---|
| 114 | std::vector<std::vector<int> > &stack = *((std::vector<std::vector<int> >*)data); |
|---|
| 115 | |
|---|
| 116 | if (node->isOfType(SoGroup::getClassTypeId())) { |
|---|
| 117 | |
|---|
| 118 | SoGroup *group = (SoGroup*)node; |
|---|
| 119 | SoGroup *affectedScene = NULL; |
|---|
| 120 | childrenTotal = group->getNumChildren(); |
|---|
| 121 | |
|---|
| 122 | for (int i=0, c=group->getNumChildren(); i<c; i++) { |
|---|
| 123 | SoNode *child = group->getChild(i); |
|---|
| 124 | if (!child->isOfType(SoSeparator::getClassTypeId()) && |
|---|
| 125 | child->affectsState()) { |
|---|
| 126 | |
|---|
| 127 | |
|---|
| 128 | SoSeparator *s = new SoSeparator; |
|---|
| 129 | s->addChild(group->getChild(i)); |
|---|
| 130 | group->replaceChild(i, s); |
|---|
| 131 | numModifiedChildren++; |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | if (!affectedScene) { |
|---|
| 135 | |
|---|
| 136 | |
|---|
| 137 | |
|---|
| 138 | const SoFullPath *path = (const SoFullPath*)action->getCurPath(); |
|---|
| 139 | assert(path->getLength() == 0 || |
|---|
| 140 | path->getNode(path->getLength()-1) == group && |
|---|
| 141 | "Group being restructured is not at the end of the path."); |
|---|
| 142 | int stackLevel = stack.size()-2; |
|---|
| 143 | for (int j=path->getLength()-2; j>=0; j--, stackLevel--) { |
|---|
| 144 | |
|---|
| 145 | |
|---|
| 146 | assert(stackLevel >=0); |
|---|
| 147 | std::vector<int> &nodesToRemove = stack[stackLevel]; |
|---|
| 148 | |
|---|
| 149 | |
|---|
| 150 | SoNode *parent = path->getNode(j); |
|---|
| 151 | int childIndex = path->getIndex(j+1); |
|---|
| 152 | const SoChildList *chl = parent->getChildren(); |
|---|
| 153 | assert(chl->operator[](childIndex) == path->getNode(j+1) && |
|---|
| 154 | "Wrong indexing."); |
|---|
| 155 | |
|---|
| 156 | |
|---|
| 157 | if (!affectedScene) |
|---|
| 158 | affectedScene = new SoGroup; |
|---|
| 159 | |
|---|
| 160 | |
|---|
| 161 | for (int k=childIndex+1, n=chl->getLength(); k<n; k++) { |
|---|
| 162 | affectedScene->addChild(chl->operator[](k)); |
|---|
| 163 | nodesToRemove.push_back(k); |
|---|
| 164 | numRemovedNodes++; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | if (nodePreservesState(parent)) |
|---|
| 170 | break; |
|---|
| 171 | } |
|---|
| 172 | } |
|---|
| 173 | |
|---|
| 174 | |
|---|
| 175 | s->addChild(affectedScene); |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 181 | if (numModifiedChildren == 0) |
|---|
| 182 | osg::notify(osg::DEBUG_INFO) << ": no changes necessary" << std::endl; |
|---|
| 183 | else |
|---|
| 184 | osg::notify(osg::DEBUG_INFO) << ": " << numModifiedChildren << |
|---|
| 185 | " nodes of " << childrenTotal << " restruc., " << |
|---|
| 186 | numRemovedNodes << " removed" << std::endl; |
|---|
| 187 | #endif |
|---|
| 188 | |
|---|
| 189 | return SoCallbackAction::CONTINUE; |
|---|
| 190 | } |
|---|
| 191 | |
|---|
| 192 | SoCallbackAction::Response |
|---|
| 193 | ConvertFromInventor::restructurePreNode(void* data, SoCallbackAction* action, |
|---|
| 194 | const SoNode* node) |
|---|
| 195 | { |
|---|
| 196 | std::vector<std::vector<int> > &stack = *((std::vector<std::vector<int> >*)data); |
|---|
| 197 | |
|---|
| 198 | stack.push_back(std::vector<int>()); |
|---|
| 199 | |
|---|
| 200 | return SoCallbackAction::CONTINUE; |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | SoCallbackAction::Response |
|---|
| 204 | ConvertFromInventor::restructurePostNode(void* data, SoCallbackAction* action, |
|---|
| 205 | const SoNode* node) |
|---|
| 206 | { |
|---|
| 207 | std::vector<std::vector<int> > &stack = *((std::vector<std::vector<int> >*)data); |
|---|
| 208 | |
|---|
| 209 | assert(stack.size() > 0 && "Stack is empty"); |
|---|
| 210 | std::vector<int> &nodesToRemove = stack.back(); |
|---|
| 211 | |
|---|
| 212 | if (nodesToRemove.size() > 0) { |
|---|
| 213 | |
|---|
| 214 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 215 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "postNode() " |
|---|
| 216 | << node->getTypeId().getName().getString() |
|---|
| 217 | << " (level " << stack.size() << ") removed " |
|---|
| 218 | << nodesToRemove.size() << " node(s)" << std::endl; |
|---|
| 219 | #endif |
|---|
| 220 | |
|---|
| 221 | assert(node->getChildren()); |
|---|
| 222 | for (int i=nodesToRemove.size()-1; i>=0; i--) { |
|---|
| 223 | assert(i==0 || nodesToRemove[i-1] < nodesToRemove[i] && |
|---|
| 224 | "Children to remove are not in order."); |
|---|
| 225 | node->getChildren()->remove(nodesToRemove[i]); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | stack.pop_back(); |
|---|
| 230 | |
|---|
| 231 | return SoCallbackAction::CONTINUE; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | void |
|---|
| 235 | ConvertFromInventor::preprocess(SoNode* root) |
|---|
| 236 | { |
|---|
| 237 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 238 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "Preprocessing..." << std::endl; |
|---|
| 239 | #endif |
|---|
| 240 | |
|---|
| 241 | SoCallbackAction action; |
|---|
| 242 | std::vector<std::vector<int> > stackOfNodesToRemove; |
|---|
| 243 | |
|---|
| 244 | |
|---|
| 245 | action.addPreCallback(SoNode::getClassTypeId(), |
|---|
| 246 | restructurePreNode, &stackOfNodesToRemove); |
|---|
| 247 | action.addPostCallback(SoLOD::getClassTypeId(), |
|---|
| 248 | restructure, &stackOfNodesToRemove); |
|---|
| 249 | action.addPostCallback(SoNode::getClassTypeId(), |
|---|
| 250 | restructurePostNode, &stackOfNodesToRemove); |
|---|
| 251 | |
|---|
| 252 | |
|---|
| 253 | action.apply(root); |
|---|
| 254 | |
|---|
| 255 | #if 0 // For debugging purposes: Write preprocessed scene to the file |
|---|
| 256 | SoOutput out; |
|---|
| 257 | out.openFile("preprocess.iv"); |
|---|
| 258 | SoWriteAction wa(&out); |
|---|
| 259 | wa.apply(root); |
|---|
| 260 | #endif |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | osg::Node* |
|---|
| 264 | ConvertFromInventor::convert(SoNode* ivRootNode) |
|---|
| 265 | { |
|---|
| 266 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 267 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "Converting..." << std::endl; |
|---|
| 268 | #endif |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | |
|---|
| 272 | osg::Matrix ivToOSGMat(osg::Matrix(1.0, 0.0, 0.0, 0.0, |
|---|
| 273 | 0.0, 0.0, 1.0, 0.0, |
|---|
| 274 | 0.0,-1.0, 0.0, 0.0, |
|---|
| 275 | 0.0, 0.0, 0.0, 1.0)); |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | osg::ref_ptr<osg::Group> osgRootNode = new osg::MatrixTransform(ivToOSGMat); |
|---|
| 279 | |
|---|
| 280 | |
|---|
| 281 | |
|---|
| 282 | |
|---|
| 283 | ivStateStack.push(IvStateItem(ivRootNode, osgRootNode)); |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | |
|---|
| 289 | SoCallbackAction cbAction; |
|---|
| 290 | |
|---|
| 291 | |
|---|
| 292 | |
|---|
| 293 | |
|---|
| 294 | |
|---|
| 295 | |
|---|
| 296 | |
|---|
| 297 | cbAction.addPreCallback(SoNode::getClassTypeId(), preNode, this); |
|---|
| 298 | |
|---|
| 299 | |
|---|
| 300 | cbAction.addPreCallback(SoTransformSeparator::getClassTypeId(), preTransformSeparator, this); |
|---|
| 301 | cbAction.addPostCallback(SoTransformSeparator::getClassTypeId(), postTransformSeparator, this); |
|---|
| 302 | |
|---|
| 303 | |
|---|
| 304 | |
|---|
| 305 | cbAction.addPreCallback(SoLOD::getClassTypeId(), preLOD, this); |
|---|
| 306 | cbAction.addPostCallback(SoLOD::getClassTypeId(), postLOD, this); |
|---|
| 307 | |
|---|
| 308 | |
|---|
| 309 | |
|---|
| 310 | cbAction.addPreCallback(SoShape::getClassTypeId(), preShape, this); |
|---|
| 311 | cbAction.addPostCallback(SoShape::getClassTypeId(), postShape, this); |
|---|
| 312 | |
|---|
| 313 | |
|---|
| 314 | cbAction.addPostCallback(SoTexture2::getClassTypeId(), |
|---|
| 315 | postTexture, this); |
|---|
| 316 | #ifdef __COIN__ |
|---|
| 317 | cbAction.addPostCallback(SoVRMLImageTexture::getClassTypeId(), |
|---|
| 318 | postTexture, this); |
|---|
| 319 | cbAction.addPostCallback(SoVRMLAppearance::getClassTypeId(), |
|---|
| 320 | postTexture, this); |
|---|
| 321 | #endif |
|---|
| 322 | |
|---|
| 323 | #ifdef __COIN__ |
|---|
| 324 | cbAction.addPreCallback(SoInfo::getClassTypeId(), preInfo, this); |
|---|
| 325 | #endif |
|---|
| 326 | |
|---|
| 327 | |
|---|
| 328 | cbAction.addPreCallback(SoLight::getClassTypeId(), preLight, this); |
|---|
| 329 | |
|---|
| 330 | |
|---|
| 331 | cbAction.addPreCallback(SoEnvironment::getClassTypeId(), preEnvironment, this); |
|---|
| 332 | |
|---|
| 333 | |
|---|
| 334 | #ifdef INVENTOR_SHADERS_AVAILABLE |
|---|
| 335 | cbAction.addPreCallback(SoShaderProgram::getClassTypeId(), preShaderProgram, this); |
|---|
| 336 | #endif |
|---|
| 337 | |
|---|
| 338 | |
|---|
| 339 | cbAction.addPreCallback(SoRotor::getClassTypeId(), preRotor, this); |
|---|
| 340 | cbAction.addPreCallback(SoPendulum::getClassTypeId(), prePendulum, this); |
|---|
| 341 | cbAction.addPreCallback(SoShuttle::getClassTypeId(), preShuttle, this); |
|---|
| 342 | |
|---|
| 343 | |
|---|
| 344 | cbAction.addTriangleCallback(SoShape::getClassTypeId(), addTriangleCB, this); |
|---|
| 345 | cbAction.addLineSegmentCallback(SoShape::getClassTypeId(), addLineSegmentCB, |
|---|
| 346 | this); |
|---|
| 347 | cbAction.addPointCallback(SoShape::getClassTypeId(), addPointCB, this); |
|---|
| 348 | |
|---|
| 349 | |
|---|
| 350 | cbAction.addPostCallback(SoNode::getClassTypeId(), postNode, this); |
|---|
| 351 | |
|---|
| 352 | |
|---|
| 353 | cbAction.apply(ivRootNode); |
|---|
| 354 | |
|---|
| 355 | |
|---|
| 356 | if (osgRootNode->getNumChildren() == 1) { |
|---|
| 357 | osg::ref_ptr<osg::Group> toRemove = osgRootNode->getChild(0)->asGroup(); |
|---|
| 358 | assert(toRemove.get() && |
|---|
| 359 | strcmp(toRemove->className(), "Group") == 0 && |
|---|
| 360 | "IvStateStack osg graph is expected to be " |
|---|
| 361 | "headed by osg::Group"); |
|---|
| 362 | osgRootNode->removeChild(0u); |
|---|
| 363 | for (int i=0, c=toRemove->getNumChildren(); i<c; i++) |
|---|
| 364 | osgRootNode->addChild(toRemove->getChild(i)); |
|---|
| 365 | } |
|---|
| 366 | |
|---|
| 367 | return osgRootNode.get(); |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | static void |
|---|
| 371 | notifyAboutMatrixContent(const osg::NotifySeverity level, const SbMatrix &m) |
|---|
| 372 | { |
|---|
| 373 | SbVec3f t,s; |
|---|
| 374 | SbRotation r,so; |
|---|
| 375 | m.getTransform(t, r, s, so); |
|---|
| 376 | SbVec3f axis; |
|---|
| 377 | float angle; |
|---|
| 378 | r.getValue(axis, angle); |
|---|
| 379 | osg::notify(level) << NOTIFY_HEADER << " Translation: " << |
|---|
| 380 | t[0] << "," << t[1] << "," << t[2] << std::endl; |
|---|
| 381 | osg::notify(level) << NOTIFY_HEADER << " Rotation: (" << |
|---|
| 382 | axis[0] << "," << axis[1] << "," << axis[2] << ")," << angle << std::endl; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | void |
|---|
| 386 | ConvertFromInventor::appendNode(osg::Node *n, const SoCallbackAction *action) |
|---|
| 387 | { |
|---|
| 388 | IvStateItem &ivState = ivStateStack.top(); |
|---|
| 389 | SbMatrix currentMatrix = action->getModelMatrix(); |
|---|
| 390 | SbMatrix inheritedMatrix = ivState.inheritedTransformation; |
|---|
| 391 | |
|---|
| 392 | |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | if (ivState.flags & IvStateItem::KEEP_CHILDREN_ORDER) { |
|---|
| 397 | |
|---|
| 398 | |
|---|
| 399 | int childIndex = -1; |
|---|
| 400 | const SoFullPath *path = (const SoFullPath*)(((SoCallbackAction*)action)->getCurPath()); |
|---|
| 401 | for (int i=path->getLength()-2; i>=0; i--) |
|---|
| 402 | if (path->getNode(i) == ivState.keepChildrenOrderParent) { |
|---|
| 403 | childIndex = path->getIndex(i+1); |
|---|
| 404 | assert(ivState.keepChildrenOrderParent->getChildren()); |
|---|
| 405 | assert((ivState.keepChildrenOrderParent->getChildren()->operator[](childIndex) == path->getNode(i+1)) && "Indexing is wrong."); |
|---|
| 406 | break; |
|---|
| 407 | } |
|---|
| 408 | assert(childIndex != -1 && "Node did not found."); |
|---|
| 409 | |
|---|
| 410 | |
|---|
| 411 | assert(int(ivState.osgStateRoot->getNumChildren()) <= childIndex && |
|---|
| 412 | "Number of children in ivState.osgStateRoot is too big."); |
|---|
| 413 | while (int(ivState.osgStateRoot->getNumChildren()) < childIndex) |
|---|
| 414 | ivState.osgStateRoot->addChild(new osg::Node); |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 418 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "appendNode: " |
|---|
| 419 | << n->className(); |
|---|
| 420 | #endif |
|---|
| 421 | |
|---|
| 422 | if (currentMatrix == inheritedMatrix) { |
|---|
| 423 | |
|---|
| 424 | |
|---|
| 425 | ivState.osgStateRoot->addChild(n); |
|---|
| 426 | ivState.lastUsedTransformation = inheritedMatrix; |
|---|
| 427 | |
|---|
| 428 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 429 | if (osg::isNotifyEnabled(osg::DEBUG_INFO)) |
|---|
| 430 | osg::notify(osg::DEBUG_INFO) << |
|---|
| 431 | " uses parent transformation" << std::endl; |
|---|
| 432 | #endif |
|---|
| 433 | |
|---|
| 434 | } else { |
|---|
| 435 | |
|---|
| 436 | if (!(ivState.flags & IvStateItem::KEEP_CHILDREN_ORDER) && |
|---|
| 437 | currentMatrix == ivState.lastUsedTransformation) { |
|---|
| 438 | |
|---|
| 439 | |
|---|
| 440 | assert(ivState.osgStateRoot->getNumChildren() != 0 && |
|---|
| 441 | "This should never happen - there is no item on " |
|---|
| 442 | "osgShapeGraphs list while want to use last one."); |
|---|
| 443 | osg::Transform *t = ivState.osgStateRoot->getChild(ivState.osgStateRoot->getNumChildren()-1)->asTransform(); |
|---|
| 444 | assert(t && "This should never happen - want to use " |
|---|
| 445 | "transformation of previous scene geometry " |
|---|
| 446 | "and it does not have Transform node."); |
|---|
| 447 | t->addChild(n); |
|---|
| 448 | |
|---|
| 449 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 450 | if (osg::isNotifyEnabled(osg::DEBUG_INFO)) |
|---|
| 451 | osg::notify(osg::DEBUG_INFO) << |
|---|
| 452 | " reuses previous transformation" << std::endl; |
|---|
| 453 | #endif |
|---|
| 454 | |
|---|
| 455 | } else { |
|---|
| 456 | |
|---|
| 457 | |
|---|
| 458 | osg::Matrix m(osg::Matrix(currentMatrix.operator float*())); |
|---|
| 459 | osg::Matrix m2; |
|---|
| 460 | m2.invert(osg::Matrix(inheritedMatrix.operator float*())); |
|---|
| 461 | m.postMult(m2); |
|---|
| 462 | osg::MatrixTransform *mt = new osg::MatrixTransform(m); |
|---|
| 463 | mt->addChild(n); |
|---|
| 464 | |
|---|
| 465 | ivState.osgStateRoot->addChild(mt); |
|---|
| 466 | ivState.lastUsedTransformation = currentMatrix; |
|---|
| 467 | |
|---|
| 468 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 469 | if (osg::isNotifyEnabled(osg::DEBUG_INFO)) { |
|---|
| 470 | osg::notify(osg::DEBUG_INFO) << |
|---|
| 471 | " uses local transformation:" << std::endl; |
|---|
| 472 | notifyAboutMatrixContent(osg::DEBUG_INFO, |
|---|
| 473 | SbMatrix((SbMat&)(*osg::Matrixf(m).ptr()))); |
|---|
| 474 | } |
|---|
| 475 | #endif |
|---|
| 476 | } |
|---|
| 477 | } |
|---|
| 478 | } |
|---|
| 479 | |
|---|
| 480 | void |
|---|
| 481 | ConvertFromInventor::ivPushState(const SoCallbackAction *action, |
|---|
| 482 | const SoNode *initiator, const int flags, |
|---|
| 483 | osg::Group *root) |
|---|
| 484 | { |
|---|
| 485 | assert(ivStateStack.size() >= 1 && "There must be at least one " |
|---|
| 486 | "value in the ivStateStack to use ivPushState function."); |
|---|
| 487 | |
|---|
| 488 | |
|---|
| 489 | if (flags & IvStateItem::APPEND_AT_PUSH) |
|---|
| 490 | appendNode(root, action); |
|---|
| 491 | |
|---|
| 492 | |
|---|
| 493 | ivStateStack.push(IvStateItem(ivStateStack.top(), action, initiator, flags, root)); |
|---|
| 494 | |
|---|
| 495 | } |
|---|
| 496 | |
|---|
| 497 | void |
|---|
| 498 | ConvertFromInventor::ivPopState(const SoCallbackAction *action, |
|---|
| 499 | const SoNode *initiator) |
|---|
| 500 | { |
|---|
| 501 | bool multipop; |
|---|
| 502 | do { |
|---|
| 503 | assert(ivStateStack.size() >= 2 && "There must be at least two " |
|---|
| 504 | "values in the ivStateStack to use ivPopState function."); |
|---|
| 505 | |
|---|
| 506 | |
|---|
| 507 | IvStateItem ivState = ivStateStack.top(); |
|---|
| 508 | multipop = ivState.flags & IvStateItem::MULTI_POP; |
|---|
| 509 | assert(multipop || |
|---|
| 510 | ivState.pushInitiator == initiator && |
|---|
| 511 | "ivStateStack push was initiated by different node."); |
|---|
| 512 | |
|---|
| 513 | |
|---|
| 514 | osg::Group *stateRoot = ivState.osgStateRoot; |
|---|
| 515 | osg::ref_ptr<osg::Group> r = stateRoot; |
|---|
| 516 | |
|---|
| 517 | |
|---|
| 518 | |
|---|
| 519 | |
|---|
| 520 | |
|---|
| 521 | |
|---|
| 522 | |
|---|
| 523 | |
|---|
| 524 | |
|---|
| 525 | |
|---|
| 526 | |
|---|
| 527 | |
|---|
| 528 | ivStateStack.pop(); |
|---|
| 529 | |
|---|
| 530 | |
|---|
| 531 | if ((ivState.flags & (IvStateItem::UPDATE_STATE | |
|---|
| 532 | IvStateItem::UPDATE_STATE_EXCEPT_TRANSFORM)) != 0) { |
|---|
| 533 | IvStateItem &newTop = ivStateStack.top(); |
|---|
| 534 | newTop.currentTexture = ivState.currentTexture; |
|---|
| 535 | newTop.currentLights = ivState.currentLights; |
|---|
| 536 | newTop.currentGLProgram = ivState.currentGLProgram; |
|---|
| 537 | } |
|---|
| 538 | |
|---|
| 539 | |
|---|
| 540 | if (!(ivState.flags & IvStateItem::APPEND_AT_PUSH)) |
|---|
| 541 | appendNode(r, action); |
|---|
| 542 | |
|---|
| 543 | } while (multipop); |
|---|
| 544 | |
|---|
| 545 | } |
|---|
| 546 | |
|---|
| 547 | SoCallbackAction::Response |
|---|
| 548 | ConvertFromInventor::preNode(void* data, SoCallbackAction* action, |
|---|
| 549 | const SoNode* node) |
|---|
| 550 | { |
|---|
| 551 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 552 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preNode() " |
|---|
| 553 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 554 | #endif |
|---|
| 555 | |
|---|
| 556 | if (nodePreservesState(node)) { |
|---|
| 557 | |
|---|
| 558 | |
|---|
| 559 | ConvertFromInventor *thisPtr = (ConvertFromInventor *) data; |
|---|
| 560 | thisPtr->ivPushState(action, node); |
|---|
| 561 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 562 | if (osg::isNotifyEnabled(osg::DEBUG_INFO)) { |
|---|
| 563 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "push state, saved values: " << std::endl; |
|---|
| 564 | notifyAboutMatrixContent(osg::DEBUG_INFO, action->getModelMatrix()); |
|---|
| 565 | } |
|---|
| 566 | #endif |
|---|
| 567 | } |
|---|
| 568 | |
|---|
| 569 | return SoCallbackAction::CONTINUE; |
|---|
| 570 | } |
|---|
| 571 | |
|---|
| 572 | SoCallbackAction::Response |
|---|
| 573 | ConvertFromInventor::postNode(void* data, SoCallbackAction* action, |
|---|
| 574 | const SoNode* node) |
|---|
| 575 | { |
|---|
| 576 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 577 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "postNode() " |
|---|
| 578 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 579 | #endif |
|---|
| 580 | |
|---|
| 581 | if (nodePreservesState(node)) { |
|---|
| 582 | |
|---|
| 583 | |
|---|
| 584 | ConvertFromInventor *thisPtr = (ConvertFromInventor *) data; |
|---|
| 585 | assert(thisPtr->ivStateStack.size() > 0 && "ivStackState underflow"); |
|---|
| 586 | thisPtr->ivPopState(action, node); |
|---|
| 587 | |
|---|
| 588 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 589 | if (osg::isNotifyEnabled(osg::DEBUG_INFO)) { |
|---|
| 590 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << |
|---|
| 591 | "pop state, restored transformation: " << std::endl; |
|---|
| 592 | notifyAboutMatrixContent(osg::DEBUG_INFO, action->getModelMatrix()); |
|---|
| 593 | } |
|---|
| 594 | #endif |
|---|
| 595 | } |
|---|
| 596 | |
|---|
| 597 | return SoCallbackAction::CONTINUE; |
|---|
| 598 | } |
|---|
| 599 | |
|---|
| 600 | SoCallbackAction::Response |
|---|
| 601 | ConvertFromInventor::preTransformSeparator(void* data, SoCallbackAction* action, |
|---|
| 602 | const SoNode* node) |
|---|
| 603 | { |
|---|
| 604 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 605 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preTransformSeparator() " |
|---|
| 606 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 607 | #endif |
|---|
| 608 | |
|---|
| 609 | |
|---|
| 610 | ConvertFromInventor *thisPtr = (ConvertFromInventor *) data; |
|---|
| 611 | thisPtr->ivPushState(action, node, IvStateItem::UPDATE_STATE_EXCEPT_TRANSFORM, |
|---|
| 612 | new osg::Group()); |
|---|
| 613 | |
|---|
| 614 | return SoCallbackAction::CONTINUE; |
|---|
| 615 | } |
|---|
| 616 | |
|---|
| 617 | SoCallbackAction::Response |
|---|
| 618 | ConvertFromInventor::postTransformSeparator(void* data, SoCallbackAction* action, |
|---|
| 619 | const SoNode* node) |
|---|
| 620 | { |
|---|
| 621 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 622 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "postTransformSeparator() " |
|---|
| 623 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 624 | #endif |
|---|
| 625 | |
|---|
| 626 | |
|---|
| 627 | ConvertFromInventor *thisPtr = (ConvertFromInventor *) data; |
|---|
| 628 | assert(thisPtr->ivStateStack.size() > 0 && "ivStackState underflow"); |
|---|
| 629 | thisPtr->ivPopState(action, node); |
|---|
| 630 | |
|---|
| 631 | return SoCallbackAction::CONTINUE; |
|---|
| 632 | } |
|---|
| 633 | |
|---|
| 634 | SoCallbackAction::Response |
|---|
| 635 | ConvertFromInventor::preLOD(void* data, SoCallbackAction* action, |
|---|
| 636 | const SoNode* node) |
|---|
| 637 | { |
|---|
| 638 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 639 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preLOD() " |
|---|
| 640 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 641 | #endif |
|---|
| 642 | |
|---|
| 643 | |
|---|
| 644 | ConvertFromInventor* thisPtr = (ConvertFromInventor*)data; |
|---|
| 645 | |
|---|
| 646 | |
|---|
| 647 | |
|---|
| 648 | |
|---|
| 649 | |
|---|
| 650 | |
|---|
| 651 | |
|---|
| 652 | |
|---|
| 653 | |
|---|
| 654 | |
|---|
| 655 | |
|---|
| 656 | |
|---|
| 657 | |
|---|
| 658 | |
|---|
| 659 | |
|---|
| 660 | |
|---|
| 661 | |
|---|
| 662 | |
|---|
| 663 | |
|---|
| 664 | |
|---|
| 665 | |
|---|
| 666 | |
|---|
| 667 | |
|---|
| 668 | |
|---|
| 669 | |
|---|
| 670 | |
|---|
| 671 | |
|---|
| 672 | |
|---|
| 673 | |
|---|
| 674 | |
|---|
| 675 | |
|---|
| 676 | |
|---|
| 677 | |
|---|
| 678 | |
|---|
| 679 | |
|---|
| 680 | if (node->isOfType(SoLOD::getClassTypeId())) { |
|---|
| 681 | |
|---|
| 682 | thisPtr->ivPushState(action, node, IvStateItem::KEEP_CHILDREN_ORDER, |
|---|
| 683 | new osg::LOD); |
|---|
| 684 | thisPtr->ivStateStack.top().keepChildrenOrderParent = node; |
|---|
| 685 | |
|---|
| 686 | return SoCallbackAction::CONTINUE; |
|---|
| 687 | } |
|---|
| 688 | |
|---|
| 689 | return SoCallbackAction::CONTINUE; |
|---|
| 690 | } |
|---|
| 691 | |
|---|
| 692 | SoCallbackAction::Response |
|---|
| 693 | ConvertFromInventor::postLOD(void* data, SoCallbackAction* action, |
|---|
| 694 | const SoNode* node) |
|---|
| 695 | { |
|---|
| 696 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 697 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "postLOD() " |
|---|
| 698 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 699 | #endif |
|---|
| 700 | |
|---|
| 701 | |
|---|
| 702 | if (node->getTypeId() == SoGroup::getClassTypeId()) |
|---|
| 703 | return SoCallbackAction::CONTINUE; |
|---|
| 704 | |
|---|
| 705 | |
|---|
| 706 | ConvertFromInventor* thisPtr = (ConvertFromInventor*)data; |
|---|
| 707 | IvStateItem &ivState = thisPtr->ivStateStack.top(); |
|---|
| 708 | |
|---|
| 709 | |
|---|
| 710 | if (node->isOfType(SoLOD::getClassTypeId())) { |
|---|
| 711 | |
|---|
| 712 | osg::LOD *lod = dynamic_cast<osg::LOD*>(ivState.osgStateRoot.get()); |
|---|
| 713 | SoLOD *ivLOD = (SoLOD*)node; |
|---|
| 714 | |
|---|
| 715 | |
|---|
| 716 | SbVec3f ivCenter = ivLOD->center.getValue(); |
|---|
| 717 | lod->setCenter(osg::Vec3(ivCenter[0], ivCenter[1], ivCenter[2])); |
|---|
| 718 | |
|---|
| 719 | |
|---|
| 720 | int num = lod->getNumChildren(); |
|---|
| 721 | if (ivLOD->range.getNum()+1 != num && |
|---|
| 722 | !(num == 0 && ivLOD->range.getNum() == 0)) { |
|---|
| 723 | osg::notify(osg::WARN) << NOTIFY_HEADER << |
|---|
| 724 | "Warning: SoLOD does not contain " |
|---|
| 725 | "correct data in range field." << std::endl; |
|---|
| 726 | if (ivLOD->range.getNum()+1 < num) { |
|---|
| 727 | lod->removeChildren(ivLOD->range.getNum() + 1, |
|---|
| 728 | num - ivLOD->range.getNum() - 1); |
|---|
| 729 | num = ivLOD->range.getNum() + 1; |
|---|
| 730 | } |
|---|
| 731 | } |
|---|
| 732 | |
|---|
| 733 | |
|---|
| 734 | if (num > 0) { |
|---|
| 735 | if (num == 1) |
|---|
| 736 | lod->setRange(0, 0.0, FLT_MAX); |
|---|
| 737 | else { |
|---|
| 738 | lod->setRange(0, 0.0, ivLOD->range[0]); |
|---|
| 739 | for (int i = 1; i < num-1; i++) |
|---|
| 740 | lod->setRange(i, ivLOD->range[i-1], ivLOD->range[i]); |
|---|
| 741 | lod->setRange(num-1, ivLOD->range[num-2], FLT_MAX); |
|---|
| 742 | } |
|---|
| 743 | } |
|---|
| 744 | |
|---|
| 745 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 746 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << |
|---|
| 747 | "Appending osg::LOD with " << num << " children." << std::endl; |
|---|
| 748 | #endif |
|---|
| 749 | |
|---|
| 750 | assert(ivState.keepChildrenOrderParent == node && |
|---|
| 751 | "Current node is not the root of keepChildrenOrder graph."); |
|---|
| 752 | thisPtr->ivPopState(action, node); |
|---|
| 753 | |
|---|
| 754 | return SoCallbackAction::CONTINUE; |
|---|
| 755 | } |
|---|
| 756 | |
|---|
| 757 | return SoCallbackAction::CONTINUE; |
|---|
| 758 | } |
|---|
| 759 | |
|---|
| 760 | SoCallbackAction::Response |
|---|
| 761 | ConvertFromInventor::preShape(void* data, SoCallbackAction* action, |
|---|
| 762 | const SoNode* node) |
|---|
| 763 | { |
|---|
| 764 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 765 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preShape() " |
|---|
| 766 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 767 | #endif |
|---|
| 768 | |
|---|
| 769 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 770 | |
|---|
| 771 | |
|---|
| 772 | static std::map<SoNormalBinding::Binding, osg::Geometry::AttributeBinding> |
|---|
| 773 | normBindingMap; |
|---|
| 774 | static std::map<SoMaterialBinding::Binding, osg::Geometry::AttributeBinding> |
|---|
| 775 | colBindingMap; |
|---|
| 776 | static bool firstTime = true; |
|---|
| 777 | if (firstTime) |
|---|
| 778 | { |
|---|
| 779 | normBindingMap[SoNormalBinding::OVERALL] |
|---|
| 780 | = osg::Geometry::BIND_OVERALL; |
|---|
| 781 | normBindingMap[SoNormalBinding::PER_PART] |
|---|
| 782 | = osg::Geometry::BIND_PER_PRIMITIVE; |
|---|
| 783 | normBindingMap[SoNormalBinding::PER_PART_INDEXED] |
|---|
| 784 | = osg::Geometry::BIND_PER_PRIMITIVE; |
|---|
| 785 | normBindingMap[SoNormalBinding::PER_FACE] |
|---|
| 786 | = osg::Geometry::BIND_PER_PRIMITIVE; |
|---|
| 787 | normBindingMap[SoNormalBinding::PER_FACE_INDEXED] |
|---|
| 788 | = osg::Geometry::BIND_PER_PRIMITIVE; |
|---|
| 789 | normBindingMap[SoNormalBinding::PER_VERTEX] |
|---|
| 790 | = osg::Geometry::BIND_PER_VERTEX; |
|---|
| 791 | normBindingMap[SoNormalBinding::PER_VERTEX_INDEXED] |
|---|
| 792 | = osg::Geometry::BIND_PER_VERTEX; |
|---|
| 793 | |
|---|
| 794 | colBindingMap[SoMaterialBinding::OVERALL] |
|---|
| 795 | = osg::Geometry::BIND_OVERALL; |
|---|
| 796 | colBindingMap[SoMaterialBinding::PER_PART] |
|---|
| 797 | = osg::Geometry::BIND_PER_PRIMITIVE; |
|---|
| 798 | colBindingMap[SoMaterialBinding::PER_PART_INDEXED] |
|---|
| 799 | = osg::Geometry::BIND_PER_PRIMITIVE; |
|---|
| 800 | colBindingMap[SoMaterialBinding::PER_FACE] |
|---|
| 801 | = osg::Geometry::BIND_PER_PRIMITIVE; |
|---|
| 802 | colBindingMap[SoMaterialBinding::PER_FACE_INDEXED] |
|---|
| 803 | = osg::Geometry::BIND_PER_PRIMITIVE; |
|---|
| 804 | colBindingMap[SoMaterialBinding::PER_VERTEX] |
|---|
| 805 | = osg::Geometry::BIND_PER_VERTEX; |
|---|
| 806 | colBindingMap[SoMaterialBinding::PER_VERTEX_INDEXED] |
|---|
| 807 | = osg::Geometry::BIND_PER_VERTEX; |
|---|
| 808 | |
|---|
| 809 | firstTime = false; |
|---|
| 810 | } |
|---|
| 811 | |
|---|
| 812 | |
|---|
| 813 | if (node->isOfType(SoVertexShape::getClassTypeId())) |
|---|
| 814 | { |
|---|
| 815 | thisPtr->normalBinding = normBindingMap[action->getNormalBinding()]; |
|---|
| 816 | thisPtr->colorBinding = colBindingMap[action->getMaterialBinding()]; |
|---|
| 817 | } |
|---|
| 818 | else |
|---|
| 819 | { |
|---|
| 820 | thisPtr->normalBinding = osg::Geometry::BIND_PER_VERTEX; |
|---|
| 821 | thisPtr->colorBinding = osg::Geometry::BIND_PER_VERTEX; |
|---|
| 822 | } |
|---|
| 823 | |
|---|
| 824 | |
|---|
| 825 | if (action->getVertexOrdering() == SoShapeHints::CLOCKWISE) |
|---|
| 826 | thisPtr->vertexOrder = CLOCKWISE; |
|---|
| 827 | else |
|---|
| 828 | thisPtr->vertexOrder = COUNTER_CLOCKWISE; |
|---|
| 829 | |
|---|
| 830 | |
|---|
| 831 | thisPtr->numPrimitives = 0; |
|---|
| 832 | thisPtr->vertices.clear(); |
|---|
| 833 | thisPtr->normals.clear(); |
|---|
| 834 | thisPtr->colors.clear(); |
|---|
| 835 | thisPtr->textureCoords.clear(); |
|---|
| 836 | |
|---|
| 837 | return SoCallbackAction::CONTINUE; |
|---|
| 838 | } |
|---|
| 839 | |
|---|
| 840 | |
|---|
| 841 | |
|---|
| 842 | |
|---|
| 843 | void |
|---|
| 844 | ConvertFromInventor::transposeMatrix(osg::Matrix& mat) |
|---|
| 845 | { |
|---|
| 846 | float tmp; |
|---|
| 847 | for (int j = 0; j < 4; j++) |
|---|
| 848 | { |
|---|
| 849 | for (int i = j + 1; i < 4; i++) |
|---|
| 850 | { |
|---|
| 851 | tmp = mat.operator()(j,i); |
|---|
| 852 | mat.operator()(j,i) = mat.operator()(i,j); |
|---|
| 853 | mat.operator()(i,j) = tmp; |
|---|
| 854 | } |
|---|
| 855 | } |
|---|
| 856 | |
|---|
| 857 | } |
|---|
| 858 | |
|---|
| 859 | SoCallbackAction::Response |
|---|
| 860 | ConvertFromInventor::postShape(void* data, SoCallbackAction* action, |
|---|
| 861 | const SoNode* node) |
|---|
| 862 | { |
|---|
| 863 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 864 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "postShape() " |
|---|
| 865 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 866 | #endif |
|---|
| 867 | |
|---|
| 868 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 869 | |
|---|
| 870 | |
|---|
| 871 | |
|---|
| 872 | osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry; |
|---|
| 873 | |
|---|
| 874 | |
|---|
| 875 | osg::ref_ptr<osg::Vec3Array> coords = new osg::Vec3Array(thisPtr->vertices.size()); |
|---|
| 876 | for (unsigned int i = 0; i < thisPtr->vertices.size(); i++) |
|---|
| 877 | (*coords)[i] = thisPtr->vertices[i]; |
|---|
| 878 | geometry->setVertexArray(coords.get()); |
|---|
| 879 | |
|---|
| 880 | osg::ref_ptr<osg::Vec3Array> norms = NULL; |
|---|
| 881 | if (thisPtr->normalBinding == osg::Geometry::BIND_OVERALL) |
|---|
| 882 | { |
|---|
| 883 | norms = new osg::Vec3Array(1); |
|---|
| 884 | const SbVec3f &norm = action->getNormal(0); |
|---|
| 885 | (*norms)[0].set(norm[0], norm[1], norm[2]); |
|---|
| 886 | } |
|---|
| 887 | else |
|---|
| 888 | { |
|---|
| 889 | norms = new osg::Vec3Array(thisPtr->normals.size()); |
|---|
| 890 | for (unsigned int i = 0; i < thisPtr->normals.size(); i++) |
|---|
| 891 | { |
|---|
| 892 | (*norms)[i] = thisPtr->normals[i]; |
|---|
| 893 | } |
|---|
| 894 | } |
|---|
| 895 | geometry->setNormalArray(norms.get()); |
|---|
| 896 | geometry->setNormalBinding(thisPtr->normalBinding); |
|---|
| 897 | |
|---|
| 898 | |
|---|
| 899 | osg::ref_ptr<osg::Vec4Array> cols; |
|---|
| 900 | if (thisPtr->colorBinding == osg::Geometry::BIND_OVERALL) |
|---|
| 901 | { |
|---|
| 902 | cols = new osg::Vec4Array(1); |
|---|
| 903 | SbColor ambient, diffuse, specular, emission; |
|---|
| 904 | float transparency, shininess; |
|---|
| 905 | action->getMaterial(ambient, diffuse, specular, emission, shininess, |
|---|
| 906 | transparency, 0); |
|---|
| 907 | (*cols)[0].set(diffuse[0], diffuse[1], diffuse[2], 1.0 - transparency); |
|---|
| 908 | } |
|---|
| 909 | else |
|---|
| 910 | { |
|---|
| 911 | cols = new osg::Vec4Array(thisPtr->colors.size()); |
|---|
| 912 | for (unsigned int i = 0; i < thisPtr->colors.size(); i++) |
|---|
| 913 | (*cols)[i] = thisPtr->colors[i]; |
|---|
| 914 | } |
|---|
| 915 | geometry->setColorArray(cols.get()); |
|---|
| 916 | geometry->setColorBinding(thisPtr->colorBinding); |
|---|
| 917 | |
|---|
| 918 | |
|---|
| 919 | if (thisPtr->textureCoords.empty()) |
|---|
| 920 | osg::notify(osg::DEBUG_INFO)<<"tex coords not found"<<std::endl; |
|---|
| 921 | else { |
|---|
| 922 | |
|---|
| 923 | |
|---|
| 924 | if (action->getNumTextureCoordinates()>0) |
|---|
| 925 | osg::notify(osg::DEBUG_INFO)<<"tex coords found"<<std::endl; |
|---|
| 926 | else |
|---|
| 927 | osg::notify(osg::DEBUG_INFO)<<"tex coords generated"<<std::endl; |
|---|
| 928 | |
|---|
| 929 | |
|---|
| 930 | osg::Matrix textureMat; |
|---|
| 931 | textureMat.set((float *) action->getTextureMatrix().getValue()); |
|---|
| 932 | |
|---|
| 933 | |
|---|
| 934 | osg::Matrix identityMat; |
|---|
| 935 | identityMat.makeIdentity(); |
|---|
| 936 | osg::ref_ptr<osg::Vec2Array> texCoords |
|---|
| 937 | = new osg::Vec2Array(thisPtr->textureCoords.size()); |
|---|
| 938 | if (textureMat == identityMat) |
|---|
| 939 | { |
|---|
| 940 | |
|---|
| 941 | for (unsigned int i = 0; i < thisPtr->textureCoords.size(); i++) |
|---|
| 942 | (*texCoords)[i] = thisPtr->textureCoords[i]; |
|---|
| 943 | } |
|---|
| 944 | else |
|---|
| 945 | { |
|---|
| 946 | |
|---|
| 947 | for (unsigned int i = 0; i < thisPtr->textureCoords.size(); i++) |
|---|
| 948 | { |
|---|
| 949 | osg::Vec3 transVec = textureMat.preMult( |
|---|
| 950 | osg::Vec3(thisPtr->textureCoords[i][0], |
|---|
| 951 | thisPtr->textureCoords[i][1], |
|---|
| 952 | 0.0)); |
|---|
| 953 | (*texCoords)[i].set(transVec.x(), transVec.y()); |
|---|
| 954 | } |
|---|
| 955 | } |
|---|
| 956 | |
|---|
| 957 | geometry->setTexCoordArray(0, texCoords.get()); |
|---|
| 958 | } |
|---|
| 959 | |
|---|
| 960 | |
|---|
| 961 | |
|---|
| 962 | geometry->addPrimitiveSet(new osg::DrawArrays(thisPtr->primitiveType,0, |
|---|
| 963 | coords->size())); |
|---|
| 964 | |
|---|
| 965 | osg::ref_ptr<osg::StateSet> stateSet = thisPtr->getStateSet(action); |
|---|
| 966 | geometry->setStateSet(stateSet.get()); |
|---|
| 967 | |
|---|
| 968 | |
|---|
| 969 | osg::ref_ptr<osg::Geode> geode = new osg::Geode; |
|---|
| 970 | geode->addDrawable(geometry.get()); |
|---|
| 971 | |
|---|
| 972 | |
|---|
| 973 | std::string name = stateSet->getName(); |
|---|
| 974 | if (name != "") { |
|---|
| 975 | geode->setName(name); |
|---|
| 976 | } |
|---|
| 977 | |
|---|
| 978 | |
|---|
| 979 | thisPtr->appendNode(geode.get(), action); |
|---|
| 980 | |
|---|
| 981 | return SoCallbackAction::CONTINUE; |
|---|
| 982 | } |
|---|
| 983 | |
|---|
| 984 | SoCallbackAction::Response |
|---|
| 985 | ConvertFromInventor::postTexture(void* data, SoCallbackAction *, |
|---|
| 986 | const SoNode* node) |
|---|
| 987 | { |
|---|
| 988 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 989 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "postTexture() " |
|---|
| 990 | << node->getTypeId().getName().getString(); |
|---|
| 991 | if (node->isOfType(SoTexture2::getClassTypeId())) { |
|---|
| 992 | SoTexture2 *t = (SoTexture2*)node; |
|---|
| 993 | if (t->filename.getValue().getLength()) |
|---|
| 994 | osg::notify(osg::DEBUG_INFO) << " " << t->filename.getValue().getString(); |
|---|
| 995 | } |
|---|
| 996 | osg::notify(osg::DEBUG_INFO) << std::endl; |
|---|
| 997 | #endif |
|---|
| 998 | |
|---|
| 999 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1000 | bool texturingEnabled = false; |
|---|
| 1001 | |
|---|
| 1002 | |
|---|
| 1003 | if (node->isOfType(SoTexture2::getClassTypeId())) { |
|---|
| 1004 | |
|---|
| 1005 | |
|---|
| 1006 | SoTexture2 *t = (SoTexture2*)node; |
|---|
| 1007 | SbVec2s size; |
|---|
| 1008 | int nc; |
|---|
| 1009 | const unsigned char *data = t->image.getValue(size, nc); |
|---|
| 1010 | texturingEnabled = t->filename.getValue().getLength() || |
|---|
| 1011 | (data && size != SbVec2s(0,0)); |
|---|
| 1012 | } |
|---|
| 1013 | |
|---|
| 1014 | #ifdef __COIN__ |
|---|
| 1015 | |
|---|
| 1016 | |
|---|
| 1017 | if (node->isOfType(SoVRMLImageTexture::getClassTypeId())) { |
|---|
| 1018 | |
|---|
| 1019 | |
|---|
| 1020 | SoVRMLImageTexture *t = (SoVRMLImageTexture*)node; |
|---|
| 1021 | texturingEnabled = t->url.getNum() > 1 || (t->url.getNum() == 1 && t->url[0].getLength() > 0); |
|---|
| 1022 | } |
|---|
| 1023 | |
|---|
| 1024 | |
|---|
| 1025 | if (node->isOfType(SoVRMLAppearance::getClassTypeId())) { |
|---|
| 1026 | |
|---|
| 1027 | |
|---|
| 1028 | |
|---|
| 1029 | |
|---|
| 1030 | |
|---|
| 1031 | |
|---|
| 1032 | SoVRMLAppearance *a = (SoVRMLAppearance*)node; |
|---|
| 1033 | if (a->texture.getValue() == NULL) |
|---|
| 1034 | thisPtr->ivStateStack.top().currentTexture = NULL; |
|---|
| 1035 | |
|---|
| 1036 | |
|---|
| 1037 | |
|---|
| 1038 | |
|---|
| 1039 | |
|---|
| 1040 | return SoCallbackAction::CONTINUE; |
|---|
| 1041 | } |
|---|
| 1042 | |
|---|
| 1043 | #endif |
|---|
| 1044 | |
|---|
| 1045 | |
|---|
| 1046 | if (texturingEnabled) |
|---|
| 1047 | thisPtr->ivStateStack.top().currentTexture = node; |
|---|
| 1048 | else |
|---|
| 1049 | thisPtr->ivStateStack.top().currentTexture = NULL; |
|---|
| 1050 | |
|---|
| 1051 | return SoCallbackAction::CONTINUE; |
|---|
| 1052 | } |
|---|
| 1053 | |
|---|
| 1054 | void ConvertFromInventor::transformLight(SoCallbackAction* action, |
|---|
| 1055 | const SbVec3f& vec, |
|---|
| 1056 | osg::Vec3& transVec) |
|---|
| 1057 | { |
|---|
| 1058 | osg::Matrix modelMat; |
|---|
| 1059 | modelMat.set((float *)action->getModelMatrix().getValue()); |
|---|
| 1060 | |
|---|
| 1061 | transVec.set(vec[0], vec[1], vec[2]); |
|---|
| 1062 | transVec = modelMat.preMult(transVec); |
|---|
| 1063 | } |
|---|
| 1064 | |
|---|
| 1065 | SoCallbackAction::Response |
|---|
| 1066 | ConvertFromInventor::preLight(void* data, SoCallbackAction* action, |
|---|
| 1067 | const SoNode* node) |
|---|
| 1068 | { |
|---|
| 1069 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1070 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preLight() " |
|---|
| 1071 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 1072 | #endif |
|---|
| 1073 | |
|---|
| 1074 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1075 | |
|---|
| 1076 | |
|---|
| 1077 | const SoLight* ivLight = (const SoLight*) node; |
|---|
| 1078 | if (!ivLight->on.getValue()) |
|---|
| 1079 | return SoCallbackAction::CONTINUE; |
|---|
| 1080 | |
|---|
| 1081 | |
|---|
| 1082 | IvStateItem &ivState = thisPtr->ivStateStack.top(); |
|---|
| 1083 | osg::ref_ptr<osg::Light> osgLight = new osg::Light; |
|---|
| 1084 | |
|---|
| 1085 | |
|---|
| 1086 | const char* name = ivLight->getName().getString(); |
|---|
| 1087 | osgLight->setName(name); |
|---|
| 1088 | |
|---|
| 1089 | |
|---|
| 1090 | SbVec3f lightColor = ivLight->color.getValue(); |
|---|
| 1091 | float intensity = ivLight->intensity.getValue(); |
|---|
| 1092 | |
|---|
| 1093 | |
|---|
| 1094 | osgLight->setAmbient(osg::Vec4(0.f, 0.f, 0.f, 1.f)); |
|---|
| 1095 | osgLight->setDiffuse(osg::Vec4(lightColor[0] * intensity, |
|---|
| 1096 | lightColor[1] * intensity, |
|---|
| 1097 | lightColor[2] * intensity, 1)); |
|---|
| 1098 | osgLight->setSpecular(osg::Vec4(lightColor[0] * intensity, |
|---|
| 1099 | lightColor[1] * intensity, |
|---|
| 1100 | lightColor[2] * intensity, 1)); |
|---|
| 1101 | |
|---|
| 1102 | |
|---|
| 1103 | if (node->isOfType(SoDirectionalLight::getClassTypeId())) |
|---|
| 1104 | { |
|---|
| 1105 | SoDirectionalLight *dirLight = (SoDirectionalLight *) node; |
|---|
| 1106 | |
|---|
| 1107 | #if 1 // Let's place the light to its place in scene graph instead of |
|---|
| 1108 | |
|---|
| 1109 | SbVec3f l(dirLight->direction.getValue()); |
|---|
| 1110 | osgLight->setPosition(osg::Vec4(l[0], l[1], l[2] , 0.)); |
|---|
| 1111 | #else |
|---|
| 1112 | osg::Vec3 transVec; |
|---|
| 1113 | thisPtr->transformLight(action, dirLight->direction.getValue(), transVec); |
|---|
| 1114 | osgLight->setPosition(osg::Vec4(transVec.x(), transVec.y(), |
|---|
| 1115 | transVec.z(), 0.)); |
|---|
| 1116 | #endif |
|---|
| 1117 | } |
|---|
| 1118 | else if (node->isOfType(SoPointLight::getClassTypeId())) |
|---|
| 1119 | { |
|---|
| 1120 | SoPointLight* ptLight = (SoPointLight *) node; |
|---|
| 1121 | |
|---|
| 1122 | #if 1 // Let's place the light to its place in scene graph instead of |
|---|
| 1123 | |
|---|
| 1124 | SbVec3f l(ptLight->location.getValue()); |
|---|
| 1125 | osgLight->setPosition(osg::Vec4(l[0], l[1], l[2] , 1.)); |
|---|
| 1126 | #else |
|---|
| 1127 | osg::Vec3 transVec; |
|---|
| 1128 | thisPtr->transformLight(action, ptLight->location.getValue(), transVec); |
|---|
| 1129 | osgLight->setPosition(osg::Vec4(transVec.x(), transVec.y(), |
|---|
| 1130 | transVec.z(), 1.)); |
|---|
| 1131 | #endif |
|---|
| 1132 | } |
|---|
| 1133 | else if (node->isOfType(SoSpotLight::getClassTypeId())) |
|---|
| 1134 | { |
|---|
| 1135 | SoSpotLight* spotLight = (SoSpotLight *) node; |
|---|
| 1136 | |
|---|
| 1137 | osgLight->setSpotExponent(spotLight->dropOffRate.getValue() * 128.0); |
|---|
| 1138 | osgLight->setSpotCutoff(spotLight->cutOffAngle.getValue()*180.0/osg::PI); |
|---|
| 1139 | |
|---|
| 1140 | #if 1 // Let's place the light to its place in scene graph instead of |
|---|
| 1141 | |
|---|
| 1142 | SbVec3f l(spotLight->location.getValue()); |
|---|
| 1143 | osgLight->setPosition(osg::Vec4(l[0], l[1], l[2] , 1.)); |
|---|
| 1144 | l = spotLight->direction.getValue(); |
|---|
| 1145 | osgLight->setDirection(osg::Vec3(l[0], l[1], l[2])); |
|---|
| 1146 | #else |
|---|
| 1147 | osg::Vec3 transVec; |
|---|
| 1148 | thisPtr->transformLight(action, spotLight->location.getValue(), transVec); |
|---|
| 1149 | osgLight->setPosition(osg::Vec4(transVec.x(), transVec.y(), |
|---|
| 1150 | transVec.z(), 1.)); |
|---|
| 1151 | |
|---|
| 1152 | thisPtr->transformLight(action, spotLight->direction.getValue(),transVec); |
|---|
| 1153 | osgLight->setDirection(osg::Vec3(transVec.x(), transVec.y(), |
|---|
| 1154 | transVec.z())); |
|---|
| 1155 | #endif |
|---|
| 1156 | } |
|---|
| 1157 | |
|---|
| 1158 | |
|---|
| 1159 | if (!node->isOfType(SoDirectionalLight::getClassTypeId())) { |
|---|
| 1160 | SbVec3f att = action->getLightAttenuation(); |
|---|
| 1161 | osgLight->setConstantAttenuation(att[2]); |
|---|
| 1162 | osgLight->setLinearAttenuation(att[1]); |
|---|
| 1163 | osgLight->setQuadraticAttenuation(att[0]); |
|---|
| 1164 | } else { |
|---|
| 1165 | |
|---|
| 1166 | |
|---|
| 1167 | } |
|---|
| 1168 | |
|---|
| 1169 | |
|---|
| 1170 | osgLight->setLightNum(ivState.currentLights.size()); |
|---|
| 1171 | ivState.currentLights.push_back(osgLight); |
|---|
| 1172 | |
|---|
| 1173 | osg::ref_ptr<osg::LightSource> ls = new osg::LightSource(); |
|---|
| 1174 | ls->setLight(osgLight.get()); |
|---|
| 1175 | ls->setName(ivLight->getName().getString()); |
|---|
| 1176 | #if 1 // Let's place the light to its place in scene graph instead of |
|---|
| 1177 | |
|---|
| 1178 | thisPtr->ivPushState(action, node, |
|---|
| 1179 | IvStateItem::MULTI_POP | IvStateItem::UPDATE_STATE | |
|---|
| 1180 | IvStateItem::APPEND_AT_PUSH, ls.get()); |
|---|
| 1181 | #else |
|---|
| 1182 | if (!(thisPtr->lightGroup.get())) |
|---|
| 1183 | thisPtr->lightGroup = new osg::Group(); |
|---|
| 1184 | thisPtr->lightGroup->addChild(ls); |
|---|
| 1185 | #endif |
|---|
| 1186 | |
|---|
| 1187 | return SoCallbackAction::CONTINUE; |
|---|
| 1188 | } |
|---|
| 1189 | |
|---|
| 1190 | SoCallbackAction::Response |
|---|
| 1191 | ConvertFromInventor::preEnvironment(void* data, SoCallbackAction* action, |
|---|
| 1192 | const SoNode* node) |
|---|
| 1193 | { |
|---|
| 1194 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1195 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preLight() " |
|---|
| 1196 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 1197 | #endif |
|---|
| 1198 | |
|---|
| 1199 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1200 | IvStateItem &ivState = thisPtr->ivStateStack.top(); |
|---|
| 1201 | |
|---|
| 1202 | ivState.currentAmbientLight = ((SoEnvironment*)node)->ambientColor.getValue() * |
|---|
| 1203 | ((SoEnvironment*)node)->ambientIntensity.getValue(); |
|---|
| 1204 | |
|---|
| 1205 | return SoCallbackAction::CONTINUE; |
|---|
| 1206 | } |
|---|
| 1207 | |
|---|
| 1208 | #ifdef INVENTOR_SHADERS_AVAILABLE |
|---|
| 1209 | static bool |
|---|
| 1210 | convertShader(osg::Shader::Type osgShaderType, |
|---|
| 1211 | const SoShaderObject *ivShader, |
|---|
| 1212 | osg::Program *osgProgram) |
|---|
| 1213 | { |
|---|
| 1214 | |
|---|
| 1215 | if (ivShader == NULL) |
|---|
| 1216 | return true; |
|---|
| 1217 | |
|---|
| 1218 | |
|---|
| 1219 | osg::ref_ptr<osg::Shader> osgShader = new osg::Shader(osgShaderType); |
|---|
| 1220 | if (ivShader->sourceType.getValue() == SoShaderObject::FILENAME) |
|---|
| 1221 | osgShader->loadShaderSourceFromFile(ivShader->sourceProgram.getValue().getString()); |
|---|
| 1222 | else |
|---|
| 1223 | if (ivShader->sourceType.getValue() == SoShaderObject::GLSL_PROGRAM) |
|---|
| 1224 | osgShader->setShaderSource(ivShader->sourceProgram.getValue().getString()); |
|---|
| 1225 | else { |
|---|
| 1226 | osg::notify(osg::WARN) << NOTIFY_HEADER << "Can not convert " |
|---|
| 1227 | << "shader. Unsupported shader language." << std::endl; |
|---|
| 1228 | return false; |
|---|
| 1229 | } |
|---|
| 1230 | |
|---|
| 1231 | return osgProgram->addShader(osgShader); |
|---|
| 1232 | } |
|---|
| 1233 | #endif // INVENTOR_SHADERS_AVAILABLE |
|---|
| 1234 | |
|---|
| 1235 | SoCallbackAction::Response |
|---|
| 1236 | ConvertFromInventor::preShaderProgram(void* data, SoCallbackAction* action, |
|---|
| 1237 | const SoNode* node) |
|---|
| 1238 | { |
|---|
| 1239 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1240 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preShaderProgram() " |
|---|
| 1241 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 1242 | #endif |
|---|
| 1243 | |
|---|
| 1244 | #ifdef INVENTOR_SHADERS_AVAILABLE |
|---|
| 1245 | |
|---|
| 1246 | ConvertFromInventor *thisPtr = (ConvertFromInventor*)data; |
|---|
| 1247 | IvStateItem &ivState = thisPtr->ivStateStack.top(); |
|---|
| 1248 | |
|---|
| 1249 | |
|---|
| 1250 | |
|---|
| 1251 | |
|---|
| 1252 | const SoShaderProgram *ivProgram = (const SoShaderProgram*)node; |
|---|
| 1253 | const SoVertexShader *ivVertexShader = NULL; |
|---|
| 1254 | const SoGeometryShader *ivGeometryShader = NULL; |
|---|
| 1255 | const SoFragmentShader *ivFragmentShader = NULL; |
|---|
| 1256 | |
|---|
| 1257 | for (int i=0, c=ivProgram->shaderObject.getNum(); i<c; i++) { |
|---|
| 1258 | |
|---|
| 1259 | const SoShaderObject *shader = (const SoShaderObject*)ivProgram->shaderObject[i]; |
|---|
| 1260 | if (!shader->isOfType(SoShaderObject::getClassTypeId())) |
|---|
| 1261 | continue; |
|---|
| 1262 | if (shader->isActive.getValue() == FALSE) |
|---|
| 1263 | continue; |
|---|
| 1264 | |
|---|
| 1265 | if (shader->isOfType(SoVertexShader::getClassTypeId())) |
|---|
| 1266 | ivVertexShader = (const SoVertexShader*)shader; |
|---|
| 1267 | if (shader->isOfType(SoGeometryShader::getClassTypeId())) |
|---|
| 1268 | ivGeometryShader = (const SoGeometryShader*)shader; |
|---|
| 1269 | if (shader->isOfType(SoFragmentShader::getClassTypeId())) |
|---|
| 1270 | ivFragmentShader = (const SoFragmentShader*)shader; |
|---|
| 1271 | } |
|---|
| 1272 | |
|---|
| 1273 | |
|---|
| 1274 | osg::Program *osgProgram = new osg::Program(); |
|---|
| 1275 | if (!convertShader(osg::Shader::VERTEX, ivVertexShader, osgProgram)) |
|---|
| 1276 | osg::notify(osg::WARN) << NOTIFY_HEADER |
|---|
| 1277 | << "Failed to add vertex shader." << std::endl; |
|---|
| 1278 | if (!convertShader(osg::Shader::GEOMETRY, ivGeometryShader, osgProgram)) |
|---|
| 1279 | osg::notify(osg::WARN) << NOTIFY_HEADER |
|---|
| 1280 | << "Failed to add geometry shader." << std::endl; |
|---|
| 1281 | if (!convertShader(osg::Shader::FRAGMENT, ivFragmentShader, osgProgram)) |
|---|
| 1282 | osg::notify(osg::WARN) << NOTIFY_HEADER |
|---|
| 1283 | << "Failed to add fragment shader." << std::endl; |
|---|
| 1284 | |
|---|
| 1285 | |
|---|
| 1286 | ivState.currentGLProgram = osgProgram; |
|---|
| 1287 | |
|---|
| 1288 | #else |
|---|
| 1289 | |
|---|
| 1290 | osg::notify(osg::WARN) << NOTIFY_HEADER << "Warning: The model " |
|---|
| 1291 | "contains shaders while your Inventor does not support " |
|---|
| 1292 | "them." << std::endl; |
|---|
| 1293 | #endif |
|---|
| 1294 | |
|---|
| 1295 | return SoCallbackAction::CONTINUE; |
|---|
| 1296 | } |
|---|
| 1297 | |
|---|
| 1298 | osg::ref_ptr<osg::StateSet> |
|---|
| 1299 | ConvertFromInventor::getStateSet(SoCallbackAction* action) |
|---|
| 1300 | { |
|---|
| 1301 | osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet; |
|---|
| 1302 | |
|---|
| 1303 | |
|---|
| 1304 | stateSet->clear(); |
|---|
| 1305 | |
|---|
| 1306 | |
|---|
| 1307 | IvStateItem &ivState = ivStateStack.top(); |
|---|
| 1308 | |
|---|
| 1309 | |
|---|
| 1310 | osg::ref_ptr<osg::Texture2D> texture; |
|---|
| 1311 | const SoNode *ivTexture = ivState.currentTexture; |
|---|
| 1312 | if (ivTexture) |
|---|
| 1313 | { |
|---|
| 1314 | |
|---|
| 1315 | if (ivToOsgTexMap[ivTexture]) |
|---|
| 1316 | texture = ivToOsgTexMap[ivTexture]; |
|---|
| 1317 | else |
|---|
| 1318 | { |
|---|
| 1319 | |
|---|
| 1320 | texture = convertIVTexToOSGTex(ivTexture, action); |
|---|
| 1321 | |
|---|
| 1322 | |
|---|
| 1323 | ivToOsgTexMap[ivTexture] = texture.get(); |
|---|
| 1324 | } |
|---|
| 1325 | |
|---|
| 1326 | stateSet->setTextureAttributeAndModes(0, texture.get(), osg::StateAttribute::ON); |
|---|
| 1327 | |
|---|
| 1328 | |
|---|
| 1329 | if(texture.valid()) |
|---|
| 1330 | { |
|---|
| 1331 | std::string name = texture->getName(); |
|---|
| 1332 | if (name != "") |
|---|
| 1333 | stateSet->setName(name); |
|---|
| 1334 | } |
|---|
| 1335 | |
|---|
| 1336 | osg::ref_ptr<osg::TexEnv> texEnv = new osg::TexEnv; |
|---|
| 1337 | switch (action->getTextureModel()) |
|---|
| 1338 | { |
|---|
| 1339 | case SoTexture2::MODULATE: |
|---|
| 1340 | texEnv->setMode(osg::TexEnv::MODULATE); |
|---|
| 1341 | break; |
|---|
| 1342 | case SoTexture2::DECAL: |
|---|
| 1343 | texEnv->setMode(osg::TexEnv::DECAL); |
|---|
| 1344 | break; |
|---|
| 1345 | case SoTexture2::BLEND: { |
|---|
| 1346 | texEnv->setMode(osg::TexEnv::BLEND); |
|---|
| 1347 | SbColor c(action->getTextureBlendColor()); |
|---|
| 1348 | texEnv->setColor(osg::Vec4(c[0], c[1], c[2], 1.f)); |
|---|
| 1349 | break; |
|---|
| 1350 | } |
|---|
| 1351 | |
|---|
| 1352 | |
|---|
| 1353 | |
|---|
| 1354 | case 0x1E01: |
|---|
| 1355 | texEnv->setMode(osg::TexEnv::REPLACE); |
|---|
| 1356 | break; |
|---|
| 1357 | default: |
|---|
| 1358 | osg::notify(osg::WARN) << "Unsupported TexEnv mode." << std::endl; |
|---|
| 1359 | break; |
|---|
| 1360 | |
|---|
| 1361 | } |
|---|
| 1362 | stateSet->setTextureAttributeAndModes(0,texEnv.get(),osg::StateAttribute::ON); |
|---|
| 1363 | } |
|---|
| 1364 | |
|---|
| 1365 | SbColor ambient, diffuse, specular, emission; |
|---|
| 1366 | float shininess, transparency; |
|---|
| 1367 | |
|---|
| 1368 | |
|---|
| 1369 | action->getMaterial(ambient, diffuse, specular, emission, |
|---|
| 1370 | shininess, transparency, 0); |
|---|
| 1371 | |
|---|
| 1372 | |
|---|
| 1373 | SbBool hasTextureTransparency = FALSE; |
|---|
| 1374 | if (ivTexture) { |
|---|
| 1375 | SbVec2s tmp; |
|---|
| 1376 | int bpp = 0; |
|---|
| 1377 | if (ivTexture->isOfType(SoTexture2::getClassTypeId())) |
|---|
| 1378 | ((SoTexture2*)ivTexture)->image.getValue(tmp, bpp); |
|---|
| 1379 | #ifdef __COIN__ |
|---|
| 1380 | else |
|---|
| 1381 | if (ivTexture->isOfType(SoVRMLImageTexture::getClassTypeId())) { |
|---|
| 1382 | const SbImage *img = ((SoVRMLImageTexture*)ivTexture)->getImage(); |
|---|
| 1383 | if (img) img->getValue(tmp, bpp); |
|---|
| 1384 | } |
|---|
| 1385 | #endif |
|---|
| 1386 | hasTextureTransparency = bpp==4 || bpp==2; |
|---|
| 1387 | } |
|---|
| 1388 | |
|---|
| 1389 | if (transparency > 0 || hasTextureTransparency) |
|---|
| 1390 | { |
|---|
| 1391 | osg::ref_ptr<osg::BlendFunc> transparency = new osg::BlendFunc; |
|---|
| 1392 | stateSet->setAttributeAndModes(transparency.get(), |
|---|
| 1393 | osg::StateAttribute::ON); |
|---|
| 1394 | |
|---|
| 1395 | |
|---|
| 1396 | stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); |
|---|
| 1397 | } |
|---|
| 1398 | |
|---|
| 1399 | |
|---|
| 1400 | if (action->getLineWidth()) |
|---|
| 1401 | { |
|---|
| 1402 | osg::ref_ptr<osg::LineWidth> lineWidth = new osg::LineWidth; |
|---|
| 1403 | lineWidth->setWidth(action->getLineWidth()); |
|---|
| 1404 | stateSet->setAttributeAndModes(lineWidth.get(), osg::StateAttribute::ON); |
|---|
| 1405 | } |
|---|
| 1406 | |
|---|
| 1407 | |
|---|
| 1408 | if (action->getPointSize()) |
|---|
| 1409 | { |
|---|
| 1410 | osg::ref_ptr<osg::Point> point = new osg::Point; |
|---|
| 1411 | point->setSize(action->getPointSize()); |
|---|
| 1412 | stateSet->setAttributeAndModes(point.get(), osg::StateAttribute::ON); |
|---|
| 1413 | } |
|---|
| 1414 | |
|---|
| 1415 | |
|---|
| 1416 | switch (action->getDrawStyle()) |
|---|
| 1417 | { |
|---|
| 1418 | case SoDrawStyle::FILLED: |
|---|
| 1419 | { |
|---|
| 1420 | #if 0 |
|---|
| 1421 | |
|---|
| 1422 | osg::PolygonMode *polygonMode = new osg::PolygonMode; |
|---|
| 1423 | polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK, |
|---|
| 1424 | osg::PolygonMode::FILL); |
|---|
| 1425 | stateSet->setAttributeAndModes(polygonMode, osg::StateAttribute::ON); |
|---|
| 1426 | #endif |
|---|
| 1427 | break; |
|---|
| 1428 | } |
|---|
| 1429 | case SoDrawStyle::LINES: |
|---|
| 1430 | { |
|---|
| 1431 | osg::ref_ptr<osg::PolygonMode> polygonMode = new osg::PolygonMode; |
|---|
| 1432 | polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK, |
|---|
| 1433 | osg::PolygonMode::LINE); |
|---|
| 1434 | stateSet->setAttributeAndModes(polygonMode.get(), osg::StateAttribute::ON); |
|---|
| 1435 | break; |
|---|
| 1436 | } |
|---|
| 1437 | case SoDrawStyle::POINTS: |
|---|
| 1438 | { |
|---|
| 1439 | osg::ref_ptr<osg::PolygonMode> polygonMode = new osg::PolygonMode; |
|---|
| 1440 | polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK, |
|---|
| 1441 | osg::PolygonMode::POINT); |
|---|
| 1442 | stateSet->setAttributeAndModes(polygonMode.get(), osg::StateAttribute::ON); |
|---|
| 1443 | break; |
|---|
| 1444 | } |
|---|
| 1445 | case SoDrawStyle::INVISIBLE: |
|---|
| 1446 | |
|---|
| 1447 | break; |
|---|
| 1448 | } |
|---|
| 1449 | |
|---|
| 1450 | |
|---|
| 1451 | if (action->getShapeType() == SoShapeHints::SOLID) |
|---|
| 1452 | { |
|---|
| 1453 | osg::ref_ptr<osg::CullFace> cullFace = new osg::CullFace; |
|---|
| 1454 | cullFace->setMode(osg::CullFace::BACK); |
|---|
| 1455 | stateSet->setAttributeAndModes(cullFace.get(), osg::StateAttribute::ON); |
|---|
| 1456 | } |
|---|
| 1457 | |
|---|
| 1458 | |
|---|
| 1459 | if (action->getLightModel() == SoLightModel::BASE_COLOR) |
|---|
| 1460 | stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
|---|
| 1461 | else |
|---|
| 1462 | { |
|---|
| 1463 | |
|---|
| 1464 | osg::ref_ptr<osg::Material> material = new osg::Material; |
|---|
| 1465 | |
|---|
| 1466 | material->setAmbient(osg::Material::FRONT_AND_BACK, |
|---|
| 1467 | osg::Vec4(ambient[0], ambient[1], ambient[2], |
|---|
| 1468 | 1.0 - transparency)); |
|---|
| 1469 | material->setDiffuse(osg::Material::FRONT_AND_BACK, |
|---|
| 1470 | osg::Vec4(diffuse[0], diffuse[1], diffuse[2], |
|---|
| 1471 | 1.0 - transparency)); |
|---|
| 1472 | material->setSpecular(osg::Material::FRONT_AND_BACK, |
|---|
| 1473 | osg::Vec4(specular[0], specular[1], specular[2], |
|---|
| 1474 | 1.0 - transparency)); |
|---|
| 1475 | material->setEmission(osg::Material::FRONT_AND_BACK, |
|---|
| 1476 | osg::Vec4(emission[0], emission[1], emission[2], |
|---|
| 1477 | 1.0 - transparency)); |
|---|
| 1478 | material->setTransparency(osg::Material::FRONT_AND_BACK, transparency); |
|---|
| 1479 | if (specular[0] || specular[1] || specular[2]) |
|---|
| 1480 | material->setShininess(osg::Material::FRONT_AND_BACK, |
|---|
| 1481 | shininess*128.0); |
|---|
| 1482 | else |
|---|
| 1483 | material->setShininess(osg::Material::FRONT_AND_BACK, 0.0); |
|---|
| 1484 | |
|---|
| 1485 | material->setColorMode(osg::Material::DIFFUSE); |
|---|
| 1486 | |
|---|
| 1487 | stateSet->setAttributeAndModes(material.get(), osg::StateAttribute::ON); |
|---|
| 1488 | stateSet->setName(appearanceName.getString()); |
|---|
| 1489 | stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON); |
|---|
| 1490 | |
|---|
| 1491 | |
|---|
| 1492 | |
|---|
| 1493 | |
|---|
| 1494 | osg::LightModel *lightModel = new osg::LightModel(); |
|---|
| 1495 | const SbColor &c = ivState.currentAmbientLight; |
|---|
| 1496 | lightModel->setAmbientIntensity(osg::Vec4(c[0], c[1], c[2], 1.0)); |
|---|
| 1497 | #if 0 |
|---|
| 1498 | |
|---|
| 1499 | update: The mentioned bug is probably just for very old NVidia drivers (commit time of the comment is 2005-03-18). |
|---|
| 1500 | The proper solution should be to set two sided lighting based on SoShapeHints node. Need to be developed. PCJohn-2010-01-20 |
|---|
| 1501 | |
|---|
| 1502 | lightModel->setTwoSided(true); |
|---|
| 1503 | #endif |
|---|
| 1504 | stateSet->setAttributeAndModes(lightModel, osg::StateAttribute::ON); |
|---|
| 1505 | |
|---|
| 1506 | |
|---|
| 1507 | for (unsigned int i = 0; i < ivState.currentLights.size(); i++) |
|---|
| 1508 | stateSet->setAttributeAndModes(ivState.currentLights[i].get(), |
|---|
| 1509 | osg::StateAttribute::ON); |
|---|
| 1510 | |
|---|
| 1511 | } |
|---|
| 1512 | |
|---|
| 1513 | |
|---|
| 1514 | if (ivState.currentGLProgram.get() != NULL) { |
|---|
| 1515 | stateSet->setAttributeAndModes(ivState.currentGLProgram.get(), |
|---|
| 1516 | osg::StateAttribute::ON); |
|---|
| 1517 | } |
|---|
| 1518 | |
|---|
| 1519 | |
|---|
| 1520 | if (ivState.currentGLProgram.get() != NULL) { |
|---|
| 1521 | for (int i=0, c=ivState.currentGLProgram->getNumShaders(); i<c; i++) { |
|---|
| 1522 | const std::string &shaderCode = ivState.currentGLProgram->getShader(i)->getShaderSource(); |
|---|
| 1523 | if (shaderCode.find("coin_texunit0_model") != std::string::npos) { |
|---|
| 1524 | int mode = (ivTexture!=NULL) ? action->getTextureModel() : 0; |
|---|
| 1525 | stateSet->addUniform(new osg::Uniform("coin_texunit0_model", mode)); |
|---|
| 1526 | break; |
|---|
| 1527 | } |
|---|
| 1528 | } |
|---|
| 1529 | } |
|---|
| 1530 | |
|---|
| 1531 | return stateSet; |
|---|
| 1532 | } |
|---|
| 1533 | |
|---|
| 1534 | osg::Texture2D* |
|---|
| 1535 | ConvertFromInventor::convertIVTexToOSGTex(const SoNode* soNode, |
|---|
| 1536 | SoCallbackAction* action) |
|---|
| 1537 | { |
|---|
| 1538 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1539 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER |
|---|
| 1540 | << "convertIVTexToOSGTex (" |
|---|
| 1541 | << soNode->getTypeId().getName().getString() |
|---|
| 1542 | << ")" << std::endl; |
|---|
| 1543 | #endif |
|---|
| 1544 | |
|---|
| 1545 | SbVec2s soSize; |
|---|
| 1546 | int soNC; |
|---|
| 1547 | |
|---|
| 1548 | |
|---|
| 1549 | const unsigned char* soImageData = action->getTextureImage(soSize, soNC); |
|---|
| 1550 | if (!soImageData) { |
|---|
| 1551 | osg::notify(osg::WARN) << NOTIFY_HEADER |
|---|
| 1552 | << "Warning: Error while loading texture data." << std::endl; |
|---|
| 1553 | return NULL; |
|---|
| 1554 | } |
|---|
| 1555 | |
|---|
| 1556 | |
|---|
| 1557 | unsigned char* osgImageData = new unsigned char[soSize[0] * soSize[1] * soNC]; |
|---|
| 1558 | |
|---|
| 1559 | |
|---|
| 1560 | memcpy(osgImageData, soImageData, soSize[0] * soSize[1] * soNC); |
|---|
| 1561 | |
|---|
| 1562 | |
|---|
| 1563 | std::string name = soNode->getName().getString(); |
|---|
| 1564 | |
|---|
| 1565 | |
|---|
| 1566 | std::string fileName; |
|---|
| 1567 | if (soNode->isOfType(SoTexture2::getClassTypeId())) |
|---|
| 1568 | fileName = ((SoTexture2*)soNode)->filename.getValue().getString(); |
|---|
| 1569 | #ifdef __COIN__ |
|---|
| 1570 | else |
|---|
| 1571 | if (soNode->isOfType(SoVRMLImageTexture::getClassTypeId())) |
|---|
| 1572 | fileName = ((SoVRMLImageTexture*)soNode)->url.getNum() >= 1 ? |
|---|
| 1573 | ((SoVRMLImageTexture*)soNode)->url.getValues(0)[0].getString() : ""; |
|---|
| 1574 | #endif |
|---|
| 1575 | else |
|---|
| 1576 | osg::notify(osg::WARN) << NOTIFY_HEADER |
|---|
| 1577 | << " Warning: Unsupported texture type: " |
|---|
| 1578 | << soNode->getTypeId().getName().getString() << std::endl; |
|---|
| 1579 | |
|---|
| 1580 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1581 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER |
|---|
| 1582 | << " Converting file name: " << fileName << " -> "; |
|---|
| 1583 | #endif |
|---|
| 1584 | if (fileName[0]=='\"') fileName.erase(fileName.begin()); |
|---|
| 1585 | if (fileName.size() > 0 && fileName[fileName.size()-1]=='\"') |
|---|
| 1586 | fileName.erase(fileName.begin()+fileName.size()-1); |
|---|
| 1587 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1588 | osg::notify(osg::DEBUG_INFO) << fileName << std::endl; |
|---|
| 1589 | #endif |
|---|
| 1590 | |
|---|
| 1591 | |
|---|
| 1592 | osg::ref_ptr<osg::Image> osgImage = new osg::Image; |
|---|
| 1593 | osgImage->setFileName(fileName); |
|---|
| 1594 | GLenum formats[] = {GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA}; |
|---|
| 1595 | osgImage->setImage(soSize[0], soSize[1], 0, soNC, formats[soNC-1], |
|---|
| 1596 | GL_UNSIGNED_BYTE, osgImageData, osg::Image::USE_NEW_DELETE); |
|---|
| 1597 | |
|---|
| 1598 | |
|---|
| 1599 | osg::Texture2D *osgTex = new osg::Texture2D; |
|---|
| 1600 | osgTex->setImage(osgImage.get()); |
|---|
| 1601 | if (name != "") { |
|---|
| 1602 | osgTex->setName(name); |
|---|
| 1603 | } |
|---|
| 1604 | |
|---|
| 1605 | static std::map<SoTexture2::Wrap, osg::Texture2D::WrapMode> texWrapMap; |
|---|
| 1606 | static bool firstTime = true; |
|---|
| 1607 | if (firstTime) |
|---|
| 1608 | { |
|---|
| 1609 | texWrapMap[SoTexture2::CLAMP] = osg::Texture2D::CLAMP; |
|---|
| 1610 | texWrapMap[SoTexture2::REPEAT] = osg::Texture2D::REPEAT; |
|---|
| 1611 | firstTime = false; |
|---|
| 1612 | } |
|---|
| 1613 | |
|---|
| 1614 | |
|---|
| 1615 | #ifdef __COIN__ |
|---|
| 1616 | if (soNode->isOfType(SoVRMLImageTexture::getClassTypeId())) { |
|---|
| 1617 | |
|---|
| 1618 | |
|---|
| 1619 | |
|---|
| 1620 | |
|---|
| 1621 | |
|---|
| 1622 | |
|---|
| 1623 | osgTex->setWrap(osg::Texture2D::WRAP_S, ((SoVRMLImageTexture*)soNode)->repeatS.getValue() ? |
|---|
| 1624 | osg::Texture2D::REPEAT : osg::Texture2D::CLAMP_TO_EDGE); |
|---|
| 1625 | osgTex->setWrap(osg::Texture2D::WRAP_T, ((SoVRMLImageTexture*)soNode)->repeatT.getValue() ? |
|---|
| 1626 | osg::Texture2D::REPEAT : osg::Texture2D::CLAMP_TO_EDGE); |
|---|
| 1627 | } |
|---|
| 1628 | else |
|---|
| 1629 | #endif |
|---|
| 1630 | { |
|---|
| 1631 | |
|---|
| 1632 | osgTex->setWrap(osg::Texture2D::WRAP_S, texWrapMap[action->getTextureWrapS()]); |
|---|
| 1633 | osgTex->setWrap(osg::Texture2D::WRAP_T, texWrapMap[action->getTextureWrapT()]); |
|---|
| 1634 | } |
|---|
| 1635 | |
|---|
| 1636 | return osgTex; |
|---|
| 1637 | } |
|---|
| 1638 | |
|---|
| 1639 | SoCallbackAction::Response |
|---|
| 1640 | ConvertFromInventor::preInfo(void* data, SoCallbackAction* action, |
|---|
| 1641 | const SoNode* node) |
|---|
| 1642 | { |
|---|
| 1643 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1644 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preInfo() " |
|---|
| 1645 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 1646 | #endif |
|---|
| 1647 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1648 | SoInfo* info = (SoInfo*)node; |
|---|
| 1649 | thisPtr->transformInfoName = info->string.getValue(); |
|---|
| 1650 | |
|---|
| 1651 | return SoCallbackAction::CONTINUE; |
|---|
| 1652 | } |
|---|
| 1653 | |
|---|
| 1654 | SoCallbackAction::Response |
|---|
| 1655 | ConvertFromInventor::preRotor(void *data, SoCallbackAction *action, |
|---|
| 1656 | const SoNode *node) |
|---|
| 1657 | { |
|---|
| 1658 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1659 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preRotor() " |
|---|
| 1660 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 1661 | #endif |
|---|
| 1662 | |
|---|
| 1663 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1664 | |
|---|
| 1665 | |
|---|
| 1666 | SoRotor *ivRotor = (SoRotor *) node; |
|---|
| 1667 | SbVec3f ivAxis; |
|---|
| 1668 | float angle; |
|---|
| 1669 | ivRotor->rotation.getValue(ivAxis, angle); |
|---|
| 1670 | |
|---|
| 1671 | |
|---|
| 1672 | osg::ref_ptr<osg::MatrixTransform> rotorTransform = new osg::MatrixTransform; |
|---|
| 1673 | |
|---|
| 1674 | |
|---|
| 1675 | osg::Vec3 pivot(0, 0, 0); |
|---|
| 1676 | osg::Vec3 axis(ivAxis[0], ivAxis[1], ivAxis[2]); |
|---|
| 1677 | osg::ref_ptr<osgUtil::TransformCallback> rotorCallback |
|---|
| 1678 | = new osgUtil::TransformCallback(pivot, axis, |
|---|
| 1679 | 2 * osg::PI * ivRotor->speed.getValue()); |
|---|
| 1680 | |
|---|
| 1681 | |
|---|
| 1682 | rotorTransform->setUpdateCallback(rotorCallback.get()); |
|---|
| 1683 | |
|---|
| 1684 | |
|---|
| 1685 | thisPtr->ivPushState(action, node, |
|---|
| 1686 | IvStateItem::MULTI_POP | IvStateItem::UPDATE_STATE | |
|---|
| 1687 | IvStateItem::APPEND_AT_PUSH, rotorTransform.get()); |
|---|
| 1688 | |
|---|
| 1689 | |
|---|
| 1690 | if (!ivRotor->rotation.isIgnored()) { |
|---|
| 1691 | SoModelMatrixElement::rotateBy(action->getState(), ivRotor, |
|---|
| 1692 | ivRotor->rotation.getValue()); |
|---|
| 1693 | } |
|---|
| 1694 | |
|---|
| 1695 | |
|---|
| 1696 | |
|---|
| 1697 | |
|---|
| 1698 | |
|---|
| 1699 | return SoCallbackAction::PRUNE; |
|---|
| 1700 | } |
|---|
| 1701 | |
|---|
| 1702 | SoCallbackAction::Response |
|---|
| 1703 | ConvertFromInventor::prePendulum(void* data, SoCallbackAction *action, |
|---|
| 1704 | const SoNode* node) |
|---|
| 1705 | { |
|---|
| 1706 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1707 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "prePendulum() " |
|---|
| 1708 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 1709 | #endif |
|---|
| 1710 | |
|---|
| 1711 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1712 | |
|---|
| 1713 | |
|---|
| 1714 | SoPendulum *ivPendulum = (SoPendulum *) node; |
|---|
| 1715 | SbVec3f ivAxis0, ivAxis1; |
|---|
| 1716 | float startAngle, endAngle; |
|---|
| 1717 | ivPendulum->rotation0.getValue(ivAxis0, startAngle); |
|---|
| 1718 | ivPendulum->rotation1.getValue(ivAxis1, endAngle); |
|---|
| 1719 | ivAxis0.normalize(); |
|---|
| 1720 | ivAxis1.normalize(); |
|---|
| 1721 | |
|---|
| 1722 | |
|---|
| 1723 | |
|---|
| 1724 | |
|---|
| 1725 | |
|---|
| 1726 | if ((ivAxis0+ivAxis1).length() < 0.5 ) { |
|---|
| 1727 | ivAxis1 = -ivAxis1; |
|---|
| 1728 | endAngle = -endAngle; |
|---|
| 1729 | } |
|---|
| 1730 | |
|---|
| 1731 | |
|---|
| 1732 | osg::ref_ptr<osg::MatrixTransform> pendulumTransform = new osg::MatrixTransform; |
|---|
| 1733 | |
|---|
| 1734 | |
|---|
| 1735 | |
|---|
| 1736 | |
|---|
| 1737 | osg::Vec3 axis; |
|---|
| 1738 | if (fabs(startAngle) > fabs(endAngle)) |
|---|
| 1739 | axis = osg::Vec3(ivAxis0[0], ivAxis0[1], ivAxis0[2]); |
|---|
| 1740 | else |
|---|
| 1741 | axis = osg::Vec3(ivAxis1[0], ivAxis1[1], ivAxis1[2]); |
|---|
| 1742 | PendulumCallback* pendulumCallback |
|---|
| 1743 | = new PendulumCallback(axis, startAngle, endAngle, |
|---|
| 1744 | ivPendulum->speed.getValue()); |
|---|
| 1745 | |
|---|
| 1746 | |
|---|
| 1747 | pendulumTransform->setUpdateCallback(pendulumCallback); |
|---|
| 1748 | |
|---|
| 1749 | |
|---|
| 1750 | thisPtr->ivPushState(action, node, |
|---|
| 1751 | IvStateItem::MULTI_POP | IvStateItem::UPDATE_STATE | |
|---|
| 1752 | IvStateItem::APPEND_AT_PUSH, pendulumTransform.get()); |
|---|
| 1753 | |
|---|
| 1754 | |
|---|
| 1755 | |
|---|
| 1756 | |
|---|
| 1757 | |
|---|
| 1758 | return SoCallbackAction::PRUNE; |
|---|
| 1759 | } |
|---|
| 1760 | |
|---|
| 1761 | SoCallbackAction::Response |
|---|
| 1762 | ConvertFromInventor::preShuttle(void* data, SoCallbackAction *action, |
|---|
| 1763 | const SoNode* node) |
|---|
| 1764 | { |
|---|
| 1765 | #ifdef DEBUG_IV_PLUGIN |
|---|
| 1766 | osg::notify(osg::DEBUG_INFO) << NOTIFY_HEADER << "preShuttle() " |
|---|
| 1767 | << node->getTypeId().getName().getString() << std::endl; |
|---|
| 1768 | #endif |
|---|
| 1769 | |
|---|
| 1770 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1771 | |
|---|
| 1772 | |
|---|
| 1773 | SoShuttle *ivShuttle = (SoShuttle *) node; |
|---|
| 1774 | SbVec3f ivStartPos, ivEndPos; |
|---|
| 1775 | ivStartPos = ivShuttle->translation0.getValue(); |
|---|
| 1776 | ivEndPos = ivShuttle->translation1.getValue(); |
|---|
| 1777 | |
|---|
| 1778 | |
|---|
| 1779 | osg::ref_ptr<osg::MatrixTransform> shuttleTransform = new osg::MatrixTransform; |
|---|
| 1780 | |
|---|
| 1781 | |
|---|
| 1782 | osg::Vec3 startPos(ivStartPos[0], ivStartPos[1], ivStartPos[2]); |
|---|
| 1783 | osg::Vec3 endPos(ivEndPos[0], ivEndPos[1], ivEndPos[2]); |
|---|
| 1784 | ShuttleCallback* shuttleCallback |
|---|
| 1785 | = new ShuttleCallback(startPos, endPos, ivShuttle->speed.getValue()); |
|---|
| 1786 | |
|---|
| 1787 | |
|---|
| 1788 | shuttleTransform->setUpdateCallback(shuttleCallback); |
|---|
| 1789 | |
|---|
| 1790 | |
|---|
| 1791 | thisPtr->ivPushState(action, node, |
|---|
| 1792 | IvStateItem::MULTI_POP | IvStateItem::UPDATE_STATE | |
|---|
| 1793 | IvStateItem::APPEND_AT_PUSH, shuttleTransform.get()); |
|---|
| 1794 | |
|---|
| 1795 | |
|---|
| 1796 | |
|---|
| 1797 | |
|---|
| 1798 | |
|---|
| 1799 | return SoCallbackAction::PRUNE; |
|---|
| 1800 | } |
|---|
| 1801 | |
|---|
| 1802 | void ConvertFromInventor::addVertex(SoCallbackAction* action, |
|---|
| 1803 | const SoPrimitiveVertex *v, |
|---|
| 1804 | int index) |
|---|
| 1805 | { |
|---|
| 1806 | |
|---|
| 1807 | SbVec3f pt = v->getPoint(); |
|---|
| 1808 | vertices.push_back(osg::Vec3(pt[0], pt[1], pt[2])); |
|---|
| 1809 | |
|---|
| 1810 | |
|---|
| 1811 | SbVec3f norm = v->getNormal(); |
|---|
| 1812 | |
|---|
| 1813 | if ((normalBinding == osg::Geometry::BIND_PER_VERTEX) || |
|---|
| 1814 | (normalBinding == osg::Geometry::BIND_PER_PRIMITIVE && index == 0)) |
|---|
| 1815 | { |
|---|
| 1816 | |
|---|
| 1817 | |
|---|
| 1818 | |
|---|
| 1819 | |
|---|
| 1820 | |
|---|
| 1821 | normals.push_back(osg::Vec3(norm[0], norm[1], norm[2])); |
|---|
| 1822 | } |
|---|
| 1823 | |
|---|
| 1824 | if (colorBinding == osg::Geometry::BIND_PER_VERTEX || |
|---|
| 1825 | colorBinding == osg::Geometry::BIND_PER_PRIMITIVE) |
|---|
| 1826 | { |
|---|
| 1827 | |
|---|
| 1828 | SbColor ambient, diffuse, specular, emission; |
|---|
| 1829 | float transparency, shininess; |
|---|
| 1830 | action->getMaterial(ambient, diffuse, specular, emission, shininess, |
|---|
| 1831 | transparency, v->getMaterialIndex()); |
|---|
| 1832 | if (colorBinding == osg::Geometry::BIND_PER_VERTEX) |
|---|
| 1833 | colors.push_back(osg::Vec4(diffuse[0], diffuse[1], diffuse[2], |
|---|
| 1834 | 1.0 - transparency)); |
|---|
| 1835 | else if (colorBinding == osg::Geometry::BIND_PER_PRIMITIVE && index == 0) |
|---|
| 1836 | colors.push_back(osg::Vec4(diffuse[0], diffuse[1], diffuse[2], |
|---|
| 1837 | 1.0 - transparency)); |
|---|
| 1838 | } |
|---|
| 1839 | |
|---|
| 1840 | |
|---|
| 1841 | SbVec4f texCoord = v->getTextureCoords(); |
|---|
| 1842 | textureCoords.push_back(osg::Vec2(texCoord[0], texCoord[1])); |
|---|
| 1843 | } |
|---|
| 1844 | |
|---|
| 1845 | void ConvertFromInventor::addTriangleCB(void* data, SoCallbackAction* action, |
|---|
| 1846 | const SoPrimitiveVertex* v0, |
|---|
| 1847 | const SoPrimitiveVertex* v1, |
|---|
| 1848 | const SoPrimitiveVertex* v2) |
|---|
| 1849 | { |
|---|
| 1850 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1851 | |
|---|
| 1852 | switch (thisPtr->vertexOrder) |
|---|
| 1853 | { |
|---|
| 1854 | case CLOCKWISE: |
|---|
| 1855 | thisPtr->addVertex(action, v0, 0); |
|---|
| 1856 | thisPtr->addVertex(action, v2, 1); |
|---|
| 1857 | thisPtr->addVertex(action, v1, 2); |
|---|
| 1858 | break; |
|---|
| 1859 | case COUNTER_CLOCKWISE: |
|---|
| 1860 | thisPtr->addVertex(action, v0, 0); |
|---|
| 1861 | thisPtr->addVertex(action, v1, 1); |
|---|
| 1862 | thisPtr->addVertex(action, v2, 2); |
|---|
| 1863 | break; |
|---|
| 1864 | } |
|---|
| 1865 | |
|---|
| 1866 | thisPtr->numPrimitives++; |
|---|
| 1867 | thisPtr->primitiveType = osg::PrimitiveSet::TRIANGLES; |
|---|
| 1868 | } |
|---|
| 1869 | |
|---|
| 1870 | void ConvertFromInventor::addLineSegmentCB(void* data, SoCallbackAction* action, |
|---|
| 1871 | const SoPrimitiveVertex* v0, |
|---|
| 1872 | const SoPrimitiveVertex* v1) |
|---|
| 1873 | { |
|---|
| 1874 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1875 | |
|---|
| 1876 | thisPtr->addVertex(action, v0, 0); |
|---|
| 1877 | thisPtr->addVertex(action, v1, 1); |
|---|
| 1878 | |
|---|
| 1879 | thisPtr->numPrimitives++; |
|---|
| 1880 | thisPtr->primitiveType = osg::PrimitiveSet::LINES; |
|---|
| 1881 | } |
|---|
| 1882 | |
|---|
| 1883 | void ConvertFromInventor::addPointCB(void* data, SoCallbackAction* action, |
|---|
| 1884 | const SoPrimitiveVertex* v0) |
|---|
| 1885 | { |
|---|
| 1886 | ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); |
|---|
| 1887 | |
|---|
| 1888 | thisPtr->addVertex(action, v0, 0); |
|---|
| 1889 | |
|---|
| 1890 | thisPtr->numPrimitives++; |
|---|
| 1891 | thisPtr->primitiveType = osg::PrimitiveSet::POINTS; |
|---|
| 1892 | } |
|---|