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
|
//
// Stiches multiple .obj files into one .obj.
//
#include "obj_writer.h"
#include "../../tiny_obj_loader.h"
#include <cassert>
#include <iostream>
#include <cstdlib>
#include <cstdio>
typedef std::vector<tinyobj::shape_t> Shape;
typedef std::vector<tinyobj::material_t> Material;
typedef tinyobj::attrib_t Attribute;
void
StichObjs(
tinyobj::attrib_t& out_attribute,
std::vector<tinyobj::shape_t>& out_shape,
std::vector<tinyobj::material_t>& out_material,
const std::vector<Attribute>& attributes,
const std::vector<Shape>& shapes,
const std::vector<Material>& materials)
{
// The amount of attributes, shape-vectors and material-vecotrs should be the same.
if(attributes.size() != shapes.size() && attributes.size() != materials.size()){
std::cerr << "Size of attributes, shapes and Materials don't fit!" << attributes.size() << " " << shapes.size() <<" " << materials.size() << std::endl;;
exit(1);
}
int num_shapes = 0;
// 4 values (vertices, normals, texcoords, colors)
std::vector<int> num_attributes(4, 0);
int num_materials = 0;
for(int i = 0; i < shapes.size(); i++){
num_shapes += shapes[i].size();
}
for(int i = 0; i < attributes.size(); i++){
num_attributes[0] += attributes[i].vertices.size();
num_attributes[1] += attributes[i].normals.size();
num_attributes[2] += attributes[i].texcoords.size();
num_attributes[3] += attributes[i].colors.size();
}
for(int i = 0; i < materials.size(); i++){
num_materials += materials[i].size();
}
// More performant, than push_back
out_attribute.vertices.resize(num_attributes[0]);
out_attribute.normals.resize(num_attributes[1]);
out_attribute.texcoords.resize(num_attributes[2]);
out_attribute.colors.resize(num_attributes[3]);
out_shape.resize(num_shapes);
out_material.resize(num_materials);
int material_id_offset = 0;
int shape_id_offset = 0;
int vertex_idx_offset = 0;
int normal_idx_offset = 0;
int texcoord_idx_offset = 0;
int color_idx_offset = 0;
// shapes.size() = attributes.size() = materials.size()
for (size_t i = 0; i < shapes.size(); i++) {
// Copy shapes
for (size_t k = 0; k < shapes[i].size(); k++) {
std::string new_name = shapes[i][k].name;
// Add suffix
char buf[1024];
sprintf(buf, "_%04d", (int)i);
new_name += std::string(buf);
printf("shape[%ld][%ld].name = %s\n", i, k, shapes[i][k].name.c_str());
tinyobj::shape_t new_shape = shapes[i][k];
// Add material offset.
for(size_t f = 0; f < new_shape.mesh.material_ids.size(); f++) {
new_shape.mesh.material_ids[f] += material_id_offset;
}
// Add indices offset.
for(size_t f = 0; f < new_shape.mesh.indices.size(); f++){
tinyobj::index_t& ref = new_shape.mesh.indices[f];
if(ref.vertex_index > -1){
ref.vertex_index += vertex_idx_offset;
}
if(ref.normal_index > -1){
ref.normal_index += normal_idx_offset;
}
if(ref.texcoord_index > -1){
ref.texcoord_index += texcoord_idx_offset;
}
}
new_shape.name = new_name;
printf("shape[%ld][%ld].new_name = %s\n", i, k, new_shape.name.c_str());
out_shape[shape_id_offset++] = new_shape;
}
// Copy materials
for (size_t k = 0; k < materials[i].size(); k++) {
out_material[material_id_offset++] = materials[i][k];
}
// Copy attributes (3 floats per vertex, 3 floats per normal, 2 floats per texture-coordinate, 3 floats per color)
// You could also include a check here, if the sizes are dividable by 3 (resp. 2), but it's safe to simply assume, they do.
std::copy(attributes[i].vertices.begin(), attributes[i].vertices.end(), out_attribute.vertices.begin() + vertex_idx_offset * 3);
vertex_idx_offset += attributes[i].vertices.size() / 3;
std::copy(attributes[i].normals.begin(), attributes[i].normals.end(), out_attribute.normals.begin() + normal_idx_offset * 3);
normal_idx_offset += attributes[i].normals.size() / 3;
std::copy(attributes[i].texcoords.begin(), attributes[i].texcoords.end(), out_attribute.texcoords.begin() + texcoord_idx_offset * 2);
texcoord_idx_offset += attributes[i].texcoords.size() / 2;
std::copy(attributes[i].colors.begin(), attributes[i].colors.end(), out_attribute.colors.begin() + color_idx_offset);
color_idx_offset += attributes[i].colors.size();
}
}
int main(int argc, char **argv)
{
if (argc < 3) {
printf("Usage: obj_sticher input0.obj input1.obj ... output.obj\n");
exit(1);
}
int num_objfiles = argc - 2;
std::string out_filename = std::string(argv[argc-1]); // last element
std::vector<Attribute> attributes(num_objfiles);
std::vector<Shape> shapes(num_objfiles);
std::vector<Material> materials(num_objfiles);
for (int i = 0; i < num_objfiles; i++) {
std::cout << "Loading " << argv[i+1] << " ... " << std::flush;
std::string warn;
std::string err;
bool ret = tinyobj::LoadObj(&attributes[i], &shapes[i], &materials[i], &warn, &err, argv[i+1]);
if (!warn.empty()) {
std::cerr << "WARN:" << warn << std::endl;
}
if (!err.empty()) {
std::cerr << err << std::endl;
}
if (!ret) {
exit(1);
}
std::cout << "DONE." << std::endl;
}
Attribute out_attribute;
Shape out_shape;
Material out_material;
StichObjs(out_attribute, out_shape, out_material, attributes, shapes, materials);
bool coordTransform = true;
bool ret = WriteObj(out_filename, out_attribute, out_shape, out_material, coordTransform);
assert(ret);
return 0;
}
|