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

📄 old_lwo2.cpp

📁 最新osg包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        }    }    else     {        // not recognized yet        notify(DEBUG_INFO) << "  skipping..." << std::endl;        _fin.seekg(size + size % 2, ios::cur);    }}// read PTAG infovoid Lwo2::_read_polygon_tag_mapping(unsigned long size) {    unsigned int type = _read_long();    size -= 4;    _print_type(type);    if (type == tag_SURF)     {        int count = size / 4;        _current_layer->_polygons_tag.resize(count);        short polygon_index;        short tag_index;        while (count--)        {            polygon_index = _read_short();            tag_index = _read_short();            _current_layer->_polygons_tag[polygon_index] = tag_index;        }    }    else     {        // not recognized yet        notify(DEBUG_INFO) << "  skipping..." << std::endl;        _fin.seekg(size + size % 2, ios::cur);    }}// read VMAD infovoid Lwo2::_read_polygons_mapping(unsigned long size) {    unsigned int type = _read_long();    size -= 4;    _print_type(type);    short dimension = _read_short();    size -= 2;    notify(DEBUG_INFO) << "  dimension \t" << dimension << std::endl;    string name;    _read_string(name);    size -= name.length() + name.length() % 2;     notify(DEBUG_INFO) << "  name   \t'" << name.c_str() << "'" << std::endl;    if (type == tag_TXUV && dimension == 2)     {        notify(DEBUG_INFO) << "  polygons mappings:" << endl;        notify(DEBUG_INFO) << "\tpoint\tpolygon\ttexcoord" <<  endl;        notify(DEBUG_INFO) << "\t=====\t=======\t========" <<  endl;        int count = size / 12;        short point_index;        short polygon_index;        float u;        float v;        while (count--)        {            point_index = _read_short();            polygon_index = _read_short();            u = _read_float();            v = _read_float();            notify(DEBUG_INFO) << "    \t" << point_index << "\t" << polygon_index << "\t" << Vec2(u, v) << endl;            // apply texture coordinates             PointsList& points_list = _current_layer->_polygons[polygon_index];            for (unsigned int i = 0; i < points_list.size(); i++)            {                if (points_list[i].point_index == point_index)                {                    points_list[i].texcoord = Vec2(u, v);                }            }        }    }    else     {        // not recognized yet        notify(DEBUG_INFO) << "  skipping..." << std::endl;        _fin.seekg(size + size % 2, ios::cur);    }}// read CLIP infovoid Lwo2::_read_image_definition(unsigned long size){    unsigned int index = _read_long();    size -= 4;    notify(DEBUG_INFO) << "  index  \t" << index << std::endl;    unsigned int type;    while (size > 0)    {        type = _read_long();        size -= 4;        _print_type(type);        // size of name        // not included in specification ??        _read_short();        size -= 2;        string name;        _read_string(name);        size -= name.length() + name.length() % 2;         if (index + 1 > _images.size())        {          _images.resize(index + 1);        }                _images[index] = name.c_str();        notify(DEBUG_INFO) << "  name   \t'" << name.c_str() << "'" << std::endl;    }}// read SURF infovoid Lwo2::_read_surface(unsigned long size){    Lwo2Surface* surface = new Lwo2Surface();    surface->image_index = -1;    surface->state_set = NULL;    _read_string(surface->name);    size -= surface->name.length() + surface->name.length() % 2;     notify(DEBUG_INFO) << "  name   \t'" << surface->name.c_str() << "'" << std::endl;    string source;    _read_string(source);    size -= source.length() + source.length() % 2;     notify(DEBUG_INFO) << "  source   \t'" << source.c_str() << "'" << std::endl;    unsigned long current_tag_name;    unsigned short current_tag_size;    while (size > 0 && !_fin.eof())     {        current_tag_name = _read_long();        size -= 4;        current_tag_size = _read_short();        size -= 2;        _print_tag(current_tag_name, current_tag_size);        if (current_tag_name == tag_BLOK)         {            // BLOK            int blok_size = current_tag_size;            size -= blok_size;            while (blok_size > 0)             {                current_tag_name = _read_long();                blok_size -= 4;                current_tag_size = _read_short();                blok_size -= 2;                notify(DEBUG_INFO) << "  ";                _print_tag(current_tag_name, current_tag_size);                if (current_tag_name == tag_IMAG)                {                    surface->image_index = _read_short();                    notify(DEBUG_INFO) << "    image index\t" << surface->image_index << std::endl;                    blok_size -= 2;                }                else if (current_tag_name == tag_IMAP)                 {                    // IMAP                    int imap_size = current_tag_size;                    blok_size -= imap_size;                    string ordinal;                    _read_string(ordinal);                    imap_size -= ordinal.length() + ordinal.length() % 2;                     notify(DEBUG_INFO) << "    ordinal   \t'" << ordinal.c_str() << "'" << std::endl;                    while(imap_size > 0)                    {                        current_tag_name = _read_long();                        imap_size -= 4;                        current_tag_size = _read_short();                        imap_size -= 2;                        notify(DEBUG_INFO) << "    ";                        _print_tag(current_tag_name, current_tag_size);                        _fin.seekg(current_tag_size + current_tag_size % 2, ios::cur);                        imap_size -= current_tag_size + current_tag_size % 2;                    }                }                else                 {                    _fin.seekg(current_tag_size + current_tag_size % 2, ios::cur);                    blok_size -= current_tag_size + current_tag_size % 2;                }            }        }        else if (current_tag_name == tag_COLR)        {            float r = _read_float();            float g = _read_float();            float b = _read_float();            surface->color.set(r,g,b);            notify(DEBUG_INFO) << "  color   \t" << surface->color << std::endl;            current_tag_size -= 12;            size -= 12;            // skip ununderstooded envelope            _fin.seekg(current_tag_size + current_tag_size % 2, ios::cur);            size -= current_tag_size + current_tag_size % 2;        }        else         {          _fin.seekg(current_tag_size + current_tag_size % 2, ios::cur);          size -= current_tag_size + current_tag_size % 2;        }    }    _surfaces[surface->name] = surface;}// Generation OSG Geode object from parsed LWO2 filebool Lwo2::GenerateGroup( Group& group ){  if (!_successfully_read) return false;  // generate StateSets for each surface  _generate_statesets_from_surfaces();  // create geometry from all layers  for (IteratorLayers itr = _layers.begin(); itr != _layers.end(); itr++)    {      osg::Geode* geode = new osg::Geode();      notify(DEBUG_INFO) << "Generate geode for layer " << (*itr).first << std::endl;      DrawableToTagMapping tag_mapping;      (*itr).second->GenerateGeode(*geode, _tags.size(), tag_mapping);      // assign StateSet for each PTAG group      for (unsigned int i = 0; i < geode->getNumDrawables(); i++)        {          notify(DEBUG_INFO) << "  Assigning surface " << _tags[tag_mapping[i]] << " to drawable " << i << std::endl;          geode->getDrawable(i)->setStateSet(_surfaces[_tags[tag_mapping[i]]]->state_set);          // copy material color to color array of geometry          // because when lighting off color not applyed          Geometry* geometry = geode->getDrawable(i)->asGeometry();          if (geometry)            {              Material* material = dynamic_cast<Material*>(_surfaces[_tags[tag_mapping[i]]]->state_set->getAttribute(StateAttribute::MATERIAL));              if (material) {                Vec4Array* colors = new Vec4Array();                colors->push_back(material->getDiffuse(Material::FRONT_AND_BACK));                geometry->setColorBinding(Geometry::BIND_OVERALL);                geometry->setColorArray(colors);              }            }        }      group.addChild(geode);    }  return true;}// generate StateSets for each surfacevoidLwo2::_generate_statesets_from_surfaces(){    ref_ptr<BlendFunc> blending = new BlendFunc();    blending->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);    ref_ptr<CullFace> culling = new CullFace();    culling->setMode(CullFace::BACK);    for (IteratorSurfaces itr_surf = _surfaces.begin(); itr_surf != _surfaces.end(); itr_surf++)    {        Lwo2Surface* surface = (*itr_surf).second;        StateSet* state_set = new osg::StateSet;        bool use_blending = false;        notify(DEBUG_INFO) << "\tcreating surface " << (*itr_surf).first << std::endl;        // check if exist texture image for this surface        if (surface->image_index >= 0)         {            Image* image = osgDB::readImageFile(_images[surface->image_index]);            notify(DEBUG_INFO) << "\tloaded image '" << _images[surface->image_index] << "'" << std::endl;            notify(DEBUG_INFO) << "\tresult - " << image << std::endl;            if (image)            {                // create texture                Texture2D* texture = new osg::Texture2D;                texture->setImage(image);                state_set->setTextureAttributeAndModes(0, texture, StateAttribute::ON);                        // setup texture wrapping                texture->setWrap(Texture::WRAP_S, Texture::REPEAT);                texture->setWrap(Texture::WRAP_T, Texture::REPEAT);                // detect blending                if (image->getPixelSizeInBits() == 32)                {                    for (int i = 0; i < image->s(); i++)                    {                        for (int j = 0; j < image->t(); j++)                        {                            unsigned char* data = image->data(i, j);                            data++; // skip r                            data++; // skip g                            data++; // skip b                            // check alpha                            if (*data < 255)                             {                                use_blending = true;                                break;                            }                        }                        if (use_blending) break;                    }                }            }        }        // set color        Material* material = new Material();        material->setDiffuse(Material::FRONT_AND_BACK, Vec4(surface->color, 1.0f));        state_set->setAttribute(material);        state_set->setMode(GL_NORMALIZE, StateAttribute::ON);        if (use_blending)        {            // setup blending            state_set->setAttribute(blending.get());            state_set->setMode(GL_BLEND, StateAttribute::ON);            // setup depth sorting            state_set->setRenderingHint(StateSet::TRANSPARENT_BIN);        }        else        {            // setup culling            state_set->setAttribute(culling.get());            state_set->setMode(GL_CULL_FACE, StateAttribute::ON);        }        surface->state_set = state_set;    }}// makes 4-byte integer tag from four chars // used in IFF standardunsigned long make_id(const char* tag) {    unsigned long result = 0;    for (unsigned int i = 0; i < strlen(tag) && i < 4; i++)    {        result <<= 8;        result += int(tag[i]);    }    return result;}                   

⌨️ 快捷键说明

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