Graphics based | achieved loaded OBJ file

1. tiny_obj_loader.h use
include the header files need to define a macro

TINYOBJLOADER_IMPLEMENTATION #define
#include "tiny_obj_loader.h"
. 1
2
2. The data structure describes tiny_obj_loader.h
2.1 attrib_t
// Vertex Attributes
typedef struct {
STD :: Vector <real_t> the vertices; // 'V'
STD :: Vector < real_t> Normals; // 'VN'
STD :: Vector <real_t> texcoords; // 'VT'
STD :: Vector <real_t> Colors; // Extension: Vertex Colors
} attrib_t;
. 1
2
. 3
. 4
. 5
. 6
. 7
attrib_t main store all the vertex data is OBJ file information such as:

vertices: vertex position information
normals: normal information
texcoords: texture coordinate information
colors: color information of
2.2 shape_t
typedef struct {
STD :: String name;
mesh_t Mesh;
path_t path;
} shape_t;
. 1
2
. 3
. 4
. 5
is an example of this representation shape_t a portion of the object
in the OBJ file, such as o xxx indicates the beginning of a portion of the information stored in the main are:

name: the name of this part xxx
mesh: This section is composed of vertex information is recorded here by way of use of the index since all data are placed in the attrib_t...
path: Lines pairs of indices for mean segment by comment . the index may be a problem because not used to this.
here the focus is mesh_t this data structure:

// Index struct to support different indices for vtx/normal/texcoord.
// -1 means not used.
typedef struct {
int vertex_index;
int normal_index;
int texcoord_index;
} index_t;

typedef struct {
std::vector<index_t> indices;
std::vector<unsigned char> num_face_vertices; // The number of vertices per
// face. 3 = polygon, 4 = quad,
// ... Up to 255.
std::vector<int> material_ids; // per-face material ID
std::vector<unsigned int> smoothing_group_ids; // per-face smoothing group
// ID(0 = off. positive value
// = group id)
std::vector<tag_t> tags; // SubD tag
} mesh_t;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Index information is important in std :: vector <index_t> indices; in.
Index_t index is an index which data it:

vertices: vertex position information
normals: normal information
texcoords: texture coordinate information
3. The reading and storing data by tiny_obj_loader.h
main reference loader_example.cc of PrintInfo (attrib, shapes, materials) function
bool Object :: make_mesh_and_material_by_obj (const char * filename, const char * basepath , bool triangulate) {

std::cout << "Loading " << filename << std::endl;

tinyobj :: attrib_t attrib; // All the data in this
STD :: Vector <tinyobj :: shape_t> Shapes;
// a Shape, showing a portion,
// is mainly stored index coordinate mesh_t class
// put in the indices
/ *
// -1 Not Used means.
typedef struct {
int vertex_index;
int normal_index;
int texcoord_index;
} index_t;
* /
STD :: Vector <tinyobj :: material_t> Materials;

std::string warn;
std::string err;

bool ret = tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename,
basepath, triangulate);

// Took over from the above that the value of property in the
IF (warn.empty ()!) {
Std :: cout << "WARN:" << std :: endl << The warn;
}

if (!err.empty()) {
std::cerr << "ERR: " << err << std::endl;
}

if (!ret) {
printf("Failed to load/parse .obj.\n");
return false;
}

// ========================== read into the model data into their own defined data structure ======== ================

STD :: COUT << "# of the vertices:" << (attrib.vertices.size () /. 3) STD :: << endl;
STD: : COUT << "# of Normals:" << (attrib.normals.size () /. 3) STD :: << endl;
STD :: COUT << "# of texcoords:" << (attrib.texcoords.size () / 2)
<< STD :: endl;

std::cout << "# of shapes : " << shapes.size() << std::endl;
std::cout << "# of materials : " << materials.size() << std::endl;

///1. 获取各种材质和纹理
{
for (int i = 0; i < materials.size(); i++) {
Material* m = new Material();
tinyobj::material_t tm = materials[i];
string name = tm.name;
if (name.size()) {
m->name = name;
}
m->ambient.r = tm.ambient[0];
m->ambient.g = tm.ambient[1];
m->ambient.b = tm.ambient[2];

m->diffuse.r = tm.diffuse[0];
m->diffuse.g = tm.diffuse[1];
m->diffuse.b = tm.diffuse[2];

m->specular.r = tm.specular[0];
m->specular.g = tm.specular[1];
m->specular.b = tm.specular[2];

m->transmittance.r = tm.transmittance[0];
m->transmittance.g = tm.transmittance[1];
m->transmittance.b = tm.transmittance[2];

m->emission.r = tm.emission[0];
m->emission.g = tm.emission[1];
m->emission.b = tm.emission[2];

m->shininess = tm.shininess;
m->ior = tm.ior;
m->dissolve = tm.dissolve;
m->illum = tm.illum;
m->pad0 = tm.pad0;

m->ambient_tex_id = -1;
m->diffuse_tex_id = -1;
m->specular_tex_id = -1;
m->specular_highlight_tex_id = -1;
m->bump_tex_id = -1;
m->displacement_tex_id = -1;
m->alpha_tex_id = -1;

m->ambient_texname = "";
m->diffuse_texname = "";
m->specular_texname = "";
m->specular_highlight_texname = "";
m->bump_texname = "";
m->displacement_texname = "";
m->alpha_texname = "";

if (tm.ambient_texname.size()) {

}
if (tm.diffuse_texname.size()) {

}
if (tm.specular_texname.size()) {

}
if (tm.specular_highlight_texname.size()) {

}
if (tm.bump_texname.size()) {

}
if (tm.displacement_texname.size()) {
}
if (tm.alpha_texname.size()) {

}

this->materials.push_back(m);
}


}

/// 2.顶点数据
{
// For each shape 遍历每一个部分
for (size_t i = 0; i < shapes.size(); i++) {
// 这部分的名称
printf("shape[%ld].name = %s\n", static_cast<long>(i),
shapes[i].name.c_str());
// 网格的点数
printf("Size of shape[%ld].mesh.indices: %lu\n", static_cast<long>(i),
static_cast<unsigned long>(shapes[i].mesh.indices.size()));
//printf("Size of shape[%ld].path.indices: %lu\n", static_cast<long>(i),static_cast<unsigned long>(shapes[i].path.indices.size()));

//assert(shapes[i].mesh.num_face_vertices.size() == shapes[i].mesh.material_ids.size());
//assert(shapes[i].mesh.num_face_vertices.size() == shapes[i].mesh.smoothing_group_ids.size());

printf("shape[%ld].num_faces: %lu\n", static_cast<long>(i),
static_cast<unsigned long>(shapes[i].mesh.num_face_vertices.size()));

Model * model = new Model () ; // every part of the data model
// = number of vertices of the number of face X3
Model-> mesh_num = Shapes [I] .mesh.num_face_vertices.size (http://www.my516.com * 3);
// open space
Vertex Vertex * mesh_data new new = [Model-> mesh_num];
size_t index_offset = 0;

// For each face
for (size_t f = 0; f < shapes[i].mesh.num_face_vertices.size(); f++) {
size_t fnum = shapes[i].mesh.num_face_vertices[f];

// 获得所索引下标
tinyobj::index_t idx;
int vertex_index[3];
int normal_index[3];
int texcoord_index[3];
for (size_t v = 0; v < fnum; v++) {
idx = shapes[i].mesh.indices[index_offset + v];
vertex_index[v] = idx.vertex_index;
texcoord_index[v] = idx.texcoord_index;
normal_index[v] = idx.normal_index;
}
for (size_t v = 0; v < fnum; v++) {
// v
mesh_data[index_offset + v].pos.x = attrib.vertices[(vertex_index[v]) * 3 + 0];
mesh_data[index_offset + v].pos.y = attrib.vertices[(vertex_index[v]) * 3 + 1];
mesh_data[index_offset + v].pos.z = attrib.vertices[(vertex_index[v]) * 3 + 2];
mesh_data[index_offset + v].pos.w = 1.0f;

// vt
mesh_data[index_offset + v].tc.u = attrib.texcoords[texcoord_index[v] * 2 + 0];
mesh_data[index_offset + v].tc.v = attrib.texcoords[texcoord_index[v] * 2 + 1];

// vn
mesh_data[index_offset + v].normal.x = attrib.normals[normal_index[v] * 3 + 0];
mesh_data[index_offset + v].normal.y = attrib.normals[normal_index[v] * 3 + 1];
mesh_data[index_offset + v].normal.z = attrib.normals[normal_index[v] * 3 + 2];
mesh_data[index_offset + v].normal.w = 1.0f;

// color
mesh_data[index_offset + v].color.r = 1.0f;
mesh_data[index_offset + v].color.g = 1.0f;
mesh_data[index_offset + v].color.b = 1.0f;
mesh_data[index_offset + v].color.a = 1.0f;
}

// 偏移
index_offset += fnum;
}
model->mesh = mesh_data;
models.push_back(model);
}
}

std::cout << "# Loading Complete #"<< std::endl;
//PrintInfo(attrib, shapes, materials);
return true;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160.
161
162
163
164 is
165
166
167 is
168
169
170.
171 is
172
173
174
175
176
177
178
179
180 [
181
182
183 is
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200 is
201
202
203
achieve the results
meshlab Watch effect.


Introducing rasterizer renderer wireframe
--------------------- 

Guess you like

Origin www.cnblogs.com/hyhy904/p/11058057.html