📄 convertfromperformer.cpp
字号:
// case(PF_OFF): // default: osgStateSet->setMode(osg::StateSet::WIREFRAME,osg::StateAttribute::OFF);break; // } // } // redundent in OSG's implementation of texmat mode // if (inherit & PFSTATE_ENTEXMAT) osgStateSet->setMode(osg::StateSet::TEXMAT,osg::StateAttribute::INHERIT); // else // { // int mode = geostate->getMode(PFSTATE_ENTEXMAT); // switch(mode) // { // case(PF_ON): osgStateSet->setMode(osg::StateSet::TEXMAT,osg::StateAttribute::ON);break; // case(PF_OFF): // default: osgStateSet->setMode(osg::StateSet::TEXMAT,osg::StateAttribute::OFF);break; // } // } // commenting out the following block since the TexGen should be set // appropriately by the osg::TexGen block below. // if (inherit & PFSTATE_ENTEXGEN) osgStateSet->setMode(osg::StateSet::TEXGEN,osg::StateAttribute::INHERIT); // else // { // int mode = geostate->getMode(PFSTATE_ENTEXGEN); // switch(mode) // { // case(PF_ON): osgStateSet->setMode(osg::StateSet::TEXGEN,osg::StateAttribute::ON);break; // case(PF_OFF): // default: osgStateSet->setMode(osg::StateSet::TEXGEN,osg::StateAttribute::OFF);break; // } // } // pfMaterial* front_mat = (pfMaterial*)geostate->getAttr(PFSTATE_FRONTMTL); pfMaterial* back_mat = (pfMaterial*)geostate->getAttr(PFSTATE_BACKMTL); visitMaterial(osgStateSet,front_mat,back_mat); pfTexture* tex = (pfTexture*)geostate->getAttr(PFSTATE_TEXTURE); visitTexture(osgStateSet,tex); pfTexEnv* texenv = (pfTexEnv*)geostate->getAttr(PFSTATE_TEXENV); if(texenv) { osg::TexEnv* osgTexEnv = new osg::TexEnv(); int mode = texenv->getMode(); float r,g,b,a; texenv->getBlendColor(&r, &g, &b, &a); switch(mode) { case(PFTE_MODULATE) : osgTexEnv->setMode(osg::TexEnv::MODULATE); osgTexEnv->setColor(osg::Vec4(r,g,b,a)); break; case(PFTE_DECAL) : osgTexEnv->setMode(osg::TexEnv::DECAL); osgTexEnv->setColor(osg::Vec4(r,g,b,a)); break; case(PFTE_BLEND) : osgTexEnv->setMode(osg::TexEnv::BLEND); osgTexEnv->setColor(osg::Vec4(r,g,b,a)); break; case(PFTE_REPLACE) : osgTexEnv->setMode(osg::TexEnv::REPLACE); osgTexEnv->setColor(osg::Vec4(r,g,b,a)); break; case(PFTE_ADD) : osgTexEnv->setMode(osg::TexEnv::ADD); osgTexEnv->setColor(osg::Vec4(r,g,b,a)); break; default: osg::notify(osg::WARN) << "TexEnv Mode "<<mode<<" not currently supported by the OSG."<<std::endl; break; } osgStateSet->setTextureAttribute(0,osgTexEnv); } pfTexGen* texgen = (pfTexGen*)geostate->getAttr(PFSTATE_TEXGEN); if (texgen) { osg::TexGen* osgTexGen = new osg::TexGen(); int mode = texgen->getMode(PF_S); // should this follow setPlane block be within the following switch? float x, y, z, d; texgen->getPlane(PF_S, &x, &y, &z, &d); osgTexGen->setPlane(osg::TexGen::S, osg::Vec4(x,y,z,d)); texgen->getPlane(PF_T, &x, &y, &z, &d); osgTexGen->setPlane(osg::TexGen::T, osg::Vec4(x,y,z,d)); switch(mode) { case(PFTG_OBJECT_LINEAR) : osgTexGen->setMode(osg::TexGen::OBJECT_LINEAR); osgStateSet->setTextureAttribute(0,osgTexGen); osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); break; case(PFTG_EYE_LINEAR_IDENT) : osg::notify(osg::WARN) << "TexGen Mode PFTG_EYE_LINEAR_IDENT not currently supported by the OSG,"<<std::endl; osg::notify(osg::WARN) << " assuming osg::TexGen::EYE_LINEAR."<<std::endl; case(PFTG_EYE_LINEAR) : osgTexGen->setMode(osg::TexGen::EYE_LINEAR); osgStateSet->setTextureAttribute(0,osgTexGen); osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); break; case(PFTG_SPHERE_MAP) : osgTexGen->setMode(osg::TexGen::SPHERE_MAP); osgStateSet->setTextureAttribute(0,osgTexGen); osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_S,osg::StateAttribute::ON); osgStateSet->setTextureMode(0,GL_TEXTURE_GEN_T,osg::StateAttribute::ON); break; case(PFTG_OFF) : osgStateSet->setAssociatedTextureModes(0,osgTexGen,osg::StateAttribute::OFF); break; case(PFTG_OBJECT_DISTANCE_TO_LINE) : osg::notify(osg::WARN) << "TexGen Mode PFTG_OBJECT_DISTANCE_TO_LINE not currently supported by the OSG."<<std::endl; osgStateSet->setAssociatedTextureModes(0,osgTexGen,osg::StateAttribute::OFF); break; case(PFTG_EYE_DISTANCE_TO_LINE) : osg::notify(osg::WARN) << "TexGen Mode PFTG_EYE_DISTANCE_TO_LINE not currently supported by the OSG."<<std::endl; osgStateSet->setAssociatedTextureModes(0,osgTexGen,osg::StateAttribute::OFF); break; default: osg::notify(osg::WARN) << "TexGen Mode "<<mode<<" not currently supported by the OSG."<<std::endl; osgStateSet->setAssociatedTextureModes(0,osgTexGen,osg::StateAttribute::OFF); break; } } pfMatrix* texmat = (pfMatrix*)geostate->getAttr(PFSTATE_TEXMAT); if (texmat) { osg::Matrix osgMatrix((*texmat)[0][0],(*texmat)[0][1],(*texmat)[0][2],(*texmat)[0][3], (*texmat)[1][0],(*texmat)[1][1],(*texmat)[1][2],(*texmat)[1][3], (*texmat)[2][0],(*texmat)[2][1],(*texmat)[2][2],(*texmat)[2][3], (*texmat)[3][0],(*texmat)[3][1],(*texmat)[3][2],(*texmat)[3][3]); osg::TexMat* osgTexMat = new osg::TexMat(); osgTexMat->setMatrix(osgMatrix); osgStateSet->setTextureAttribute(0,osgTexMat); } return osgStateSet;}osg::Material* ConvertFromPerformer::visitMaterial(osg::StateSet* osgStateSet,pfMaterial* front_mat,pfMaterial* back_mat){ if (front_mat==NULL && back_mat==NULL) return NULL; osg::Material* osgMaterial = new osg::Material; if (osgStateSet) osgStateSet->setAttribute(osgMaterial); pfMaterial* material = NULL; if (front_mat==back_mat) material = front_mat; else if (back_mat==NULL) material = front_mat; else if (front_mat==NULL) material = back_mat; if (material) // single materials for front and back. { int colorMode = material->getColorMode(material->getSide()); switch(colorMode) { case(PFMTL_CMODE_AMBIENT_AND_DIFFUSE): osgMaterial->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE); break; case(PFMTL_CMODE_AMBIENT): osgMaterial->setColorMode(osg::Material::AMBIENT); break; case(PFMTL_CMODE_DIFFUSE): osgMaterial->setColorMode(osg::Material::DIFFUSE); break; case(PFMTL_CMODE_EMISSION): osgMaterial->setColorMode(osg::Material::EMISSION); break; case(PFMTL_CMODE_SPECULAR): osgMaterial->setColorMode(osg::Material::SPECULAR); break; case(PFMTL_CMODE_OFF): osgMaterial->setColorMode(osg::Material::OFF); break; } osgMaterial->setShininess(osg::Material::FRONT_AND_BACK,material->getShininess()); float a = material->getAlpha(); float r,g,b; material->getColor(PFMTL_AMBIENT,&r,&g,&b); osgMaterial->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a)); material->getColor(PFMTL_DIFFUSE,&r,&g,&b); osgMaterial->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a)); material->getColor(PFMTL_EMISSION,&r,&g,&b); osgMaterial->setEmission(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a)); material->getColor(PFMTL_SPECULAR,&r,&g,&b); osgMaterial->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(r,g,b,a)); } else // separate materials for front and back. { int colorMode = front_mat->getColorMode(front_mat->getSide()); switch(colorMode) { case(PFMTL_CMODE_AMBIENT_AND_DIFFUSE): osgMaterial->setColorMode(osg::Material::AMBIENT_AND_DIFFUSE); break; case(PFMTL_CMODE_AMBIENT): osgMaterial->setColorMode(osg::Material::AMBIENT); break; case(PFMTL_CMODE_DIFFUSE): osgMaterial->setColorMode(osg::Material::DIFFUSE); break; case(PFMTL_CMODE_EMISSION): osgMaterial->setColorMode(osg::Material::EMISSION); break; case(PFMTL_CMODE_SPECULAR): osgMaterial->setColorMode(osg::Material::SPECULAR); break; case(PFMTL_CMODE_OFF): osgMaterial->setColorMode(osg::Material::OFF); break; } float a; float r,g,b; // front material osgMaterial->setShininess(osg::Material::FRONT,front_mat->getShininess()); a = front_mat->getAlpha(); front_mat->getColor(PFMTL_AMBIENT,&r,&g,&b); osgMaterial->setAmbient(osg::Material::FRONT,osg::Vec4(r,g,b,a)); front_mat->getColor(PFMTL_DIFFUSE,&r,&g,&b); osgMaterial->setDiffuse(osg::Material::FRONT,osg::Vec4(r,g,b,a)); front_mat->getColor(PFMTL_EMISSION,&r,&g,&b); osgMaterial->setEmission(osg::Material::FRONT,osg::Vec4(r,g,b,a)); front_mat->getColor(PFMTL_SPECULAR,&r,&g,&b); osgMaterial->setSpecular(osg::Material::FRONT,osg::Vec4(r,g,b,a)); // back material osgMaterial->setShininess(osg::Material::BACK,back_mat->getShininess()); a = back_mat->getAlpha(); back_mat->getColor(PFMTL_AMBIENT,&r,&g,&b); osgMaterial->setAmbient(osg::Material::BACK,osg::Vec4(r,g,b,a)); back_mat->getColor(PFMTL_DIFFUSE,&r,&g,&b); osgMaterial->setDiffuse(osg::Material::BACK,osg::Vec4(r,g,b,a)); back_mat->getColor(PFMTL_EMISSION,&r,&g,&b); osgMaterial->setEmission(osg::Material::BACK,osg::Vec4(r,g,b,a)); back_mat->getColor(PFMTL_SPECULAR,&r,&g,&b); osgMaterial->setSpecular(osg::Material::BACK,osg::Vec4(r,g,b,a)); } return osgMaterial;}static osg::Texture2D::FilterMode getTexfilter(int filter, int pftype){ if (filter == PFTEX_MINFILTER) { if (pftype & PFTEX_LINEAR) return osg::Texture2D::NEAREST_MIPMAP_LINEAR; else if (pftype & PFTEX_BILINEAR) return osg::Texture2D::LINEAR_MIPMAP_NEAREST; else if (pftype & PFTEX_TRILINEAR) return osg::Texture2D::LINEAR_MIPMAP_LINEAR; return osg::Texture2D::NEAREST_MIPMAP_LINEAR; } else { // MAGFILTER // not quite sure what is supposed to be interpret the Peformer // filter modes here so will simple go with OpenGL default. return osg::Texture2D::LINEAR; }}osg::Texture2D* ConvertFromPerformer::visitTexture(osg::StateSet* osgStateSet,pfTexture* tex){ if (tex==NULL) return NULL; osg::Texture2D* osgTexture = dynamic_cast<osg::Texture2D*>(getOsgObject(tex)); if (osgTexture) { if (osgStateSet) osgStateSet->setTextureAttribute(0,osgTexture); return osgTexture; } osgTexture = new osg::Texture2D; registerPfObjectForOsgObject(tex, osgTexture); if (osgStateSet) osgStateSet->setTextureAttribute(0,osgTexture); int repeat_r = tex->getRepeat(PFTEX_WRAP_R); int repeat_s = tex->getRepeat(PFTEX_WRAP_S); int repeat_t = tex->getRepeat(PFTEX_WRAP_T); if (repeat_r==PFTEX_CLAMP) osgTexture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::CLAMP); else osgTexture->setWrap(osg::Texture2D::WRAP_R,osg::Texture2D::REPEAT); if (repeat_s==PFTEX_CLAMP) osgTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::CLAMP); else osgTexture->setWrap(osg::Texture2D::WRAP_S,osg::Texture2D::REPEAT); if (repeat_t==PFTEX_CLAMP) osgTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::CLAMP); else osgTexture->setWrap(osg::Texture2D::WRAP_T,osg::Texture2D::REPEAT); // filter#if 1 osgTexture->setFilter(osg::Texture2D::MIN_FILTER, getTexfilter(PFTEX_MINFILTER, tex->getFilter(PFTEX_MINFILTER))); osgTexture->setFilter(osg::Texture2D::MAG_FILTER, getTexfilter(PFTEX_MAGFILTER, tex->getFilter(PFTEX_MAGFILTER)));#endif // image std::string texName = tex->getName(); if (_saveImagesAsRGB) { std::string strippedName = osgDB::getStrippedName(texName); pfList* imgList = tex->getList(); if (imgList) { // save image list char buf[8]; for (int i = 0; i < imgList->getNum(); i++) { pfTexture* t = (pfTexture*) imgList->get(i); if (t) { snprintf(buf, sizeof(buf)-1, "_%04d", i); texName = _saveImageDirectory+strippedName+buf+".rgb"; t->saveFile(texName.c_str()); } } } else { // save single image texName = _saveImageDirectory+strippedName+".rgb"; tex->saveFile(texName.c_str()); } } if (!_saveAbsoluteImagePath) texName = osgDB::getSimpleFileName(texName); int s=0; int t=0; int r=0; int comp=0; unsigned int* imageData = NULL; tex->getImage(&imageData,&comp,&s,&t,&r); int internalFormat = comp; unsigned int pixelFormat = comp == 1 ? GL_LUMINANCE : comp == 2 ? GL_LUMINANCE_ALPHA : comp == 3 ? GL_RGB : comp == 4 ? GL_RGBA : (GLenum)-1; unsigned int dataType = GL_UNSIGNED_BYTE; // copy image data int size = s * t * r * comp; unsigned char* data = (unsigned char*) malloc(size); memcpy(data, imageData, size); osg::Image* image = new osg::Image; image->setFileName(texName.c_str()); image->setImage(s,t,r, internalFormat, pixelFormat, dataType,data, osg::Image::USE_MALLOC_FREE); osgTexture->setImage(image); return osgTexture;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -