📄 convertfrominventor.cpp
字号:
textureMat.set((float *) action->getTextureMatrix().getValue()); // Transform texture coordinates if texture matrix is not an identity mat osg::Matrix identityMat; identityMat.makeIdentity(); osg::ref_ptr<osg::Vec2Array> texCoords = new osg::Vec2Array(thisPtr->textureCoords.size()); if (textureMat == identityMat) { // Set the texture coordinates for (unsigned int i = 0; i < thisPtr->textureCoords.size(); i++) (*texCoords)[i] = thisPtr->textureCoords[i]; } else { // Transform and set the texture coordinates for (unsigned int i = 0; i < thisPtr->textureCoords.size(); i++) { osg::Vec3 transVec = textureMat.preMult( osg::Vec3(thisPtr->textureCoords[i][0], thisPtr->textureCoords[i][1], 0.0)); (*texCoords)[i].set(transVec.x(), transVec.y()); } } geometry->setTexCoordArray(0, texCoords.get()); } // Set the parameters for the geometry geometry->addPrimitiveSet(new osg::DrawArrays(thisPtr->primitiveType,0, coords->size())); // Get the StateSet for the geoset osg::ref_ptr<osg::StateSet> stateSet = thisPtr->getStateSet(action); geometry->setStateSet(stateSet.get()); // Add the geoset to a geode osg::ref_ptr<osg::Geode> geode = new osg::Geode; geode->addDrawable(geometry.get()); // copy name std::string name = stateSet->getName(); if (name != "") { geode->setName(name); } // Add geode to scenegraph thisPtr->groupStack.top()->addChild(geode.get()); return SoCallbackAction::CONTINUE;}///////////////////////////////////////////////////////////////SoCallbackAction::Response ConvertFromInventor::preTexture(void* data, SoCallbackAction *, const SoNode* node){#ifdef DEBUG_IV_PLUGIN osg::notify(osg::INFO) << "preTexture() " << node->getTypeId().getName().getString() << std::endl;#endif ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); if (thisPtr->soTexStack.size()) thisPtr->soTexStack.pop(); thisPtr->soTexStack.push(node); return SoCallbackAction::CONTINUE;}//////////////////////////////////////////////////////////////////////////////////SoCallbackAction::Response ConvertFromInventor::preVRMLAppearance(void* data, SoCallbackAction* action, const SoNode* node){#ifdef DEBUG_IV_PLUGIN osg::notify(osg::INFO) << "preVRMLAppearance() " << node->getTypeId().getName().getString() << std::endl;#endif#ifdef __COIN__ ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); // If there is a VRML appearance node without a texture node, then // we push a NULL texture onto the stack bool foundTex = false; SoChildList *kids = node->getChildren(); for (int i=0; i<kids->getLength(); i++) { SoNode* kid = (SoNode*)kids->get(i); if (kid->isOfType(SoVRMLMaterial::getClassTypeId())) { thisPtr->appearanceName = kid->getName(); } if (kid->isOfType(SoVRMLTexture::getClassTypeId())) { foundTex = true; } } if (!foundTex) { thisPtr->soTexStack.push(NULL); thisPtr->inAppearanceWithNoTexture = true; }#endif return SoCallbackAction::CONTINUE;}//////////////////////////////////////////////////////////////////////////////////SoCallbackAction::Response ConvertFromInventor::preVRMLImageTexture(void* data, SoCallbackAction* action, const SoNode* node){#ifdef DEBUG_IV_PLUGIN osg::notify(osg::INFO) << "preVRMLImageTexture() " << node->getTypeId().getName().getString() << std::endl;#endif ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); if (thisPtr->soTexStack.size()) thisPtr->soTexStack.pop(); thisPtr->soTexStack.push(node); return SoCallbackAction::CONTINUE;}//////////////////////////////////////////////////////////////////void ConvertFromInventor::transformLight(SoCallbackAction* action, const SbVec3f& vec, osg::Vec3& transVec){ osg::Matrix modelMat; modelMat.set((float *)action->getModelMatrix().getValue()); transVec.set(vec[0], vec[1], vec[2]); transVec = modelMat.preMult(transVec);}///////////////////////////////////////////////////////////////////SoCallbackAction::Response ConvertFromInventor::preLight(void* data, SoCallbackAction* action, const SoNode* node){#ifdef DEBUG_IV_PLUGIN osg::notify(osg::INFO) << "preLight() " << node->getTypeId().getName().getString() << std::endl;#endif ConvertFromInventor* thisPtr = (ConvertFromInventor *) (data); static int lightNum = 1; // Return if the light is not on const SoLight* ivLight = (const SoLight*) node; if (!ivLight->on.getValue()) return SoCallbackAction::CONTINUE; osg::ref_ptr<osg::Light> osgLight = new osg::Light; osgLight->setLightNum(lightNum++); const char* name = ivLight->getName().getString(); osgLight->setName(name); // Get color and intensity SbVec3f lightColor = ivLight->color.getValue(); float intensity = ivLight->intensity.getValue(); // Set color and intensity osgLight->setDiffuse(osg::Vec4(lightColor[0] * intensity, lightColor[1] * intensity, lightColor[2] * intensity, 1)); if (node->isOfType(SoDirectionalLight::getClassTypeId())) { SoDirectionalLight *dirLight = (SoDirectionalLight *) node; osg::Vec3 transVec; thisPtr->transformLight(action, dirLight->direction.getValue(), transVec); osgLight->setPosition(osg::Vec4(transVec.x(), transVec.y(), transVec.z(), 0)); } else if (node->isOfType(SoPointLight::getClassTypeId())) { SoPointLight* ptLight = (SoPointLight *) node; osg::Vec3 transVec; thisPtr->transformLight(action, ptLight->location.getValue(), transVec); osgLight->setPosition(osg::Vec4(transVec.x(), transVec.y(), transVec.z(), 0)); } else if (node->isOfType(SoSpotLight::getClassTypeId())) { SoSpotLight* spotLight = (SoSpotLight *) node; osgLight->setSpotExponent(spotLight->dropOffRate.getValue() * 128.0); osgLight->setSpotCutoff(spotLight->cutOffAngle.getValue()*180.0/osg::PI); osg::Vec3 transVec; thisPtr->transformLight(action, spotLight->location.getValue(), transVec); osgLight->setPosition(osg::Vec4(transVec.x(), transVec.y(), transVec.z(), 0)); thisPtr->transformLight(action, spotLight->direction.getValue(),transVec); osgLight->setDirection(osg::Vec3(transVec.x(), transVec.y(), transVec.z())); } // Add light to list in the current level if (thisPtr->lightStack.size()) { LightList lightList; lightList = thisPtr->lightStack.top(); lightList.push_back(osgLight.get()); thisPtr->lightStack.pop(); thisPtr->lightStack.push(lightList); } // add a light source node to the scene graph osg::ref_ptr<osg::LightSource> ls = new osg::LightSource(); ls->setLight(osgLight.get()); ls->setName(ivLight->getName().getString()); if (thisPtr->lightGroup == NULL) { thisPtr->lightGroup = new osg::Group; thisPtr->lightGroup->setName("IvLightGroup"); thisPtr->_root->addChild(thisPtr->lightGroup.get()); } thisPtr->lightGroup->addChild(ls.get()); return SoCallbackAction::CONTINUE;}///////////////////////////////////////////////////////////////////////////////////////osg::ref_ptr<osg::StateSet>ConvertFromInventor::getStateSet(SoCallbackAction* action){ osg::ref_ptr<osg::StateSet> stateSet = new osg::StateSet; // Inherit modes from the global state stateSet->clear(); // Convert the IV texture to OSG texture if any osg::ref_ptr<osg::Texture2D> texture; const SoNode *ivTexture = soTexStack.top(); if (ivTexture) { osg::notify(osg::INFO)<<"Have texture"<<std::endl; // Found a corresponding OSG texture object if (ivToOsgTexMap[ivTexture]) texture = ivToOsgTexMap[ivTexture]; else { // Create a new osg texture texture = convertIVTexToOSGTex(ivTexture, action); // Add the new texture to the database ivToOsgTexMap[ivTexture] = texture.get(); } stateSet->setTextureAttributeAndModes(0, texture.get(), osg::StateAttribute::ON); // propogate name std::string name = texture->getName(); if (name != "") stateSet->setName(name); // Set the texture environment osg::ref_ptr<osg::TexEnv> texEnv = new osg::TexEnv; switch (action->getTextureModel()) { case SoTexture2::MODULATE: texEnv->setMode(osg::TexEnv::MODULATE); break; case SoTexture2::DECAL: texEnv->setMode(osg::TexEnv::DECAL); break; case SoTexture2::BLEND: texEnv->setMode(osg::TexEnv::BLEND); break; // SGI's Inventor does not have REPLACE mode, but the Coin 3D library does. // Coin supports REPLACE since 2.2 release, TGS Inventor from 4.0. // Let's convert to the TexEnv anyway. case (SoTexture2::Model)GL_REPLACE: // SoTexture2::REPLACE is the same as GL_REPLACE texEnv->setMode(osg::TexEnv::REPLACE); break; } stateSet->setTextureAttributeAndModes(0,texEnv.get(),osg::StateAttribute::ON); } SbColor ambient, diffuse, specular, emission; float shininess, transparency; // Get the material colors action->getMaterial(ambient, diffuse, specular, emission, shininess, transparency, 0); // Set transparency SbBool hasTextureTransparency = FALSE; if (ivTexture) { SbVec2s tmp; int bpp; if (ivTexture->isOfType(SoTexture2::getClassTypeId())) ((SoTexture2*)ivTexture)->image.getValue(tmp, bpp);#ifdef __COIN__ else if (ivTexture->isOfType(SoVRMLImageTexture::getClassTypeId())) { const SbImage *img = ((SoVRMLImageTexture*)ivTexture)->getImage(); if (img) img->getValue(tmp, bpp); else bpp = 0; }#endif hasTextureTransparency = bpp==4 || bpp==2; } if (transparency > 0 || hasTextureTransparency) { osg::ref_ptr<osg::BlendFunc> transparency = new osg::BlendFunc; stateSet->setAttributeAndModes(transparency.get(), osg::StateAttribute::ON); // Enable depth sorting for transparent objects stateSet->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); } // Set linewidth
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -