📄 converttoinventor.cpp
字号:
} else { ((SoLineSet*)shape)->numVertices.setNum(1); ((SoLineSet*)shape)->numVertices.set1Value(0, elementsCount); } break; default: osg::notify(osg::WARN) << "IvWriter: NOT IMPLEMENTED" << std::endl; assert(0); } } else if (shape->isOfType(SoTriangleStripSet::getClassTypeId())) { switch (pset->getMode()) { case GL_TRIANGLES: n = elementsCount/3; assert(n*3 == elementsCount && "elementsCount is not multiple of 3."); ((SoTriangleStripSet*)shape)->numVertices.setNum(n); for (i=0; i<n; i++) ((SoTriangleStripSet*)shape)->numVertices.set1Value(i, 3); break; case GL_TRIANGLE_STRIP: if (drawArrayLengths) { ((SoTriangleStripSet*)shape)->numVertices.setNum(drawArrayLengthsElems); int i=0; for (osg::DrawArrayLengths::const_iterator primItr=drawArrayLengths->begin(); primItr!=drawArrayLengths->end(); ++primItr, i++) ((SoTriangleStripSet*)shape)->numVertices.set1Value(i, *primItr); } else { ((SoTriangleStripSet*)shape)->numVertices.setNum(1); ((SoTriangleStripSet*)shape)->numVertices.set1Value(0, elementsCount); } break; case GL_TRIANGLE_FAN: osg::notify(osg::WARN) << "IvWriter: GL_TRIANGLE_FAN NOT IMPLEMENTED" << std::endl; ((SoTriangleStripSet*)shape)->numVertices.setNum(1); ((SoTriangleStripSet*)shape)->numVertices.set1Value(0, elementsCount); break; case GL_QUAD_STRIP: assert(elementsCount % 2 == 0 && "elementsCount is not multiple of 2."); if (drawArrayLengths) { ((SoTriangleStripSet*)shape)->numVertices.setNum(drawArrayLengthsElems); int i=0; for (osg::DrawArrayLengths::const_iterator primItr=drawArrayLengths->begin(); primItr!=drawArrayLengths->end(); ++primItr, i++) ((SoTriangleStripSet*)shape)->numVertices.set1Value(i, *primItr); } else { ((SoTriangleStripSet*)shape)->numVertices.setNum(1); ((SoTriangleStripSet*)shape)->numVertices.set1Value(0, elementsCount); } break; default: osg::notify(osg::WARN) << "IvWriter: NOT IMPLEMENTED" << std::endl; assert(0); } } else if (shape->isOfType(SoFaceSet::getClassTypeId())) { switch (pset->getMode()) { case GL_QUADS: n = elementsCount/4; assert(n*4 == elementsCount && "elementsCount is not multiple of 4."); ((SoFaceSet*)shape)->numVertices.setNum(n); for (i=0; i<n; i++) ((SoFaceSet*)shape)->numVertices.set1Value(i, 4); break; case GL_POLYGON: if (drawArrayLengths) { ((SoFaceSet*)shape)->numVertices.setNum(drawArrayLengthsElems); int i=0; for (osg::DrawArrayLengths::const_iterator primItr=drawArrayLengths->begin(); primItr!=drawArrayLengths->end(); ++primItr, i++) ((SoFaceSet*)shape)->numVertices.set1Value(i, *primItr); } else { ((SoFaceSet*)shape)->numVertices.setNum(1); ((SoFaceSet*)shape)->numVertices.set1Value(0, elementsCount); } break; default: osg::notify(osg::WARN) << "IvWriter: NOT IMPLEMENTED" << std::endl; assert(0); } } else { osg::notify(osg::WARN) << "IvWriter: NOT IMPLEMENTED" << std::endl; assert(0 && "Unknown non-indexed shape type."); } } // Construct graph // Each osg::Drawable will have its own SoSeparator (render caching, culling, etc.) SoSeparator *sep = new SoSeparator; if (nonIndexedCoords) { assert(shape->isOfType(SoNonIndexedShape::getClassTypeId()) && "Not nonIndexed shape."); if (!ok) { // handle errors nonIndexedCoords->ref(); nonIndexedCoords->unref(); if (nonIndexedTexCoords) { nonIndexedTexCoords->ref(); nonIndexedTexCoords->unref(); } if (nonIndexedNormals) { nonIndexedNormals->ref(); nonIndexedNormals->unref(); } nonIndexedMaterial->ref(); nonIndexedMaterial->unref(); shape->ref(); shape->unref(); sep->ref(); sep->unref(); } else { // make scene graph if (nonIndexedRoot == NULL) { nonIndexedRoot = new SoSeparator; if (ivTexture) nonIndexedRoot->addChild(ivTexture); if (ivTexture) nonIndexedRoot->addChild(createTexCoordBinding(FALSE)); nonIndexedRoot->addChild(createMaterialBinding(g, FALSE)); if (ivNormals) nonIndexedRoot->addChild(createNormalBinding(g, FALSE)); } if (nonIndexedMaterial) sep->addChild(nonIndexedMaterial); sep->addChild(nonIndexedCoords); if (nonIndexedNormals) sep->addChild(nonIndexedNormals); if (nonIndexedTexCoords) sep->addChild(nonIndexedTexCoords); sep->addChild(shape); nonIndexedRoot->addChild(sep); } } else { assert(shape->isOfType(SoIndexedShape::getClassTypeId()) && "Not indexed shape."); assert(nonIndexedCoords==NULL && nonIndexedNormals==NULL && nonIndexedMaterial==NULL); if (indexedRoot == NULL) { indexedRoot = new SoSeparator; if (ivTexture) indexedRoot->addChild(ivTexture); if (ivTexture) indexedRoot->addChild(createTexCoordBinding(TRUE)); if (ivMaterial) indexedRoot->addChild(ivMaterial); indexedRoot->addChild(createMaterialBinding(g, TRUE)); indexedRoot->addChild(ivCoords); if (ivNormals) indexedRoot->addChild(ivNormals); if (ivNormals) indexedRoot->addChild(createNormalBinding(g, TRUE)); if (ivTexCoords) indexedRoot->addChild(ivTexCoords); } sep->addChild(shape); indexedRoot->addChild(sep); } return ok;}void ConvertToInventor::processGeometry(const osg::Geometry *g, InventorState *ivState){ int normalIndex = 0; int colorIndex = 0; // Inventor scene graph roots SoSeparator *indexedRoot = NULL; SoSeparator *nonIndexedRoot = NULL; // Active material SoMaterial *ivStateMaterial = ivState->ivMaterial; SoNode *ivMaterial = NULL; if (ivState->osgLighting || vrml1Conversion) // SoMaterial if (g->getColorArray()) if (ivStateMaterial) ivMaterial = ivStateMaterial->copy(); else ivMaterial = new SoMaterial; // FIXME: check default values of SoMaterial and OSG lighting else if (ivStateMaterial) ivMaterial = ivStateMaterial; else ivMaterial = NULL; else // SoBaseColor if (g->getColorArray()) if (ivStateMaterial) { ivMaterial = new SoBaseColor; ((SoBaseColor*)ivMaterial)->rgb.setValue(ivStateMaterial->diffuseColor[0]); // copy first value } else ivMaterial = new SoBaseColor; // FIXME: check default values of SoBaseColor and OSG pre-lit scene else if (ivStateMaterial) { ivMaterial = new SoBaseColor; ((SoBaseColor*)ivMaterial)->rgb.setValue(ivStateMaterial->diffuseColor[0]); // copy first value } else ivMaterial = NULL; // Convert color array into the SoMaterial if (g->getColorArray()) { assert(ivMaterial); // Choose correct color field SoMFColor *colorField; if (ivMaterial->isOfType(SoMaterial::getClassTypeId())) { if (vrml1Conversion && ivState->osgLighting==false) { // special case of pre-lit VRML1 scene ((SoMaterial*)ivMaterial)->ambientColor.setValue(0.f,0.f,0.f); ((SoMaterial*)ivMaterial)->diffuseColor.setValue(0.f,0.f,0.f); ((SoMaterial*)ivMaterial)->specularColor.setValue(0.f,0.f,0.f); colorField = &((SoMaterial*)ivMaterial)->emissiveColor; } else // regular diffuse color colorField = &((SoMaterial*)ivMaterial)->diffuseColor; } else // Using of SoBaseColor colorField = &((SoBaseColor*)ivMaterial)->rgb; // Color array with material if (ivState->osgMaterial == NULL || ivState->osgMaterial->getColorMode() == osg::Material::DIFFUSE || ivState->osgMaterial->getColorMode() == osg::Material::AMBIENT_AND_DIFFUSE) osgArray2ivMField(g->getColorArray(), *colorField); else; // FIXME: implement some workaround for non-diffuse cases? // note: Warning was already shown in createInventorState(). // note2: There is no effect to convert SoMaterial::[ambient|specular|emissive]color // here because Inventor does not set them per-vertex (performance reasons). See // Inventor documentation for more details. } // Convert coordinates // OSG represents coordinates by: Vec2, Vec3, Vec4 // Inventor by: SbVec3f, SbVec4f SoNode *coords; if (g->getVertexArray()->getDataSize() == 4) { coords = new SoCoordinate4; osgArray2ivMField(g->getVertexArray(), ((SoCoordinate4*)coords)->point); } else { coords = new SoCoordinate3; osgArray2ivMField(g->getVertexArray(), ((SoCoordinate3*)coords)->point); } coords->ref(); // Convert texture coordinates SoNode *texCoords = NULL; if (ivState->ivTexture) { if (ivState->osgTexGenS && ivState->osgTexGenT && ivState->osgTexGen && ivState->osgTexGen->getMode()==osg::TexGen::SPHERE_MAP) texCoords = new SoTextureCoordinateEnvironment; else if (g->getTexCoordArray(0)) { if (g->getTexCoordArray(0)->getDataSize() <= 2) { texCoords = new SoTextureCoordinate2; osgArray2ivMField(g->getTexCoordArray(0), ((SoTextureCoordinate2*)texCoords)->point); } #ifdef __COIN__ else { texCoords = new SoTextureCoordinate3; osgArray2ivMField(g->getTexCoordArray(0), ((SoTextureCoordinate3*)texCoords)->point); }#endif // __COIN__ } if (texCoords) texCoords->ref(); } // Convert normals // OSG represents normals by: Vec3,Vec3s,Vec3b // and can handle: Vec4s,Vec4b by truncating them to three components // Inventor by: SbVec3f SoNormal *normals = NULL; if (g->getNormalArray()) { normals = new SoNormal; osgArray2ivMField(g->getNormalArray(), normals->vector); normals->ref(); } // Convert osg::PrimitiveSets to Inventor's SoShapes int psetIndex,numPsets = g->getNumPrimitiveSets(); for (psetIndex=0; psetIndex<numPsets; psetIndex++) { // Get PrimitiveSet const osg::PrimitiveSet *pset = g->getPrimitiveSet(psetIndex); osg::PrimitiveSet::Type type = pset->getType(); GLenum mode = pset->getMode(); // Create appropriate SoShape bool useIndices = g->getVertexIndices() != NULL || vrml1Conversion; bool needSeparateTriangles = false; SoVertexShape *shape = NULL; switch (mode) { case GL_POINTS: shape = new SoPointSet; break; case GL_LINES: case GL_LINE_STRIP: case GL_LINE_LOOP: if (useIndices) shape = new SoIndexedLineSet; else shape = new SoLineSet; break; case GL_TRIANGLES: case GL_TRIANGLE_STRIP: case GL_QUAD_STRIP: if (useIndices) if (vrml1Conversion) { shape = new SoIndexedFaceSet; needSeparateTriangles = true; } else shape = new SoIndexedTriangleStripSet; else shape = new SoTriangleStripSet; break; case GL_TRIANGLE_FAN: needSeparateTriangles = true; shape = (vrml1Conversion) ? (SoVertexShape*)new SoIndexedFaceSet : new SoIndexedTriangleStripSet; break; case GL_QUADS: case GL_POLYGON: if (useIndices) shape = new SoIndexedFaceSet; else shape = new SoFaceSet; break; default: assert(0); } // Size of single geometric primitive int primSize; switch (mode) { case GL_LINES: primSize = 2; break; case GL_TRIANGLES: primSize = 3; break; case GL_QUADS: primSize = 4; break; default: primSize = 0; }; bool ok = true; switch (type) { case osg
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -