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:
22 typedef illum::obj_material::color_type color_type;
23protected:
25 unsigned group_index;
27 unsigned nr_groups;
31 unsigned nr_materials;
33 std::map<std::string, unsigned> material_index_lut;
36
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
51
52 std::string path_name;
54 unsigned get_current_group() const;
56 unsigned get_current_material() const;
58
60
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:
81 obj_reader_base();
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:
111
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
125
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
137typedef obj_reader_generic<float> obj_readerf;
138typedef obj_reader_generic<double> obj_readerd;
139typedef obj_reader_generic<double> obj_reader;
140
141 }
142 }
143}
144
145#include <cgv/config/lib_end.h>
A vector with zero based index.
Definition fvec.h:29
virtual bool read_mtl(const std::string &file_name)
read a material file
virtual void process_line(unsigned vcount, int *vertices, int *texcoords=0, int *normals=0)
overide this function to process a line strip, the indices start with 0
virtual bool parse_obj(const std::string &content, const std::string path_name="")
parse the content of an obj file already read to memory, where path_name is used to find material fil...
color_type parse_color(const std::vector< cgv::utils::token > &t, unsigned off=0) const
parse a color, if alpha not given it defaults to 1
virtual void process_comment(const std::string &comment)
overide this function to process a comment
virtual void process_color(const color_type &c)
overide this function to process a color (this called for vc prefixes which is is not in the standard...
unsigned material_index
keep track of current material index
Definition obj_reader.h:29
virtual void process_material(const cgv::media::illum::obj_material &mtl, unsigned idx)
process a material definition. If a material with a certain name is overwritten, it will receive the ...
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
virtual bool read_obj(const std::string &file_name)
read an obj file
virtual void process_group(const std::string &name, const std::string &parameters)
overide this function to process a group given by name and parameter string
std::map< std::string, unsigned > material_index_lut
mapping from material names to material indices
Definition obj_reader.h:33
unsigned get_current_group() const
return the index of the currently selected group or -1 if no group is defined
virtual void process_face(unsigned vcount, int *vertices, int *texcoords=0, int *normals=0)
overide this function to process a face, the indices start with 0
unsigned get_current_material() const
return the index of the currently selected material or -1 if no material is defined
virtual void clear()
clear the reader such that a new file can be read
unsigned nr_groups
number of groups
Definition obj_reader.h:27
void convert_to_positive(unsigned vcount, int *vertices, int *texcoords, int *normals, unsigned v, unsigned n, unsigned t)
convert negative indices to positive ones by adding the number of elements
implements the pure reading of an obj file and calls virtual callback functions to allow a derived cl...
Definition obj_reader.h:98
vec3_type parse_vec3(const std::vector< cgv::utils::token > &t) const
parse 3d vector
virtual void process_vertex(const vec3_type &p)
overide this function to process a vertex
virtual void process_texcoord(const vec2_type &t)
overide this function to process a texcoord
virtual void process_normal(const vec3_type &n)
overide this function to process a normal
obj_reader_generic()
default constructor
cgv::math::fvec< T, 3 > vec3_type
Definition obj_reader.h:105
cgv::math::fvec< T, 2 > vec2_type
Definition obj_reader.h:103
vec2_type parse_vec2(const std::vector< cgv::utils::token > &t) const
parse 2d vector
this header is dependency free
Definition print.h:11
Extension of a phong material with support for texture-mapped color channels.