⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readerwriter3ds.cpp

📁 最新osg包
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                lib3ds_vector_transform(c,*matrix, m->pointL[i].pos);                (*osg_coords)[orig2NewMapping[i]].set(c[0],c[1],c[2]);            }            else            {                // original no transform code.                (*osg_coords)[orig2NewMapping[i]].set(m->pointL[i].pos[0],m->pointL[i].pos[1],m->pointL[i].pos[2]);            }        }    }    // create texture coords if needed.    if (m->texels>0)    {        if (m->texels==m->points)        {            osg::Vec2Array* osg_tcoords = new osg::Vec2Array(noVertex);            geom->setTexCoordArray(0,osg_tcoords);            for (i=0; i<m->texels; ++i)            {                if (orig2NewMapping[i]>=0) (*osg_tcoords)[orig2NewMapping[i]].set(m->texelL[i][0],m->texelL[i][1]);            }        }        else        {            osg::notify(osg::WARN)<<"Warning: in 3ds loader m->texels ("<<m->texels<<") != m->points ("<<m->points<<")"<< std::endl;        }    }    // create normals.    if (_usePerVertexNormals)    {        osg::Vec3Array* osg_normals = new osg::Vec3Array(noVertex);                // initialize normal list to zero's.        for (i=0; i<noVertex; ++i)        {            (*osg_normals)[i].set(0.0f,0.0f,0.0f);        }        for (fitr=faceList.begin();            fitr!=faceList.end();            ++fitr)        {            Lib3dsFace& face = m->faceL[*fitr];            (*osg_normals)[orig2NewMapping[face.points[0]]] += osg::Vec3(face.normal[0],face.normal[1],face.normal[2]);;            (*osg_normals)[orig2NewMapping[face.points[1]]] += osg::Vec3(face.normal[0],face.normal[1],face.normal[2]);;            (*osg_normals)[orig2NewMapping[face.points[2]]] += osg::Vec3(face.normal[0],face.normal[1],face.normal[2]);;        }        if (matrix)        {            osg::Matrix osg_matrix;            copyLib3dsMatrixToOsgMatrix(osg_matrix, *matrix);            for (i=0; i<noVertex; ++i)            {                (*osg_normals)[i] = osg::Matrix::transform3x3((*osg_normals)[i], osg_matrix);            }        }        // normalize the normal list to unit length normals.        for (i=0; i<noVertex; ++i)        {            (*osg_normals)[i].normalize();        }        geom->setNormalArray(osg_normals);        geom->setNormalBinding(osg::Geometry::BIND_PER_VERTEX);    }    else     {        osg::Vec3Array* osg_normals = new osg::Vec3Array(faceList.size());        osg::Vec3Array::iterator normal_itr = osg_normals->begin();        for (fitr=faceList.begin();            fitr!=faceList.end();            ++fitr)        {            Lib3dsFace& face = m->faceL[*fitr];            *(normal_itr++) =  osg::Vec3(face.normal[0],face.normal[1],face.normal[2]);        }        geom->setNormalArray(osg_normals);        geom->setNormalBinding(osg::Geometry::BIND_PER_PRIMITIVE);    }        osg::Vec4ubArray* osg_colors = new osg::Vec4ubArray(1);    (*osg_colors)[0].set(255,255,255,255);    geom->setColorArray(osg_colors);    geom->setColorBinding(osg::Geometry::BIND_OVERALL);        // create primitives    int numIndices = faceList.size()*3;    DrawElementsUShort* elements = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLES,numIndices);    DrawElementsUShort::iterator index_itr = elements->begin();    for (fitr=faceList.begin();        fitr!=faceList.end();        ++fitr)    {        Lib3dsFace& face = m->faceL[*fitr];        *(index_itr++) = orig2NewMapping[face.points[0]];        *(index_itr++) = orig2NewMapping[face.points[1]];        *(index_itr++) = orig2NewMapping[face.points[2]];    }       geom->addPrimitiveSet(elements);#if 0    osgUtil::TriStripVisitor tsv;    tsv.stripify(*geom);#endif    return geom;}osg::Texture2D*  ReaderWriter3DS::ReaderObject::createTexture(Lib3dsTextureMap *texture,const char* label,bool& transparancy, const osgDB::ReaderWriter::Options* options){    if (texture && *(texture->name))    {        std::string fileName = osgDB::findFileInDirectory(texture->name,_directory,osgDB::CASE_INSENSITIVE);        if (fileName.empty())         {            // file not found in .3ds file's directory, so we'll look in the datafile path list.            fileName = osgDB::findDataFile(texture->name,options, osgDB::CASE_INSENSITIVE);        }                if (fileName.empty())        {            osg::notify(osg::WARN) << "texture '"<<texture->name<<"' not found"<< std::endl;            return NULL;        }        if (label) osg::notify(osg::DEBUG_INFO) << label;        else osg::notify(osg::DEBUG_INFO) << "texture name";        osg::notify(osg::DEBUG_INFO) << " '"<<texture->name<<"'"<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    texture flag        "<<texture->flags<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_DECALE       "<<((texture->flags)&LIB3DS_DECALE)<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_MIRROR       "<<((texture->flags)&LIB3DS_MIRROR)<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_NEGATE       "<<((texture->flags)&LIB3DS_NEGATE)<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_NO_TILE      "<<((texture->flags)&LIB3DS_NO_TILE)<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_SUMMED_AREA  "<<((texture->flags)&LIB3DS_SUMMED_AREA)<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_ALPHA_SOURCE "<<((texture->flags)&LIB3DS_ALPHA_SOURCE)<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_TINT         "<<((texture->flags)&LIB3DS_TINT)<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_IGNORE_ALPHA "<<((texture->flags)&LIB3DS_IGNORE_ALPHA)<< std::endl;        osg::notify(osg::DEBUG_INFO) << "    LIB3DS_RGB_TINT     "<<((texture->flags)&LIB3DS_RGB_TINT)<< std::endl;        osg::Image* osg_image = osgDB::readImageFile(fileName.c_str());        if (osg_image==NULL)        {            osg::notify(osg::NOTICE) << "Warning: Cannot create texture "<<texture->name<< std::endl;            return NULL;        }        osg::Texture2D* osg_texture = new osg::Texture2D;        osg_texture->setImage(osg_image);        // does the texture support transparancy?        transparancy = ((texture->flags)&LIB3DS_ALPHA_SOURCE)!=0;        // what is the wrap mode of the texture.        osg::Texture2D::WrapMode wm = ((texture->flags)&LIB3DS_NO_TILE) ?                osg::Texture2D::CLAMP :                osg::Texture2D::REPEAT;        osg_texture->setWrap(osg::Texture2D::WRAP_S,wm);        osg_texture->setWrap(osg::Texture2D::WRAP_T,wm);        osg_texture->setWrap(osg::Texture2D::WRAP_R,wm);                                 // bilinear.        osg_texture->setFilter(osg::Texture2D::MIN_FILTER,osg::Texture2D::LINEAR_MIPMAP_NEAREST);        return osg_texture;    }    else        return NULL;}osg::StateSet* ReaderWriter3DS::ReaderObject::createStateSet(Lib3dsMaterial *mat, const osgDB::ReaderWriter::Options* options){    if (mat==NULL) return NULL;    osg::StateSet* stateset = new osg::StateSet;    osg::Material* material = new osg::Material;    float transparency = mat->transparency;    float alpha = 1.0f-transparency;    osg::Vec4 ambient(mat->ambient[0],mat->ambient[1],mat->ambient[2],alpha);    osg::Vec4 diffuse(mat->diffuse[0],mat->diffuse[1],mat->diffuse[2],alpha);    osg::Vec4 specular(mat->specular[0],mat->specular[1],mat->specular[2],alpha);    specular *= mat->shin_strength;    float shininess = mat->shininess;    material->setName(mat->name);    material->setAmbient(osg::Material::FRONT_AND_BACK,ambient);    material->setDiffuse(osg::Material::FRONT_AND_BACK,diffuse);    material->setSpecular(osg::Material::FRONT_AND_BACK,specular);    material->setShininess(osg::Material::FRONT_AND_BACK,shininess*128.0f);    stateset->setAttribute(material);    bool textureTransparancy=false;    osg::Texture2D* texture1_map = createTexture(&(mat->texture1_map),"texture1_map",textureTransparancy, options);    if (texture1_map)    {        stateset->setTextureAttributeAndModes(0,texture1_map,osg::StateAttribute::ON);                if (!textureTransparancy)        {                    // from an email from Eric Hamil, September 30, 2003.            // According to the 3DS spec, and other            // software (like Max, Lightwave, and Deep Exploration) a 3DS material that has            // a non-white diffuse base color and a 100% opaque bitmap texture, will show the            // texture with no influence from the base color.                        // so we'll override material back to white.            // and no longer require the decal hack below...#if 0            // Eric original fallback            osg::Vec4 white(1.0f,1.0f,1.0f,alpha);            material->setAmbient(osg::Material::FRONT_AND_BACK,white);            material->setDiffuse(osg::Material::FRONT_AND_BACK,white);            material->setSpecular(osg::Material::FRONT_AND_BACK,white);#else            // try alternative to avoid saturating with white            // setting white as per OpenGL defaults.            material->setAmbient(osg::Material::FRONT_AND_BACK,osg::Vec4(0.2f,0.2f,0.2f,alpha));            material->setDiffuse(osg::Material::FRONT_AND_BACK,osg::Vec4(0.8f,0.8f,0.8f,alpha));            material->setSpecular(osg::Material::FRONT_AND_BACK,osg::Vec4(0.0f,0.0f,0.0f,alpha));#endif                    }        // no longer required...        //         bool decal = false;//         //         // not sure exactly how to interpret what is best for .3ds//         // but the default text env MODULATE doesn't work well, and//         // DECAL seems to work better.//         osg::TexEnv* texenv = new osg::TexEnv;//         if (decal)//         {//             texenv->setMode(osg::TexEnv::DECAL);//         }//         else//         {//             texenv->setMode(osg::TexEnv::MODULATE);//         }//        stateset->setTextureAttribute(0,texenv);    }    if (transparency>0.0f || textureTransparancy)    {        stateset->setMode(GL_BLEND,osg::StateAttribute::ON);        stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);    }/*    osg::ref_ptr<osg::Texture> texture1_mask = createTexture(&(mat->texture1_mask),"texture1_mask",textureTransparancy);    osg::ref_ptr<osg::Texture> texture2_map = createTexture(&(mat->texture2_map),"texture2_map",textureTransparancy);    osg::ref_ptr<osg::Texture> texture2_mask = createTexture(&(mat->texture2_mask),"texture2_mask",textureTransparancy);    osg::ref_ptr<osg::Texture> opacity_map = createTexture(&(mat->opacity_map),"opacity_map",textureTransparancy);    osg::ref_ptr<osg::Texture> opacity_mask = createTexture(&(mat->opacity_mask),"opacity_mask",textureTransparancy);    osg::ref_ptr<osg::Texture> bump_map = createTexture(&(mat->bump_map),"bump_map",textureTransparancy);    osg::ref_ptr<osg::Texture> bump_mask = createTexture(&(mat->bump_mask),"bump_mask",textureTransparancy);    osg::ref_ptr<osg::Texture> specular_map = createTexture(&(mat->specular_map),"specular_map",textureTransparancy);    osg::ref_ptr<osg::Texture> specular_mask = createTexture(&(mat->specular_mask),"specular_mask",textureTransparancy);    osg::ref_ptr<osg::Texture> shininess_map = createTexture(&(mat->shininess_map),"shininess_map",textureTransparancy);    osg::ref_ptr<osg::Texture> shininess_mask = createTexture(&(mat->shininess_mask),"shininess_mask",textureTransparancy);    osg::ref_ptr<osg::Texture> self_illum_map = createTexture(&(mat->self_illum_map),"self_illum_map",textureTransparancy);    osg::ref_ptr<osg::Texture> self_illum_mask = createTexture(&(mat->self_illum_mask),"self_illum_mask",textureTransparancy);    osg::ref_ptr<osg::Texture> reflection_map = createTexture(&(mat->reflection_map),"reflection_map",textureTransparancy);    osg::ref_ptr<osg::Texture> reflection_mask = createTexture(&(mat->reflection_mask),"reflection_mask",textureTransparancy);*/    return stateset;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -