Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

aseloader.h

Go to the documentation of this file.
00001 /*
00002     Sheep - A Rigid Body Dynamics Engine
00003     Copyright (C) 2001-2004 Francois Beaune
00004     Contact: http://toxicengine.sourceforge.net/
00005 
00006     This file is part of Sheep.
00007 
00008     Sheep is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     Sheep is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with Sheep; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 */
00022 
00023 #ifndef SHEEP_MESHIO_ASELOADER_H
00024 #define SHEEP_MESHIO_ASELOADER_H
00025 
00026 #include "imeshbuilder.h"
00027 #include "imeshloader.h"
00028 #include "keywordtable.h"
00029 
00030 #include <cassert>
00031 #include <cstdio>
00032 #include <map>
00033 #include <string>
00034 #include <vector>
00035 
00036 namespace sheep {
00037 
00038     class ProgressMonitor;
00039 
00040     class ASELoader : public IMeshLoader {
00041     public:
00042         struct ExtendedLoadingException : public LoadingException {
00043             ExtendedLoadingException(int line) :
00044                 m_line(line) {}
00045 
00046             const int m_line;   //!< The line at which the parse error occured.
00047         };
00048 
00049         //! File structure is broken.
00050         struct ParsingException : public ExtendedLoadingException {
00051             ParsingException(int line) : ExtendedLoadingException(line) {}
00052         };
00053 
00054         ASELoader();
00055 
00056         virtual void Load(
00057             const std::string &filename,
00058             IMeshBuilder &builder,
00059             int option_mask = DEFAULT_CONFIGURATION_BIT,
00060             ProgressMonitor *progmon = 0
00061         ) throw(LoadingException);
00062 
00063     private:
00064         enum symbol {
00065             END_OF_FILE = 256, IDENTIFIER,
00066             STRING, INTEGER, DOUBLE,
00067 
00068             //! Keywords.
00069             BITMAP, GEOMOBJECT, MAP_DIFFUSE, MATERIAL, MATERIAL_AMBIENT,
00070             MATERIAL_DIFFUSE, MATERIAL_LIST, MATERIAL_NAME, MATERIAL_REF,
00071             MATERIAL_SPECULAR, MESH, MESH_FACE, MESH_FACE_LIST,
00072             MESH_FACENORMAL, MESH_MTLID, MESH_NORMALS, MESH_SMOOTHING,
00073             MESH_TFACE, MESH_TFACELIST, MESH_TVERT, MESH_TVERTLIST, MESH_VERTEX,
00074             MESH_VERTEX_LIST, MESH_VERTEXNORMAL, NODE_NAME, SUBMATERIAL,
00075             WIREFRAME_COLOR,
00076 
00077             //! Unknown keyword.
00078             UNKNOWN_KEYWORD
00079         };
00080 
00081         struct look_ahead {
00082             int m_sym;
00083             struct m_val_u {
00084                 std::string m_str;  //!< For IDENTIFIER and STRING.
00085                 int m_int;          //!< For INTEGER.
00086                 double m_dbl;       //!< For DOUBLE.
00087             } m_val;
00088         };
00089 
00090         KeywordTable m_keyword_table;
00091 
00092         std::string m_path;
00093 
00094         IMeshBuilder::IGeometryBuilder *m_geombuilder;
00095         IMeshBuilder::IMaterialBuilder *m_matbuilder;
00096 
00097         int m_option_mask;
00098 
00099         ProgressMonitor *m_progmon; //!< May be 0.
00100 
00101         FILE *m_file;
00102         int m_line;     //!< Current line number.
00103         look_ahead m_look_ahead;
00104 
00105         int m_geomobject_index; //!< This is used to number each *GEOMOBJECT, starting from 0.
00106 
00107         struct material_key {
00108             int m_material_index;
00109             int m_submaterial_index;
00110 
00111             material_key(int material_index, int submaterial_index) :
00112                 m_material_index(material_index),
00113                 m_submaterial_index(submaterial_index) {}
00114 
00115             bool operator<(const material_key &k) const {
00116                 if(m_material_index < k.m_material_index)
00117                     return true;
00118                 if(m_material_index > k.m_material_index)
00119                     return false;
00120                 return m_submaterial_index < k.m_submaterial_index;
00121             }
00122         };
00123 
00124         //! Material ID for each pair (material_index, submaterial_index).
00125         std::map<material_key, IMeshBuilder::FeatureId> m_mat_key_to_mat_id;
00126 
00127         //! This map associates to each material index the number of submaterials for
00128         //! the corresponding material.
00129         std::map<int, int> m_submaterial_count;
00130 
00131         struct mesh_data {
00132             //!< Structure of a triangular face.
00133             struct face {
00134                 int m_vertex_index[3];      //!< Face vertices (indices into the m_vertices vector).
00135                 int m_texcoord_index[3];    //!< Face texture coordinates (indices into the m_texcoords vector).
00136                 int m_normal_index[3];      //!< Face normals (indices into the m_normals vector).
00137                 std::vector<int> m_sg;      //!< Indices of each smooth group this face belongs to.
00138                 int m_submaterial_index;    //!< Submaterial index.
00139             };
00140 
00141             std::vector<Vector3> m_vertices;
00142             std::vector<Vector2> m_texcoords;
00143             std::vector<Vector3> m_normals;
00144             std::vector<face> m_faces;
00145 
00146             //! Value of the *MATERIAL_REF property, or -1 if this property has not been defined.
00147             int m_material_index;
00148 
00149             //! True if a color has been assigned to this mesh using the *WIREFRAME_COLOR keyword.
00150             bool m_has_wf_color;
00151 
00152             //! Color to use if the *MATERIAL_REF property was not defined and the *WIREFRAME_COLOR
00153             //! was defined.
00154             double m_wf_r, m_wf_g, m_wf_b;
00155 
00156             mesh_data() :
00157                 m_material_index(-1),   // no material assigned to this mesh yet
00158                 m_has_wf_color(false)   // no wireframe color
00159             {
00160                 // Wireframe color is left undefined.
00161             }
00162         };
00163 
00164         typedef std::vector<mesh_data *> mesh_data_vector;
00165 
00166         mesh_data_vector m_mesh_data;
00167 
00168         void parse_error();
00169 
00170         int read_char();
00171         void unread_char(int c);
00172         int look_ahead() const;
00173 
00174         void next();
00175 
00176         void accept(int token);
00177         std::string accept_string();
00178         int accept_integer();
00179         double accept_double();
00180 
00181         void skip_compound();
00182         void skip_statement();
00183 
00184         void parse_file();
00185 
00186         void parse_geomobject();
00187         void parse_map_diffuse();
00188         void parse_material(
00189             int material_index,
00190             bool is_submaterial = false,
00191             int submaterial_index = 0);
00192         void parse_material_list();
00193         void parse_mesh();
00194         void parse_mesh_face_list();
00195         void parse_mesh_normals();
00196         void parse_mesh_tfacelist();
00197         void parse_mesh_tvertlist();
00198         void parse_mesh_vertex_list();
00199 
00200         void dispatch_mesh_data();
00201     };
00202 
00203 #include "aseloader.inl"
00204 
00205 }
00206 
00207 #endif  // !SHEEP_MESHIO_ASELOADER_H

Generated on Tue May 11 01:31:49 2004 for toxic by doxygen 1.3.6