Changeset 8570

Show
Ignore:
Timestamp:
07/12/08 14:00:58
Author:
robert
Message:

From Doug McCorkle?, "Attached is patch that corrects/improves the following issues with the OBJ loader:

1. Added options to control wether the osgUtil::Tessellator or osgUtil::TriStripVisitor? are run. By default they still run just as before.
2. Added support for the Emissive material. The data was being read from the mtl file but was never being applied to the model.
3. This is the main bug addressed, when a model is read in with an alpha value specified like:

newmtl Material8

Ns 24
d 0.33
illum 2
Kd 0.204 0.204 0.204
Ks 0 0 0
Ka 0.153 0.153 0.153

where the alpha value is d. The loader would then overwrite the alpha value when reading the diffuse, specular, and ambient colors. I have changed all the material color readers to only set the values they read and to use the default colors specified in the constructor of the obj class. With these changes, the obj reader now handles opacity correctly if the alpha value is specified before the material colo"

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • OpenSceneGraph/trunk/src/osgPlugins/obj/ReaderWriterOBJ.cpp

    r8305 r8570  
    123123    osg::Geometry* convertElementListToGeometry(obj::Model& model, obj::Model::ElementList& elementList, bool& rotate) const; 
    124124     
    125     osg::Node* convertModelToSceneGraph(obj::Model& model, bool& rotate) const; 
     125    osg::Node* convertModelToSceneGraph(obj::Model& model, bool& rotate, 
     126        bool& noTesselateLargePolygons, bool& noTriStripPolygons) const; 
    126127 
    127128    inline osg::Vec3 transformVertex(const osg::Vec3& vec, const bool rotate) const ; 
     
    268269            osg_material->setAmbient(osg::Material::FRONT_AND_BACK,material.ambient); 
    269270            osg_material->setDiffuse(osg::Material::FRONT_AND_BACK,material.diffuse); 
     271            osg_material->setEmission(osg::Material::FRONT_AND_BACK,material.emissive); 
     272 
    270273            if (material.illum == 2) { 
    271274                osg_material->setSpecular(osg::Material::FRONT_AND_BACK,material.specular); 
     
    277280            if (material.ambient[3]!=1.0 || 
    278281                material.diffuse[3]!=1.0 || 
    279                 material.specular[3]!=1.0) 
     282                material.specular[3]!=1.0|| 
     283                material.emissive[3]!=1.0) 
    280284            { 
    281285                osg::notify(osg::INFO)<<"Found transparent material"<<std::endl; 
     
    293297        load_material_texture( model, material, stateset.get(), material.map_opacity,  TEXTURE_UNIT_OPACITY ); 
    294298         
     299        if (isTransparent) 
     300        { 
     301            stateset->setMode(GL_BLEND, osg::StateAttribute::ON); 
     302            stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); 
     303        } 
     304 
    295305        materialToStateSetMap[material.name] = stateset.get(); 
    296          
    297306    } 
    298307} 
     
    551560} 
    552561 
    553 osg::Node* ReaderWriterOBJ::convertModelToSceneGraph(obj::Model& model, bool& rotate) const 
     562osg::Node* ReaderWriterOBJ::convertModelToSceneGraph(obj::Model& model, 
     563    bool& rotate, bool& noTesselateLargePolygons, bool& noTriStripPolygons) const 
    554564{ 
    555565 
     
    580590         
    581591            // tesseleate any large polygons 
    582             osgUtil::Tessellator tessellator; 
    583             tessellator.retessellatePolygons(*geometry); 
    584  
     592            if (!noTesselateLargePolygons) 
     593            { 
     594                osgUtil::Tessellator tessellator; 
     595                tessellator.retessellatePolygons(*geometry); 
     596            } 
     597             
    585598            // tri strip polygons to improve graphics peformance 
    586             osgUtil::TriStripVisitor tsv; 
    587             tsv.stripify(*geometry); 
    588  
     599            if (!noTriStripPolygons) 
     600            { 
     601                osgUtil::TriStripVisitor tsv; 
     602                tsv.stripify(*geometry); 
     603            } 
     604             
    589605            // if no normals present add them. 
    590606            if (!geometry->getNormalArray() || geometry->getNormalArray()->getNumElements()==0) 
    591607            { 
    592                 osgUtil::SmoothingVisitor tsv; 
    593                 tsv.smooth(*geometry); 
     608                osgUtil::SmoothingVisitor sv; 
     609                sv.smooth(*geometry); 
    594610            } 
    595611 
     
    642658        model.readOBJ(fin, local_opt.get()); 
    643659         
    644         // code for checking the noRotation 
     660        // code for checking the nonRotation, noTesselateLargePolygons, 
     661        // and noTriStripPolygons 
    645662        bool rotate = true; 
    646         if ((options!=NULL) && (options->getOptionString() == "noRotation")) 
    647         { 
    648             rotate = false; 
    649         } 
    650  
    651         osg::Node* node = convertModelToSceneGraph(model,rotate); 
     663        bool noTesselateLargePolygons = false; 
     664        bool noTriStripPolygons = false; 
     665         
     666        if (options!=NULL) 
     667        { 
     668            if (options->getOptionString() == "noRotation") 
     669            { 
     670                rotate = false; 
     671            } 
     672             
     673            if (options->getOptionString() == "noTesselateLargePolygons") 
     674            { 
     675                noTesselateLargePolygons = true; 
     676            } 
     677             
     678            if (options->getOptionString() == "noTriStripPolygons") 
     679            { 
     680                noTesselateLargePolygons = true; 
     681            } 
     682        } 
     683         
     684        osg::Node* node = convertModelToSceneGraph(model,rotate, 
     685            noTesselateLargePolygons,noTriStripPolygons); 
    652686        return node; 
    653687    } 
     
    663697        model.readOBJ(fin, options); 
    664698         
    665         // code for checking the nonRotation 
     699        // code for checking the nonRotation, noTesselateLargePolygons, 
     700        // and noTriStripPolygons 
    666701        bool rotate = true; 
    667         if ((options!=NULL) && (options->getOptionString() == "noRotation")) 
    668         { 
    669             rotate = false; 
    670         } 
    671  
    672         osg::Node* node = convertModelToSceneGraph(model,rotate); 
     702        bool noTesselateLargePolygons = false; 
     703        bool noTriStripPolygons = false; 
     704         
     705        if (options!=NULL) 
     706        { 
     707            if (options->getOptionString() == "noRotation") 
     708            { 
     709                rotate = false; 
     710            } 
     711             
     712            if (options->getOptionString() == "noTesselateLargePolygons") 
     713            { 
     714                noTesselateLargePolygons = true; 
     715            } 
     716             
     717            if (options->getOptionString() == "noTriStripPolygons") 
     718            { 
     719                noTesselateLargePolygons = true; 
     720            } 
     721        } 
     722         
     723        osg::Node* node = convertModelToSceneGraph(model,rotate, 
     724            noTesselateLargePolygons,noTriStripPolygons); 
    673725        return node; 
    674726    } 
  • OpenSceneGraph/trunk/src/osgPlugins/obj/obj.cpp

    r8305 r8570  
    156156                    unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); 
    157157 
    158                     if (fieldsRead==1)      material->ambient.set(r,0.0f,0.0f,1.0f); 
    159                     else if (fieldsRead==2) material->ambient.set(r,g,0.0f,1.0f); 
    160                     else if (fieldsRead==3) material->ambient.set(r,g,b,1.0f); 
    161                     else if (fieldsRead==4) material->ambient.set(r,g,b,a); 
     158                    if (fieldsRead==1) 
     159                    { 
     160                        material->ambient[ 0 ] = r; 
     161                    } 
     162                    else if (fieldsRead==2) 
     163                    { 
     164                        material->ambient[ 0 ] = r; 
     165                        material->ambient[ 1 ] = g; 
     166                    } 
     167                    else if (fieldsRead==3) 
     168                    { 
     169                        material->ambient[ 0 ] = r; 
     170                        material->ambient[ 1 ] = g; 
     171                        material->ambient[ 2 ] = b; 
     172                    } 
     173                    else if (fieldsRead==4) 
     174                    { 
     175                        material->ambient[ 0 ] = r; 
     176                        material->ambient[ 1 ] = g; 
     177                        material->ambient[ 2 ] = b; 
     178                        material->ambient[ 3 ] = a; 
     179                    } 
    162180                } 
    163181                else if (strncmp(line,"Kd ",3)==0) 
     
    165183                    unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); 
    166184 
    167                     if (fieldsRead==1)      material->diffuse.set(r,0.0f,0.0f,1.0f); 
    168                     else if (fieldsRead==2) material->diffuse.set(r,g,0.0f,1.0f); 
    169                     else if (fieldsRead==3) material->diffuse.set(r,g,b,1.0f); 
    170                     else if (fieldsRead==4) material->diffuse.set(r,g,b,a); 
     185                    if (fieldsRead==1) 
     186                    { 
     187                        material->diffuse[ 0 ] = r; 
     188                    } 
     189                    else if (fieldsRead==2) 
     190                    { 
     191                        material->diffuse[ 0 ] = r; 
     192                        material->diffuse[ 1 ] = g; 
     193                    } 
     194                    else if (fieldsRead==3) 
     195                    { 
     196                        material->diffuse[ 0 ] = r; 
     197                        material->diffuse[ 1 ] = g; 
     198                        material->diffuse[ 2 ] = b; 
     199                    } 
     200                    else if (fieldsRead==4) 
     201                    { 
     202                        material->diffuse[ 0 ] = r; 
     203                        material->diffuse[ 1 ] = g; 
     204                        material->diffuse[ 2 ] = b; 
     205                        material->diffuse[ 3 ] = a; 
     206                    } 
    171207                } 
    172208                else if (strncmp(line,"Ks ",3)==0) 
     
    174210                    unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); 
    175211 
    176                     if (fieldsRead==1)      material->specular.set(r,0.0f,0.0f,1.0f); 
    177                     else if (fieldsRead==2) material->specular.set(r,g,0.0f,1.0f); 
    178                     else if (fieldsRead==3) material->specular.set(r,g,b,1.0f); 
    179                     else if (fieldsRead==4) material->specular.set(r,g,b,a); 
     212                    if (fieldsRead==1) 
     213                    { 
     214                        material->specular[ 0 ] = r; 
     215                    } 
     216                    else if (fieldsRead==2) 
     217                    { 
     218                        material->specular[ 0 ] = r; 
     219                        material->specular[ 1 ] = g; 
     220                    } 
     221                    else if (fieldsRead==3) 
     222                    { 
     223                        material->specular[ 0 ] = r; 
     224                        material->specular[ 1 ] = g; 
     225                        material->specular[ 2 ] = b; 
     226                    } 
     227                    else if (fieldsRead==4) 
     228                    { 
     229                        material->specular[ 0 ] = r; 
     230                        material->specular[ 1 ] = g; 
     231                        material->specular[ 2 ] = b; 
     232                        material->specular[ 3 ] = a; 
     233                    } 
    180234                } 
    181235                else if (strncmp(line,"Ke ",3)==0) 
     
    183237                    unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); 
    184238 
    185                     if (fieldsRead==1)      material->emissive.set(r,0.0f,0.0f,1.0f); 
    186                     else if (fieldsRead==2) material->emissive.set(r,g,0.0f,1.0f); 
    187                     else if (fieldsRead==3) material->emissive.set(r,g,b,1.0f); 
    188                     else if (fieldsRead==4) material->emissive.set(r,g,b,a); 
     239                    if (fieldsRead==1) 
     240                    { 
     241                        material->emissive[ 0 ] = r; 
     242                    } 
     243                    else if (fieldsRead==2) 
     244                    { 
     245                        material->emissive[ 0 ] = r; 
     246                        material->emissive[ 1 ] = g; 
     247                    } 
     248                    else if (fieldsRead==3) 
     249                    { 
     250                        material->emissive[ 0 ] = r; 
     251                        material->emissive[ 1 ] = g; 
     252                        material->emissive[ 2 ] = b; 
     253                    } 
     254                    else if (fieldsRead==4) 
     255                    { 
     256                        material->emissive[ 0 ] = r; 
     257                        material->emissive[ 1 ] = g; 
     258                        material->emissive[ 2 ] = b; 
     259                        material->emissive[ 3 ] = a; 
     260                    } 
    189261                } 
    190262                else if (strncmp(line,"Tf ",3)==0) 
     
    192264                    unsigned int fieldsRead = sscanf(line+3,"%f %f %f %f", &r, &g, &b, &a); 
    193265 
    194                     if (fieldsRead==1)      material->Tf.set(r,0.0f,0.0f,1.0f); 
    195                     else if (fieldsRead==2) material->Tf.set(r,g,0.0f,1.0f); 
    196                     else if (fieldsRead==3) material->Tf.set(r,g,b,1.0f); 
    197                     else if (fieldsRead==4) material->Tf.set(r,g,b,a); 
     266                    if (fieldsRead==1) 
     267                    { 
     268                        material->Tf[ 0 ] = r; 
     269                    } 
     270                    else if (fieldsRead==2) 
     271                    { 
     272                        material->Tf[ 0 ] = r; 
     273                        material->Tf[ 1 ] = g; 
     274                    } 
     275                    else if (fieldsRead==3) 
     276                    { 
     277                        material->Tf[ 0 ] = r; 
     278                        material->Tf[ 1 ] = g; 
     279                        material->Tf[ 2 ] = b; 
     280                    } 
     281                    else if (fieldsRead==4) 
     282                    { 
     283                        material->Tf[ 0 ] = r; 
     284                        material->Tf[ 1 ] = g; 
     285                        material->Tf[ 2 ] = b; 
     286                        material->Tf[ 3 ] = a; 
     287                    } 
    198288                } 
    199289                else if (strncmp(line,"sharpness ",10)==0)