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

📄 old_lwo2.cpp

📁 最新osg包
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* * Lightwave Object version 2 loader for Open Scene Graph * Version 2 introduced in Lightwave v6.0 * * Copyright (C) 2002 Pavel Moloshtan <pasha@moloshtan.com> * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA * * The Open Scene Graph (OSG) is a cross platform C++/OpenGL library for  * real-time rendering of large 3D photo-realistic models.  * The OSG homepage is http://www.openscenegraph.org/ */#include <osg/Notify>#include <osg/Geode>#include <osg/Group>#include <osg/Texture2D>#include <osg/Material>#include <osg/CullFace>#include <osg/BlendFunc>#include <osg/io_utils>#include <osgDB/Registry>#include <osgDB/ReadFile>#include <iostream>#include <fstream>#include "old_Lwo2.h"#include "old_Lwo2Layer.h"Lwo2::Lwo2():  _current_layer(0),  _successfully_read(false){}Lwo2::~Lwo2(){  // delete all layers  for (IteratorLayers itr = _layers.begin(); itr != _layers.end(); itr++)    {      delete (*itr).second;    }  // delete all surfaces  for (IteratorSurfaces itr_surf = _surfaces.begin(); itr_surf != _surfaces.end(); itr_surf++)    {      delete (*itr_surf).second;    }}bool Lwo2::ReadFile( const string& filename ){    notify(INFO)  << "Opening file: " << filename << std::endl;    _fin.open(filename.c_str(), ios::in | ios::binary );    if (!_fin.is_open())    {        notify(INFO) << "Can't open file '" << filename << "'" << std::endl;        return false;    }    // checking EA-IFF85 format    // http://www.lightwave3d.com/developer/75lwsdk/docs/filefmts/eaiff85.html    if (_read_long() != tag_FORM)     {        notify(INFO) << "File '" << filename << "' is not IFF format file." << std::endl;        _fin.close();        return false;    }    else     {        notify(INFO) << "Detected EA-IFF85 format" << std::endl;    }    unsigned long form_size = _read_long();    notify(INFO) << "Form size: " << form_size << std::endl;    // checking LWO2 format     // http://www.lightwave3d.com/developer/75lwsdk/docs/filefmts/lwo2.html    if (_read_long() != tag_LWO2)     {        unsigned long make_id(const char*);        notify(INFO) << "File '" << filename << "' is not LWO2 format file." << std::endl;        _fin.close();        return false;    }    else     {        notify(INFO) << "Detected LWO2 format" << std::endl;    }    unsigned long read_bytes = 4;    unsigned long current_tag_name;    unsigned long current_tag_size;    // main loop for reading tags    while (read_bytes < form_size && !_fin.eof())    {        current_tag_name = _read_long();        current_tag_size = _read_long();        read_bytes += 8 + current_tag_size + current_tag_size % 2;        _print_tag(current_tag_name, current_tag_size);        if (current_tag_name == tag_TAGS)         {            _read_tag_strings(current_tag_size);        }         else if (current_tag_name == tag_LAYR)         {            _read_layer(current_tag_size);        }        else if (current_tag_name == tag_PNTS)         {            _read_points(current_tag_size);        }        else if (current_tag_name == tag_VMAP)         {            _read_vertex_mapping(current_tag_size);        }        else if (current_tag_name == tag_VMAD)         {            _read_polygons_mapping(current_tag_size);        }        else if (current_tag_name == tag_POLS)         {            _read_polygons(current_tag_size);        }        else if (current_tag_name == tag_PTAG)         {            _read_polygon_tag_mapping(current_tag_size);        }        else if (current_tag_name == tag_CLIP)         {            _read_image_definition(current_tag_size);        }        else if (current_tag_name == tag_SURF)         {            _read_surface(current_tag_size);        }         else         {            _fin.seekg(current_tag_size + current_tag_size % 2, ios::cur);        }    }    _fin.close();    return _successfully_read = true;}unsigned char Lwo2::_read_char(){  char c = 0;  if (_fin.is_open())    {      _fin.read(&c, 1);    }  return static_cast<unsigned char>(c);}unsigned long Lwo2::_read_long(){  return    (_read_char() << 24) |     (_read_char() << 16) |      (_read_char() <<  8) |     _read_char();}unsigned short Lwo2::_read_short(){  return    (_read_char() <<  8) |     _read_char();}float Lwo2::_read_float(){  unsigned long x = _read_long();  return *(float*)&x;}// read null terminated string string&Lwo2::_read_string(string& str){  char c;  do {    c = _read_char();    str += c;  } while (c != 0);  // if length of string (including \0) is odd skip another byte   if (str.length() % 2) {    _read_char();  }  return str;}// print 4-char tag to debug outvoid Lwo2::_print_tag(unsigned int tag, unsigned int size) {  notify(DEBUG_INFO) << "Found tag "                      << char(tag >> 24)                      << char(tag >> 16)                      << char(tag >>  8)                      << char(tag)                      << " size " << size << " bytes"                      << std::endl;}// print 4-char typevoid Lwo2::_print_type(unsigned int type) {  notify(DEBUG_INFO) << "  type   \t"                      << char(type >> 24)                      << char(type >> 16)                      << char(type >>  8)                      << char(type)                      << std::endl;}// read TAGS infovoid Lwo2::_read_tag_strings(unsigned long size){    while (size > 0)    {        string name;        _read_string(name);        size -= name.length() + name.length() % 2;         _tags.push_back(name);        notify(DEBUG_INFO) << "  name   \t'" << name.c_str() << "'" << std::endl;    }}// read LAYR infovoid Lwo2::_read_layer(unsigned long size) {    unsigned short number = _read_short();    size -= 2;    Lwo2Layer* layer = new Lwo2Layer();    _layers[number] = layer;    _current_layer = layer;    layer->_number = number;    layer->_flags = _read_short();    size -= 2;    float x = _read_float();    float y = _read_float();    float z = _read_float();    layer->_pivot.set(x, y, z);    size -= 4 * 3;    _read_string(layer->_name);    size -= layer->_name.length() + layer->_name.length() % 2;     if (size > 2)     {        layer->_parent = _read_short();        size -= 2;    }    _fin.seekg(size + size % 2, ios::cur);}// read PNTS infovoid Lwo2::_read_points(unsigned long size) {    int count = size / 12;    notify(DEBUG_INFO) << "  count \t" << count << std::endl;    while (count--)    {        PointData point;        float x = _read_float();        float y = _read_float();        float z = _read_float();        point.coord = Vec3(x, y, z);        _current_layer->_points.push_back(point);    } }// read VMAP infovoid Lwo2::_read_vertex_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)     {        int count = size / 10;        unsigned short n;        float u;        float v;        while (count--)        {            n = _read_short();            u = _read_float();            v = _read_float();            // point coords must be read previously            if (n < _current_layer->_points.size())            {                _current_layer->_points[n].texcoord = Vec2(u, v);            }        }    }    else     {        // not recognized yet        notify(DEBUG_INFO) << "  skipping..." << std::endl;        _fin.seekg(size + size % 2, ios::cur);    }}// read POLS infovoid Lwo2::_read_polygons(unsigned long size) {    unsigned int type = _read_long();    size -= 4;    _print_type(type);    if (type == tag_FACE)     {        unsigned short vertex_count;        while (size > 0)        {            PointData point;            vertex_count = _read_short() & 0x03FF;            size -= 2;            PointsList points_list;            while (vertex_count--)            {                unsigned short point_index = _read_short();                point = _current_layer->_points[point_index];                point.point_index = point_index;                points_list.push_back(point);                size -= 2;            }                 _current_layer->_polygons.push_back(points_list);

⌨️ 快捷键说明

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