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

objloader.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_OBJLOADER_H
00024 #define SHEEP_MESHIO_OBJLOADER_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 OBJLoader : 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         //! An invalid face (with less than 3 vertices) has been encountered.
00055         struct InvalidFaceException : public ExtendedLoadingException {
00056             InvalidFaceException(int line) : ExtendedLoadingException(line) {}
00057         };
00058 
00059         //! A polygon could not be triangulated, because it is either
00060         //! self-intersecting or not planar.
00061         struct TriangulationException : public ExtendedLoadingException {
00062             TriangulationException(int line) : ExtendedLoadingException(line) {}
00063         };
00064 
00065         OBJLoader();
00066 
00067         virtual void Load(
00068             const std::string &filename,
00069             IMeshBuilder &builder,
00070             int option_mask = DEFAULT_CONFIGURATION_BIT,
00071             ProgressMonitor *progmon = 0
00072         ) throw(LoadingException);
00073 
00074     private:
00075         enum symbol {
00076             END_OF_FILE = 256, END_OF_LINE,
00077             IDENTIFIER, INTEGER, DOUBLE,
00078 
00079             //! Keywords.
00080             D,          //!< Material transparency (same as TR).
00081             F,          //!< Face.
00082             G,          //!< Group name.
00083             ILLUM,      //!< Illumination model.
00084             KA,         //!< Material ambient color.
00085             KD,         //!< Material diffuse color.
00086             KS,         //!< Material specular color.
00087             MAP_KD,     //!< Material diffuse map.
00088             MTLLIB,     //!< Material library.
00089             NEWMTL,     //!< New material.
00090             O,          //!< Object name.
00091             S,          //!< Smoothing group.
00092             TR,         //!< Material transparency (same as D).
00093             USEMTL,     //!< Material name.
00094             V,          //!< Geometric vertex.
00095             VN,         //!< Vertex normal.
00096             VT,         //!< Texture vertex.
00097         };
00098 
00099         KeywordTable m_keyword_table;
00100 
00101         std::string m_path;
00102 
00103         IMeshBuilder::IGeometryBuilder *m_geombuilder;
00104         IMeshBuilder::IMaterialBuilder *m_matbuilder;
00105 
00106         int m_option_mask;
00107 
00108         ProgressMonitor *m_progmon; //!< May be 0.
00109 
00110         FILE *m_file;
00111         int m_line;
00112 
00113         typedef std::map<std::string, IMeshBuilder::FeatureId> string_to_feature_id_map;
00114 
00115         string_to_feature_id_map m_material_id;
00116 
00117         //! All features are stored in these vectors as the file is parsed.
00118         std::vector<Vector3> m_vertices;
00119         std::vector<Vector3> m_normals;
00120         std::vector<Vector2> m_texcoords;
00121 
00122         //! This vector is used to avoid inserting duplicate vertices into the current mesh.
00123         //! It is cleared each time a new mesh is created.
00124         std::map<int, IMeshBuilder::FeatureId> m_global_vertex_id;
00125 
00126         bool m_create_submesh;      //!< If true, create a new submesh before processing new faces.
00127         bool m_set_submesh_material;                    //!< If true, apply the material specified by 'm_material_id' to the new submesh.
00128         IMeshBuilder::FeatureId m_submesh_material_id;  //!< Id of the material to apply to the new submesh.
00129         bool m_inside_submesh_def;  //!< If true, we are currently in the middle of a submesh definition.
00130         bool m_inside_material_def; //!< If true, we are currently in the middle of a material definition.
00131 
00132         void parse_error();
00133 
00134         int look_ahead();
00135 
00136         void eat_leading_blanks();
00137         void eat_blanks();
00138         void eat_line();
00139 
00140         void accept_char(int ref);
00141         void accept_newline();
00142         int accept_integer();
00143         double accept_double();
00144         std::string accept_string();
00145 
00146         bool is_eol();
00147 
00148         void select_submesh();
00149 
00150         void parse_file();
00151 
00152         void parse_f_statement();
00153         void parse_mtllib_statement();
00154         void parse_usemtl_statement();
00155         void parse_v_statement();
00156         void parse_vt_statement();
00157         void parse_vn_statement();
00158 
00159         void load_material_file(const std::string &filename);
00160         void parse_material_file();
00161 
00162         void parse_ka_statement();
00163         void parse_kd_statement();
00164         void parse_ks_statement();
00165         void parse_map_kd_statement();
00166         void parse_newmtl_statement();
00167     };
00168 
00169 #include "objloader.inl"
00170 
00171 }
00172 
00173 #endif  // !SHEEP_MESHIO_OBJLOADER_H

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