00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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;
00047 };
00048
00049
00050 struct ParsingException : public ExtendedLoadingException {
00051 ParsingException(int line) : ExtendedLoadingException(line) {}
00052 };
00053
00054
00055 struct InvalidFaceException : public ExtendedLoadingException {
00056 InvalidFaceException(int line) : ExtendedLoadingException(line) {}
00057 };
00058
00059
00060
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
00080 D,
00081 F,
00082 G,
00083 ILLUM,
00084 KA,
00085 KD,
00086 KS,
00087 MAP_KD,
00088 MTLLIB,
00089 NEWMTL,
00090 O,
00091 S,
00092 TR,
00093 USEMTL,
00094 V,
00095 VN,
00096 VT,
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;
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
00118 std::vector<Vector3> m_vertices;
00119 std::vector<Vector3> m_normals;
00120 std::vector<Vector2> m_texcoords;
00121
00122
00123
00124 std::map<int, IMeshBuilder::FeatureId> m_global_vertex_id;
00125
00126 bool m_create_submesh;
00127 bool m_set_submesh_material;
00128 IMeshBuilder::FeatureId m_submesh_material_id;
00129 bool m_inside_submesh_def;
00130 bool m_inside_material_def;
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