diff --git a/src/osgPlugins/lwo/CMakeLists.txt b/src/osgPlugins/lwo/CMakeLists.txt index 0af1240c8..26fdf0a56 100644 --- a/src/osgPlugins/lwo/CMakeLists.txt +++ b/src/osgPlugins/lwo/CMakeLists.txt @@ -1,11 +1,9 @@ -SET(TARGET_SRC +SET(TARGET_SRC Block.cpp Clip.cpp Converter.cpp Object.cpp old_lw.cpp - old_Lwo2.cpp - old_Lwo2Layer.cpp Polygon.cpp ReaderWriterLWO.cpp Surface.cpp @@ -26,8 +24,6 @@ SET(TARGET_H lwo2types.h Object.h old_lw.h - old_Lwo2.h - old_Lwo2Layer.h Polygon.h Surface.h Tessellator.h diff --git a/src/osgPlugins/lwo/ReaderWriterLWO.cpp b/src/osgPlugins/lwo/ReaderWriterLWO.cpp index 16f58e938..5bd5b08f2 100644 --- a/src/osgPlugins/lwo/ReaderWriterLWO.cpp +++ b/src/osgPlugins/lwo/ReaderWriterLWO.cpp @@ -43,7 +43,6 @@ #include "VertexMap.h" #include "old_lw.h" -#include "old_Lwo2.h" class ReaderWriterLWO : public osgDB::ReaderWriter { @@ -72,18 +71,12 @@ public: ReadResult result = readNode_LWO1(fileName,local_opt.get()); if (result.success()) return result; - if (!options || options->getOptionString() != "USE_OLD_READER") { - result = readNode_LWO2(fileName, local_opt.get()); - if (result.success()) return result; - } - - return readNode_old_LWO2(fileName, local_opt.get()); + return readNode_LWO2(fileName, local_opt.get()); } lwosg::Converter::Options parse_options(const Options *options) const; virtual ReadResult readNode_LWO2(const std::string& fileName, const osgDB::ReaderWriter::Options*) const; - virtual ReadResult readNode_old_LWO2(const std::string& fileName, const osgDB::ReaderWriter::Options*) const; virtual ReadResult readNode_LWO1(const std::string& fileName, const osgDB::ReaderWriter::Options*) const; protected: @@ -143,21 +136,6 @@ osgDB::ReaderWriter::ReadResult ReaderWriterLWO::readNode_LWO2(const std::string } -osgDB::ReaderWriter::ReadResult ReaderWriterLWO::readNode_old_LWO2(const std::string& fileName, const osgDB::ReaderWriter::Options*) const -{ - std::auto_ptr lwo2(new Lwo2()); - if (lwo2->ReadFile(fileName)) - { - osg::ref_ptr group = new osg::Group(); - if (lwo2->GenerateGroup(*group)) return group.release(); - } - return ReadResult::FILE_NOT_HANDLED; -} - - - - - // collect all the data relavent to a particular osg::Geometry being created. struct GeometryCollection { diff --git a/src/osgPlugins/lwo/old_Lwo2.cpp b/src/osgPlugins/lwo/old_Lwo2.cpp deleted file mode 100644 index ef061538f..000000000 --- a/src/osgPlugins/lwo/old_Lwo2.cpp +++ /dev/null @@ -1,810 +0,0 @@ -/* - * Lightwave Object version 2 loader for Open Scene Graph - * Version 2 introduced in Lightwave v6.0 - * - * Copyright (C) 2002 Pavel Moloshtan - * - * 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 -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include -#include -#include - -#include "old_Lwo2.h" -#include "old_Lwo2Layer.h" -#include "lwo2read.h" - -// makes 4-byte integer tag from four chars -// used in IFF standard - -unsigned 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; -} - -const unsigned long tag_FORM = make_id("FORM"); -const unsigned long tag_LWO2 = make_id("LWO2"); -const unsigned long tag_LAYR = make_id("LAYR"); -const unsigned long tag_TAGS = make_id("TAGS"); -const unsigned long tag_PNTS = make_id("PNTS"); -const unsigned long tag_VMAP = make_id("VMAP"); -const unsigned long tag_VMAD = make_id("VMAD"); -const unsigned long tag_TXUV = make_id("TXUV"); -const unsigned long tag_POLS = make_id("POLS"); -const unsigned long tag_FACE = make_id("FACE"); -const unsigned long tag_PTAG = make_id("PTAG"); -const unsigned long tag_SURF = make_id("SURF"); -const unsigned long tag_CLIP = make_id("CLIP"); -const unsigned long tag_BLOK = make_id("BLOK"); -const unsigned long tag_IMAP = make_id("IMAP"); -const unsigned long tag_IMAG = make_id("IMAG"); -const unsigned long tag_COLR = make_id("COLR"); - -#if 0 -const unsigned long tag_STIL = make_id("STIL"); -const unsigned long tag_TMAP = make_id("TMAP"); -#endif - -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 ) -{ - OSG_INFO << "Opening file: " << filename << std::endl; - - _fin.open(filename.c_str(), ios::in | ios::binary ); - if (!_fin.is_open()) - { - OSG_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_uint() != tag_FORM) - { - OSG_INFO << "File '" << filename << "' is not IFF format file." << std::endl; - _fin.close(); - return false; - } - else - { - OSG_INFO << "Detected EA-IFF85 format" << std::endl; - } - - unsigned int form_size = _read_uint(); - OSG_INFO << "Form size: " << form_size << std::endl; - - // checking LWO2 format - // http://www.lightwave3d.com/developer/75lwsdk/docs/filefmts/lwo2.html - if (_read_uint() != tag_LWO2) - { - unsigned long make_id(const char*); - OSG_INFO << "File '" << filename << "' is not LWO2 format file." << std::endl; - _fin.close(); - return false; - } - else - { - OSG_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_uint(); - current_tag_size = _read_uint(); - 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(c); -} - -unsigned int -Lwo2::_read_uint() -{ - 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() -{ - return lwo2::changeType4(_read_uint()); -} - -// 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 out - -void -Lwo2::_print_tag(unsigned int tag, unsigned int size) { - OSG_DEBUG << "Found tag " - << char(tag >> 24) - << char(tag >> 16) - << char(tag >> 8) - << char(tag) - << " size " << size << " bytes" - << std::endl; -} - -// print 4-char type -void -Lwo2::_print_type(unsigned int type) { - OSG_DEBUG << " type \t" - << char(type >> 24) - << char(type >> 16) - << char(type >> 8) - << char(type) - << std::endl; -} - -// read TAGS info - -void -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); - - OSG_DEBUG << " name \t'" << name.c_str() << "'" << std::endl; - } -} - -// read LAYR info - -void 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 info - -void Lwo2::_read_points(unsigned long size) -{ - int count = size / 12; - OSG_DEBUG << " 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 info - -void Lwo2::_read_vertex_mapping(unsigned long size) -{ - unsigned int type = _read_uint(); - size -= 4; - - _print_type(type); - - short dimension = _read_short(); - size -= 2; - - OSG_DEBUG << " dimension \t" << dimension << std::endl; - - string name; - _read_string(name); - size -= name.length() + name.length() % 2; - OSG_DEBUG << " 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 - OSG_DEBUG << " skipping..." << std::endl; - _fin.seekg(size + size % 2, ios::cur); - } -} - -// read POLS info - -void -Lwo2::_read_polygons(unsigned long size) -{ - unsigned int type = _read_uint(); - 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); - } - } - else - { - - // not recognized yet - OSG_DEBUG << " skipping..." << std::endl; - _fin.seekg(size + size % 2, ios::cur); - } -} - -// read PTAG info - -void Lwo2::_read_polygon_tag_mapping(unsigned long size) -{ - unsigned int type = _read_uint(); - 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 - OSG_DEBUG << " skipping..." << std::endl; - _fin.seekg(size + size % 2, ios::cur); - } -} - -// read VMAD info - -void Lwo2::_read_polygons_mapping(unsigned long size) -{ - unsigned int type = _read_uint(); - size -= 4; - - _print_type(type); - - short dimension = _read_short(); - size -= 2; - - OSG_DEBUG << " dimension \t" << dimension << std::endl; - - string name; - _read_string(name); - size -= name.length() + name.length() % 2; - OSG_DEBUG << " name \t'" << name.c_str() << "'" << std::endl; - - if (type == tag_TXUV && dimension == 2) - { - OSG_DEBUG << " polygons mappings:" << endl; - OSG_DEBUG << "\tpoint\tpolygon\ttexcoord" << endl; - OSG_DEBUG << "\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(); - - OSG_DEBUG << " \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 - OSG_DEBUG << " skipping..." << std::endl; - _fin.seekg(size + size % 2, ios::cur); - } - -} - -// read CLIP info - -void -Lwo2::_read_image_definition(unsigned long size) -{ - unsigned int index = _read_uint(); - size -= 4; - OSG_DEBUG << " index \t" << index << std::endl; - - unsigned int type; - while (size > 0) - { - type = _read_uint(); - 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(); - - OSG_DEBUG << " name \t'" << name.c_str() << "'" << std::endl; - } -} - -// read SURF info - -void 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; - OSG_DEBUG << " name \t'" << surface->name.c_str() << "'" << std::endl; - - string source; - _read_string(source); - size -= source.length() + source.length() % 2; - OSG_DEBUG << " 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_uint(); - 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_uint(); - blok_size -= 4; - current_tag_size = _read_short(); - blok_size -= 2; - OSG_DEBUG << " "; - _print_tag(current_tag_name, current_tag_size); - - if (current_tag_name == tag_IMAG) - { - surface->image_index = _read_short(); - OSG_DEBUG << " 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; - OSG_DEBUG << " ordinal \t'" << ordinal.c_str() << "'" << std::endl; - - while(imap_size > 0) - { - current_tag_name = _read_uint(); - imap_size -= 4; - current_tag_size = _read_short(); - imap_size -= 2; - OSG_DEBUG << " "; - _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); - OSG_DEBUG << " 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 file - -bool -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(); - - OSG_DEBUG << "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++) - { - OSG_DEBUG << " 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(_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->setColorArray(colors, osg::Array::BIND_OVERALL); - } - } - } - - group.addChild(geode); - } - - return true; -} - -// generate StateSets for each surface -void -Lwo2::_generate_statesets_from_surfaces() -{ - ref_ptr blending = new BlendFunc(); - blending->setFunction(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); - ref_ptr 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; - - OSG_DEBUG << "\tcreating surface " << (*itr_surf).first << std::endl; - - // check if exist texture image for this surface - if (surface->image_index >= 0) - { - osg::ref_ptr image = osgDB::readRefImageFile(_images[surface->image_index]); - OSG_DEBUG << "\tloaded image '" << _images[surface->image_index] << "'" << std::endl; - OSG_DEBUG << "\tresult - " << image << std::endl; - if (image.valid()) - { - // create texture - Texture2D* texture = new osg::Texture2D; - texture->setImage(image.get()); - 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; - } -} diff --git a/src/osgPlugins/lwo/old_Lwo2.h b/src/osgPlugins/lwo/old_Lwo2.h deleted file mode 100644 index 94c910182..000000000 --- a/src/osgPlugins/lwo/old_Lwo2.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - * Lightwave Object version 2 loader for Open Scene Graph - * Version 2 introduced in Lightwave v6.0 - * - * Copyright (C) 2002 Pavel Moloshtan - * - * 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/ - */ - -#ifndef LWO2_H -#define LWO2_H 1 - -#include -#include -#include - -#include -#include -#include -#include -#include -#include - -#include - -using namespace osg; -using namespace std; - -class Lwo2Layer; -struct Lwo2Surface; -struct Lwo2PolygonMapping; - -typedef vector< string >::iterator IteratorString; -typedef map< int, Lwo2Layer* >::iterator IteratorLayers; -typedef map< string, Lwo2Surface* >::iterator IteratorSurfaces; -typedef pair< const short, Lwo2PolygonMapping > PairVMAD; - -class Lwo2 -{ - public: - Lwo2(); - ~Lwo2(); - bool ReadFile( const string& filename ); - bool GenerateGroup( Group& ); - - private: - map< int, Lwo2Layer* > _layers; - map< string, Lwo2Surface* > _surfaces; - Lwo2Layer* _current_layer; - vector< string > _tags; - vector< string > _images; - osgDB::ifstream _fin; - - unsigned char _read_char(); - unsigned short _read_short(); - unsigned int _read_uint(); - float _read_float(); - string& _read_string(string&); - - bool _successfully_read; - - void _print_tag(unsigned int, unsigned int); - void _print_type(unsigned int); - - void _read_tag_strings(unsigned long); - void _read_layer(unsigned long); - void _read_points(unsigned long); - void _read_vertex_mapping(unsigned long); - void _read_polygons(unsigned long); - void _read_polygons_mapping(unsigned long); - void _read_polygon_tag_mapping(unsigned long); - void _read_image_definition(unsigned long); - void _read_surface(unsigned long); - - // generate StateSets for each surface - void _generate_statesets_from_surfaces(); -}; - -#endif diff --git a/src/osgPlugins/lwo/old_Lwo2Layer.cpp b/src/osgPlugins/lwo/old_Lwo2Layer.cpp deleted file mode 100644 index 3fd53a944..000000000 --- a/src/osgPlugins/lwo/old_Lwo2Layer.cpp +++ /dev/null @@ -1,444 +0,0 @@ -/* - * Lightwave Object version 2 loader for Open Scene Graph - * Version 2 introduced in Lightwave v6.0 - * - * Copyright (C) 2002 Pavel Moloshtan - * - * 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 "old_Lwo2Layer.h" -#include - -Lwo2Layer::Lwo2Layer(): - _number(0), - _flags(0), - _parent(0) -{ -} - -Lwo2Layer::~Lwo2Layer() -{ -} - - -void -Lwo2Layer::notify(NotifySeverity severity) -{ - OSG_NOTIFY(severity) << "Current layer: " << _number << endl; - OSG_NOTIFY(severity) << " flags \t" << _flags << endl; - OSG_NOTIFY(severity) << " pivot \t" << _pivot << endl; - OSG_NOTIFY(severity) << " name: \t'" << _name.c_str() << "'" << endl; - OSG_NOTIFY(severity) << " parent:\t" << _parent << endl; - - // points - OSG_NOTIFY(severity) << " points:\t" << _points.size() << endl; - OSG_NOTIFY(severity) << "\tcoord\t\t\t\ttexcoord" << endl; - OSG_NOTIFY(severity) << "\t=====\t\t\t\t========" << endl; - IteratorPoint itr; - for (itr = _points.begin(); itr != _points.end(); itr++) - { - OSG_NOTIFY(severity) << " \t" << (*itr).coord << "\t\t" << (*itr).texcoord << endl; - } - - // polygons - OSG_NOTIFY(severity) << " polygons:\t" << _polygons.size() << endl; - OSG_NOTIFY(severity) << "\tcoord\t\t\t\ttexcoord" << endl; - OSG_NOTIFY(severity) << "\t=====\t\t\t\t========" << endl; - IteratorPolygonsList polygon_iterator; - int polygon_index = 0; - for (polygon_iterator = _polygons.begin(); polygon_iterator != _polygons.end(); polygon_iterator++, polygon_index++) - { - OSG_NOTIFY(severity) << " \t" << polygon_index << " ("<< (*polygon_iterator).size() << " vertexes" << "):" << endl; - for (itr = (*polygon_iterator).begin(); itr != (*polygon_iterator).end(); itr++) - { - OSG_NOTIFY(severity) << " \t" << (*itr).coord << "\t\t" << (*itr).texcoord << endl; - } - OSG_NOTIFY(severity) << endl; - } - - // polygons tags - OSG_NOTIFY(severity) << " polygons tags:\t" << _polygons_tag.size() << endl; - IteratorShort short_itr; - for (short_itr = _polygons_tag.begin(); short_itr != _polygons_tag.end(); short_itr++) - { - OSG_NOTIFY(severity) << "\t" << (*short_itr) << endl; - } -} - -void -Lwo2Layer::GenerateGeode( Geode& geode, short tags_count, DrawableToTagMapping& tag_mapping) -{ - OSG_DEBUG; - - // variable used to track using textures - bool have_texture_coords; - - // create diffirent geomerty for each tag - for (short current_tag = 0; current_tag < tags_count; current_tag++) - { - have_texture_coords = false; - - // new geometry - ref_ptr geometry = new Geometry; - - // create coords array - ref_ptr coords = new Vec3Array; - - // create texture array - ref_ptr texcoords = new Vec2Array; - - // selecting polygons for current layer only - int polygon_index = 0; - PolygonsList polygons; - IteratorPolygonsList polygon_iterator; - for (polygon_iterator = _polygons.begin(); polygon_iterator != _polygons.end(); polygon_iterator++, polygon_index++) - { - // *polygon_iterator it's a PolygonsList - - // polygons of current tag only - if (_polygons_tag[polygon_index] == current_tag) - { - - // reset point_index member for later comparing poins data - PointsList points_list = *polygon_iterator; - for (unsigned int i = 0; i < points_list.size(); i++) - { - points_list[i].point_index = 0; - } - - polygons.push_back(*polygon_iterator); - } - } - - // find and compose triangle fans - PolygonsList triangle_fans; - _find_triangle_fans(polygons, triangle_fans); - - // find and compose triangle strips - PolygonsList triangle_strips; - _find_triangle_strips(polygons, triangle_strips); - - // polygons of current layer - polygon_index = 0; - for (polygon_iterator = polygons.begin(); polygon_iterator != polygons.end(); polygon_iterator++, polygon_index++) - { - if ((*polygon_iterator)[0].point_index != -1) - { - // all points of polygon - for (IteratorPoint itr = (*polygon_iterator).begin(); itr != (*polygon_iterator).end(); itr++) - { - // *itr - it's a PointData - - // polygons data - (*coords).push_back((*itr).coord); - (*texcoords).push_back((*itr).texcoord); - - if ((*itr).texcoord.x() != -1.0f || (*itr).texcoord.y() != -1.0f) - { - have_texture_coords = true; - } - } - - unsigned int points_start = (*coords).size() - (*polygon_iterator).size(); - unsigned int points_count = (*polygon_iterator).size(); - if (points_count == 3) - { - geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLES, points_start, points_count)); - } - else if (points_count == 4) - { - geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::QUADS, points_start, points_count)); - } - else - { - geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::POLYGON, points_start, points_count)); - } - } - } - - // triangle fans of current layer - polygon_index = 0; - for (polygon_iterator = triangle_fans.begin(); polygon_iterator != triangle_fans.end(); polygon_iterator++, polygon_index++) - { - - // all points of polygon - for (IteratorPoint itr = (*polygon_iterator).begin(); itr != (*polygon_iterator).end(); itr++) - { - // *itr - it's a PointData - - // polygons data - (*coords).push_back((*itr).coord); - (*texcoords).push_back((*itr).texcoord); - - if ((*itr).texcoord.x() != -1.0f || (*itr).texcoord.y() != -1.0f) - { - have_texture_coords = true; - } - } - - unsigned int points_start = (*coords).size() - (*polygon_iterator).size(); - unsigned int points_count = (*polygon_iterator).size(); - geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLE_FAN, points_start, points_count)); - } - - // triangle strips of current layer - polygon_index = 0; - for (polygon_iterator = triangle_strips.begin(); polygon_iterator != triangle_strips.end(); polygon_iterator++, polygon_index++) - { - - // all points of polygon - for (IteratorPoint itr = (*polygon_iterator).begin(); itr != (*polygon_iterator).end(); itr++) - { - // *itr - it's a PointData - - // polygons data - (*coords).push_back((*itr).coord); - (*texcoords).push_back((*itr).texcoord); - - if ((*itr).texcoord.x() != -1.0f || (*itr).texcoord.y() != -1.0f) - { - have_texture_coords = true; - } - } - - unsigned int points_start = (*coords).size() - (*polygon_iterator).size(); - unsigned int points_count = (*polygon_iterator).size(); - geometry->addPrimitiveSet(new DrawArrays(PrimitiveSet::TRIANGLE_STRIP, points_start, points_count)); - } - - // add geometry if it contains any points - if (coords->size() != 0) - { - geometry->setVertexArray(coords.get()); - - // assign texture array - if (have_texture_coords) - { - geometry->setTexCoordArray(0, texcoords.get()); - } - - // generate normals - osgUtil::SmoothingVisitor smoother; - smoother.smooth(*(geometry.get())); - - geode.addDrawable(geometry.get()); - - OSG_DEBUG << " inserting tag " << geode.getNumDrawables() - 1 << ":" << current_tag << std::endl; - tag_mapping.insert(PairDrawableToTag(geode.getNumDrawables() - 1, current_tag)); - } - } -} - -bool -Lwo2Layer::_find_triangle_fans(PolygonsList& polygons, PolygonsList& triangle_fans) -{ - bool found = false; - - while (_find_triangle_fan(polygons, triangle_fans)) - { - found = true; - } - - if (triangle_fans.size() > 0) - { - OSG_INFO << "LWO2 loader, optimizing: found " << triangle_fans.size() << " triangle fans" << endl; - } - - return found; -} - -bool -Lwo2Layer::_find_triangle_strips(PolygonsList& polygons, PolygonsList& triangle_strips) -{ - bool found = false; - - while (_find_triangle_strip(polygons, triangle_strips)) - { - found = true; - } - - if (triangle_strips.size() > 0) - { - OSG_INFO << "LWO2 loader, optimizing: found " << triangle_strips.size() << " triangle strips" << endl; - } - - return found; -} - -bool -Lwo2Layer::_find_triangle_fan(PolygonsList& polygons, PolygonsList& triangle_fans) -{ - bool found = false; - IteratorPolygonsList polygon_iterator = polygons.begin(); - while (polygon_iterator != polygons.end()) - { - PointsList& points_list = *polygon_iterator; - if (points_list.size() == 3 && points_list[0].point_index != -1) - { - PointData a = points_list[0]; - PointData b = points_list[1]; - PointData c = points_list[2]; - - int next_polygon_index = _find_triangle_begins_with(polygons, a, c); - while (next_polygon_index >= 0) - { - found = true; - PointData d = polygons[next_polygon_index][2]; - - PointsList point_list; - point_list.push_back(a); - point_list.push_back(b); - point_list.push_back(c); - point_list.push_back(d); - - // delete second triangle (mark as deleted) - (*(polygons.begin() + next_polygon_index))[0].point_index = -1; - - // delete current (first) triangle (mark as deleted) - (*polygon_iterator)[0].point_index = -1; - - c = d; - while ((next_polygon_index = _find_triangle_begins_with(polygons, a, c)) >= 0) - { - d = polygons[next_polygon_index][2]; - point_list.push_back(d); - - // delete next triangle (mark as deleted) - (*(polygons.begin() + next_polygon_index))[0].point_index = -1; - - c = d; - } - - triangle_fans.push_back(point_list); - } - } - polygon_iterator++; - } - return found; -} - -bool -Lwo2Layer::_find_triangle_strip(PolygonsList& polygons, PolygonsList& triangle_strips) -{ - bool found = false; - IteratorPolygonsList polygon_iterator = polygons.begin(); - int polygon_index = 0; - while (polygon_iterator != polygons.end()) - { - PointsList& points_list = *polygon_iterator; - if (points_list.size() == 3 && points_list[0].point_index != -1) - { - PointData a = points_list[0]; - PointData b = points_list[1]; - PointData c = points_list[2]; - - int next_polygon_index = _find_triangle_begins_with(polygons, c, b); - - while (next_polygon_index >= 0) - { - found = true; - PointData d = polygons[next_polygon_index][2]; - - PointsList point_list; - point_list.push_back(a); - point_list.push_back(b); - point_list.push_back(c); - point_list.push_back(d); - - // delete second triangle (mark as deleted) - (*(polygons.begin() + next_polygon_index))[0].point_index = -1; - - // delete current (first) triangle (mark as deleted) - (*polygon_iterator)[0].point_index = -1; - - PointData strip_a = c; - PointData strip_b = d; - bool current_strip_a = true; - - while ((next_polygon_index = _find_triangle_begins_with(polygons, strip_a, strip_b)) >= 0) - { - d = polygons[next_polygon_index][2]; - point_list.push_back(d); - - if (current_strip_a) - { - strip_a = d; - } - else - { - strip_b = d; - } - current_strip_a = !current_strip_a; - - // delete next triangle (mark as deleted) - (*(polygons.begin() + next_polygon_index))[0].point_index = -1; - } - - triangle_strips.push_back(point_list); - } - } - polygon_iterator++; - polygon_index++; - } - return found; -} - -int -Lwo2Layer::_find_triangle_begins_with(PolygonsList& polygons, PointData& a, PointData& b) -{ - int result = -1; - - int polygon_index = 0; - for (IteratorPolygonsList polygon_iterator = polygons.begin(); polygon_iterator != polygons.end(); polygon_iterator++, polygon_index++) - { - PointsList& points_list = *polygon_iterator; - if (points_list.size() == 3 && points_list[0].point_index != -1) - { - if (points_list[0] == a && points_list[1] == b) - { - result = polygon_index; - break; - } - else if (points_list[1] == a && points_list[2] == b) - { - // shift points - PointData temp = points_list[0]; - points_list[0] = points_list[1]; - points_list[1] = points_list[2]; - points_list[2] = temp; - - result = polygon_index; - break; - } - else if (points_list[2] == a && points_list[0] == b) - { - // shift points - PointData temp = points_list[2]; - points_list[2] = points_list[1]; - points_list[1] = points_list[0]; - points_list[0] = temp; - - result = polygon_index; - break; - } - } - } - return result; -} diff --git a/src/osgPlugins/lwo/old_Lwo2Layer.h b/src/osgPlugins/lwo/old_Lwo2Layer.h deleted file mode 100644 index d4979b88d..000000000 --- a/src/osgPlugins/lwo/old_Lwo2Layer.h +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Lightwave Object version 2 loader for Open Scene Graph - * Version 2 introduced in Lightwave v6.0 - * - * Copyright (C) 2002 Pavel Moloshtan - * - * 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/ - */ - -#ifndef LWO2LAYER_H -#define LWO2LAYER_H - -#include -#include -#include -#include -#include -#include - -#include - -#include -#include -#include - -using namespace osg; -using namespace std; - -struct PointData -{ - PointData(): - point_index(0), - coord(Vec3(0.0f, 0.0f, 0.0f)), - texcoord(Vec2(-1.0f, -1.0f)) {} - - short point_index; - Vec3 coord; - Vec2 texcoord; - - inline bool operator == (const PointData& p) const - { - return coord == p.coord && texcoord == p.texcoord; - } - -}; - -struct Lwo2Surface -{ - Lwo2Surface(): - image_index(-1), - state_set(0) {} - - short image_index; - string name; - Vec3 color; - StateSet* state_set; -}; - -typedef vector< PointData > PointsList; -typedef vector< PointsList > PolygonsList; -typedef PolygonsList::iterator IteratorPolygonsList; - -typedef map< int, int > DrawableToTagMapping; -typedef pair< int, int > PairDrawableToTag; - -typedef vector< PointData >::iterator IteratorPoint; -typedef vector< Vec2 >::iterator IteratorVec2; -typedef vector< short >::iterator IteratorShort; - -class Lwo2Layer -{ - public: - friend class Lwo2; - Lwo2Layer(); - ~Lwo2Layer(); - void notify(NotifySeverity); - void GenerateGeode( Geode&, short, DrawableToTagMapping& ); - - private: - bool _find_triangle_fans(PolygonsList&, PolygonsList&); - bool _find_triangle_fan(PolygonsList&, PolygonsList&); - bool _find_triangle_strips(PolygonsList&, PolygonsList&); - bool _find_triangle_strip(PolygonsList&, PolygonsList&); - int _find_triangle_begins_with(PolygonsList&, PointData&, PointData&); - - short _number; - short _flags; - short _parent; - Vec3 _pivot; - string _name; - vector< PointData > _points; - PolygonsList _polygons; - vector< short > _polygons_tag; -}; - -#endif