Changeset 11290

Show
Ignore:
Timestamp:
03/25/10 15:19:01 (3 years ago)
Author:
robert
Message:

From Mathias Froehlich, "I added some features to the vrml plugin.

The plugin can now handle embeded PixelTexture? fields in addition to the
already implemented ImageTexture? fields.

Fixed a bug with texture repeat being applied to the wrong texture dimension.

Added handling for IndexedLineSet? geometries."

Location:
OpenSceneGraph/trunk/src/osgPlugins/vrml
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • OpenSceneGraph/trunk/src/osgPlugins/vrml/Primitives.cpp

    r10785 r11290  
    1414 
    1515#include <osg/CullFace> 
     16 
     17osg::ref_ptr<osg::Geometry> ReaderWriterVRML2::convertVRML97IndexedLineSet(openvrml::node *vrml_ifs) const 
     18{ 
     19    osg::ref_ptr<osg::Geometry> osg_geom = new osg::Geometry(); 
     20 
     21    osg_geom->addPrimitiveSet(new osg::DrawArrayLengths(osg::PrimitiveSet::LINE_STRIP)); 
     22 
     23    // get array of vertex coordinate_nodes 
     24    if(vrml_ifs->type().id() == "IndexedLineSet") 
     25    { 
     26        std::auto_ptr<openvrml::field_value> fv = vrml_ifs->field("coord"); 
     27        const openvrml::sfnode *sfn = dynamic_cast<const openvrml::sfnode *>(fv.get()); 
     28 
     29        openvrml::coordinate_node *vrml_coord_node = dynamic_cast<openvrml::coordinate_node *>((sfn->value()).get()); 
     30        const std::vector<openvrml::vec3f> &vrml_coord = vrml_coord_node->point(); 
     31 
     32        osg::ref_ptr<osg::Vec3Array> osg_vertices = new osg::Vec3Array(); 
     33 
     34        unsigned i; 
     35        for (i = 0; i < vrml_coord.size(); i++) 
     36        { 
     37            openvrml::vec3f vec = vrml_coord[i]; 
     38            osg_vertices->push_back(osg::Vec3(vec[0], vec[1], vec[2])); 
     39        } 
     40 
     41        osg_geom->setVertexArray(osg_vertices.get()); 
     42 
     43        // get array of vertex indices 
     44        std::auto_ptr<openvrml::field_value> fv2 = vrml_ifs->field("coordIndex"); 
     45        const openvrml::mfint32 *vrml_coord_index = dynamic_cast<const openvrml::mfint32 *>(fv2.get()); 
     46 
     47        osg::ref_ptr<osg::IntArray> osg_vert_index = new osg::IntArray(); 
     48 
     49        int num_vert = 0; 
     50        for (i = 0; i < vrml_coord_index->value().size(); i++) 
     51        { 
     52            int index = vrml_coord_index->value()[i]; 
     53            if (index == -1) 
     54            { 
     55                static_cast<osg::DrawArrayLengths*>(osg_geom->getPrimitiveSet(0))->push_back(num_vert); 
     56                num_vert = 0; 
     57            } 
     58            else 
     59            { 
     60                osg_vert_index->push_back(index); 
     61                ++num_vert; 
     62            } 
     63        } 
     64 
     65        if (num_vert) 
     66        { 
     67            //GvdB: Last coordIndex wasn't -1 
     68            static_cast<osg::DrawArrayLengths*>(osg_geom->getPrimitiveSet(0))->push_back(num_vert); 
     69        } 
     70 
     71        osg_geom->setVertexIndices(osg_vert_index.get()); 
     72    } 
     73 
     74    // get array of colours per vertex (if specified) 
     75    { 
     76        std::auto_ptr<openvrml::field_value> fv = vrml_ifs->field("color"); 
     77        const openvrml::sfnode *sfn = dynamic_cast<const openvrml::sfnode *>(fv.get()); 
     78        openvrml::color_node *vrml_color_node = dynamic_cast<openvrml::color_node *>(sfn->value().get()); 
     79 
     80        if (vrml_color_node != 0) // if no colors, node is NULL pointer 
     81        { 
     82            const std::vector<openvrml::color> &vrml_colors = vrml_color_node->color(); 
     83 
     84            osg::ref_ptr<osg::Vec3Array> osg_colors = new osg::Vec3Array(); 
     85 
     86            unsigned i; 
     87            for (i = 0; i < vrml_colors.size(); i++) 
     88            { 
     89                const openvrml::color color = vrml_colors[i]; 
     90                osg_colors->push_back(osg::Vec3(color.r(), color.g(), color.b())); 
     91            } 
     92            osg_geom->setColorArray(osg_colors.get()); 
     93 
     94            // get array of color indices 
     95            std::auto_ptr<openvrml::field_value> fv2 = vrml_ifs->field("colorIndex"); 
     96            const openvrml::mfint32 *vrml_color_index = dynamic_cast<const openvrml::mfint32 *>(fv2.get()); 
     97 
     98            osg::ref_ptr<osg::IntArray> osg_color_index = new osg::IntArray(); 
     99 
     100            if(vrml_color_index->value().size() > 0) 
     101            { 
     102                for (i = 0; i < vrml_color_index->value().size(); i++) 
     103                { 
     104                    int index = vrml_color_index->value()[i]; 
     105                    if (index != -1) { 
     106                        osg_color_index->push_back(index); 
     107                    } 
     108                } 
     109                osg_geom->setColorIndices(osg_color_index.get()); 
     110            } else 
     111                // unspecified, use coordIndices field 
     112                osg_geom->setColorIndices(osg_geom->getVertexIndices()); 
     113 
     114            // get color binding 
     115            std::auto_ptr<openvrml::field_value> fv3 = vrml_ifs->field("colorPerVertex"); 
     116            const openvrml::sfbool *vrml_color_per_vertex = dynamic_cast<const openvrml::sfbool *>(fv3.get()); 
     117 
     118            if (vrml_color_per_vertex->value()) 
     119            { 
     120                osg_geom->setColorBinding(osg::Geometry::BIND_PER_VERTEX); 
     121            } else 
     122            { 
     123                osg_geom->setColorBinding(osg::Geometry::BIND_PER_PRIMITIVE); 
     124            } 
     125        } 
     126    } 
     127 
     128    osg_geom->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF); 
     129 
     130    return osg_geom; 
     131} 
    16132 
    17133osg::ref_ptr<osg::Geometry> ReaderWriterVRML2::convertVRML97Box(openvrml::node* vrml_box) const 
  • OpenSceneGraph/trunk/src/osgPlugins/vrml/ReaderWriterVRML2.cpp

    r10785 r11290  
    316316                    osg_geom = convertVRML97IndexedFaceSet(node_ptr.get()); 
    317317 
     318                else if (node_ptr->type().id()=="IndexedLineSet") 
     319                    osg_geom = convertVRML97IndexedLineSet(node_ptr.get()); 
     320 
    318321                else if (node_ptr->type().id() == "Box") 
    319322                    osg_geom = convertVRML97Box(node_ptr.get()); 
     
    353356 
    354357                const boost::intrusive_ptr<openvrml::node> vrml_material_node = vrml_app->material(); 
    355                 const boost::intrusive_ptr<openvrml::node> vrml_texture_node = vrml_app->texture(); 
     358                const boost::intrusive_ptr<openvrml::texture_node> vrml_texture_node = openvrml::node_cast<openvrml::texture_node*>(vrml_app->texture().get()); 
    356359                const openvrml::material_node *vrml_material = dynamic_cast<const openvrml::material_node *>(vrml_material_node.get()); 
    357360 
     
    401404                if (vrml_texture_node != 0) 
    402405                { 
    403                     std::auto_ptr<openvrml::field_value> texture_url_fv = vrml_texture_node->field("url"); 
    404                     const openvrml::mfstring *mfs = dynamic_cast<const openvrml::mfstring *>(texture_url_fv.get()); 
    405                     const std::string &url = mfs->value()[0]; 
    406  
    407                     osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile(url); 
    408  
    409                     if (image != 0) 
     406                    osg::ref_ptr<osg::Image> image; 
     407 
     408                    if (vrml_texture_node->type().id() == "ImageTexture") 
     409                    { 
     410                        try 
     411                        { 
     412                            std::auto_ptr<openvrml::field_value> texture_url_fv = vrml_texture_node->field("url"); 
     413                            const openvrml::mfstring *mfs = dynamic_cast<const openvrml::mfstring *>(texture_url_fv.get()); 
     414                            const std::string &url = mfs->value()[0]; 
     415                             
     416                            image = osgDB::readRefImageFile(url); 
     417                             
     418                            if (!image.valid()) 
     419                            { 
     420                                std::cerr << "texture file " << url << " not found !" << std::endl << std::flush; 
     421                            } 
     422                        } 
     423                        catch (openvrml::unsupported_interface&) 
     424                        { 
     425                            // no url field in the texture 
     426                        } 
     427                    } 
     428 
     429                    if (!image.valid()) 
     430                    { 
     431                        // If we cannot read the image try the openvrml builtin mechanisms to read the image. 
     432                        // This includes PixelTexture fields. 
     433                        const openvrml::image& vrml_image = vrml_texture_node->image(); 
     434 
     435                        // Convert to an osg image 
     436                        image = new osg::Image; 
     437                        image->allocateImage(vrml_image.x(), vrml_image.y(), 1, GL_RGBA, GL_UNSIGNED_BYTE); 
     438                        for (std::size_t y = 0; y < vrml_image.y(); ++y) 
     439                        { 
     440                            for (std::size_t x = 0; x < vrml_image.x(); ++x) 
     441                            { 
     442                                openvrml::int32 p = vrml_image.pixel(x, y); 
     443                                unsigned char* data = image->data(x, y); 
     444                                data[0] = 0xff & (p >> 24); 
     445                                data[1] = 0xff & (p >> 16); 
     446                                data[2] = 0xff & (p >> 8); 
     447                                data[3] = 0xff & (p >> 0); 
     448                            } 
     449                        } 
     450                    } 
     451 
     452                    if (image.valid()) 
    410453                    { 
    411454                        osg::ref_ptr<osg::Texture2D> texture = new osg::Texture2D; 
    412455                        texture->setImage(image.get()); 
    413456 
    414                         // defaults 
    415                         texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); 
     457                        // get the real texture wrapping parameters 
     458                        if (vrml_texture_node->repeat_s()) 
     459                        { 
     460                            texture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT); 
     461                        } 
     462                        else 
     463                        { 
     464                            texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP); 
     465                        } 
     466 
     467                        if (vrml_texture_node->repeat_t()) 
     468                        { 
     469                            texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); 
     470                        } 
     471                        else 
     472                        { 
     473                            texture->setWrap(osg::Texture::WRAP_T, osg::Texture::CLAMP); 
     474                        } 
    416475                        texture->setWrap(osg::Texture::WRAP_R, osg::Texture::REPEAT); 
    417                         texture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT); 
    418  
    419                         // get the real texture wrapping parameters (if any) 
    420                         try 
    421                         { 
    422                             std::auto_ptr<openvrml::field_value> wrap_fv = vrml_texture_node->field("repeatS"); 
    423                             const openvrml::sfbool *sfb = dynamic_cast<const openvrml::sfbool *>(wrap_fv.get()); 
    424  
    425                             if (!sfb->value()) 
    426                                 texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP); 
    427  
    428                         } 
    429                         catch (...) 
    430                         { 
    431                             // nothing specified 
    432                         } 
    433  
    434                         try 
    435                         { 
    436                             std::auto_ptr<openvrml::field_value> wrap_fv = vrml_texture_node->field("repeatT"); 
    437                             const openvrml::sfbool *sfb = dynamic_cast<const openvrml::sfbool *>(wrap_fv.get()); 
    438  
    439                             if (!sfb->value()) 
    440                                 texture->setWrap(osg::Texture::WRAP_S, osg::Texture::CLAMP); 
    441                         } 
    442                         catch (...) 
    443                         { 
    444                             // nothing specified 
    445                         } 
    446476 
    447477                        osg_stateset->setTextureAttributeAndModes(0, texture.get()); 
    448                         //osg_stateset->setMode(GL_BLEND,osg::StateAttribute::ON);  //bhbn 
    449  
    450                     } 
    451                     else 
    452                     { 
    453                         std::cerr << "texture file " << url << " not found !" << std::endl << std::flush; 
     478 
     479                        if (image->isImageTranslucent()) { 
     480                            osg_stateset->setMode(GL_BLEND, osg::StateAttribute::ON); 
     481                            osg_stateset->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); 
     482                        } 
    454483                    } 
    455484                } 
  • OpenSceneGraph/trunk/src/osgPlugins/vrml/ReaderWriterVRML2.h

    r10795 r11290  
    8686 
    8787    osg::ref_ptr<osg::Geometry> convertVRML97IndexedFaceSet(openvrml::node *vrml_ifs) const; 
     88    osg::ref_ptr<osg::Geometry> convertVRML97IndexedLineSet(openvrml::node *vrml_ifs) const; 
    8889    osg::ref_ptr<osg::Geometry> convertVRML97Box(openvrml::node* vrml_box) const; 
    8990    osg::ref_ptr<osg::Geometry> convertVRML97Sphere(openvrml::node* vrml_sphere) const;