📄 convertfrominventor.cpp
字号:
if (action->getLineWidth()) { osg::ref_ptr<osg::LineWidth> lineWidth = new osg::LineWidth; lineWidth->setWidth(action->getLineWidth()); stateSet->setAttributeAndModes(lineWidth.get(), osg::StateAttribute::ON); } // Set pointsize if (action->getPointSize()) { osg::ref_ptr<osg::Point> point = new osg::Point; point->setSize(action->getPointSize()); stateSet->setAttributeAndModes(point.get(), osg::StateAttribute::ON); } // Set draw mode switch (action->getDrawStyle()) { case SoDrawStyle::FILLED: {#if 0// OSG defaults to filled draw style, so no need to set redundent state. osg::PolygonMode *polygonMode = new osg::PolygonMode; polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::FILL); stateSet->setAttributeAndModes(polygonMode, osg::StateAttribute::ON);#endif break; } case SoDrawStyle::LINES: { osg::ref_ptr<osg::PolygonMode> polygonMode = new osg::PolygonMode; polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::LINE); stateSet->setAttributeAndModes(polygonMode.get(), osg::StateAttribute::ON); break; } case SoDrawStyle::POINTS: { osg::ref_ptr<osg::PolygonMode> polygonMode = new osg::PolygonMode; polygonMode->setMode(osg::PolygonMode::FRONT_AND_BACK, osg::PolygonMode::POINT); stateSet->setAttributeAndModes(polygonMode.get(), osg::StateAttribute::ON); break; } case SoDrawStyle::INVISIBLE: // check how to handle this in osg. break; } // Set back face culling if (action->getShapeType() == SoShapeHints::SOLID) { osg::ref_ptr<osg::CullFace> cullFace = new osg::CullFace; cullFace->setMode(osg::CullFace::BACK); stateSet->setAttributeAndModes(cullFace.get(), osg::StateAttribute::ON); } // Set lighting if (action->getLightModel() == SoLightModel::BASE_COLOR) stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF); else { // Set the material osg::ref_ptr<osg::Material> material = new osg::Material; material->setAmbient(osg::Material::FRONT_AND_BACK, osg::Vec4(ambient[0], ambient[1], ambient[2], 1.0 - transparency)); material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(diffuse[0], diffuse[1], diffuse[2], 1.0 - transparency)); material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(specular[0], specular[1], specular[2], 1.0 - transparency)); material->setEmission(osg::Material::FRONT_AND_BACK, osg::Vec4(emission[0], emission[1], emission[2], 1.0 - transparency)); material->setTransparency(osg::Material::FRONT_AND_BACK, transparency); if (specular[0] || specular[1] || specular[2]) material->setShininess(osg::Material::FRONT_AND_BACK, shininess*128.0); else material->setShininess(osg::Material::FRONT_AND_BACK, 0.0); material->setColorMode(osg::Material::DIFFUSE); stateSet->setAttributeAndModes(material.get(), osg::StateAttribute::ON); stateSet->setName(appearanceName.getString()); stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON);#if 0// disable as two sided lighting causes problem under NVidia, and the above osg::Material settings are single sided anway.. // Set two sided lighting osg::LightModel* lightModel = new osg::LightModel; lightModel->setTwoSided(true); stateSet->setAttributeAndModes(lightModel, osg::StateAttribute::ON);#endif // Set lights LightList lightList = lightStack.top(); for (unsigned int i = 0; i < lightList.size(); i++) stateSet->setAttributeAndModes(lightList[i], osg::StateAttribute::ON); } return stateSet;}////////////////////////////////////////////////////////////////////osg::Texture2D*ConvertFromInventor::convertIVTexToOSGTex(const SoNode* soNode, SoCallbackAction* action){ osg::notify(osg::INFO)<<"convertIVTexToOSGTex of type "<< soNode->getTypeId().getName().getString()<<std::endl; SbVec2s soSize; int soNC; // Get the texture size and components const unsigned char* soImageData = action->getTextureImage(soSize, soNC); if (!soImageData) { osg::notify(osg::WARN) << "IV import warning: Error while loading texture data." << std::endl; return NULL; } // Allocate memory for image data unsigned char* osgImageData = new unsigned char[soSize[0] * soSize[1] * soNC]; // Copy the texture image data from the inventor texture memcpy(osgImageData, soImageData, soSize[0] * soSize[1] * soNC); // Copy the name std::string name = soNode->getName().getString(); // File name std::string fileName; if (soNode->isOfType(SoTexture2::getClassTypeId())) fileName = ((SoTexture2*)soNode)->filename.getValue().getString();#ifdef __COIN__ else if (soNode->isOfType(SoVRMLImageTexture::getClassTypeId())) fileName = ((SoVRMLImageTexture*)soNode)->url.getNum() >= 1 ? ((SoVRMLImageTexture*)soNode)->url.getValues(0)[0].getString() : "";#endif else osg::notify(osg::WARN) << "IV import warning: Unsupported texture type: " << soNode->getTypeId().getName().getString() << std::endl; osg::notify(osg::INFO) << fileName << " -> "; if (fileName[0]=='\"') fileName.erase(fileName.begin()); if (fileName.size() > 0 && fileName[fileName.size()-1]=='\"') fileName.erase(fileName.begin()+fileName.size()-1); osg::notify(osg::INFO) << fileName << std::endl; // Create the osg::Image osg::ref_ptr<osg::Image> osgImage = new osg::Image; osgImage->setFileName(fileName); GLenum formats[] = {GL_LUMINANCE, GL_LUMINANCE_ALPHA, GL_RGB, GL_RGBA}; osgImage->setImage(soSize[0], soSize[1], 0, soNC, formats[soNC-1], GL_UNSIGNED_BYTE, osgImageData, osg::Image::USE_NEW_DELETE); // Create the osg::Texture2D osg::Texture2D *osgTex = new osg::Texture2D; osgTex->setImage(osgImage.get()); if (name != "") { osgTex->setName(name); } static std::map<SoTexture2::Wrap, osg::Texture2D::WrapMode> texWrapMap; static bool firstTime = true; if (firstTime) { texWrapMap[SoTexture2::CLAMP] = osg::Texture2D::CLAMP; texWrapMap[SoTexture2::REPEAT] = osg::Texture2D::REPEAT; firstTime = false; } // Set texture wrap mode#ifdef __COIN__ if (soNode->isOfType(SoVRMLImageTexture::getClassTypeId())) { // It looks like there is a high probability of bug in Coin (investigated on version 2.4.6). // action->getTextureWrap() returns correct value on SoTexture2 (SoTexture2::CLAMP = 0x2900, // and REPEAT = 0x2901), but SoVRMLImageTexture returns incorrect value of // SoGLImage::REPEAT = 0, CLAMP = 1, CLAMP_TO_EDGE = 2). // So, let's not use action and try to get correct value directly from texture node. // PCJohn-2007-04-22 osgTex->setWrap(osg::Texture2D::WRAP_S, ((SoVRMLImageTexture*)soNode)->repeatS.getValue() ? osg::Texture2D::REPEAT : osg::Texture2D::CLAMP_TO_EDGE); osgTex->setWrap(osg::Texture2D::WRAP_T, ((SoVRMLImageTexture*)soNode)->repeatT.getValue() ? osg::Texture2D::REPEAT : osg::Texture2D::CLAMP_TO_EDGE); } else#endif { // Proper way to determine wrap mode osgTex->setWrap(osg::Texture2D::WRAP_S, texWrapMap[action->getTextureWrapS()]); osgTex->setWrap(osg::Texture2D::WRAP_T, texWrapMap[action->getTextureWrapT()]); } return osgTex; }///////////////////////////////////////////////////////////////////SoCallbackAction::Response ConvertFromInventor::preInfo(void* data, SoCallbackAction* action, const SoNode* node){#ifdef DEBUG_IV_PLUGIN osg::notify(osg::INFO) << "preInfo() " << node->getTypeId().getName().getString() << std::endl;#endif ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); SoInfo* info = (SoInfo*)node; thisPtr->transformInfoName = info->string.getValue(); return SoCallbackAction::CONTINUE;} ///////////////////////////////////////////////////////////////////SoCallbackAction::Response ConvertFromInventor::preGroup(void* data, SoCallbackAction* action, const SoNode* node){#ifdef DEBUG_IV_PLUGIN osg::notify(osg::INFO) << "preGroup() " << node->getTypeId().getName().getString() << std::endl;#endif ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); // Create a new Group or LOD and add it to the stack osg::ref_ptr<osg::Group> group; if (node->isOfType(SoLOD::getClassTypeId())) { group = new osg::LOD; } else { group = new osg::Group; } thisPtr->groupStack.top()->addChild(group.get()); thisPtr->groupStack.push(group.get()); // handle transform nodes if (node->isOfType(SoTransform::getClassTypeId())) { SoTransform* t = (SoTransform*)node; SbVec3f axis, center, trans, scale; float angle; center = t->center.getValue(); t->rotation.getValue(axis, angle); trans = t->translation.getValue(); scale = t->scaleFactor.getValue(); std::string name = t->getName().getString(); thisPtr->addMatrixTransform(name, axis, angle, center, trans, scale); }#ifdef __COIN__ if (node->isOfType(SoVRMLTransform::getClassTypeId())) { std::string name; if (thisPtr->transformInfoName != "") { name = std::string("INFO_"); name += thisPtr->transformInfoName.getString(); name += "_trans"; } else { name = node->getName(); } SoVRMLTransform* vt = (SoVRMLTransform*)node; SbVec3f axis, center, trans, scale; float angle; center = vt->center.getValue(); vt->rotation.getValue(axis, angle); trans = vt->translation.getValue(); scale = vt->scale.getValue(); thisPtr->addMatrixTransform(name, axis, angle, center, trans, scale); }#endif if (node->isOfType(SoSeparator::getClassTypeId())) { if (thisPtr->soTexStack.size()) thisPtr->soTexStack.push(thisPtr->soTexStack.top()); else thisPtr->soTexStack.push(NULL); if (thisPtr->lightStack.size()) { LightList lightList = thisPtr->lightStack.top(); thisPtr->lightStack.push(lightList); } } return SoCallbackAction::CONTINUE;}//////////////////////////////////////////////////////////////SoCallbackAction::Response ConvertFromInventor::postGroup(void* data, SoCallbackAction* action, const SoNode* node){ // Handle SoLOD nodes specially if (node->isOfType(SoLOD::getClassTypeId())) return postLOD(data, action, node);#ifdef DEBUG_IV_PLUGIN osg::notify(osg::INFO) << "postGroup() " << node->getTypeId().getName().getString() << std::endl;#endif ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); // Pop all the groups that are Transforms osg::ref_ptr<osg::Group> group = thisPtr->groupStack.top(); while (strcmp(group->className(), "MatrixTransform") == 0) { thisPtr->groupStack.pop(); group = thisPtr->groupStack.top(); } // Pop the group from the stack thisPtr->groupStack.pop();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -