cgv
Loading...
Searching...
No Matches
obj_reader.h
1#pragma once
2
3#include <cgv/math/fvec.h>
4#include <cgv/media/illum/obj_material.h>
5#include <cgv/utils/tokenizer.h>
6#include <set>
7#include <map>
8#include <vector>
9#include <string>
10
11#include <cgv/media/lib_begin.h>
12
13namespace cgv {
14 namespace media {
15 namespace mesh {
16
18class CGV_API obj_reader_base
19{
20public:
23protected:
25 unsigned group_index;
27 unsigned nr_groups;
31 unsigned nr_materials;
33 std::map<std::string, unsigned> material_index_lut;
37 color_type parse_color(const std::vector<cgv::utils::token>& t, unsigned off = 0) const;
38 int minus;
39 unsigned nr_normals, nr_texcoords;
40 bool have_default_material;
41 std::set<std::string> mtl_lib_files;
42 void parse_face(const std::vector<cgv::utils::token>& tokens, bool is_line = false);
43 void parse_material(const std::vector<cgv::utils::token>& tokens);
44 virtual void parse_and_process_vertex(const std::vector<cgv::utils::token>& tokens) = 0;
45 virtual void parse_and_process_normal(const std::vector<cgv::utils::token>& tokens) = 0;
46 virtual void parse_and_process_texcoord(const std::vector<cgv::utils::token>& tokens) = 0;
48
52 std::string path_name;
54 unsigned get_current_group() const;
56 unsigned get_current_material() const;
58
61 virtual void process_comment(const std::string& comment);
63 virtual void process_color(const color_type& c);
65 void convert_to_positive(unsigned vcount, int *vertices,
66 int *texcoords, int *normals,
67 unsigned v, unsigned n, unsigned t);
69 virtual void process_line(unsigned vcount, int *vertices,
70 int *texcoords = 0, int *normals = 0);
72 virtual void process_face(unsigned vcount, int* vertices,
73 int* texcoords = 0, int* normals = 0);
75 virtual void process_group(const std::string& name, const std::string& parameters);
77 virtual void process_material(const cgv::media::illum::obj_material& mtl, unsigned idx);
79public:
83 virtual bool parse_obj(const std::string& content, const std::string path_name = "");
85 virtual bool read_obj(const std::string& file_name);
87 virtual bool read_mtl(const std::string& file_name);
89 virtual void clear();
90};
91
96template <typename T>
97class CGV_API obj_reader_generic : public obj_reader_base
98{
99public:
101 typedef T coord_type;
107 static bool is_double(const char* begin, const char* end, coord_type& value);
108protected:
112 vec2_type parse_vec2(const std::vector<cgv::utils::token>& t) const;
114 vec3_type parse_vec3(const std::vector<cgv::utils::token>& t) const;
116 void parse_and_process_vertex(const std::vector<cgv::utils::token>& tokens);
118 void parse_and_process_normal(const std::vector<cgv::utils::token>& tokens);
120 void parse_and_process_texcoord(const std::vector<cgv::utils::token>& tokens);
122
126 virtual void process_vertex(const vec3_type& p);
128 virtual void process_texcoord(const vec2_type& t);
130 virtual void process_normal(const vec3_type& n);
132public:
135};
136
140
141 }
142 }
143}
144
145#include <cgv/config/lib_end.h>
A vector with zero based index.
Definition fvec.h:26
>extension of a phong material with support for texture mapped color channels
base class for obj reader with implementation that is independent of coordinate type.
Definition obj_reader.h:19
unsigned material_index
keep track of current material index
Definition obj_reader.h:29
illum::obj_material::color_type color_type
type used for rgba colors
Definition obj_reader.h:22
std::string path_name
store the path name
Definition obj_reader.h:52
unsigned group_index
keep track of the current group
Definition obj_reader.h:25
unsigned nr_materials
number of materials
Definition obj_reader.h:31
std::map< std::string, unsigned > material_index_lut
mapping from material names to material indices
Definition obj_reader.h:33
unsigned nr_groups
number of groups
Definition obj_reader.h:27
implements the pure reading of an obj file and calls virtual callback functions to allow a derived cl...
Definition obj_reader.h:98
cgv::math::fvec< T, 3 > vec3_type
type used to store positions and normal vectors
Definition obj_reader.h:105
cgv::math::fvec< T, 2 > vec2_type
type used to store texture coordinates
Definition obj_reader.h:103
the cgv namespace
Definition print.h:11