1 Commits

Author SHA1 Message Date
Syoyo Fujita
98da6829ac Initial support of parsing curve primitive('curv')
Add clang-format style file.
2015-08-20 18:17:13 +09:00
22 changed files with 1871 additions and 2539 deletions

View File

@@ -2,6 +2,6 @@
BasedOnStyle: LLVM
IndentWidth: 2
TabWidth: 2
UseTab: Never
UseTab: Always
BreakBeforeBraces: Attach
Standard: Cpp03

View File

@@ -1,66 +0,0 @@
language: cpp
sudo: false
matrix:
include:
- addons: &1
apt:
sources:
- george-edison55-precise-backports
- ubuntu-toolchain-r-test
- llvm-toolchain-precise-3.7
packages:
- cmake
- cmake-data
- ninja-build
- g++-4.9
- clang-3.7
compiler: clang
env: COMPILER_VERSION=3.7 BUILD_TYPE=Debug
- addons: *1
compiler: clang
env: COMPILER_VERSION=3.7 BUILD_TYPE=Release
- addons: &2
apt:
sources:
- george-edison55-precise-backports
- ubuntu-toolchain-r-test
packages:
- cmake
- cmake-data
- ninja-build
- g++-4.9
compiler: gcc
env: COMPILER_VERSION=4.9 BUILD_TYPE=Debug
- addons: *2
compiler: gcc
env: COMPILER_VERSION=4.9 BUILD_TYPE=Release
- addons: *1
compiler: clang
env: COMPILER_VERSION=3.7 BUILD_TYPE=Debug CFLAGS="-O0 --coverage" CXXFLAGS="-O0
--coverage" REPORT_COVERAGE=1
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
- 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 ..
deploy:
provider: releases
api_key:
secure: AsXameK4GJn6h6wMmDrKTr7q/o9EI7hX7zWg1W6VaFBQKfkBvOmjJolWimjl6HMoRZ1NpMmK5GDm3zBlTUeABtgVBIyNWgE9vWS39ff6D5iQKcgScFsJkyILt0GikBqbN2pLGQ2t/M1Qh6n1sEIfzqekiCcF5Qvy5yYlYvHtaRGV02QeYAej/xx15/9SMuKTncHhjf63ClYPu8ODid7QUegJUvlQUeXoPsBDbaXMH2uDWoBWF7etX7G2Iob4NE8GX+ZP6dj+Ogi7p4HXThK650mzLL/pUl584EjjY/vePqx0cFhtpiRwvrW8SNPI1aJ1Phwa1enLRUgfS3bnkwQAMw/SCXSK2lnCvkUAXyTgpG03HWrZURj4vhEPXc7qHooO+dsfmi+JanYLaSDyrGpgQznLGjCMnVATimry0KxSufUY8Wt72Wh+nf7N0IgTUCjl32sWnQd/MRZPkxFuaf1h7r9RoH9KZY0yIOV09gABEFCGrOIZA2FcyhC2G26Bc4zyNrfMFpZ2DI76qdcWNdJGkRkpxtH9sGU8JgZu6Em2f1e6+SLgkBsPxbhRk5PwdhA9AXE2p9PmQqhO3jJKusGBZSoHAF7TlwagRY2J01yJxF7ge6zG9U8QuBqs1bB1zdnE34fHWOgs4st3inC+oBDOhvnEg1Nm/qeYVWMBzpwclSg=
file: tiny_obj_loader.h
all_branches: true
on:
repo: syoyo/tinyobjloader
tags: true
skip_cleanup: true

175
README.md
View File

@@ -3,17 +3,13 @@ tinyobjloader
[![Join the chat at https://gitter.im/syoyo/tinyobjloader](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/syoyo/tinyobjloader?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
[![Build Status](https://travis-ci.org/syoyo/tinyobjloader.svg)](https://travis-ci.org/syoyo/tinyobjloader)
[![wercker status](https://app.wercker.com/status/495a3bac400212cdacdeb4dd9397bf4f/m "wercker status")](https://app.wercker.com/project/bykey/495a3bac400212cdacdeb4dd9397bf4f)
[![Build status](https://ci.appveyor.com/api/projects/status/tlb421q3t2oyobcn/branch/master?svg=true)](https://ci.appveyor.com/project/syoyo/tinyobjloader/branch/master)
[![Coverage Status](https://coveralls.io/repos/github/syoyo/tinyobjloader/badge.svg?branch=master)](https://coveralls.io/github/syoyo/tinyobjloader?branch=master)
http://syoyo.github.io/tinyobjloader/
Tiny but powerful single file wavefront obj loader written in C++. No dependency except for C++ STL. It can parse 10M over polygons with moderate memory and time.
Tiny but poweful single file wavefront obj loader written in C++. No dependency except for C++ STL. It can parse 10M over polygons with moderate memory and time.
`tinyobjloader` is good for embedding .obj loader to your (global illumination) renderer ;-)
@@ -21,9 +17,6 @@ Tiny but powerful single file wavefront obj loader written in C++. No dependency
What's new
----------
* Jan 29, 2016 : Support n-polygon(no triangulation) and OpenSubdiv crease tag! Thanks dboogert!
* Nov 26, 2015 : Now single-header only!.
* Nov 08, 2015 : Improved API.
* Jun 23, 2015 : Various fixes and added more projects using tinyobjloader. Thanks many contributors!
* Mar 03, 2015 : Replace atof() with hand-written parser for robust reading of numeric value. Thanks skurmedel!
* Feb 06, 2015 : Fix parsing multi-material object
@@ -50,16 +43,14 @@ Use case
TinyObjLoader is successfully used in ...
* bullet3 https://github.com/erwincoumans/bullet3
* pbrt-v2 https://github.com/mmp/pbrt-v2
* pbrt-v2 https://https://github.com/mmp/pbrt-v2
* OpenGL game engine development http://swarminglogic.com/jotting/2013_10_gamedev01
* mallie https://lighttransport.github.io/mallie
* IBLBaker (Image Based Lighting Baker). http://www.derkreature.com/iblbaker/
* Stanford CS148 http://web.stanford.edu/class/cs148/assignments/assignment3.pdf
* Awesome Bump http://awesomebump.besaba.com/about/
* sdlgl3-wavefront OpenGL .obj viewer https://github.com/chrisliebert/sdlgl3-wavefront
* pbrt-v3 https://github.com/mmp/pbrt-v3
* cocos2d-x https://github.com/cocos2d/cocos2d-x/
* Android Vulkan demo https://github.com/SaschaWillems/Vulkan
* pbrt-v3 https://https://github.com/mmp/pbrt-v3
* Your project here!
Features
@@ -71,13 +62,16 @@ Features
* Normal
* 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)
Notes
-----
Polygon is converted into triangle.
TODO
----
* [ ] Support different indices for vertex/normal/texcoord
- [ ] Support quad polygon and some tags for OpenSubdiv http://graphics.pixar.com/opensubdiv/
License
-------
@@ -87,108 +81,59 @@ 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"
std::string inputfile = "cornell_box.obj";
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string inputfile = "cornell_box.obj";
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::material_t> materials;
std::string err;
bool ret = tinyobj::LoadObj(shapes, materials, err, inputfile.c_str());
if (!err.empty()) { // `err` may contain warning message.
std::cerr << err << std::endl;
}
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");
}
```
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;
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++) {
unsigend 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]);
std::string err = tinyobj::LoadObj(shapes, materials, inputfile.c_str());
if (!err.empty()) {
std::cerr << err << std::endl;
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].normal_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");
}
indexOffset += ngon;
}
}
```

View File

@@ -1,16 +0,0 @@
# build.ninja
cc = clang
cxx = clang++
cflags = -Werror -Weverything
cxxflags = -Werror -Weverything
rule compile
command = $cxx $cxxflags -c $in -o $out
rule link
command = $cxx $in -o $out
build test.o: compile test.cc
build test: link test.o
default test

View File

@@ -1,101 +0,0 @@
#
# Copyright 2013 Pixar
#
# Licensed under the Apache License, Version 2.0 (the "Apache License")
# with the following modification; you may not use this file except in
# compliance with the Apache License and the following modification to it:
# Section 6. Trademarks. is deleted and replaced with:
#
# 6. Trademarks. This License does not grant permission to use the trade
# names, trademarks, service marks, or product names of the Licensor
# and its affiliates, except as required to comply with Section 4(c) of
# the License and to reproduce the content of the NOTICE file.
#
# You may obtain a copy of the Apache License at
#
# http:#www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the Apache License with the above modification is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the Apache License for the specific
# language governing permissions and limitations under the Apache License.
#
# This file uses centimeters as units for non-parametric coordinates.
v 1.25052 0.517982 0.353553
v 0.597239 0.247384 0.353553
v 0.597239 0.247384 -0.353553
v 1.25052 0.517982 -0.353553
v 0.517982 1.25052 0.353553
v 0.247384 0.597239 0.353553
v 0.247384 0.597239 -0.353553
v 0.517982 1.25052 -0.353553
v -0.517982 1.25052 0.353553
v -0.247384 0.597239 0.353553
v -0.247384 0.597239 -0.353553
v -0.517982 1.25052 -0.353553
v -1.25052 0.517982 0.353553
v -0.597239 0.247384 0.353553
v -0.597239 0.247384 -0.353553
v -1.25052 0.517982 -0.353553
v -1.25052 -0.517982 0.353553
v -0.597239 -0.247384 0.353553
v -0.597239 -0.247384 -0.353553
v -1.25052 -0.517982 -0.353553
v -0.517982 -1.25052 0.353553
v -0.247384 -0.597239 0.353553
v -0.247384 -0.597239 -0.353553
v -0.517982 -1.25052 -0.353553
v 0.517982 -1.25052 0.353553
v 0.247384 -0.597239 0.353553
v 0.247384 -0.597239 -0.353553
v 0.517982 -1.25052 -0.353553
v 1.25052 -0.517982 0.353553
v 0.597239 -0.247384 0.353553
v 0.597239 -0.247384 -0.353553
v 1.25052 -0.517982 -0.353553
vt 0 0
vt 1 0
vt 1 1
vt 0 1
f 5/1/1 6/2/2 2/3/3 1/4/4
f 6/1/5 7/2/6 3/3/7 2/4/8
f 7/1/9 8/2/10 4/3/11 3/4/12
f 8/1/13 5/2/14 1/3/15 4/4/16
f 9/1/17 10/2/18 6/3/19 5/4/20
f 10/1/21 11/2/22 7/3/23 6/4/24
f 11/1/25 12/2/26 8/3/27 7/4/28
f 12/1/29 9/2/30 5/3/31 8/4/32
f 13/1/33 14/2/34 10/3/35 9/4/36
f 14/1/37 15/2/38 11/3/39 10/4/40
f 15/1/41 16/2/42 12/3/43 11/4/44
f 16/1/45 13/2/46 9/3/47 12/4/48
f 17/1/49 18/2/50 14/3/51 13/4/52
f 18/1/53 19/2/54 15/3/55 14/4/56
f 19/1/57 20/2/58 16/3/59 15/4/60
f 20/1/61 17/2/62 13/3/63 16/4/64
f 21/1/65 22/2/66 18/3/67 17/4/68
f 22/1/69 23/2/70 19/3/71 18/4/72
f 23/1/73 24/2/74 20/3/75 19/4/76
f 24/1/77 21/2/78 17/3/79 20/4/80
f 25/1/81 26/2/82 22/3/83 21/4/84
f 26/1/85 27/2/86 23/3/87 22/4/88
f 27/1/89 28/2/90 24/3/91 23/4/92
f 28/1/93 25/2/94 21/3/95 24/4/96
f 29/1/97 30/2/98 26/3/99 25/4/100
f 30/1/101 31/2/102 27/3/103 26/4/104
f 31/1/105 32/2/106 28/3/107 27/4/108
f 32/1/109 29/2/110 25/3/111 28/4/112
f 1/1/113 2/2/114 30/3/115 29/4/116
f 2/1/117 3/2/118 31/3/119 30/4/120
f 3/1/121 4/2/122 32/3/123 31/4/124
f 4/1/125 1/2/126 29/3/127 32/4/128
t crease 2/1/0 1 5 4.7
t crease 2/1/0 5 9 4.7
t crease 2/1/0 9 13 4.7
t crease 2/1/0 13 17 4.7
t crease 2/1/0 17 21 4.7
t crease 2/1/0 21 25 4.7
t crease 2/1/0 25 29 4.7
t crease 2/1/0 29 1 4.7

View File

@@ -86,12 +86,9 @@ main(
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]);
std::string err = tinyobj::LoadObj(shapes[i], materials[i], argv[i+1]);
if (!err.empty()) {
std::cerr << err << std::endl;
}
if (!ret) {
exit(1);
}

View File

@@ -1,12 +0,0 @@
# A simple test for the minimal standard C++ library
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := tinyobjloader
LOCAL_SRC_FILES := ../tiny_obj_loader.cc ../test.cc
LOCAL_C_INCLUDES := ../
include $(BUILD_EXECUTABLE)

View File

@@ -1,2 +0,0 @@
APP_ABI := all
APP_STL := stlport_static

View File

@@ -1,2 +0,0 @@
all:
ndk-build

View File

@@ -1 +0,0 @@
Just tests compilation with Android NDK r10.

View File

@@ -1,145 +0,0 @@
# cornell_box.obj and cornell_box.mtl are grabbed from Intel's embree project.
# original cornell box data
# comment
# empty line including some space
#mtllib no_material.mtl
o floor
usemtl white
v 552.8 0.0 0.0
v 0.0 0.0 0.0
v 0.0 0.0 559.2
v 549.6 0.0 559.2
v 130.0 0.0 65.0
v 82.0 0.0 225.0
v 240.0 0.0 272.0
v 290.0 0.0 114.0
v 423.0 0.0 247.0
v 265.0 0.0 296.0
v 314.0 0.0 456.0
v 472.0 0.0 406.0
f 1 2 3 4
f 8 7 6 5
f 12 11 10 9
o light
usemtl light
v 343.0 548.0 227.0
v 343.0 548.0 332.0
v 213.0 548.0 332.0
v 213.0 548.0 227.0
f -4 -3 -2 -1
o ceiling
usemtl white
v 556.0 548.8 0.0
v 556.0 548.8 559.2
v 0.0 548.8 559.2
v 0.0 548.8 0.0
f -4 -3 -2 -1
o back_wall
usemtl white
v 549.6 0.0 559.2
v 0.0 0.0 559.2
v 0.0 548.8 559.2
v 556.0 548.8 559.2
f -4 -3 -2 -1
o front_wall
usemtl blue
v 549.6 0.0 0
v 0.0 0.0 0
v 0.0 548.8 0
v 556.0 548.8 0
#f -1 -2 -3 -4
o green_wall
usemtl green
v 0.0 0.0 559.2
v 0.0 0.0 0.0
v 0.0 548.8 0.0
v 0.0 548.8 559.2
f -4 -3 -2 -1
o red_wall
usemtl red
v 552.8 0.0 0.0
v 549.6 0.0 559.2
v 556.0 548.8 559.2
v 556.0 548.8 0.0
f -4 -3 -2 -1
o short_block
usemtl white
v 130.0 165.0 65.0
v 82.0 165.0 225.0
v 240.0 165.0 272.0
v 290.0 165.0 114.0
f -4 -3 -2 -1
v 290.0 0.0 114.0
v 290.0 165.0 114.0
v 240.0 165.0 272.0
v 240.0 0.0 272.0
f -4 -3 -2 -1
v 130.0 0.0 65.0
v 130.0 165.0 65.0
v 290.0 165.0 114.0
v 290.0 0.0 114.0
f -4 -3 -2 -1
v 82.0 0.0 225.0
v 82.0 165.0 225.0
v 130.0 165.0 65.0
v 130.0 0.0 65.0
f -4 -3 -2 -1
v 240.0 0.0 272.0
v 240.0 165.0 272.0
v 82.0 165.0 225.0
v 82.0 0.0 225.0
f -4 -3 -2 -1
o tall_block
usemtl white
v 423.0 330.0 247.0
v 265.0 330.0 296.0
v 314.0 330.0 456.0
v 472.0 330.0 406.0
f -4 -3 -2 -1
usemtl white
v 423.0 0.0 247.0
v 423.0 330.0 247.0
v 472.0 330.0 406.0
v 472.0 0.0 406.0
f -4 -3 -2 -1
v 472.0 0.0 406.0
v 472.0 330.0 406.0
v 314.0 330.0 456.0
v 314.0 0.0 456.0
f -4 -3 -2 -1
v 314.0 0.0 456.0
v 314.0 330.0 456.0
v 265.0 330.0 296.0
v 265.0 0.0 296.0
f -4 -3 -2 -1
v 265.0 0.0 296.0
v 265.0 330.0 296.0
v 423.0 330.0 247.0
v 423.0 0.0 247.0
f -4 -3 -2 -1

View File

@@ -1,133 +0,0 @@
# cornell_box.obj and cornell_box.mtl are grabbed from Intel's embree project.
# original cornell box data
# comment
# empty line including some space
o floor
v 552.8 0.0 0.0
v 0.0 0.0 0.0
v 0.0 0.0 559.2
v 549.6 0.0 559.2
v 130.0 0.0 65.0
v 82.0 0.0 225.0
v 240.0 0.0 272.0
v 290.0 0.0 114.0
v 423.0 0.0 247.0
v 265.0 0.0 296.0
v 314.0 0.0 456.0
v 472.0 0.0 406.0
f 1 2 3 4
f 8 7 6 5
f 12 11 10 9
o light
v 343.0 548.0 227.0
v 343.0 548.0 332.0
v 213.0 548.0 332.0
v 213.0 548.0 227.0
f -4 -3 -2 -1
o ceiling
v 556.0 548.8 0.0
v 556.0 548.8 559.2
v 0.0 548.8 559.2
v 0.0 548.8 0.0
f -4 -3 -2 -1
o back_wall
v 549.6 0.0 559.2
v 0.0 0.0 559.2
v 0.0 548.8 559.2
v 556.0 548.8 559.2
f -4 -3 -2 -1
o front_wall
v 549.6 0.0 0
v 0.0 0.0 0
v 0.0 548.8 0
v 556.0 548.8 0
#f -1 -2 -3 -4
o green_wall
v 0.0 0.0 559.2
v 0.0 0.0 0.0
v 0.0 548.8 0.0
v 0.0 548.8 559.2
f -4 -3 -2 -1
o red_wall
v 552.8 0.0 0.0
v 549.6 0.0 559.2
v 556.0 548.8 559.2
v 556.0 548.8 0.0
f -4 -3 -2 -1
o short_block
v 130.0 165.0 65.0
v 82.0 165.0 225.0
v 240.0 165.0 272.0
v 290.0 165.0 114.0
f -4 -3 -2 -1
v 290.0 0.0 114.0
v 290.0 165.0 114.0
v 240.0 165.0 272.0
v 240.0 0.0 272.0
f -4 -3 -2 -1
v 130.0 0.0 65.0
v 130.0 165.0 65.0
v 290.0 165.0 114.0
v 290.0 0.0 114.0
f -4 -3 -2 -1
v 82.0 0.0 225.0
v 82.0 165.0 225.0
v 130.0 165.0 65.0
v 130.0 0.0 65.0
f -4 -3 -2 -1
v 240.0 0.0 272.0
v 240.0 165.0 272.0
v 82.0 165.0 225.0
v 82.0 0.0 225.0
f -4 -3 -2 -1
o tall_block
v 423.0 330.0 247.0
v 265.0 330.0 296.0
v 314.0 330.0 456.0
v 472.0 330.0 406.0
f -4 -3 -2 -1
v 423.0 0.0 247.0
v 423.0 330.0 247.0
v 472.0 330.0 406.0
v 472.0 0.0 406.0
f -4 -3 -2 -1
v 472.0 0.0 406.0
v 472.0 330.0 406.0
v 314.0 330.0 456.0
v 314.0 0.0 456.0
f -4 -3 -2 -1
v 314.0 0.0 456.0
v 314.0 330.0 456.0
v 265.0 330.0 296.0
v 265.0 0.0 296.0
f -4 -3 -2 -1
v 265.0 0.0 296.0
v 265.0 330.0 296.0
v 423.0 330.0 247.0
v 423.0 0.0 247.0
f -4 -3 -2 -1

View File

@@ -1,3 +1,7 @@
lib_sources = {
"tiny_obj_loader.cc"
}
sources = {
"test.cc",
}
@@ -16,7 +20,7 @@ solution "TinyObjLoaderSolution"
project "tinyobjloader"
kind "ConsoleApp"
language "C++"
files { sources }
files { lib_sources, sources }
configuration "Debug"
defines { "DEBUG" } -- -DDEBUG

View File

@@ -1,581 +0,0 @@
{
"shapes": {
"back_wall": {
"material_ids": [
0.0,
0.0
],
"normals": [],
"positions": [
549.5999755859375,
0.0,
559.2000122070312,
0.0,
0.0,
559.2000122070312,
0.0,
548.7999877929688,
559.2000122070312,
556.0,
548.7999877929688,
559.2000122070312
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"texcoords": []
},
"floor": {
"material_ids": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
],
"normals": [],
"positions": [
552.7999877929688,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
559.2000122070312,
549.5999755859375,
0.0,
559.2000122070312,
290.0,
0.0,
114.0,
240.0,
0.0,
272.0,
82.0,
0.0,
225.0,
130.0,
0.0,
65.0,
472.0,
0.0,
406.0,
314.0,
0.0,
456.0,
265.0,
0.0,
296.0,
423.0,
0.0,
247.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0,
4.0,
5.0,
6.0,
4.0,
6.0,
7.0,
8.0,
9.0,
10.0,
8.0,
10.0,
11.0
],
"texcoords": []
},
"red_wall": {
"material_ids": [
1.0,
1.0
],
"normals": [],
"positions": [
552.7999877929688,
0.0,
0.0,
549.5999755859375,
0.0,
559.2000122070312,
556.0,
548.7999877929688,
559.2000122070312,
556.0,
548.7999877929688,
0.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"texcoords": []
},
"light": {
"material_ids": [
4.0,
4.0
],
"normals": [],
"positions": [
343.0,
548.0,
227.0,
343.0,
548.0,
332.0,
213.0,
548.0,
332.0,
213.0,
548.0,
227.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"texcoords": []
},
"tall_block": {
"material_ids": [
2.0,
2.0,
2.0,
2.0
],
"normals": [],
"positions": [
314.0,
0.0,
456.0,
314.0,
330.0,
456.0,
265.0,
330.0,
296.0,
265.0,
0.0,
296.0,
265.0,
0.0,
296.0,
265.0,
330.0,
296.0,
423.0,
330.0,
247.0,
423.0,
0.0,
247.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0,
4.0,
5.0,
6.0,
4.0,
6.0,
7.0
],
"texcoords": []
},
"short_block": {
"material_ids": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
],
"normals": [],
"positions": [
130.0,
165.0,
65.0,
82.0,
165.0,
225.0,
240.0,
165.0,
272.0,
290.0,
165.0,
114.0,
290.0,
0.0,
114.0,
290.0,
165.0,
114.0,
240.0,
165.0,
272.0,
240.0,
0.0,
272.0,
130.0,
0.0,
65.0,
130.0,
165.0,
65.0,
290.0,
165.0,
114.0,
290.0,
0.0,
114.0,
82.0,
0.0,
225.0,
82.0,
165.0,
225.0,
130.0,
165.0,
65.0,
130.0,
0.0,
65.0,
240.0,
0.0,
272.0,
240.0,
165.0,
272.0,
82.0,
165.0,
225.0,
82.0,
0.0,
225.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0,
4.0,
5.0,
6.0,
4.0,
6.0,
7.0,
8.0,
9.0,
10.0,
8.0,
10.0,
11.0,
12.0,
13.0,
14.0,
12.0,
14.0,
15.0,
16.0,
17.0,
18.0,
16.0,
18.0,
19.0
],
"texcoords": []
},
"green_wall": {
"material_ids": [
2.0,
2.0
],
"normals": [],
"positions": [
0.0,
0.0,
559.2000122070312,
0.0,
0.0,
0.0,
0.0,
548.7999877929688,
0.0,
0.0,
548.7999877929688,
559.2000122070312
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"texcoords": []
},
"ceiling": {
"material_ids": [
0.0,
0.0
],
"normals": [],
"positions": [
556.0,
548.7999877929688,
0.0,
556.0,
548.7999877929688,
559.2000122070312,
0.0,
548.7999877929688,
559.2000122070312,
0.0,
548.7999877929688,
0.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"texcoords": []
}
},
"materials": {
"blue": {
"diffuse_texname": "",
"ambient_texname": "",
"illum": 0,
"displacement_texname": "",
"alpha_texname": "",
"emission": [
0.0,
0.0,
0.0
],
"transmittance": [
0.0,
0.0,
0.0
],
"ambient": [
0.0,
0.0,
0.0
],
"bump_texname": "",
"diffuse": [
0.0,
0.0,
1.0
],
"shininess": 1.0,
"specular_highlight_texname": "",
"unknown_parameter": {},
"ior": 1.0,
"dissolve": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": ""
},
"white": {
"diffuse_texname": "",
"ambient_texname": "",
"illum": 0,
"displacement_texname": "",
"alpha_texname": "",
"emission": [
0.0,
0.0,
0.0
],
"transmittance": [
0.0,
0.0,
0.0
],
"ambient": [
0.0,
0.0,
0.0
],
"bump_texname": "",
"diffuse": [
1.0,
1.0,
1.0
],
"shininess": 1.0,
"specular_highlight_texname": "",
"unknown_parameter": {},
"ior": 1.0,
"dissolve": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": ""
},
"red": {
"diffuse_texname": "",
"ambient_texname": "",
"illum": 0,
"displacement_texname": "",
"alpha_texname": "",
"emission": [
0.0,
0.0,
0.0
],
"transmittance": [
0.0,
0.0,
0.0
],
"ambient": [
0.0,
0.0,
0.0
],
"bump_texname": "",
"diffuse": [
1.0,
0.0,
0.0
],
"shininess": 1.0,
"specular_highlight_texname": "",
"unknown_parameter": {},
"ior": 1.0,
"dissolve": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": ""
},
"light": {
"diffuse_texname": "",
"ambient_texname": "",
"illum": 0,
"displacement_texname": "",
"alpha_texname": "",
"emission": [
0.0,
0.0,
0.0
],
"transmittance": [
0.0,
0.0,
0.0
],
"ambient": [
20.0,
20.0,
20.0
],
"bump_texname": "",
"diffuse": [
1.0,
1.0,
1.0
],
"shininess": 1.0,
"specular_highlight_texname": "",
"unknown_parameter": {},
"ior": 1.0,
"dissolve": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": ""
},
"green": {
"diffuse_texname": "",
"ambient_texname": "",
"illum": 0,
"displacement_texname": "",
"alpha_texname": "",
"emission": [
0.0,
0.0,
0.0
],
"transmittance": [
0.0,
0.0,
0.0
],
"ambient": [
0.0,
0.0,
0.0
],
"bump_texname": "",
"diffuse": [
0.0,
1.0,
0.0
],
"shininess": 1.0,
"specular_highlight_texname": "",
"unknown_parameter": {},
"ior": 1.0,
"dissolve": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": ""
}
}
}

View File

@@ -0,0 +1,601 @@
{
"shapes": {
"ceiling": {
"texcoords": [],
"positions": [
556.0,
548.7999877929688,
0.0,
556.0,
548.7999877929688,
559.2000122070312,
0.0,
548.7999877929688,
559.2000122070312,
0.0,
548.7999877929688,
0.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"material_ids": [
0.0,
0.0
],
"normals": []
},
"floor": {
"texcoords": [],
"positions": [
552.7999877929688,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
559.2000122070312,
549.5999755859375,
0.0,
559.2000122070312,
290.0,
0.0,
114.0,
240.0,
0.0,
272.0,
82.0,
0.0,
225.0,
130.0,
0.0,
65.0,
472.0,
0.0,
406.0,
314.0,
0.0,
456.0,
265.0,
0.0,
296.0,
423.0,
0.0,
247.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0,
4.0,
5.0,
6.0,
4.0,
6.0,
7.0,
8.0,
9.0,
10.0,
8.0,
10.0,
11.0
],
"material_ids": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
],
"normals": []
},
"light": {
"texcoords": [],
"positions": [
343.0,
548.0,
227.0,
343.0,
548.0,
332.0,
213.0,
548.0,
332.0,
213.0,
548.0,
227.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"material_ids": [
4.0,
4.0
],
"normals": []
},
"green_wall": {
"texcoords": [],
"positions": [
0.0,
0.0,
559.2000122070312,
0.0,
0.0,
0.0,
0.0,
548.7999877929688,
0.0,
0.0,
548.7999877929688,
559.2000122070312
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"material_ids": [
2.0,
2.0
],
"normals": []
},
"back_wall": {
"texcoords": [],
"positions": [
549.5999755859375,
0.0,
559.2000122070312,
0.0,
0.0,
559.2000122070312,
0.0,
548.7999877929688,
559.2000122070312,
556.0,
548.7999877929688,
559.2000122070312
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"material_ids": [
0.0,
0.0
],
"normals": []
},
"short_block": {
"texcoords": [],
"positions": [
130.0,
165.0,
65.0,
82.0,
165.0,
225.0,
240.0,
165.0,
272.0,
290.0,
165.0,
114.0,
290.0,
0.0,
114.0,
290.0,
165.0,
114.0,
240.0,
165.0,
272.0,
240.0,
0.0,
272.0,
130.0,
0.0,
65.0,
130.0,
165.0,
65.0,
290.0,
165.0,
114.0,
290.0,
0.0,
114.0,
82.0,
0.0,
225.0,
82.0,
165.0,
225.0,
130.0,
165.0,
65.0,
130.0,
0.0,
65.0,
240.0,
0.0,
272.0,
240.0,
165.0,
272.0,
82.0,
165.0,
225.0,
82.0,
0.0,
225.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0,
4.0,
5.0,
6.0,
4.0,
6.0,
7.0,
8.0,
9.0,
10.0,
8.0,
10.0,
11.0,
12.0,
13.0,
14.0,
12.0,
14.0,
15.0,
16.0,
17.0,
18.0,
16.0,
18.0,
19.0
],
"material_ids": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
],
"normals": []
},
"tall_block": {
"texcoords": [],
"positions": [
423.0,
0.0,
247.0,
423.0,
330.0,
247.0,
472.0,
330.0,
406.0,
472.0,
0.0,
406.0,
472.0,
0.0,
406.0,
472.0,
330.0,
406.0,
314.0,
330.0,
456.0,
314.0,
0.0,
456.0,
314.0,
0.0,
456.0,
314.0,
330.0,
456.0,
265.0,
330.0,
296.0,
265.0,
0.0,
296.0,
265.0,
0.0,
296.0,
265.0,
330.0,
296.0,
423.0,
330.0,
247.0,
423.0,
0.0,
247.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0,
4.0,
5.0,
6.0,
4.0,
6.0,
7.0,
8.0,
9.0,
10.0,
8.0,
10.0,
11.0,
12.0,
13.0,
14.0,
12.0,
14.0,
15.0
],
"material_ids": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
],
"normals": []
},
"red_wall": {
"texcoords": [],
"positions": [
552.7999877929688,
0.0,
0.0,
549.5999755859375,
0.0,
559.2000122070312,
556.0,
548.7999877929688,
559.2000122070312,
556.0,
548.7999877929688,
0.0
],
"indicies": [
0.0,
1.0,
2.0,
0.0,
2.0,
3.0
],
"material_ids": [
1.0,
1.0
],
"normals": []
}
},
"materials": {
"blue": {
"transmittance": [
0.0,
0.0,
0.0
],
"illum": 0,
"emission": [
0.0,
0.0,
0.0
],
"diffuse_texname": "",
"ambient_texname": "",
"normal_texname": "",
"shininess": 1.0,
"ior": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": "",
"diffuse": [
0.0,
0.0,
1.0
],
"ambient": [
0.0,
0.0,
0.0
],
"dissolve": 1.0
},
"light": {
"transmittance": [
0.0,
0.0,
0.0
],
"illum": 0,
"emission": [
0.0,
0.0,
0.0
],
"diffuse_texname": "",
"ambient_texname": "",
"normal_texname": "",
"shininess": 1.0,
"ior": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": "",
"diffuse": [
1.0,
1.0,
1.0
],
"ambient": [
20.0,
20.0,
20.0
],
"dissolve": 1.0
},
"white": {
"transmittance": [
0.0,
0.0,
0.0
],
"illum": 0,
"emission": [
0.0,
0.0,
0.0
],
"diffuse_texname": "",
"ambient_texname": "",
"normal_texname": "",
"shininess": 1.0,
"ior": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": "",
"diffuse": [
1.0,
1.0,
1.0
],
"ambient": [
0.0,
0.0,
0.0
],
"dissolve": 1.0
},
"green": {
"transmittance": [
0.0,
0.0,
0.0
],
"illum": 0,
"emission": [
0.0,
0.0,
0.0
],
"diffuse_texname": "",
"ambient_texname": "",
"normal_texname": "",
"shininess": 1.0,
"ior": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": "",
"diffuse": [
0.0,
1.0,
0.0
],
"ambient": [
0.0,
0.0,
0.0
],
"dissolve": 1.0
},
"red": {
"transmittance": [
0.0,
0.0,
0.0
],
"illum": 0,
"emission": [
0.0,
0.0,
0.0
],
"diffuse_texname": "",
"ambient_texname": "",
"normal_texname": "",
"shininess": 1.0,
"ior": 1.0,
"specular": [
0.0,
0.0,
0.0
],
"specular_texname": "",
"diffuse": [
1.0,
0.0,
0.0
],
"ambient": [
0.0,
0.0,
0.0
],
"dissolve": 1.0
}
}
}

View File

@@ -1,9 +1,11 @@
import tinyobjloader as tol
import json
model = tol.LoadObj("cornell_box_multimaterial.obj")
model = tol.LoadObj("cornell_box.obj")
#print(model["shapes"], model["materials"])
print( json.dumps(model, indent=4) )
#see cornell_box_output.json

View File

@@ -44,8 +44,7 @@ pyLoadObj(PyObject* self, PyObject* args)
if(!PyArg_ParseTuple(args, "s", &filename))
return NULL;
std::string err;
tinyobj::LoadObj(shapes, materials, err, filename);
tinyobj::LoadObj(shapes, materials, filename);
pyshapes = PyDict_New();
pymaterials = PyDict_New();
@@ -98,13 +97,6 @@ pyLoadObj(PyObject* self, PyObject* args)
mat != materials.end(); mat++)
{
PyObject *matobj = PyDict_New();
PyObject *unknown_parameter = PyDict_New();
for (std::map<std::string, std::string>::iterator p = (*mat).unknown_parameter.begin() ;
p != (*mat).unknown_parameter.end(); ++p)
{
PyDict_SetItemString(unknown_parameter, p->first.c_str(), PyUnicode_FromString(p->second.c_str()));
}
PyDict_SetItemString(matobj, "shininess", PyFloat_FromDouble((*mat).shininess));
PyDict_SetItemString(matobj, "ior", PyFloat_FromDouble((*mat).ior));
@@ -113,16 +105,12 @@ pyLoadObj(PyObject* self, PyObject* args)
PyDict_SetItemString(matobj, "ambient_texname", PyUnicode_FromString((*mat).ambient_texname.c_str()));
PyDict_SetItemString(matobj, "diffuse_texname", PyUnicode_FromString((*mat).diffuse_texname.c_str()));
PyDict_SetItemString(matobj, "specular_texname", PyUnicode_FromString((*mat).specular_texname.c_str()));
PyDict_SetItemString(matobj, "specular_highlight_texname", PyUnicode_FromString((*mat).specular_highlight_texname.c_str()));
PyDict_SetItemString(matobj, "bump_texname", PyUnicode_FromString((*mat).bump_texname.c_str()));
PyDict_SetItemString(matobj, "displacement_texname", PyUnicode_FromString((*mat).displacement_texname.c_str()));
PyDict_SetItemString(matobj, "alpha_texname", PyUnicode_FromString((*mat).alpha_texname.c_str()));
PyDict_SetItemString(matobj, "normal_texname", PyUnicode_FromString((*mat).normal_texname.c_str()));
PyDict_SetItemString(matobj, "ambient", pyTupleFromfloat3((*mat).ambient));
PyDict_SetItemString(matobj, "diffuse", pyTupleFromfloat3((*mat).diffuse));
PyDict_SetItemString(matobj, "specular", pyTupleFromfloat3((*mat).specular));
PyDict_SetItemString(matobj, "transmittance", pyTupleFromfloat3((*mat).transmittance));
PyDict_SetItemString(matobj, "emission", pyTupleFromfloat3((*mat).emission));
PyDict_SetItemString(matobj, "unknown_parameter", unknown_parameter);
PyDict_SetItemString(pymaterials, (*mat).name.c_str(), matobj);
}

96
python/pyTOL.cbp.mak Normal file
View File

@@ -0,0 +1,96 @@
#------------------------------------------------------------------------------#
# This makefile was generated by 'cbp2make' tool rev.147 #
#------------------------------------------------------------------------------#
WORKDIR = `pwd`
CC = gcc
CXX = g++
AR = ar
LD = g++
WINDRES = windres
INC =
CFLAGS = -Wall -fexceptions `python3-config --cflags`
RESINC =
LIBDIR =
LIB =
LDFLAGS = `python3-config --ldflags`
INC_DEBUG = $(INC)
CFLAGS_DEBUG = $(CFLAGS) -g
RESINC_DEBUG = $(RESINC)
RCFLAGS_DEBUG = $(RCFLAGS)
LIBDIR_DEBUG = $(LIBDIR)
LIB_DEBUG = $(LIB)
LDFLAGS_DEBUG = $(LDFLAGS)
OBJDIR_DEBUG = obj/Debug
DEP_DEBUG =
OUT_DEBUG = bin/Debug/tinyobjloader.so
INC_RELEASE = $(INC)
CFLAGS_RELEASE = $(CFLAGS) -O2
RESINC_RELEASE = $(RESINC)
RCFLAGS_RELEASE = $(RCFLAGS)
LIBDIR_RELEASE = $(LIBDIR)
LIB_RELEASE = $(LIB)
LDFLAGS_RELEASE = $(LDFLAGS) -s
OBJDIR_RELEASE = obj/Release
DEP_RELEASE =
OUT_RELEASE = bin/Release/tinyobjloader.so
OBJ_DEBUG = $(OBJDIR_DEBUG)/main.o $(OBJDIR_DEBUG)/tiny_obj_loader.o
OBJ_RELEASE = $(OBJDIR_RELEASE)/main.o $(OBJDIR_RELEASE)/tiny_obj_loader.o
all: debug release
clean: clean_debug clean_release
before_debug:
test -d bin/Debug || mkdir -p bin/Debug
test -d $(OBJDIR_DEBUG) || mkdir -p $(OBJDIR_DEBUG)
after_debug:
debug: before_debug out_debug after_debug
out_debug: before_debug $(OBJ_DEBUG) $(DEP_DEBUG)
$(LD) -shared $(LIBDIR_DEBUG) $(OBJ_DEBUG) -o $(OUT_DEBUG) $(LDFLAGS_DEBUG) $(LIB_DEBUG)
$(OBJDIR_DEBUG)/main.o: main.cpp
$(CXX) $(CFLAGS_DEBUG) $(INC_DEBUG) -c main.cpp -o $(OBJDIR_DEBUG)/main.o
$(OBJDIR_DEBUG)/tiny_obj_loader.o: ../tiny_obj_loader.cc
$(CC) $(CFLAGS_DEBUG) $(INC_DEBUG) -c ../tiny_obj_loader.cc -o $(OBJDIR_DEBUG)/tiny_obj_loader.o
clean_debug:
rm -f $(OBJ_DEBUG) $(OUT_DEBUG)
rm -rf bin/Debug
rm -rf $(OBJDIR_DEBUG)
before_release:
test -d bin/Release || mkdir -p bin/Release
test -d $(OBJDIR_RELEASE) || mkdir -p $(OBJDIR_RELEASE)
after_release:
release: before_release out_release after_release
out_release: before_release $(OBJ_RELEASE) $(DEP_RELEASE)
$(LD) -shared $(LIBDIR_RELEASE) $(OBJ_RELEASE) -o $(OUT_RELEASE) $(LDFLAGS_RELEASE) $(LIB_RELEASE)
$(OBJDIR_RELEASE)/main.o: main.cpp
$(CXX) $(CFLAGS_RELEASE) $(INC_RELEASE) -c main.cpp -o $(OBJDIR_RELEASE)/main.o
$(OBJDIR_RELEASE)/tiny_obj_loader.o: ../tiny_obj_loader.cc
$(CC) $(CFLAGS_RELEASE) $(INC_RELEASE) -c ../tiny_obj_loader.cc -o $(OBJDIR_RELEASE)/tiny_obj_loader.o
clean_release:
rm -f $(OBJ_RELEASE) $(OUT_RELEASE)
rm -rf bin/Release
rm -rf $(OBJDIR_RELEASE)
.PHONY: before_debug after_debug clean_debug before_release after_release clean_release

View File

@@ -1,145 +0,0 @@
# cornell_box.obj and cornell_box.mtl are grabbed from Intel's embree project.
# original cornell box data
# comment
# empty line including some space
mtllib cornell_box.mtl
o floor
usemtl white
v nan -nan nan
v inf -inf inf
v 1.#IND -1.#IND 1.#IND
v 1.#INF -1.#INF 1.#INF
v 130.0 0.0 65.0
v 82.0 0.0 225.0
v 240.0 0.0 272.0
v 290.0 0.0 114.0
v 423.0 0.0 247.0
v 265.0 0.0 296.0
v 314.0 0.0 456.0
v 472.0 0.0 406.0
f 1 2 3 4
f 8 7 6 5
f 12 11 10 9
o light
usemtl light
v 343.0 548.0 227.0
v 343.0 548.0 332.0
v 213.0 548.0 332.0
v 213.0 548.0 227.0
f -4 -3 -2 -1
o ceiling
usemtl white
v 556.0 548.8 0.0
v 556.0 548.8 559.2
v 0.0 548.8 559.2
v 0.0 548.8 0.0
f -4 -3 -2 -1
o back_wall
usemtl white
v 549.6 0.0 559.2
v 0.0 0.0 559.2
v 0.0 548.8 559.2
v 556.0 548.8 559.2
f -4 -3 -2 -1
o front_wall
usemtl blue
v 549.6 0.0 0
v 0.0 0.0 0
v 0.0 548.8 0
v 556.0 548.8 0
#f -1 -2 -3 -4
o green_wall
usemtl green
v 0.0 0.0 559.2
v 0.0 0.0 0.0
v 0.0 548.8 0.0
v 0.0 548.8 559.2
f -4 -3 -2 -1
o red_wall
usemtl red
v 552.8 0.0 0.0
v 549.6 0.0 559.2
v 556.0 548.8 559.2
v 556.0 548.8 0.0
f -4 -3 -2 -1
o short_block
usemtl white
v 130.0 165.0 65.0
v 82.0 165.0 225.0
v 240.0 165.0 272.0
v 290.0 165.0 114.0
f -4 -3 -2 -1
v 290.0 0.0 114.0
v 290.0 165.0 114.0
v 240.0 165.0 272.0
v 240.0 0.0 272.0
f -4 -3 -2 -1
v 130.0 0.0 65.0
v 130.0 165.0 65.0
v 290.0 165.0 114.0
v 290.0 0.0 114.0
f -4 -3 -2 -1
v 82.0 0.0 225.0
v 82.0 165.0 225.0
v 130.0 165.0 65.0
v 130.0 0.0 65.0
f -4 -3 -2 -1
v 240.0 0.0 272.0
v 240.0 165.0 272.0
v 82.0 165.0 225.0
v 82.0 0.0 225.0
f -4 -3 -2 -1
o tall_block
usemtl white
v 423.0 330.0 247.0
v 265.0 330.0 296.0
v 314.0 330.0 456.0
v 472.0 330.0 406.0
f -4 -3 -2 -1
usemtl white
v 423.0 0.0 247.0
v 423.0 330.0 247.0
v 472.0 330.0 406.0
v 472.0 0.0 406.0
f -4 -3 -2 -1
v 472.0 0.0 406.0
v 472.0 330.0 406.0
v 314.0 330.0 456.0
v 314.0 0.0 456.0
f -4 -3 -2 -1
v 314.0 0.0 456.0
v 314.0 330.0 456.0
v 265.0 330.0 296.0
v 265.0 0.0 296.0
f -4 -3 -2 -1
v 265.0 0.0 296.0
v 265.0 330.0 296.0
v 423.0 330.0 247.0
v 423.0 0.0 247.0
f -4 -3 -2 -1

128
test.cc
View File

@@ -1,4 +1,3 @@
#define TINYOBJLOADER_IMPLEMENTATION
#include "tiny_obj_loader.h"
#include <cstdio>
@@ -8,40 +7,19 @@
#include <sstream>
#include <fstream>
static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::material_t>& materials, bool triangulate = true)
static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::vector<tinyobj::curve_t>& curves, const std::vector<tinyobj::material_t>& materials)
{
std::cout << "# of shapes : " << shapes.size() << std::endl;
std::cout << "# of curves : " << curves.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());
if (triangulate)
{
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]);
}
} else {
for (size_t f = 0; f < shapes[i].mesh.indices.size(); f++) {
printf(" idx[%ld] = %d\n", f, shapes[i].mesh.indices[f]);
}
printf("Size of shape[%ld].material_ids: %ld\n", i, shapes[i].mesh.material_ids.size());
assert(shapes[i].mesh.material_ids.size() == shapes[i].mesh.num_vertices.size());
for (size_t m = 0; m < shapes[i].mesh.material_ids.size(); m++) {
printf(" material_id[%ld] = %d\n", m,
shapes[i].mesh.material_ids[m]);
}
}
printf("shape[%ld].num_faces: %ld\n", i, shapes[i].mesh.num_vertices.size());
for (size_t v = 0; v < shapes[i].mesh.num_vertices.size(); v++) {
printf(" num_vertices[%ld] = %ld\n", v,
static_cast<long>(shapes[i].mesh.num_vertices[v]));
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());
@@ -52,46 +30,31 @@ static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::ve
shapes[i].mesh.positions[3*v+1],
shapes[i].mesh.positions[3*v+2]);
}
}
printf("shape[%ld].num_tags: %ld\n", i, shapes[i].mesh.tags.size());
for (size_t t = 0; t < shapes[i].mesh.tags.size(); t++) {
printf(" tag[%ld] = %s ", t, shapes[i].mesh.tags[t].name.c_str());
printf(" ints: [");
for (size_t j = 0; j < shapes[i].mesh.tags[t].intValues.size(); ++j)
{
printf("%ld", static_cast<long>(shapes[i].mesh.tags[t].intValues[j]));
if (j < (shapes[i].mesh.tags[t].intValues.size()-1))
{
printf(", ");
}
}
printf("]");
for (size_t i = 0; i < curves.size(); i++) {
printf("curve[%ld].name = %s\n", i, curves[i].name.c_str());
printf("Size of curve[%ld].indices: %ld\n", i, curves[i].indices.size());
printf(" floats: [");
for (size_t j = 0; j < shapes[i].mesh.tags[t].floatValues.size(); ++j)
{
printf("%f", shapes[i].mesh.tags[t].floatValues[j]);
if (j < (shapes[i].mesh.tags[t].floatValues.size()-1))
{
printf(", ");
}
}
printf("]");
printf("curves[%ld].vertices: %ld\n", i, curves[i].positions.size());
assert((curves[i].positions.size() % 3) == 0);
for (size_t v = 0; v < curves[i].positions.size() / 3; v++) {
printf(" v[%ld] = (%f, %f, %f)\n", v,
curves[i].positions[3*v+0],
curves[i].positions[3*v+1],
curves[i].positions[3*v+2]);
}
printf(" strings: [");
for (size_t j = 0; j < shapes[i].mesh.tags[t].stringValues.size(); ++j)
{
printf("%s", shapes[i].mesh.tags[t].stringValues[j].c_str());
if (j < (shapes[i].mesh.tags[t].stringValues.size()-1))
{
printf(", ");
}
}
printf("]");
printf("\n");
for (size_t v = 0; v < curves[i].u_params.size(); v++) {
printf(" u[%ld] = %f\n", v, curves[i].u_params[v]);
}
for (size_t v = 0; v < curves[i].v_params.size(); v++) {
printf(" u[%ld] = %f\n", v, curves[i].v_params[v]);
}
}
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]);
@@ -106,13 +69,9 @@ static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::ve
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());
printf(" material.map_bump = %s\n", materials[i].bump_texname.c_str());
printf(" material.map_d = %s\n", materials[i].alpha_texname.c_str());
printf(" material.disp = %s\n", materials[i].displacement_texname.c_str());
printf(" material.map_Ns = %s\n", materials[i].normal_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());
}
@@ -123,27 +82,21 @@ static void PrintInfo(const std::vector<tinyobj::shape_t>& shapes, const std::ve
static bool
TestLoadObj(
const char* filename,
const char* basepath = NULL,
bool triangulate = true)
const char* basepath = NULL)
{
std::cout << "Loading " << filename << std::endl;
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::curve_t> curves;
std::vector<tinyobj::material_t> materials;
std::string err;
bool ret = tinyobj::LoadObj(shapes, materials, err, filename, basepath, triangulate);
std::string err = tinyobj::LoadObj(shapes, curves, materials, filename, basepath);
if (!err.empty()) {
std::cerr << err << std::endl;
}
if (!ret) {
printf("Failed to load/parse .obj.\n");
return false;
}
PrintInfo(shapes, materials, triangulate);
PrintInfo(shapes, curves, materials);
return true;
}
@@ -221,16 +174,12 @@ std::string matStream(
public:
MaterialStringStreamReader(const std::string& matSStream): m_matSStream(matSStream) {}
virtual ~MaterialStringStreamReader() {}
virtual bool operator() (
virtual std::string operator() (
const std::string& matId,
std::vector<material_t>& materials,
std::map<std::string, int>& matMap,
std::string& err)
std::map<std::string, int>& matMap)
{
(void)matId;
(void)err;
LoadMtl(matMap, materials, m_matSStream);
return true;
return LoadMtl(matMap, materials, m_matSStream);
}
private:
@@ -239,19 +188,16 @@ std::string matStream(
MaterialStringStreamReader matSSReader(matStream);
std::vector<tinyobj::shape_t> shapes;
std::vector<tinyobj::curve_t> curves;
std::vector<tinyobj::material_t> materials;
std::string err;
bool ret = tinyobj::LoadObj(shapes, materials, err, objStream, matSSReader);
std::string err = tinyobj::LoadObj(shapes, curves, materials, objStream, matSSReader);
if (!err.empty()) {
std::cerr << err << std::endl;
}
if (!ret) {
return false;
}
PrintInfo(shapes, materials);
PrintInfo(shapes, curves, materials);
return true;
}
@@ -261,6 +207,7 @@ main(
int argc,
char **argv)
{
if (argc > 1) {
const char* basepath = NULL;
if (argc > 2) {
@@ -271,7 +218,6 @@ main(
//assert(true == TestLoadObj("cornell_box.obj"));
//assert(true == TestLoadObj("cube.obj"));
assert(true == TestStreamLoadObj());
assert(true == TestLoadObj("catmark_torus_creases0.obj", NULL, false));
}
return 0;

View File

@@ -1,2 +1,989 @@
#define TINYOBJLOADER_IMPLEMENTATION
//
// Copyright 2012-2015, Syoyo Fujita.
//
// Licensed under 2-clause BSD liecense.
//
//
// version 0.9.14: Initial support of parsing curve primitive.
// version 0.9.13: Report "Material file not found message" in `err`(#46)
// version 0.9.12: Fix groups being ignored if they have 'usemtl' just before
// 'g' (#44)
// version 0.9.11: Invert `Tr` parameter(#43)
// version 0.9.10: Fix seg fault on windows.
// version 0.9.9 : Replace atof() with custom parser.
// version 0.9.8 : Fix multi-materials(per-face material ID).
// version 0.9.7 : Support multi-materials(per-face material ID) per
// object/group.
// version 0.9.6 : Support Ni(index of refraction) mtl parameter.
// Parse transmittance material parameter correctly.
// version 0.9.5 : Parse multiple group name.
// Add support of specifying the base path to load material
// file.
// version 0.9.4 : Initial suupport of group tag(g)
// version 0.9.3 : Fix parsing triple 'x/y/z'
// version 0.9.2 : Add more .mtl load support
// version 0.9.1 : Add initial .mtl load support
// version 0.9.0 : Initial
//
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <string>
#include <vector>
#include <map>
#include <fstream>
#include <sstream>
#include "tiny_obj_loader.h"
namespace tinyobj {
#define TINYOBJ_SSCANF_BUFFER_SIZE (4096)
struct vertex_index {
int v_idx, vt_idx, vn_idx;
vertex_index(){};
vertex_index(int idx) : v_idx(idx), vt_idx(idx), vn_idx(idx){};
vertex_index(int vidx, int vtidx, int vnidx)
: v_idx(vidx), vt_idx(vtidx), vn_idx(vnidx){};
};
// for std::map
static inline bool operator<(const vertex_index &a, const vertex_index &b) {
if (a.v_idx != b.v_idx)
return (a.v_idx < b.v_idx);
if (a.vn_idx != b.vn_idx)
return (a.vn_idx < b.vn_idx);
if (a.vt_idx != b.vt_idx)
return (a.vt_idx < b.vt_idx);
return false;
}
struct obj_shape {
std::vector<float> v;
std::vector<float> vn;
std::vector<float> vt;
};
static inline bool isSpace(const char c) { return (c == ' ') || (c == '\t'); }
static inline bool isNewLine(const char c) {
return (c == '\r') || (c == '\n') || (c == '\0');
}
// Make index zero-base, and also support relative index.
static inline int fixIndex(int idx, int n) {
if (idx > 0)
return idx - 1;
if (idx == 0)
return 0;
return n + idx; // negative value = relative
}
static inline std::string parseString(const char *&token) {
std::string s;
token += strspn(token, " \t");
size_t e = strcspn(token, " \t\r");
s = std::string(token, &token[e]);
token += e;
return s;
}
static inline int parseInt(const char *&token) {
token += strspn(token, " \t");
int i = atoi(token);
token += strcspn(token, " \t\r");
return i;
}
// Tries to parse a floating point number located at s.
//
// s_end should be a location in the string where reading should absolutely
// stop. For example at the end of the string, to prevent buffer overflows.
//
// Parses the following EBNF grammar:
// sign = "+" | "-" ;
// END = ? anything not in digit ?
// digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
// integer = [sign] , digit , {digit} ;
// decimal = integer , ["." , integer] ;
// float = ( decimal , END ) | ( decimal , ("E" | "e") , integer , END ) ;
//
// Valid strings are for example:
// -0 +3.1417e+2 -0.0E-3 1.0324 -1.41 11e2
//
// If the parsing is a success, result is set to the parsed value and true
// is returned.
//
// The function is greedy and will parse until any of the following happens:
// - a non-conforming character is encountered.
// - s_end is reached.
//
// The following situations triggers a failure:
// - s >= s_end.
// - parse failure.
//
static bool tryParseDouble(const char *s, const char *s_end, double *result) {
if (s >= s_end) {
return false;
}
double mantissa = 0.0;
// This exponent is base 2 rather than 10.
// However the exponent we parse is supposed to be one of ten,
// thus we must take care to convert the exponent/and or the
// mantissa to a * 2^E, where a is the mantissa and E is the
// exponent.
// To get the final double we will use ldexp, it requires the
// exponent to be in base 2.
int exponent = 0;
// NOTE: THESE MUST BE DECLARED HERE SINCE WE ARE NOT ALLOWED
// TO JUMP OVER DEFINITIONS.
char sign = '+';
char exp_sign = '+';
char const *curr = s;
// How many characters were read in a loop.
int read = 0;
// Tells whether a loop terminated due to reaching s_end.
bool end_not_reached = false;
/*
BEGIN PARSING.
*/
// Find out what sign we've got.
if (*curr == '+' || *curr == '-') {
sign = *curr;
curr++;
} else if (isdigit(*curr)) { /* Pass through. */
} else {
goto fail;
}
// Read the integer part.
while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) {
mantissa *= 10;
mantissa += static_cast<int>(*curr - 0x30);
curr++;
read++;
}
// We must make sure we actually got something.
if (read == 0)
goto fail;
// We allow numbers of form "#", "###" etc.
if (!end_not_reached)
goto assemble;
// Read the decimal part.
if (*curr == '.') {
curr++;
read = 1;
while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) {
// NOTE: Don't use powf here, it will absolutely murder precision.
mantissa += static_cast<int>(*curr - 0x30) * pow(10.0, -read);
read++;
curr++;
}
} else if (*curr == 'e' || *curr == 'E') {
} else {
goto assemble;
}
if (!end_not_reached)
goto assemble;
// Read the exponent part.
if (*curr == 'e' || *curr == 'E') {
curr++;
// Figure out if a sign is present and if it is.
if ((end_not_reached = (curr != s_end)) && (*curr == '+' || *curr == '-')) {
exp_sign = *curr;
curr++;
} else if (isdigit(*curr)) { /* Pass through. */
} else {
// Empty E is not allowed.
goto fail;
}
read = 0;
while ((end_not_reached = (curr != s_end)) && isdigit(*curr)) {
exponent *= 10;
exponent += static_cast<int>(*curr - 0x30);
curr++;
read++;
}
exponent *= (exp_sign == '+' ? 1 : -1);
if (read == 0)
goto fail;
}
assemble:
*result =
(sign == '+' ? 1 : -1) * ldexp(mantissa * pow(5.0, exponent), exponent);
return true;
fail:
return false;
}
static inline float parseFloat(const char *&token) {
token += strspn(token, " \t");
#ifdef TINY_OBJ_LOADER_OLD_FLOAT_PARSER
float f = (float)atof(token);
token += strcspn(token, " \t\r");
#else
const char *end = token + strcspn(token, " \t\r");
double val = 0.0;
tryParseDouble(token, end, &val);
float f = static_cast<float>(val);
token = end;
#endif
return f;
}
static inline void parseFloat2(float &x, float &y, const char *&token) {
x = parseFloat(token);
y = parseFloat(token);
}
static inline void parseFloat3(float &x, float &y, float &z,
const char *&token) {
x = parseFloat(token);
y = parseFloat(token);
z = parseFloat(token);
}
// Parse triples: i, i/j/k, i//k, i/j
static vertex_index parseTriple(const char *&token, int vsize, int vnsize,
int vtsize) {
vertex_index vi(-1);
vi.v_idx = fixIndex(atoi(token), vsize);
token += strcspn(token, "/ \t\r");
if (token[0] != '/') {
return vi;
}
token++;
// i//k
if (token[0] == '/') {
token++;
vi.vn_idx = fixIndex(atoi(token), vnsize);
token += strcspn(token, "/ \t\r");
return vi;
}
// i/j/k or i/j
vi.vt_idx = fixIndex(atoi(token), vtsize);
token += strcspn(token, "/ \t\r");
if (token[0] != '/') {
return vi;
}
// i/j/k
token++; // skip '/'
vi.vn_idx = fixIndex(atoi(token), vnsize);
token += strcspn(token, "/ \t\r");
return vi;
}
static unsigned int
updateVertex(std::map<vertex_index, unsigned int> &vertexCache,
std::vector<float> &positions, std::vector<float> &normals,
std::vector<float> &texcoords,
const std::vector<float> &in_positions,
const std::vector<float> &in_normals,
const std::vector<float> &in_texcoords, const vertex_index &i) {
const std::map<vertex_index, unsigned int>::iterator it = vertexCache.find(i);
if (it != vertexCache.end()) {
// found cache
return it->second;
}
assert(in_positions.size() > (unsigned int)(3 * i.v_idx + 2));
positions.push_back(in_positions[3 * i.v_idx + 0]);
positions.push_back(in_positions[3 * i.v_idx + 1]);
positions.push_back(in_positions[3 * i.v_idx + 2]);
if (i.vn_idx >= 0) {
normals.push_back(in_normals[3 * i.vn_idx + 0]);
normals.push_back(in_normals[3 * i.vn_idx + 1]);
normals.push_back(in_normals[3 * i.vn_idx + 2]);
}
if (i.vt_idx >= 0) {
texcoords.push_back(in_texcoords[2 * i.vt_idx + 0]);
texcoords.push_back(in_texcoords[2 * i.vt_idx + 1]);
}
unsigned int idx = static_cast<unsigned int>(positions.size() / 3 - 1);
vertexCache[i] = idx;
return idx;
}
void InitMaterial(material_t &material) {
material.name = "";
material.ambient_texname = "";
material.diffuse_texname = "";
material.specular_texname = "";
material.normal_texname = "";
for (int i = 0; i < 3; i++) {
material.ambient[i] = 0.f;
material.diffuse[i] = 0.f;
material.specular[i] = 0.f;
material.transmittance[i] = 0.f;
material.emission[i] = 0.f;
}
material.illum = 0;
material.dissolve = 1.f;
material.shininess = 1.f;
material.ior = 1.f;
material.unknown_parameter.clear();
}
static bool exportFaceGroupToShape(
shape_t &shape, std::map<vertex_index, unsigned int> vertexCache,
const std::vector<float> &in_positions,
const std::vector<float> &in_normals,
const std::vector<float> &in_texcoords,
const std::vector<std::vector<vertex_index> > &faceGroup,
const int material_id, const std::string &name, bool clearCache) {
if (faceGroup.empty()) {
return false;
}
// Flatten vertices and indices
for (size_t i = 0; i < faceGroup.size(); i++) {
const std::vector<vertex_index> &face = faceGroup[i];
vertex_index i0 = face[0];
vertex_index i1(-1);
vertex_index i2 = face[1];
size_t npolys = face.size();
// Polygon -> triangle fan conversion
for (size_t k = 2; k < npolys; k++) {
i1 = i2;
i2 = face[k];
unsigned int v0 = updateVertex(
vertexCache, shape.mesh.positions, shape.mesh.normals,
shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i0);
unsigned int v1 = updateVertex(
vertexCache, shape.mesh.positions, shape.mesh.normals,
shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i1);
unsigned int v2 = updateVertex(
vertexCache, shape.mesh.positions, shape.mesh.normals,
shape.mesh.texcoords, in_positions, in_normals, in_texcoords, i2);
shape.mesh.indices.push_back(v0);
shape.mesh.indices.push_back(v1);
shape.mesh.indices.push_back(v2);
shape.mesh.material_ids.push_back(material_id);
}
}
shape.name = name;
if (clearCache)
vertexCache.clear();
return true;
}
std::string LoadMtl(std::map<std::string, int> &material_map,
std::vector<material_t> &materials,
std::istream &inStream) {
std::stringstream err;
// Create a default material anyway.
material_t material;
InitMaterial(material);
int maxchars = 8192; // Alloc enough size.
std::vector<char> buf(maxchars); // Alloc enough size.
while (inStream.peek() != -1) {
inStream.getline(&buf[0], maxchars);
std::string linebuf(&buf[0]);
// Trim newline '\r\n' or '\n'
if (linebuf.size() > 0) {
if (linebuf[linebuf.size() - 1] == '\n')
linebuf.erase(linebuf.size() - 1);
}
if (linebuf.size() > 0) {
if (linebuf[linebuf.size() - 1] == '\r')
linebuf.erase(linebuf.size() - 1);
}
// Skip if empty line.
if (linebuf.empty()) {
continue;
}
// Skip leading space.
const char *token = linebuf.c_str();
token += strspn(token, " \t");
assert(token);
if (token[0] == '\0')
continue; // empty line
if (token[0] == '#')
continue; // comment line
// new mtl
if ((0 == strncmp(token, "newmtl", 6)) && isSpace((token[6]))) {
// flush previous material.
if (!material.name.empty()) {
material_map.insert(std::pair<std::string, int>(
material.name, static_cast<int>(materials.size())));
materials.push_back(material);
}
// initial temporary material
InitMaterial(material);
// set new mtl name
char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
token += 7;
#ifdef _MSC_VER
sscanf_s(token, "%s", namebuf, _countof(namebuf));
#else
sscanf(token, "%s", namebuf);
#endif
material.name = namebuf;
continue;
}
// ambient
if (token[0] == 'K' && token[1] == 'a' && isSpace((token[2]))) {
token += 2;
float r, g, b;
parseFloat3(r, g, b, token);
material.ambient[0] = r;
material.ambient[1] = g;
material.ambient[2] = b;
continue;
}
// diffuse
if (token[0] == 'K' && token[1] == 'd' && isSpace((token[2]))) {
token += 2;
float r, g, b;
parseFloat3(r, g, b, token);
material.diffuse[0] = r;
material.diffuse[1] = g;
material.diffuse[2] = b;
continue;
}
// specular
if (token[0] == 'K' && token[1] == 's' && isSpace((token[2]))) {
token += 2;
float r, g, b;
parseFloat3(r, g, b, token);
material.specular[0] = r;
material.specular[1] = g;
material.specular[2] = b;
continue;
}
// transmittance
if (token[0] == 'K' && token[1] == 't' && isSpace((token[2]))) {
token += 2;
float r, g, b;
parseFloat3(r, g, b, token);
material.transmittance[0] = r;
material.transmittance[1] = g;
material.transmittance[2] = b;
continue;
}
// ior(index of refraction)
if (token[0] == 'N' && token[1] == 'i' && isSpace((token[2]))) {
token += 2;
material.ior = parseFloat(token);
continue;
}
// emission
if (token[0] == 'K' && token[1] == 'e' && isSpace(token[2])) {
token += 2;
float r, g, b;
parseFloat3(r, g, b, token);
material.emission[0] = r;
material.emission[1] = g;
material.emission[2] = b;
continue;
}
// shininess
if (token[0] == 'N' && token[1] == 's' && isSpace(token[2])) {
token += 2;
material.shininess = parseFloat(token);
continue;
}
// illum model
if (0 == strncmp(token, "illum", 5) && isSpace(token[5])) {
token += 6;
material.illum = parseInt(token);
continue;
}
// dissolve
if ((token[0] == 'd' && isSpace(token[1]))) {
token += 1;
material.dissolve = parseFloat(token);
continue;
}
if (token[0] == 'T' && token[1] == 'r' && isSpace(token[2])) {
token += 2;
// Invert value of Tr(assume Tr is in range [0, 1])
material.dissolve = 1.0f - parseFloat(token);
continue;
}
// ambient texture
if ((0 == strncmp(token, "map_Ka", 6)) && isSpace(token[6])) {
token += 7;
material.ambient_texname = token;
continue;
}
// diffuse texture
if ((0 == strncmp(token, "map_Kd", 6)) && isSpace(token[6])) {
token += 7;
material.diffuse_texname = token;
continue;
}
// specular texture
if ((0 == strncmp(token, "map_Ks", 6)) && isSpace(token[6])) {
token += 7;
material.specular_texname = token;
continue;
}
// normal texture
if ((0 == strncmp(token, "map_Ns", 6)) && isSpace(token[6])) {
token += 7;
material.normal_texname = token;
continue;
}
// unknown parameter
const char *_space = strchr(token, ' ');
if (!_space) {
_space = strchr(token, '\t');
}
if (_space) {
std::ptrdiff_t len = _space - token;
std::string key(token, len);
std::string value = _space + 1;
material.unknown_parameter.insert(
std::pair<std::string, std::string>(key, value));
}
}
// flush last material.
material_map.insert(std::pair<std::string, int>(
material.name, static_cast<int>(materials.size())));
materials.push_back(material);
return err.str();
}
std::string MaterialFileReader::operator()(const std::string &matId,
std::vector<material_t> &materials,
std::map<std::string, int> &matMap) {
std::string filepath;
if (!m_mtlBasePath.empty()) {
filepath = std::string(m_mtlBasePath) + matId;
} else {
filepath = matId;
}
std::ifstream matIStream(filepath.c_str());
std::string err = LoadMtl(matMap, materials, matIStream);
if (!matIStream) {
std::stringstream ss;
ss << "WARN: Material file [ " << filepath
<< " ] not found. Created a default material.";
err += ss.str();
}
return err;
}
std::string LoadObj(std::vector<shape_t> &shapes, std::vector<curve_t> &curves,
std::vector<material_t> &materials, // [output]
const char *filename, const char *mtl_basepath) {
shapes.clear();
std::stringstream err;
std::ifstream ifs(filename);
if (!ifs) {
err << "Cannot open file [" << filename << "]" << std::endl;
return err.str();
}
std::string basePath;
if (mtl_basepath) {
basePath = mtl_basepath;
}
MaterialFileReader matFileReader(basePath);
return LoadObj(shapes, curves, materials, ifs, matFileReader);
}
std::string LoadObj(std::vector<shape_t> &shapes, // [output]
std::vector<curve_t> &curves, // [output]
std::vector<material_t> &materials, // [output]
std::istream &inStream, MaterialReader &readMatFn) {
std::stringstream err;
std::vector<float> v;
std::vector<float> vn;
std::vector<float> vt;
std::vector<std::vector<vertex_index> > faceGroup;
std::string name;
// curve
int curveType = -1;
int curveDegree = -1;
std::vector<unsigned int> curve_indices;
std::vector<float> u_params;
std::vector<float> v_params;
// material
std::map<std::string, int> material_map;
std::map<vertex_index, unsigned int> vertexCache;
int material = -1;
shape_t shape;
int maxchars = 8192; // Alloc enough size.
std::vector<char> buf(maxchars); // Alloc enough size.
while (inStream.peek() != -1) {
inStream.getline(&buf[0], maxchars);
std::string linebuf(&buf[0]);
// Trim newline '\r\n' or '\n'
if (linebuf.size() > 0) {
if (linebuf[linebuf.size() - 1] == '\n')
linebuf.erase(linebuf.size() - 1);
}
if (linebuf.size() > 0) {
if (linebuf[linebuf.size() - 1] == '\r')
linebuf.erase(linebuf.size() - 1);
}
// Skip if empty line.
if (linebuf.empty()) {
continue;
}
// Skip leading space.
const char *token = linebuf.c_str();
token += strspn(token, " \t");
assert(token);
if (token[0] == '\0')
continue; // empty line
if (token[0] == '#')
continue; // comment line
// vertex
if (token[0] == 'v' && isSpace((token[1]))) {
token += 2;
float x, y, z;
parseFloat3(x, y, z, token);
v.push_back(x);
v.push_back(y);
v.push_back(z);
continue;
}
// normal
if (token[0] == 'v' && token[1] == 'n' && isSpace((token[2]))) {
token += 3;
float x, y, z;
parseFloat3(x, y, z, token);
vn.push_back(x);
vn.push_back(y);
vn.push_back(z);
continue;
}
// texcoord
if (token[0] == 'v' && token[1] == 't' && isSpace((token[2]))) {
token += 3;
float x, y;
parseFloat2(x, y, token);
vt.push_back(x);
vt.push_back(y);
continue;
}
// face
if (token[0] == 'f' && isSpace((token[1]))) {
token += 2;
token += strspn(token, " \t");
std::vector<vertex_index> face;
while (!isNewLine(token[0])) {
vertex_index vi = parseTriple(token, static_cast<int>(v.size() / 3),
static_cast<int>(vn.size() / 3),
static_cast<int>(vt.size() / 2));
face.push_back(vi);
size_t n = strspn(token, " \t\r");
token += n;
}
faceGroup.push_back(face);
continue;
}
// use mtl
if ((0 == strncmp(token, "usemtl", 6)) && isSpace((token[6]))) {
char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
token += 7;
#ifdef _MSC_VER
sscanf_s(token, "%s", namebuf, _countof(namebuf));
#else
sscanf(token, "%s", namebuf);
#endif
// Create face group per material.
bool ret = exportFaceGroupToShape(shape, vertexCache, v, vn, vt,
faceGroup, material, name, true);
if (ret) {
shapes.push_back(shape);
}
shape = shape_t();
faceGroup.clear();
if (material_map.find(namebuf) != material_map.end()) {
material = material_map[namebuf];
} else {
// { error!! material not found }
material = -1;
}
continue;
}
// load mtl
if ((0 == strncmp(token, "mtllib", 6)) && isSpace((token[6]))) {
char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
token += 7;
#ifdef _MSC_VER
sscanf_s(token, "%s", namebuf, _countof(namebuf));
#else
sscanf(token, "%s", namebuf);
#endif
std::string err_mtl = readMatFn(namebuf, materials, material_map);
if (!err_mtl.empty()) {
faceGroup.clear(); // for safety
return err_mtl;
}
continue;
}
// group name
if (token[0] == 'g' && isSpace((token[1]))) {
// flush previous face group.
bool ret = exportFaceGroupToShape(shape, vertexCache, v, vn, vt,
faceGroup, material, name, true);
if (ret) {
shapes.push_back(shape);
}
shape = shape_t();
// material = -1;
faceGroup.clear();
std::vector<std::string> names;
while (!isNewLine(token[0])) {
std::string str = parseString(token);
names.push_back(str);
token += strspn(token, " \t\r"); // skip tag
}
assert(names.size() > 0);
// names[0] must be 'g', so skip the 0th element.
if (names.size() > 1) {
name = names[1];
} else {
name = "";
}
continue;
}
// object name
if (token[0] == 'o' && isSpace((token[1]))) {
// flush previous face group.
bool ret = exportFaceGroupToShape(shape, vertexCache, v, vn, vt,
faceGroup, material, name, true);
if (ret) {
shapes.push_back(shape);
}
// material = -1;
faceGroup.clear();
shape = shape_t();
// @todo { multiple object name? }
char namebuf[TINYOBJ_SSCANF_BUFFER_SIZE];
token += 2;
#ifdef _MSC_VER
sscanf_s(token, "%s", namebuf, _countof(namebuf));
#else
sscanf(token, "%s", namebuf);
#endif
name = std::string(namebuf);
continue;
}
// curve type
if ((0 == strncmp(token, "cstype", 6)) && isSpace((token[6]))) {
token += 7;
std::string type = parseString(token);
if (type == "bspline") { // 0
curveType = 0;
} else if (type == "bezier") { // 1
curveType = 1;
} else if (type == "cardinal") {
curveType = 2;
}
}
// degree(curve)
if (token[0] == 'd' && token[1] == 'e' && token[2] == 'g' &&
isSpace((token[3]))) {
token += 4;
curveDegree = parseInt(token);
continue;
}
// curve
if (token[0] == 'c' && token[1] == 'u' && token[2] == 'r' &&
token[3] == 'v' && isSpace((token[4]))) {
token += 5;
// parse u0 and u1
float u0, u1;
parseFloat2(u0, u1, token);
size_t n = strspn(token, " \t\r");
token += n;
// parse vertex indices.
curve_indices.clear();
while (!isNewLine(token[0])) {
vertex_index vi = parseTriple(token, static_cast<int>(v.size() / 3),
static_cast<int>(vn.size() / 3),
static_cast<int>(vt.size() / 2));
curve_indices.push_back(vi.v_idx);
size_t n = strspn(token, " \t\r");
token += n;
}
continue;
}
// curve param
if ((0 == strncmp(token, "parm", 4)) && isSpace((token[4]))) {
token += 5;
std::string name = parseString(token);
if (name == "u") {
u_params.clear();
} else if (name == "v") {
v_params.clear();
}
// parse parameter values.
while (!isNewLine(token[0])) {
float value = parseFloat(token);
if (name == "u") {
u_params.push_back(value);
} else if (name == "v") {
v_params.push_back(value);
}
}
continue;
}
// end(curve)
if (token[0] == 'e' && token[1] == 'n' && token[2] == 'd') {
// size_t n = strspn(token, " \t\r");
// token += n;
// @todo { Consider 'end' at the end of line: parm u 0 0 0 0 ... end }
if ((curveType > -1) && (curveDegree > 0) && (curve_indices.size() > 1)) {
curve_t curve;
curve.name = name;
curve.type = curveType;
curve.degree = curveDegree;
curve.positions = v;
curve.indices = curve_indices;
curve.u_params = u_params;
curve.v_params = v_params;
curves.push_back(curve);
}
u_params.clear();
v_params.clear();
curve_indices.clear();
continue;
}
// Ignore unknown command.
}
bool ret = exportFaceGroupToShape(shape, vertexCache, v, vn, vt, faceGroup,
material, name, true);
if (ret) {
shapes.push_back(shape);
}
faceGroup.clear(); // for safety
return err.str();
}
} // namespace

File diff suppressed because it is too large Load Diff