Merge branch 'develop' of github.com:syoyo/tinyobjloader into develop

This commit is contained in:
Syoyo Fujita
2016-04-18 20:07:48 +09:00
8 changed files with 174 additions and 100 deletions

View File

@@ -42,15 +42,8 @@ before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew upgrade; fi
- if [ -n "$REPORT_COVERAGE" ]; then pip install --user cpp-coveralls; fi
script:
- mkdir build && cd build
- export CC="${CC}-${COMPILER_VERSION}"
- export CXX="${CXX}-${COMPILER_VERSION}"
- ${CC} -v
- cmake --version
- cmake -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DTINYOBJLOADER_BUILD_TEST_LOADER=On -G Ninja
..
- ninja
- ./test_loader ../cornell_box.obj
- cd tests
- make check
- if [ -n "$REPORT_COVERAGE" ]; then coveralls -b . -r .. -e examples -e tools -e
jni -e python -e images -E ".*CompilerId.*" -E ".*feature_tests.*" ; fi
- cd ..

View File

@@ -73,12 +73,13 @@ Features
* Material
* Unknown material attributes are returned as key-value(value is string) map.
* Crease tag('t'). This is OpenSubdiv specific(not in wavefront .obj specification)
* Callback API for custom loading.
TODO
----
* [ ] Support different indices for vertex/normal/texcoord
* [ ] Read .obj/.mtl from memory
License
-------
@@ -88,7 +89,6 @@ Licensed under 2 clause BSD.
Usage
-----
TinyObjLoader triangulate input .obj by default.
```c++
#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
#include "tiny_obj_loader.h"
@@ -108,88 +108,11 @@ if (!ret) {
exit(1);
}
std::cout << "# of shapes : " << shapes.size() << std::endl;
std::cout << "# of materials : " << materials.size() << std::endl;
for (size_t i = 0; i < shapes.size(); i++) {
printf("shape[%ld].name = %s\n", i, shapes[i].name.c_str());
printf("Size of shape[%ld].indices: %ld\n", i, shapes[i].mesh.indices.size());
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size());
assert((shapes[i].mesh.indices.size() % 3) == 0);
for (size_t f = 0; f < shapes[i].mesh.indices.size() / 3; f++) {
printf(" idx[%ld] = %d, %d, %d. mat_id = %d\n", f, shapes[i].mesh.indices[3*f+0], shapes[i].mesh.indices[3*f+1], shapes[i].mesh.indices[3*f+2], shapes[i].mesh.material_ids[f]);
}
printf("shape[%ld].vertices: %ld\n", i, shapes[i].mesh.positions.size());
assert((shapes[i].mesh.positions.size() % 3) == 0);
for (size_t v = 0; v < shapes[i].mesh.positions.size() / 3; v++) {
printf(" v[%ld] = (%f, %f, %f)\n", v,
shapes[i].mesh.positions[3*v+0],
shapes[i].mesh.positions[3*v+1],
shapes[i].mesh.positions[3*v+2]);
}
}
for (size_t i = 0; i < materials.size(); i++) {
printf("material[%ld].name = %s\n", i, materials[i].name.c_str());
printf(" material.Ka = (%f, %f ,%f)\n", materials[i].ambient[0], materials[i].ambient[1], materials[i].ambient[2]);
printf(" material.Kd = (%f, %f ,%f)\n", materials[i].diffuse[0], materials[i].diffuse[1], materials[i].diffuse[2]);
printf(" material.Ks = (%f, %f ,%f)\n", materials[i].specular[0], materials[i].specular[1], materials[i].specular[2]);
printf(" material.Tr = (%f, %f ,%f)\n", materials[i].transmittance[0], materials[i].transmittance[1], materials[i].transmittance[2]);
printf(" material.Ke = (%f, %f ,%f)\n", materials[i].emission[0], materials[i].emission[1], materials[i].emission[2]);
printf(" material.Ns = %f\n", materials[i].shininess);
printf(" material.Ni = %f\n", materials[i].ior);
printf(" material.dissolve = %f\n", materials[i].dissolve);
printf(" material.illum = %d\n", materials[i].illum);
printf(" material.map_Ka = %s\n", materials[i].ambient_texname.c_str());
printf(" material.map_Kd = %s\n", materials[i].diffuse_texname.c_str());
printf(" material.map_Ks = %s\n", materials[i].specular_texname.c_str());
printf(" material.map_Ns = %s\n", materials[i].specular_highlight_texname.c_str());
std::map<std::string, std::string>::const_iterator it(materials[i].unknown_parameter.begin());
std::map<std::string, std::string>::const_iterator itEnd(materials[i].unknown_parameter.end());
for (; it != itEnd; it++) {
printf(" material.%s = %s\n", it->first.c_str(), it->second.c_str());
}
printf("\n");
}
// See loader_example.cc for more details.
```
Reading .obj without triangulation. Use `num_vertices[i]` to iterate over faces(indices). `num_vertices[i]` stores the number of vertices for ith face.
```c++
#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
#include "tiny_obj_loader.h"
std::string inputfile = "cornell_box.obj";
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
Tests
-----
std::string err;
bool triangulate = false;
bool ret = tinyobj::LoadObj(shapes, materials, err, inputfile.c_str(), triangulate);
if (!err.empty()) { // `err` may contain warning message.
std::cerr << err << std::endl;
}
if (!ret) {
exit(1);
}
for (size_t i = 0; i < shapes.size(); i++) {
size_t indexOffset = 0;
for (size_t n = 0; n < shapes[i].mesh.num_vertices.size(); n++) {
int ngon = shapes[i].mesh.num_vertices[n];
for (size_t f = 0; f < ngon; f++) {
unsigned int v = shapes[i].mesh.indices[indexOffset + f];
printf(" face[%ld] v[%ld] = (%f, %f, %f)\n", n,
shapes[i].mesh.positions[3*v+0],
shapes[i].mesh.positions[3*v+1],
shapes[i].mesh.positions[3*v+2]);
}
indexOffset += ngon;
}
}
```
Unit tests are provided in `tests` directory. See `tests/README.md` for details.

View File

@@ -0,0 +1,2 @@
all:
clang++ -I../../ -Wall -g -o example main.cc

View File

@@ -0,0 +1,155 @@
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <iostream>
#include <sstream>
#include <fstream>
typedef struct
{
std::vector<float> vertices;
std::vector<float> normals;
std::vector<float> texcoords;
std::vector<int> v_indices;
std::vector<int> vn_indices;
std::vector<int> vt_indices;
std::vector<tinyobj::material_t> materials;
} MyMesh;
void vertex_cb(void *user_data, float x, float y, float z)
{
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
printf("v[%ld] = %f, %f, %f\n", mesh->vertices.size() / 3, x, y, z);
mesh->vertices.push_back(x);
mesh->vertices.push_back(y);
mesh->vertices.push_back(z);
}
void normal_cb(void *user_data, float x, float y, float z)
{
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
printf("vn[%ld] = %f, %f, %f\n", mesh->normals.size() / 3, x, y, z);
mesh->normals.push_back(x);
mesh->normals.push_back(y);
mesh->normals.push_back(z);
}
void texcoord_cb(void *user_data, float x, float y)
{
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
printf("vt[%ld] = %f, %f\n", mesh->texcoords.size() / 2, x, y);
mesh->texcoords.push_back(x);
mesh->texcoords.push_back(y);
}
void index_cb(void *user_data, int v_idx, int vn_idx, int vt_idx)
{
// NOTE: the value of each index is raw value.
// For example, the application must manually adjust the index with offset
// (e.g. v_indices.size()) when the value is negative(relative index).
// See fixIndex() function in tiny_obj_loader.h for details.
// Also, -2147483648(0x80000000) is set for the index value which does not exist in .obj
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
printf("idx[%ld] = %d, %d, %d\n", mesh->v_indices.size(), v_idx, vn_idx, vt_idx);
if (v_idx != 0x80000000) {
mesh->v_indices.push_back(v_idx);
}
if (vn_idx != 0x80000000) {
mesh->vn_indices.push_back(vn_idx);
}
if (vt_idx != 0x80000000) {
mesh->vt_indices.push_back(vt_idx);
}
}
void usemtl_cb(void *user_data, const char* name, int material_idx)
{
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
if ((material_idx > -1) && (material_idx < mesh->materials.size())) {
printf("usemtl. material id = %d(name = %s)\n", material_idx, mesh->materials[material_idx].name.c_str());
} else {
printf("usemtl. name = %s\n", name);
}
}
void mtllib_cb(void *user_data, const tinyobj::material_t *materials, int num_materials)
{
MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
printf("mtllib. # of materials = %d\n", num_materials);
for (int i = 0; i < num_materials; i++) {
mesh->materials.push_back(materials[i]);
}
}
void group_cb(void *user_data, const char **names, int num_names)
{
//MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
printf("group : name = \n");
for (int i = 0; i < num_names; i++) {
printf(" %s\n", names[i]);
}
}
void object_cb(void *user_data, const char *name)
{
//MyMesh *mesh = reinterpret_cast<MyMesh*>(user_data);
printf("object : name = %s\n", name);
}
int
main(int argc, char** argv)
{
tinyobj::callback_t cb;
cb.vertex_cb = vertex_cb;
cb.normal_cb = normal_cb;
cb.texcoord_cb = texcoord_cb;
cb.index_cb = index_cb;
cb.usemtl_cb = usemtl_cb;
cb.mtllib_cb = mtllib_cb;
cb.group_cb = group_cb;
cb.object_cb = object_cb;
MyMesh mesh;
std::string err;
std::ifstream ifs("../../models/cornell_box.obj");
if (ifs.fail()) {
std::cerr << "file not found." << std::endl;
return EXIT_FAILURE;
}
tinyobj::MaterialFileReader mtlReader("../../models/");
bool ret = tinyobj::LoadObjWithCallback(&mesh, cb, &err, &ifs, &mtlReader);
if (!err.empty()) {
std::cerr << err << std::endl;
}
if (!ret) {
std::cerr << "Failed to parse .obj" << std::endl;
return EXIT_FAILURE;
}
printf("# of vertices = %ld\n", mesh.vertices.size() / 3);
printf("# of normals = %ld\n", mesh.normals.size() / 3);
printf("# of texcoords = %ld\n", mesh.texcoords.size() / 2);
printf("# of vertex indices = %ld\n", mesh.v_indices.size());
printf("# of normal indices = %ld\n", mesh.vn_indices.size());
printf("# of texcoord indices = %ld\n", mesh.vt_indices.size());
printf("# of materials = %ld\n", mesh.materials.size());
return EXIT_SUCCESS;
}

View File

@@ -5,8 +5,8 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := tinyobjloader
LOCAL_SRC_FILES := ../tiny_obj_loader.cc ../test.cc
LOCAL_SRC_FILES := ../tiny_obj_loader.cc
LOCAL_C_INCLUDES := ../
include $(BUILD_EXECUTABLE)
include $(BUILD_STATIC_LIBRARY)

View File

@@ -1,7 +1,10 @@
.PHONY: clean
CXX ?= g++
CXXFLAGS ?= -g -O2
tester: tester.cc
g++ -g -O0 -o tester tester.cc
$(CXX) $(CXXFLAGS) -o tester tester.cc
all: tester

View File

@@ -493,7 +493,7 @@ static vertex_index parseTriple(const char **token, int vsize, int vnsize,
// Parse raw triples: i, i/j/k, i//k, i/j
static vertex_index parseRawTriple(const char **token) {
vertex_index vi(0x8000000); // -2147483648 = invalid
vertex_index vi(0x80000000); // 0x80000000 = -2147483648 = invalid
vi.v_idx = atoi((*token));
(*token) += strcspn((*token), "/ \t\r");

View File

@@ -6,7 +6,5 @@ build:
name: build
code: |
git clone https://github.com/syoyo/orebuildenv.git
chmod +x ./orebuildenv/build/linux/bin/premake4
./orebuildenv/build/linux/bin/premake4 gmake
make
./test_tinyobjloader
cd tests
make check