2#include <cgv/utils/file.h>
3#include <cgv/type/standard_types.h>
5using namespace cgv::utils::file;
7using namespace cgv::media::illum;
10#pragma warning (disable:4996)
19 first_vertex_index(_vi0),
20 first_texcoord_index(_ti0),
21 first_normal_index(_ni0),
28 first_vertex_index(_vi0),
29 first_texcoord_index(_ti0),
30 first_normal_index(_ni0),
40 vertices.push_back(p);
47 texcoords.push_back(t);
65 int* texcoords,
int* normals)
67 this->convert_to_positive(vcount, vertices, texcoords, normals,
68 (
unsigned)this->vertices.size(), (
unsigned)this->normals.size(), (
unsigned)this->texcoords.size());
69 lines.push_back(
line_info(vcount, (
unsigned)vertex_indices.size(),
70 texcoords == 0 ? -1 : (int)texcoord_indices.size(),
71 normals == 0 ? -1 : (int)normal_indices.size(),
72 this->get_current_group()));
74 for (i = 0; i < vcount; ++i)
75 vertex_indices.push_back(vertices[i]);
77 for (i = 0; i < vcount; ++i)
78 normal_indices.push_back(normals[i]);
80 for (i = 0; i < vcount; ++i)
81 texcoord_indices.push_back(texcoords[i]);
87 int *texcoords,
int *normals)
89 this->convert_to_positive(vcount,vertices,texcoords,normals,
90 (
unsigned)this->vertices.size(), (
unsigned)this->normals.size(), (
unsigned)this->texcoords.size());
91 faces.push_back(
face_info(vcount,(
unsigned)vertex_indices.size(),
92 texcoords == 0 ? -1 : (int)texcoord_indices.size(),
93 normals == 0 ? -1 : (int)normal_indices.size(),
94 this->get_current_group(),this->get_current_material()));
96 for (i=0; i<vcount; ++i)
97 vertex_indices.push_back(vertices[i]);
99 for (i=0; i<vcount; ++i)
100 normal_indices.push_back(normals[i]);
102 for (i=0; i<vcount; ++i)
103 texcoord_indices.push_back(texcoords[i]);
114 groups.push_back(gi);
121 if (idx >= materials.size())
122 materials.push_back(mtl);
124 materials[idx] = mtl;
128const char* get_bin_extension()
134const char* get_bin_extension<float>()
144 std::string bin_fn = drop_extension(file_name) + get_bin_extension<T>();
145 if (exists(bin_fn) &&
146 get_last_write_time(bin_fn) > get_last_write_time(file_name) &&
147 read_obj_bin(bin_fn)) {
148 this->path_name = get_path(file_name);
149 if (!this->path_name.empty())
150 this->path_name +=
"/";
165 bool do_correct =
false;
166 for (i = 0; i < colors.size(); ++i) {
167 if (colors[i][0] > 5 ||colors[i][1] > 5 ||colors[i][2] > 5) {
173 for (i = 0; i < colors.size(); ++i) {
174 colors[i] *= 1.0f/255;
177 write_obj_bin(bin_fn);
185 FILE* fp = fopen(file_name.c_str(),
"rb");
201 bool has_colors =
false;
202 if (v > 0x7FFFFFFF) {
206 bool has_lines =
false;
207 if (f > 0x7FFFFFFF) {
222 vertex_indices.resize(h);
223 if (v != fread(&vertices[0],
sizeof(
vec3_type), v, fp) ||
224 h > 0 && h != fread(&vertex_indices[0],
sizeof(
unsigned), h, fp) )
230 if (v != fread(&colors[0],
sizeof(
color_type), v, fp))
238 normal_indices.resize(h);
239 if (n != fread(&normals[0],
sizeof(
vec3_type), n, fp) ||
240 h > 0 && h != fread(&normal_indices[0],
sizeof(
unsigned), h, fp) )
248 texcoord_indices.resize(h);
249 if (t != fread(&texcoords[0],
sizeof(
vec2_type), t, fp) ||
250 h > 0 && h != fread(&texcoord_indices[0],
sizeof(
unsigned), h, fp) )
257 if (l > 0 && l != fread(&lines[0],
sizeof(
line_info), l, fp))
263 if (f > 0 && f != fread(&faces[0],
sizeof(
face_info), f, fp))
269 for (
unsigned gi=0; gi<g; ++gi) {
270 if (!read_string_bin(groups[gi].name, fp) ||
271 !read_string_bin(groups[gi].parameters, fp))
277 if (1 != fread(&this->have_default_material,
sizeof(
bool), 1, fp))
282 if (this->have_default_material)
285 for (
unsigned mi=0; mi<m; ++mi) {
287 if (!read_string_bin(s, fp)) {
307 vertex_indices.clear();
308 normal_indices.clear();
309 texcoord_indices.clear();
320 FILE* fp = fopen(file_name.c_str(),
"wb");
328 n = (unsigned) normals.size(),
329 t = (unsigned) texcoords.size(),
330 f = (unsigned) faces.size(),
331 l = (unsigned) lines.size(),
332 h = (unsigned) vertex_indices.size(),
333 g = (unsigned) groups.size(),
334 m = (unsigned) this->mtl_lib_files.size();
336 bool has_colors = (colors.size() == vertices.size());
338 v_write = 0xFFFFFFFF - v;
342 f_write = 0xFFFFFFFF - f;
344 if (1!=fwrite(&v_write,
sizeof(
uint32_type), 1, fp) ||
360 if (v != fwrite(&vertices[0],
sizeof(
vec3_type), v, fp) ||
361 h > 0 && h != fwrite(&vertex_indices[0],
sizeof(
unsigned), h, fp) )
368 if (v != fwrite(&colors[0],
sizeof(
color_type), v, fp) )
375 if (n != fwrite(&normals[0],
sizeof(
vec3_type), n, fp) ||
376 h > 0 && h != fwrite(&normal_indices[0],
sizeof(
unsigned), h, fp) )
383 if (t != fwrite(&texcoords[0],
sizeof(
vec2_type), t, fp) ||
384 h > 0 && h != fwrite(&texcoord_indices[0],
sizeof(
unsigned), h, fp) )
390 if (l > 0 && l != fwrite(&lines[0],
sizeof(
line_info), l, fp))
395 if (f > 0 && f != fwrite(&faces[0],
sizeof(
face_info), f, fp))
400 for (
unsigned gi=0; gi<g; ++gi) {
401 if (!write_string_bin(groups[gi].name, fp) ||
402 !write_string_bin(groups[gi].parameters, fp))
409 if (1 != fwrite(&this->have_default_material,
sizeof(
bool), 1, fp))
415 std::set<std::string>::const_iterator mi = this->mtl_lib_files.begin();
416 for (; mi != this->mtl_lib_files.end(); ++mi) {
417 if (!write_string_bin(*mi, fp))
430 std::cout <<
"num vertices "<<vertices.size()<<std::endl;
431 std::cout <<
"num normals "<<normals.size()<<std::endl;
432 std::cout <<
"num texcoords "<<texcoords.size()<<std::endl;
433 std::cout <<
"num faces "<<faces.size()<<std::endl;
434 std::cout <<
"num materials "<<materials.size()<<std::endl;
435 std::cout <<
"num groups "<<groups.size()<<std::endl;
438template class obj_loader_generic < float >;
439template class obj_loader_generic < double >;
complete implementation of method actions that only call one method when entering a node
namespace for compile time type information