| 1 |
/* |
| 2 |
* |
| 3 |
* MIT License |
| 4 |
* |
| 5 |
* Copyright (c) 2018 Richard Knight |
| 6 |
* |
| 7 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 |
* of this software and associated documentation files (the "Software"), to deal |
| 9 |
* in the Software without restriction, including without limitation the rights |
| 10 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 |
* copies of the Software, and to permit persons to whom the Software is |
| 12 |
* furnished to do so, subject to the following conditions: |
| 13 |
* |
| 14 |
* The above copyright notice and this permission notice shall be included in all |
| 15 |
* copies or substantial portions of the Software. |
| 16 |
* |
| 17 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 |
* SOFTWARE. |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#define FAST_OBJ_IMPLEMENTATION |
| 28 |
#include "fast_obj.h" |
| 29 |
|
| 30 |
#define TINYOBJLOADER_IMPLEMENTATION |
| 31 |
#include "tiny_obj_loader.h" |
| 32 |
|
| 33 |
#include <chrono> |
| 34 |
|
| 35 |
using namespace tinyobj; |
| 36 |
|
| 37 |
|
| 38 |
struct tinyObj |
| 39 |
{ |
| 40 |
attrib_t attrib; |
| 41 |
std::vector<shape_t> shapes; |
| 42 |
std::vector<material_t> materials; |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
static |
| 48 |
bool read_tiny_obj(const char* path, tinyObj* o) |
| 49 |
{ |
| 50 |
std::string err; |
| 51 |
std::string warn; |
| 52 |
return LoadObj(&o->attrib, &o->shapes, &o->materials, &warn, &err, path, 0, false); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
static |
| 57 |
void check(bool c, const char* m) |
| 58 |
{ |
| 59 |
if (!c) |
| 60 |
printf("CHECK FAILED : %s\n", m); |
| 61 |
} |
| 62 |
|
| 63 |
#define CHECK(_c) check(_c, #_c) |
| 64 |
|
| 65 |
|
| 66 |
static |
| 67 |
void compare_mesh(fastObjMesh* m, tinyObj* o) |
| 68 |
{ |
| 69 |
CHECK(m->group_count == o->shapes.size()); |
| 70 |
|
| 71 |
for (unsigned int ii = 0; ii < m->group_count; ii++) |
| 72 |
{ |
| 73 |
const fastObjGroup& grp = m->groups[ii]; |
| 74 |
const shape_t& shp = o->shapes[ii]; |
| 75 |
|
| 76 |
std::string grp_name; |
| 77 |
if (grp.name) |
| 78 |
grp_name = std::string(grp.name); |
| 79 |
|
| 80 |
CHECK(shp.name == grp_name); |
| 81 |
CHECK(shp.mesh.num_face_vertices.size() == grp.face_count); |
| 82 |
|
| 83 |
int idx = 0; |
| 84 |
for (unsigned int jj = 0; jj < grp.face_count; jj++) |
| 85 |
{ |
| 86 |
unsigned int fv = m->face_vertices[grp.face_offset + jj]; |
| 87 |
|
| 88 |
CHECK(shp.mesh.num_face_vertices[jj] == fv); |
| 89 |
|
| 90 |
for (unsigned int kk = 0; kk < fv; kk++) |
| 91 |
{ |
| 92 |
index_t oi = shp.mesh.indices[idx]; |
| 93 |
fastObjIndex mi = m->indices[grp.index_offset + idx]; |
| 94 |
|
| 95 |
CHECK(oi.vertex_index + 1 == mi.p); |
| 96 |
CHECK(oi.texcoord_index + 1 == mi.t); |
| 97 |
CHECK(oi.normal_index + 1 == mi.n); |
| 98 |
|
| 99 |
if (mi.p) |
| 100 |
{ |
| 101 |
CHECK(o->attrib.vertices[3 * oi.vertex_index + 0] == m->positions[3 * mi.p + 0]); |
| 102 |
CHECK(o->attrib.vertices[3 * oi.vertex_index + 1] == m->positions[3 * mi.p + 1]); |
| 103 |
CHECK(o->attrib.vertices[3 * oi.vertex_index + 2] == m->positions[3 * mi.p + 2]); |
| 104 |
} |
| 105 |
|
| 106 |
if (mi.t) |
| 107 |
{ |
| 108 |
CHECK(o->attrib.texcoords[2 * oi.texcoord_index + 0] == m->texcoords[2 * mi.t + 0]); |
| 109 |
CHECK(o->attrib.texcoords[2 * oi.texcoord_index + 1] == m->texcoords[2 * mi.t + 1]); |
| 110 |
} |
| 111 |
|
| 112 |
if (mi.n) |
| 113 |
{ |
| 114 |
CHECK(o->attrib.normals[3 * oi.normal_index + 0] == m->normals[3 * mi.n + 0]); |
| 115 |
CHECK(o->attrib.normals[3 * oi.normal_index + 1] == m->normals[3 * mi.n + 1]); |
| 116 |
CHECK(o->attrib.normals[3 * oi.normal_index + 2] == m->normals[3 * mi.n + 2]); |
| 117 |
} |
| 118 |
|
| 119 |
idx++; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
int main(int argc, const char* argv[]) |
| 127 |
{ |
| 128 |
if (argc != 2) |
| 129 |
{ |
| 130 |
printf("%s <obj file>\n", argv[0]); |
| 131 |
return -1; |
| 132 |
} |
| 133 |
|
| 134 |
printf("Reading with fast_obj\n"); |
| 135 |
|
| 136 |
auto fast_start = std::chrono::high_resolution_clock::now(); |
| 137 |
|
| 138 |
fastObjMesh* m = fast_obj_read(argv[1]); |
| 139 |
|
| 140 |
auto fast_end = std::chrono::high_resolution_clock::now(); |
| 141 |
std::chrono::duration<double> fast_time = fast_end - fast_start; |
| 142 |
|
| 143 |
if (!m) |
| 144 |
{ |
| 145 |
printf("Failed!\n"); |
| 146 |
return -1; |
| 147 |
} |
| 148 |
|
| 149 |
printf("Took %0.2f secs\n", fast_time.count()); |
| 150 |
|
| 151 |
|
| 152 |
printf("Reading with tiny_obj_loader\n"); |
| 153 |
tinyObj o; |
| 154 |
|
| 155 |
auto tiny_start = std::chrono::high_resolution_clock::now(); |
| 156 |
|
| 157 |
bool success = read_tiny_obj(argv[1], &o); |
| 158 |
|
| 159 |
auto tiny_end = std::chrono::high_resolution_clock::now(); |
| 160 |
std::chrono::duration<double> tiny_time = tiny_end - tiny_start; |
| 161 |
|
| 162 |
if (!success) |
| 163 |
{ |
| 164 |
printf("Failed!\n"); |
| 165 |
return -1; |
| 166 |
} |
| 167 |
|
| 168 |
printf("Took %0.2f secs\n", tiny_time.count()); |
| 169 |
|
| 170 |
printf("Comparing...\n"); |
| 171 |
compare_mesh(m, &o); |
| 172 |
|
| 173 |
printf("Done\n"); |
| 174 |
|
| 175 |
fast_obj_destroy(m); |
| 176 |
|
| 177 |
return 0; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|