cgv
Loading...
Searching...
No Matches
obj_loader.h
1#pragma once
2
3#include "obj_reader.h"
4#include <map>
5#include <set>
6#include <cgv/math/fvec.h>
7
8#include <cgv/media/lib_begin.h>
9
10namespace cgv {
11 namespace media {
12 namespace mesh {
13
15struct CGV_API face_info
16{
18 unsigned degree;
30 face_info(unsigned _nr = 0, unsigned _vi0 = 0, int _ti0 = -1, int _ni0 = -1, unsigned gi=-1, unsigned mi=-1);
31};
32
34struct CGV_API line_info
35{
37 unsigned length;
47 line_info(unsigned _nr = 0, unsigned _vi0 = 0, int _ti0 = -1, int _ni0 = -1, unsigned gi = -1);
48};
49
50
53{
55 std::string name;
57 std::string parameters;
58};
59
63template <typename T>
64class CGV_API obj_loader_generic : public obj_reader_generic<T>
65{
66public:
67 // We need to repeat the typenames here, because gcc will not inherit them in templates
68 typedef typename obj_reader_generic<T>::vec3_type vec3_type;
69 typedef typename obj_reader_generic<T>::vec2_type vec2_type;
70 typedef obj_reader_base::color_type color_type;
71
72 std::vector<vec3_type> vertices;
73 std::vector<vec3_type> normals;
74 std::vector<vec2_type> texcoords;
75 std::vector<color_type> colors;
76
77 std::vector<unsigned> vertex_indices;
78 std::vector<unsigned> normal_indices;
79 std::vector<unsigned> texcoord_indices;
80
81 std::vector<line_info> lines;
82 std::vector<face_info> faces;
83 std::vector<group_info> groups;
84 std::vector<cgv::media::illum::obj_material> materials;
85protected:
88
89 void process_vertex(const vec3_type& p);
91 void process_texcoord(const vec2_type& t);
93 void process_normal(const vec3_type& n);
95 void process_color(const color_type& c);
97 void process_line(unsigned vcount, int* vertices, int* texcoords = 0, int* normals = 0);
99 void process_face(unsigned vcount, int* vertices, int* texcoords = 0, int* normals = 0);
101 void process_group(const std::string& name, const std::string& parameters);
103 void process_material(const cgv::media::illum::obj_material& mtl, unsigned idx);
105public:
107 bool read_obj(const std::string& file_name);
109 bool read_obj_bin(const std::string& file_name);
111 bool write_obj_bin(const std::string& file_name) const;
113 void show_stats() const;
115 void clear();
116};
117
118
119typedef obj_loader_generic<float> obj_loaderf;
120typedef obj_loader_generic<double> obj_loaderd;
121typedef obj_loader_generic<double> obj_loader;
122
123 }
124 }
125}
126
127#include <cgv/config/lib_end.h>
implements the virtual interface of the obj_reader and stores all read information.
Definition obj_loader.h:65
bool write_obj_bin(const std::string &file_name) const
write the information from the last read obj file in binary format
void process_vertex(const vec3_type &p)
overide this function to process a vertex
void process_normal(const vec3_type &n)
overide this function to process a normal
bool read_obj(const std::string &file_name)
overloads reading to support binary file format
void process_face(unsigned vcount, int *vertices, int *texcoords=0, int *normals=0)
overide this function to process a face
void process_material(const cgv::media::illum::obj_material &mtl, unsigned idx)
process a material definition
void show_stats() const
use this after reading to show status information about the number of read entities
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...
void clear()
prepare for reading another file
bool read_obj_bin(const std::string &file_name)
read a binary version of an obj file
void process_group(const std::string &name, const std::string &parameters)
overide this function to process a group given by name
void process_line(unsigned vcount, int *vertices, int *texcoords=0, int *normals=0)
overide this function to process a line
void process_texcoord(const vec2_type &t)
overide this function to process a texcoord
illum::obj_material::color_type color_type
type used for rgba colors
Definition obj_reader.h:22
obj_reader_generic()
default constructor
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
this header is dependency free
Definition print.h:11
Extension of a phong material with support for texture-mapped color channels.
unsigned first_vertex_index
index into vertex index array
Definition obj_loader.h:20
int first_normal_index
index into normal index array or -1 if not specified
Definition obj_loader.h:24
int first_texcoord_index
index into texcoord index array or -1 if not specified
Definition obj_loader.h:22
face_info(unsigned _nr=0, unsigned _vi0=0, int _ti0=-1, int _ni0=-1, unsigned gi=-1, unsigned mi=-1)
construct face info
int group_index
index of group to which the face belongs
Definition obj_loader.h:26
int material_index
material index to which the face belongs
Definition obj_loader.h:28
unsigned degree
degree of face
Definition obj_loader.h:18
simple structure to describe a group
Definition obj_loader.h:53
std::string parameters
parameters string
Definition obj_loader.h:57
std::string name
name of the group
Definition obj_loader.h:55
int first_normal_index
index into normal index array or -1 if not specified
Definition obj_loader.h:43
line_info(unsigned _nr=0, unsigned _vi0=0, int _ti0=-1, int _ni0=-1, unsigned gi=-1)
construct face info
int group_index
index of group to which the face belongs
Definition obj_loader.h:45
int first_texcoord_index
index into texcoord index array or -1 if not specified
Definition obj_loader.h:41
unsigned first_vertex_index
index into vertex index array
Definition obj_loader.h:39
unsigned length
length of line strip
Definition obj_loader.h:37