aboutsummaryrefslogtreecommitdiff
path: root/examples/obj_sticher/obj_sticher.cc
blob: f59fee4c2eb0d983fb958a70ec10f15a0dbb8f3e (plain)
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
//
// Stiches multiple .obj files into one .obj. 
//
#include "../../tiny_obj_loader.h"
#include "obj_writer.h"

#include <cassert>
#include <iostream>
#include <cstdlib>
#include <cstdio>

typedef std::vector<tinyobj::shape_t> Shape;
typedef std::vector<tinyobj::material_t> Material;

void
StichObjs(
  std::vector<tinyobj::shape_t>& out_shape,
  std::vector<tinyobj::material_t>& out_material,
  const std::vector<Shape>& shapes,
  const std::vector<Material>& materials)
{
  int numShapes = 0;
  for (size_t i = 0; i < shapes.size(); i++) {
    numShapes += (int)shapes[i].size();
  }

  printf("Total # of shapes = %d\n", numShapes);
  int materialIdOffset = 0;

  size_t face_offset = 0;
  for (size_t i = 0; i < shapes.size(); i++) {

    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());
      assert((shapes[i][k].mesh.indices.size() % 3) == 0);
      assert((shapes[i][k].mesh.positions.size() % 3) == 0);

      tinyobj::shape_t new_shape = shapes[i][k];
      // Add offset.
      for (size_t f = 0; f < new_shape.mesh.material_ids.size(); f++) {
        new_shape.mesh.material_ids[f] += materialIdOffset;
      }

      new_shape.name = new_name;
      printf("shape[%ld][%ld].new_name = %s\n", i, k, new_shape.name.c_str());

      out_shape.push_back(new_shape);
    }

    materialIdOffset += materials[i].size();
  }

  for (size_t i = 0; i < materials.size(); i++) {
    for (size_t k = 0; k < materials[i].size(); k++) {
      out_material.push_back(materials[i][k]);
    }
  }

}

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<Shape> shapes;
  std::vector<Material> materials;
  shapes.resize(num_objfiles);
  materials.resize(num_objfiles);

  for (int i = 0; i < num_objfiles; i++) {
    std::cout << "Loading " << argv[i+1] << " ... " << std::flush;
    
    std::string err;
    bool ret = tinyobj::LoadObj(shapes[i], materials[i], err, argv[i+1]);
    if (!err.empty()) {
      std::cerr << err << std::endl;
    }
    if (!ret) {
      exit(1);
    }

    std::cout << "DONE." << std::endl;
  }

  std::vector<tinyobj::shape_t> out_shape;
  std::vector<tinyobj::material_t> out_material;
  StichObjs(out_shape, out_material, shapes, materials);

  bool coordTransform = true;
  bool ret = WriteObj(out_filename, out_shape, out_material, coordTransform);
  assert(ret);

  return 0;
}