diff --git a/README.md b/README.md index 110697ce..3f22b602 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,156 @@ -CUDA Path Tracer -================ +**University of Pennsylvania, CIS 565: GPU Programming and Architecture** +# Project 3 - CUDA Path Tracer -**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 3** +* Jonas Oppenheim ([LinkedIn](https://www.linkedin.com/in/jonasoppenheim/), [GitHub](https://github.com/oppenheimj/), [personal](http://www.jonasoppenheim.com/)) +* Tested on: Windows 10, Ryzen 9 5950x, 32GB, RTX 3080 (personal machine) -* (TODO) YOUR NAME HERE -* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab) +
+ +
Image 1: Abduction A
+
-### (TODO: Your README) +
+ +
Image 2: Abduction B
+
-*DO NOT* leave the README to the last minute! It is a crucial part of the -project, and we will not be able to grade you without a good README. +## Introduction +In the simulation that we call reality, photon [waves/particles](https://en.wikipedia.org/wiki/Double-slit_experiment) flow from light sources and bounce around the physical world. If we're interested in generating photo-realistic images by modeling this behavior of light, then it is most efficient to only model the few photons that reach our eyeballs. This is the idea behind path tracing. The procedure is as follows. Every iteration, fire one ray into the scene for every pixel. Allow each of these rays to bounce around the scene a certain number of times (depth). At every bounce, change the ray's color and direction based on the material it hit. If a ray hits a light source, stop bouncing it. So there is a notion of _iterations_, which is the number of times a set of rays is shot into the scene, and of _depth_, which is how many bounces are permitted per ray per iteraton. Multiple iterations are required because for a given pixel, the ray's path from iteration to iteration will likely be different because rays do not reflect from most materials deterministically. The following three images show a section of pixels after 1, 10, and 100 iterations. It is seen that over time, each pixel converges to its "correct" color. + +
+ +| 1 iteration | 10 iterations | 100 iterations | +|---|---|---| +|![](img/1_iter.png)|![](img/10_iter.png)|![](img/100_iter.png)| + +
+ +The following image shows the variety of features implemented in this project. The next section covers qualitative and quantitative descriptions of these features. + +
+ +
Image 3: Feature list
+
+ +## Description and analysis of features + +
+ +
Image 4: Test image for stream compaction, material sorting, and first-bounce caching
+
+ +### Stream compaction +The purpose of stream compaction is to efficiently remove elements matching a particlar criteria from an array. Stream compaction is essentially high performacne filtering. This is useful in the context of path tracing because if a ray has zero bounces left, then that ray no longer needs to be processed. + +Stream compaction performance analysis was test on Image 3 using `thrust::stable_partition`. The canvas was 800x800 pixels and the bounce depth was eight. Invocations of both the `computeIntersections` and `shadeMaterial` kernels were timed for each bounce of each iteration between iteratons 30 and 50, which warmed up the cache and smoothed the results. + +Within a particular iteration, rays bounce around up to a certain number of times, but are terminated early if they don't hit anything. The expectation was that removing these early terminated rays would result in smaller and therefore faster kernel invocations. Indeed, Figure 1 shows that kernels run faster with stream compaction and Figure 2 shows why: later bounces have fewer and fewer rays. + +It is worth mentioning that stream compaction itself took approximately 13,000 microseconds, which is far longer than it took any of the kernels to run. For a scene the size of the one tested, there is a net performance loss. For a larger image, though, there improvement in kernel performance is likely to outweigh the cost of running stream compaction every bounce. + +
+ +
Figure 1
+
+ +
+ +
Figure 2
+
+ +### Material sorting +The idea behind material sorting is to sort our rays and intersections by the material that was hit. This way, threads within warps are more likely to take similar logical paths through kernel conditionals and there will be fewer warp divergences. Figure 3 shows that indeed, when material sorting is enabled, performance improves for both kernels. + +It is obvious why sorting by material made the shadeMaterials kernel run faster. The shadeMaterials kernel represents the BSDF that has a bunch of if-else statements depending on the material type. But within an iteration, the material sorting actually happens _after_ computeIntersections, so it was surprising to see such an improvement. I believe that running computeIntersections during subsequent bounces benefits for the same reason, namely that rays that hit the same material are likely to intersect with similar objects later in the scene. + +It is noteworthy that running `thrust::stable_sort_by_key` takes 50,167 microseconds, which means that it results in a net decrease in performance. Still, it was useful to implement to see that it speeds up these kernel calls. + +
+ +
Figure 3
+
+ +### Cache first bounce +Recall that path tracing involves many iterations of shooting rays into the scene and those rays bounce up to a certain number of times. The reason to cache the first bounce is that every iteration, the initial bounce of each ray will be the every time. It is only later bonuces that are non-deterministic, since some materials reflect light randomly. We can save time by caching the first bounce into a separate buffer of global memory during the first iteration and then loading that cache during subsequent iterations. + +The following values are based on Image 2. The first iteration's invocation of computeIntersections and subsequent writing to the cache took 295 and 114 microseconds, respectively. Later iterations only took 154 microseconds to load the first bounce from the cache. So based on the image tested, it takes twice as long to compute the intersections as it does to load from the cache. This difference would be even greater if the scene included a more complex mesh. Obviously, the benefits of first-bounce caching become harder and harder to detect as the number of bounces increase because more and more time is spent having to run computeIntersections anyway. + + +### Arbitrary mesh loading +The initial codebase supported spawning only cubes and spheres. These shapes were generated without loading `.obj` files and without any representation of vertices, faces, triangles, or anything in memory. Instead, the code simply used the geometric properties of cubes and spheres to determine whether a ray headed in a particular direction from a particular location would intersect the object. The goal of this feature was to be able to spawn objects in the scene using `.obj` files. + +The [tinyobjloader](https://github.com/tinyobjloader/tinyobjloader) library was used to parse an `.obj` file into a set of `Triangle` structs (each `Triangle` is 60 bytes). The cow mesh has 5,803 triangles and the UFO mesh has 3,854 triangles. These triangles are organized into an octree before being loaded onto the GPU, which is detailed next. +
+ +
Image 5: Meshes loaded
+
+ +### Octree + +An [octree](https://en.wikipedia.org/wiki/Octree#:~:text=An%20octree%20is%20a%20tree,three%2Ddimensional%20analog%20of%20quadtrees.) is a space partitioning data structure that makes it faster to determine if a ray intersects a mesh of triangles, and if so, which exact triangle. The tree root represents the entire bounding box around the mesh. The root's eight children represent eight subdivisions of the mesh's bounding box. These subdivisons are recursively subdivided a chosen number of times. Then, each triangle in the mesh is associated with one or more boxes that the triangle intersects. + +In order to quickly find the triangle(s) intersected by a particular ray, start at the root and test whether the ray intersects the bounding box. If yes, do the same check on each child bounding box. Repeat this, recursively, until you reach some number of leaf nodes. At that point, you can see whether the ray intersects any of those triangles that the leaf nodes point to. This is a major improvement because the number of triangles searched grows logarithmically instead of linearly. + +
+ +
Figure 4
+
+ +After generating the set of `Triangle` structs, the goal was to build an octree and then insert each `Triangle`, associating it with one or more leaf nodes. + +The octree was represented linearly, as an array, where the eight children of node `i` are found starting at position `8*i+1`. For example, the root is stored at index `0` and its children are at indices `1` through `8`. In addition, an octree of depth `d` has `(8^d-1)/7` nodes, so an octree of depth 1 is just the root which represents the bounding box. Octrees of depth `2`, `3`, and `4` have `9`, `73`, and `585`, nodes respectively. + +The process of inserting the `Triangle` structs is similar to the process described for querying for intersections; we recursively ask whether a node contains the triangle. If yes, query each of the child nodes. Repeat until one or more leaf is reached. + +I'd like to share one particularly pernicious bug that stumped me for longer than I am willing to admit. While inserting triangles into the tree, you have to have some way of determining if a node _contains_ that triangle. My initial approach was to determine whether the node contained any of the three points representing the triangle. If yes, associate the triangle with the node. Image 7 shows the result of this approach for a tree of depth 5. The problem got worse the deeper I made the tree. I thought it might be a stack or heap size issue because I used recurison in my computeIntersection kernel. + +
+ +
Image 7: Octree bug
+
+ +It turns out that as the node volumes got smaller and smaller, certain triangles were intersecting nodes _but didn't have any point in the node_, so they weren't getting associated properly. The solution was to _yoink_ and adapt the triangle-cube intersection formula from [here](https://github.com/juj/MathGeoLib/blob/master/src/Geometry/Triangle.cpp#L697). Live and learn. + +Image 6 shows the scene used to test various octree depths. Figure 5 shows that there is a significant beneift to adding depth (granularity) to the tree until a depth of three, and then the querying becomes burdensome. I suspect that this result could have to do with thread stack size, since the recursion depth gets deeper the deeper the tree gets. Also, the deeper the tree, the more nodes a single triangle intersects, so there end up actually being a lot of node checks. + +
+ +
Image 6: Test image for octree
+
+ +
+ +
Figure 5
+
+ +### Refraction with Fresnel effect +Refraction was fun and relatively painless to implement. Using `glm::refract()` made it pretty easy. The most interesting part was realizing how to determine if a ray was intersecting a mesh triangle from the "inside" or "outside". The solution is to compute the angle between the triangle's normal and the ray's direction. If this angle is greater than 90 degrees, then the ray must have hit the outside of the triangle. This handy dandy formula takes care of it: +``` +float ang = glm::acos(glm::dot(r.direction, normal) / (glm::length(r.direction) * glm::length(normal))); +bool outside = glm::degrees(ang) > 90; +``` + +
+ +
Image 7: Refraction with Fresnel effect
+
+ +As for the Fresnel effect, I used the function that I found [here](https://blog.demofox.org/2017/01/09/raytracing-reflection-refraction-fresnel-total-internal-reflection-and-beers-law/) and then did probabilistic thresholding using R. + +### Custom Python script for generating scene files +It became tiresome editing the scene `.txt` files and making sure the various objects pointed to the correct materials, and making sure the numbering on objects and materials was correct. I wrote a Python script, located in `/scenes/scenegenerator.py`, which made it much easier to automatically generate these files and retain sanity. + + +## Concluding thoughts + +
+ +
me irl
+
+ +## Refrences +- Ray-triangle intersection function from [here](https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm) +- Triangle-box intersection function from [here](https://github.com/juj/MathGeoLib/blob/master/src/Geometry/Triangle.cpp#L697) +- Fresnel effect function from [here](https://blog.demofox.org/2017/01/09/raytracing-reflection-refraction-fresnel-total-internal-reflection-and-beers-law/) +- How to actually use `thrust` from [here](https://stackoverflow.com/questions/12047961/cuda-thrust-how-to-realize-partition-that-supports-stencil) \ No newline at end of file diff --git a/data/fb_data b/data/fb_data new file mode 100644 index 00000000..2c634ab7 --- /dev/null +++ b/data/fb_data @@ -0,0 +1,9 @@ +cache first bounce OFF + + + +cache first bounce ON +computeIntersections 295 +cacheWrite 114 + +cacheRead 154 \ No newline at end of file diff --git a/data/figures.xlsx b/data/figures.xlsx new file mode 100644 index 00000000..5162fd43 Binary files /dev/null and b/data/figures.xlsx differ diff --git a/data/ot_data b/data/ot_data new file mode 100644 index 00000000..7c3d59f4 --- /dev/null +++ b/data/ot_data @@ -0,0 +1,222 @@ +DEPTH = 0 +Allocated triangle memory (348242 bytes) +Allocated tree memory (248 bytes) +iteration times +0 11113 +1 37670 +2 37349 +3 37624 +4 37711 +5 36435 +6 36596 +7 36309 +TOTAL 270810 +computeIntersections times +0 10904 +1 37466 +2 37157 +3 37430 +4 37545 +5 36282 +6 36449 +7 36163 +TOTAL 269399 +shadeFakeMaterial times +0 200 +1 190 +2 179 +3 179 +4 154 +5 142 +6 136 +7 135 +TOTAL 1318 + + + +DEPTH = 1 +Allocated triangle memory (370082 bytes) +Allocated tree memory (2232 bytes) + +iteration times +0 3316 +1 30423 +2 31358 +3 32140 +4 29289 +5 26898 +6 24610 +7 22560 +TOTAL 200597 +computeIntersections times +0 3099 +1 30235 +2 31164 +3 31974 +4 29129 +5 26753 +6 24471 +7 22423 +TOTAL 199251 +shadeFakeMaterial times +0 210 +1 183 +2 188 +3 159 +4 155 +5 141 +6 136 +7 131 +TOTAL 1307 + + +DEPTH = 2 +Allocated triangle memory (441242 bytes) +Allocated tree memory (18104 bytes) + +iteration times +0 1517 +1 22031 +2 21591 +3 19786 +4 17585 +5 15514 +6 13855 +7 12545 +TOTAL 124428 +computeIntersections times +0 1322 +1 21839 +2 21394 +3 19620 +4 17429 +5 15362 +6 13701 +7 12411 +TOTAL 123080 +shadeFakeMaterial times +0 188 +1 189 +2 190 +3 161 +4 150 +5 149 +6 148 +7 129 +TOTAL 1307 + + +DEPTH = 3 +Allocated triangle memory (561182 bytes) +Allocated tree memory (145080 bytes) + +iteration times +0 1626 +1 18450 +2 17742 +3 14936 +4 13597 +5 11771 +6 10155 +7 9650 +TOTAL 97929 +computeIntersections times +0 1391 +1 18264 +2 17558 +3 14768 +4 13436 +5 11619 +6 10011 +7 9516 +TOTAL 96566 +shadeFakeMaterial times +0 229 +1 182 +2 171 +3 160 +4 151 +5 144 +6 136 +7 127 +TOTAL 1303 + +DEPTH = 4 +Allocated triangle memory (798602 bytes) +Allocated tree memory (1160888 bytes) + +iteration times +0 3018 +1 38168 +2 35103 +3 29848 +4 25680 +5 21351 +6 18054 +7 15600 +TOTAL 186825 +computeIntersections times +0 2814 +1 37979 +2 34924 +3 29677 +4 25526 +5 21198 +6 17914 +7 15470 +TOTAL 185505 +shadeFakeMaterial times +0 198 +1 183 +2 173 +3 167 +4 150 +5 149 +6 136 +7 127 +TOTAL 1287 + + +DEPTH = 5 +Allocated triangle memory (1373282 bytes) +Allocated tree memory (9287352 bytes) +numPaths +0 640000 +1 640000 +2 640000 +3 640000 +4 640000 +5 640000 +6 640000 +7 640000 +TOTAL 5120001 +iteration times +0 9945 +1 97615 +2 86318 +3 70876 +4 58390 +5 48984 +6 40971 +7 34946 +TOTAL 448049 +computeIntersections times +0 9747 +1 97413 +2 86135 +3 70706 +4 58227 +5 48833 +6 40827 +7 34810 +TOTAL 446701 +shadeFakeMaterial times +0 189 +1 194 +2 172 +3 162 +4 153 +5 142 +6 135 +7 127 +TOTAL 1278 diff --git a/data/sc_data b/data/sc_data new file mode 100644 index 00000000..66873829 --- /dev/null +++ b/data/sc_data @@ -0,0 +1,126 @@ +depth = 8 +800x800 image + +STREAM COMPACTION OFF +numPaths +0 640000 +1 640000 +2 640000 +3 640000 +4 640000 +5 640000 +6 640000 +7 640000 +TOTAL 5120001 +iteration times +0 463 +1 488 +2 494 +3 503 +4 472 +5 439 +6 442 +7 442 +TOTAL 3746 +computeIntersections times +0 269 +1 299 +2 316 +3 313 +4 304 +5 276 +6 290 +7 295 +TOTAL 2365 +shadeFakeMaterial times +0 189 +1 187 +2 175 +3 187 +4 166 +5 160 +6 150 +7 145 +TOTAL 1362 +sortMaterials times +0 0 +1 0 +2 0 +3 0 +4 0 +5 0 +6 0 +7 0 +TOTAL 0 +streamCompaction times +0 0 +1 0 +2 0 +3 0 +4 0 +5 0 +6 0 +7 0 +TOTAL 0 + +STREAM COMPACTION ON +numPaths +0 640000 +1 523681 +2 362000 +3 289316 +4 240794 +5 202080 +6 170631 +7 144874 +TOTAL 2573378 +iteration times +0 3432 +1 3033 +2 2207 +3 1798 +4 1656 +5 1435 +6 1376 +7 1278 +TOTAL 16219 +computeIntersections times +0 298 +1 292 +2 224 +3 207 +4 181 +5 163 +6 148 +7 137 +TOTAL 1654 +shadeFakeMaterial times +0 231 +1 209 +2 166 +3 129 +4 119 +5 102 +6 103 +7 88 +TOTAL 1151 +sortMaterials times +0 0 +1 0 +2 0 +3 0 +4 0 +5 0 +6 0 +7 0 +TOTAL 0 +streamCompaction times +0 2896 +1 2526 +2 1810 +3 1456 +4 1351 +5 1165 +6 1118 +7 1046 +TOTAL 13372 \ No newline at end of file diff --git a/data/sm_data b/data/sm_data new file mode 100644 index 00000000..f08301cf --- /dev/null +++ b/data/sm_data @@ -0,0 +1,75 @@ + +SM OFF +iteration times +0 585 +1 652 +2 603 +3 507 +4 437 +5 437 +6 426 +7 428 +TOTAL 4078 +computeIntersections times +0 334 +1 394 +2 387 +3 326 +4 286 +5 281 +6 282 +7 284 +TOTAL 2577 +shadeFakeMaterial times +0 245 +1 255 +2 213 +3 179 +4 149 +5 153 +6 142 +7 141 +TOTAL 1479 + + +SM ON +iteration times +0 7310 +1 6663 +2 6976 +3 6445 +4 6222 +5 6387 +6 6612 +7 6427 +TOTAL 53044 +computeIntersections times +0 282 +1 268 +2 253 +3 204 +4 189 +5 174 +6 164 +7 158 +TOTAL 1695 +shadeFakeMaterial times +0 210 +1 175 +2 154 +3 138 +4 131 +5 117 +6 109 +7 104 +TOTAL 1142 +sortMaterials times +0 6812 +1 6213 +2 6564 +3 6097 +4 5895 +5 6091 +6 6333 +7 6159 +TOTAL 50167 diff --git a/external/include/common.h b/external/include/common.h new file mode 100644 index 00000000..d2c1fed9 --- /dev/null +++ b/external/include/common.h @@ -0,0 +1,132 @@ +#pragma once + +#include +#include + +#include +#include +#include +#include +#include +#include + +#define FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) +#define checkCUDAError(msg) checkCUDAErrorFn(msg, FILENAME, __LINE__) + +/** + * Check for CUDA errors; print and exit if there was a problem. + */ +void checkCUDAErrorFn(const char *msg, const char *file = NULL, int line = -1); + +inline int ilog2(int x) { + int lg = 0; + while (x >>= 1) { + ++lg; + } + return lg; +} + +inline int ilog2ceil(int x) { + return x == 1 ? 0 : ilog2(x - 1) + 1; +} + +namespace StreamCompaction { + namespace Common { + __global__ void kernMapToBoolean(int n, int *bools, const int *idata); + + __global__ void kernScatter(int n, int *odata, + const int *idata, const int *bools, const int *indices); + + /** + * This class is used for timing the performance + * Uncopyable and unmovable + * + * Adapted from WindyDarian(https://github.com/WindyDarian) + */ + class PerformanceTimer + { + public: + PerformanceTimer() + { + cudaEventCreate(&event_start); + cudaEventCreate(&event_end); + } + + ~PerformanceTimer() + { + cudaEventDestroy(event_start); + cudaEventDestroy(event_end); + } + + void startCpuTimer() + { + if (cpu_timer_started) { throw std::runtime_error("CPU timer already started"); } + cpu_timer_started = true; + + time_start_cpu = std::chrono::high_resolution_clock::now(); + } + + void endCpuTimer() + { + time_end_cpu = std::chrono::high_resolution_clock::now(); + + if (!cpu_timer_started) { throw std::runtime_error("CPU timer not started"); } + + std::chrono::duration duro = time_end_cpu - time_start_cpu; + prev_elapsed_time_cpu_milliseconds = + static_cast(duro.count()); + + cpu_timer_started = false; + } + + void startGpuTimer() + { + if (gpu_timer_started) { throw std::runtime_error("GPU timer already started"); } + gpu_timer_started = true; + + cudaEventRecord(event_start); + } + + void endGpuTimer() + { + cudaEventRecord(event_end); + cudaEventSynchronize(event_end); + + if (!gpu_timer_started) { throw std::runtime_error("GPU timer not started"); } + + cudaEventElapsedTime(&prev_elapsed_time_gpu_milliseconds, event_start, event_end); + gpu_timer_started = false; + } + + float getCpuElapsedTimeForPreviousOperation() //noexcept //(damn I need VS 2015 + { + return prev_elapsed_time_cpu_milliseconds; + } + + float getGpuElapsedTimeForPreviousOperation() //noexcept + { + return prev_elapsed_time_gpu_milliseconds; + } + + // remove copy and move functions + PerformanceTimer(const PerformanceTimer&) = delete; + PerformanceTimer(PerformanceTimer&&) = delete; + PerformanceTimer& operator=(const PerformanceTimer&) = delete; + PerformanceTimer& operator=(PerformanceTimer&&) = delete; + + private: + cudaEvent_t event_start = nullptr; + cudaEvent_t event_end = nullptr; + + using time_point_t = std::chrono::high_resolution_clock::time_point; + time_point_t time_start_cpu; + time_point_t time_end_cpu; + + bool cpu_timer_started = false; + bool gpu_timer_started = false; + + float prev_elapsed_time_cpu_milliseconds = 0.f; + float prev_elapsed_time_gpu_milliseconds = 0.f; + }; + } +} diff --git a/external/include/earcut.hpp b/external/include/earcut.hpp new file mode 100644 index 00000000..64c6d3d8 --- /dev/null +++ b/external/include/earcut.hpp @@ -0,0 +1,820 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace mapbox { + +namespace util { + +template struct nth { + inline static typename std::tuple_element::type + get(const T& t) { return std::get(t); }; +}; + +} + +namespace detail { + +template +class Earcut { +public: + std::vector indices; + std::size_t vertices = 0; + + template + void operator()(const Polygon& points); + +private: + struct Node { + Node(N index, double x_, double y_) : i(index), x(x_), y(y_) {} + Node(const Node&) = delete; + Node& operator=(const Node&) = delete; + Node(Node&&) = delete; + Node& operator=(Node&&) = delete; + + const N i; + const double x; + const double y; + + // previous and next vertice nodes in a polygon ring + Node* prev = nullptr; + Node* next = nullptr; + + // z-order curve value + int32_t z = 0; + + // previous and next nodes in z-order + Node* prevZ = nullptr; + Node* nextZ = nullptr; + + // indicates whether this is a steiner point + bool steiner = false; + }; + + template Node* linkedList(const Ring& points, const bool clockwise); + Node* filterPoints(Node* start, Node* end = nullptr); + void earcutLinked(Node* ear, int pass = 0); + bool isEar(Node* ear); + bool isEarHashed(Node* ear); + Node* cureLocalIntersections(Node* start); + void splitEarcut(Node* start); + template Node* eliminateHoles(const Polygon& points, Node* outerNode); + Node* eliminateHole(Node* hole, Node* outerNode); + Node* findHoleBridge(Node* hole, Node* outerNode); + bool sectorContainsSector(const Node* m, const Node* p); + void indexCurve(Node* start); + Node* sortLinked(Node* list); + int32_t zOrder(const double x_, const double y_); + Node* getLeftmost(Node* start); + bool pointInTriangle(double ax, double ay, double bx, double by, double cx, double cy, double px, double py) const; + bool isValidDiagonal(Node* a, Node* b); + double area(const Node* p, const Node* q, const Node* r) const; + bool equals(const Node* p1, const Node* p2); + bool intersects(const Node* p1, const Node* q1, const Node* p2, const Node* q2); + bool onSegment(const Node* p, const Node* q, const Node* r); + int sign(double val); + bool intersectsPolygon(const Node* a, const Node* b); + bool locallyInside(const Node* a, const Node* b); + bool middleInside(const Node* a, const Node* b); + Node* splitPolygon(Node* a, Node* b); + template Node* insertNode(std::size_t i, const Point& p, Node* last); + void removeNode(Node* p); + + bool hashing; + double minX, maxX; + double minY, maxY; + double inv_size = 0; + + template > + class ObjectPool { + public: + ObjectPool() { } + ObjectPool(std::size_t blockSize_) { + reset(blockSize_); + } + ~ObjectPool() { + clear(); + } + template + T* construct(Args&&... args) { + if (currentIndex >= blockSize) { + currentBlock = alloc_traits::allocate(alloc, blockSize); + allocations.emplace_back(currentBlock); + currentIndex = 0; + } + T* object = ¤tBlock[currentIndex++]; + alloc_traits::construct(alloc, object, std::forward(args)...); + return object; + } + void reset(std::size_t newBlockSize) { + for (auto allocation : allocations) { + alloc_traits::deallocate(alloc, allocation, blockSize); + } + allocations.clear(); + blockSize = std::max(1, newBlockSize); + currentBlock = nullptr; + currentIndex = blockSize; + } + void clear() { reset(blockSize); } + private: + T* currentBlock = nullptr; + std::size_t currentIndex = 1; + std::size_t blockSize = 1; + std::vector allocations; + Alloc alloc; + typedef typename std::allocator_traits alloc_traits; + }; + ObjectPool nodes; +}; + +template template +void Earcut::operator()(const Polygon& points) { + // reset + indices.clear(); + vertices = 0; + + if (points.empty()) return; + + double x; + double y; + int threshold = 80; + std::size_t len = 0; + + for (size_t i = 0; threshold >= 0 && i < points.size(); i++) { + threshold -= static_cast(points[i].size()); + len += points[i].size(); + } + + //estimate size of nodes and indices + nodes.reset(len * 3 / 2); + indices.reserve(len + points[0].size()); + + Node* outerNode = linkedList(points[0], true); + if (!outerNode || outerNode->prev == outerNode->next) return; + + if (points.size() > 1) outerNode = eliminateHoles(points, outerNode); + + // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox + hashing = threshold < 0; + if (hashing) { + Node* p = outerNode->next; + minX = maxX = outerNode->x; + minY = maxY = outerNode->y; + do { + x = p->x; + y = p->y; + minX = std::min(minX, x); + minY = std::min(minY, y); + maxX = std::max(maxX, x); + maxY = std::max(maxY, y); + p = p->next; + } while (p != outerNode); + + // minX, minY and size are later used to transform coords into integers for z-order calculation + inv_size = std::max(maxX - minX, maxY - minY); + inv_size = inv_size != .0 ? (1. / inv_size) : .0; + } + + earcutLinked(outerNode); + + nodes.clear(); +} + +// create a circular doubly linked list from polygon points in the specified winding order +template template +typename Earcut::Node* +Earcut::linkedList(const Ring& points, const bool clockwise) { + using Point = typename Ring::value_type; + double sum = 0; + const std::size_t len = points.size(); + std::size_t i, j; + Node* last = nullptr; + + // calculate original winding order of a polygon ring + for (i = 0, j = len > 0 ? len - 1 : 0; i < len; j = i++) { + const auto& p1 = points[i]; + const auto& p2 = points[j]; + const double p20 = util::nth<0, Point>::get(p2); + const double p10 = util::nth<0, Point>::get(p1); + const double p11 = util::nth<1, Point>::get(p1); + const double p21 = util::nth<1, Point>::get(p2); + sum += (p20 - p10) * (p11 + p21); + } + + // link points into circular doubly-linked list in the specified winding order + if (clockwise == (sum > 0)) { + for (i = 0; i < len; i++) last = insertNode(vertices + i, points[i], last); + } else { + for (i = len; i-- > 0;) last = insertNode(vertices + i, points[i], last); + } + + if (last && equals(last, last->next)) { + removeNode(last); + last = last->next; + } + + vertices += len; + + return last; +} + +// eliminate colinear or duplicate points +template +typename Earcut::Node* +Earcut::filterPoints(Node* start, Node* end) { + if (!end) end = start; + + Node* p = start; + bool again; + do { + again = false; + + if (!p->steiner && (equals(p, p->next) || area(p->prev, p, p->next) == 0)) { + removeNode(p); + p = end = p->prev; + + if (p == p->next) break; + again = true; + + } else { + p = p->next; + } + } while (again || p != end); + + return end; +} + +// main ear slicing loop which triangulates a polygon (given as a linked list) +template +void Earcut::earcutLinked(Node* ear, int pass) { + if (!ear) return; + + // interlink polygon nodes in z-order + if (!pass && hashing) indexCurve(ear); + + Node* stop = ear; + Node* prev; + Node* next; + + int iterations = 0; + + // iterate through ears, slicing them one by one + while (ear->prev != ear->next) { + iterations++; + prev = ear->prev; + next = ear->next; + + if (hashing ? isEarHashed(ear) : isEar(ear)) { + // cut off the triangle + indices.emplace_back(prev->i); + indices.emplace_back(ear->i); + indices.emplace_back(next->i); + + removeNode(ear); + + // skipping the next vertice leads to less sliver triangles + ear = next->next; + stop = next->next; + + continue; + } + + ear = next; + + // if we looped through the whole remaining polygon and can't find any more ears + if (ear == stop) { + // try filtering points and slicing again + if (!pass) earcutLinked(filterPoints(ear), 1); + + // if this didn't work, try curing all small self-intersections locally + else if (pass == 1) { + ear = cureLocalIntersections(filterPoints(ear)); + earcutLinked(ear, 2); + + // as a last resort, try splitting the remaining polygon into two + } else if (pass == 2) splitEarcut(ear); + + break; + } + } +} + +// check whether a polygon node forms a valid ear with adjacent nodes +template +bool Earcut::isEar(Node* ear) { + const Node* a = ear->prev; + const Node* b = ear; + const Node* c = ear->next; + + if (area(a, b, c) >= 0) return false; // reflex, can't be an ear + + // now make sure we don't have other points inside the potential ear + Node* p = ear->next->next; + + while (p != ear->prev) { + if (pointInTriangle(a->x, a->y, b->x, b->y, c->x, c->y, p->x, p->y) && + area(p->prev, p, p->next) >= 0) return false; + p = p->next; + } + + return true; +} + +template +bool Earcut::isEarHashed(Node* ear) { + const Node* a = ear->prev; + const Node* b = ear; + const Node* c = ear->next; + + if (area(a, b, c) >= 0) return false; // reflex, can't be an ear + + // triangle bbox; min & max are calculated like this for speed + const double minTX = std::min(a->x, std::min(b->x, c->x)); + const double minTY = std::min(a->y, std::min(b->y, c->y)); + const double maxTX = std::max(a->x, std::max(b->x, c->x)); + const double maxTY = std::max(a->y, std::max(b->y, c->y)); + + // z-order range for the current triangle bbox; + const int32_t minZ = zOrder(minTX, minTY); + const int32_t maxZ = zOrder(maxTX, maxTY); + + // first look for points inside the triangle in increasing z-order + Node* p = ear->nextZ; + + while (p && p->z <= maxZ) { + if (p != ear->prev && p != ear->next && + pointInTriangle(a->x, a->y, b->x, b->y, c->x, c->y, p->x, p->y) && + area(p->prev, p, p->next) >= 0) return false; + p = p->nextZ; + } + + // then look for points in decreasing z-order + p = ear->prevZ; + + while (p && p->z >= minZ) { + if (p != ear->prev && p != ear->next && + pointInTriangle(a->x, a->y, b->x, b->y, c->x, c->y, p->x, p->y) && + area(p->prev, p, p->next) >= 0) return false; + p = p->prevZ; + } + + return true; +} + +// go through all polygon nodes and cure small local self-intersections +template +typename Earcut::Node* +Earcut::cureLocalIntersections(Node* start) { + Node* p = start; + do { + Node* a = p->prev; + Node* b = p->next->next; + + // a self-intersection where edge (v[i-1],v[i]) intersects (v[i+1],v[i+2]) + if (!equals(a, b) && intersects(a, p, p->next, b) && locallyInside(a, b) && locallyInside(b, a)) { + indices.emplace_back(a->i); + indices.emplace_back(p->i); + indices.emplace_back(b->i); + + // remove two nodes involved + removeNode(p); + removeNode(p->next); + + p = start = b; + } + p = p->next; + } while (p != start); + + return filterPoints(p); +} + +// try splitting polygon into two and triangulate them independently +template +void Earcut::splitEarcut(Node* start) { + // look for a valid diagonal that divides the polygon into two + Node* a = start; + do { + Node* b = a->next->next; + while (b != a->prev) { + if (a->i != b->i && isValidDiagonal(a, b)) { + // split the polygon in two by the diagonal + Node* c = splitPolygon(a, b); + + // filter colinear points around the cuts + a = filterPoints(a, a->next); + c = filterPoints(c, c->next); + + // run earcut on each half + earcutLinked(a); + earcutLinked(c); + return; + } + b = b->next; + } + a = a->next; + } while (a != start); +} + +// link every hole into the outer loop, producing a single-ring polygon without holes +template template +typename Earcut::Node* +Earcut::eliminateHoles(const Polygon& points, Node* outerNode) { + const size_t len = points.size(); + + std::vector queue; + for (size_t i = 1; i < len; i++) { + Node* list = linkedList(points[i], false); + if (list) { + if (list == list->next) list->steiner = true; + queue.push_back(getLeftmost(list)); + } + } + std::sort(queue.begin(), queue.end(), [](const Node* a, const Node* b) { + return a->x < b->x; + }); + + // process holes from left to right + for (size_t i = 0; i < queue.size(); i++) { + outerNode = eliminateHole(queue[i], outerNode); + outerNode = filterPoints(outerNode, outerNode->next); + } + + return outerNode; +} + +// find a bridge between vertices that connects hole with an outer ring and and link it +template +typename Earcut::Node* +Earcut::eliminateHole(Node* hole, Node* outerNode) { + Node* bridge = findHoleBridge(hole, outerNode); + if (!bridge) { + return outerNode; + } + + Node* bridgeReverse = splitPolygon(bridge, hole); + + // filter collinear points around the cuts + Node* filteredBridge = filterPoints(bridge, bridge->next); + filterPoints(bridgeReverse, bridgeReverse->next); + + // Check if input node was removed by the filtering + return outerNode == bridge ? filteredBridge : outerNode; +} + +// David Eberly's algorithm for finding a bridge between hole and outer polygon +template +typename Earcut::Node* +Earcut::findHoleBridge(Node* hole, Node* outerNode) { + Node* p = outerNode; + double hx = hole->x; + double hy = hole->y; + double qx = -std::numeric_limits::infinity(); + Node* m = nullptr; + + // find a segment intersected by a ray from the hole's leftmost Vertex to the left; + // segment's endpoint with lesser x will be potential connection Vertex + do { + if (hy <= p->y && hy >= p->next->y && p->next->y != p->y) { + double x = p->x + (hy - p->y) * (p->next->x - p->x) / (p->next->y - p->y); + if (x <= hx && x > qx) { + qx = x; + if (x == hx) { + if (hy == p->y) return p; + if (hy == p->next->y) return p->next; + } + m = p->x < p->next->x ? p : p->next; + } + } + p = p->next; + } while (p != outerNode); + + if (!m) return 0; + + if (hx == qx) return m; // hole touches outer segment; pick leftmost endpoint + + // look for points inside the triangle of hole Vertex, segment intersection and endpoint; + // if there are no points found, we have a valid connection; + // otherwise choose the Vertex of the minimum angle with the ray as connection Vertex + + const Node* stop = m; + double tanMin = std::numeric_limits::infinity(); + double tanCur = 0; + + p = m; + double mx = m->x; + double my = m->y; + + do { + if (hx >= p->x && p->x >= mx && hx != p->x && + pointInTriangle(hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p->x, p->y)) { + + tanCur = std::abs(hy - p->y) / (hx - p->x); // tangential + + if (locallyInside(p, hole) && + (tanCur < tanMin || (tanCur == tanMin && (p->x > m->x || sectorContainsSector(m, p))))) { + m = p; + tanMin = tanCur; + } + } + + p = p->next; + } while (p != stop); + + return m; +} + +// whether sector in vertex m contains sector in vertex p in the same coordinates +template +bool Earcut::sectorContainsSector(const Node* m, const Node* p) { + return area(m->prev, m, p->prev) < 0 && area(p->next, m, m->next) < 0; +} + +// interlink polygon nodes in z-order +template +void Earcut::indexCurve(Node* start) { + assert(start); + Node* p = start; + + do { + p->z = p->z ? p->z : zOrder(p->x, p->y); + p->prevZ = p->prev; + p->nextZ = p->next; + p = p->next; + } while (p != start); + + p->prevZ->nextZ = nullptr; + p->prevZ = nullptr; + + sortLinked(p); +} + +// Simon Tatham's linked list merge sort algorithm +// http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html +template +typename Earcut::Node* +Earcut::sortLinked(Node* list) { + assert(list); + Node* p; + Node* q; + Node* e; + Node* tail; + int i, numMerges, pSize, qSize; + int inSize = 1; + + for (;;) { + p = list; + list = nullptr; + tail = nullptr; + numMerges = 0; + + while (p) { + numMerges++; + q = p; + pSize = 0; + for (i = 0; i < inSize; i++) { + pSize++; + q = q->nextZ; + if (!q) break; + } + + qSize = inSize; + + while (pSize > 0 || (qSize > 0 && q)) { + + if (pSize == 0) { + e = q; + q = q->nextZ; + qSize--; + } else if (qSize == 0 || !q) { + e = p; + p = p->nextZ; + pSize--; + } else if (p->z <= q->z) { + e = p; + p = p->nextZ; + pSize--; + } else { + e = q; + q = q->nextZ; + qSize--; + } + + if (tail) tail->nextZ = e; + else list = e; + + e->prevZ = tail; + tail = e; + } + + p = q; + } + + tail->nextZ = nullptr; + + if (numMerges <= 1) return list; + + inSize *= 2; + } +} + +// z-order of a Vertex given coords and size of the data bounding box +template +int32_t Earcut::zOrder(const double x_, const double y_) { + // coords are transformed into non-negative 15-bit integer range + int32_t x = static_cast(32767.0 * (x_ - minX) * inv_size); + int32_t y = static_cast(32767.0 * (y_ - minY) * inv_size); + + x = (x | (x << 8)) & 0x00FF00FF; + x = (x | (x << 4)) & 0x0F0F0F0F; + x = (x | (x << 2)) & 0x33333333; + x = (x | (x << 1)) & 0x55555555; + + y = (y | (y << 8)) & 0x00FF00FF; + y = (y | (y << 4)) & 0x0F0F0F0F; + y = (y | (y << 2)) & 0x33333333; + y = (y | (y << 1)) & 0x55555555; + + return x | (y << 1); +} + +// find the leftmost node of a polygon ring +template +typename Earcut::Node* +Earcut::getLeftmost(Node* start) { + Node* p = start; + Node* leftmost = start; + do { + if (p->x < leftmost->x || (p->x == leftmost->x && p->y < leftmost->y)) + leftmost = p; + p = p->next; + } while (p != start); + + return leftmost; +} + +// check if a point lies within a convex triangle +template +bool Earcut::pointInTriangle(double ax, double ay, double bx, double by, double cx, double cy, double px, double py) const { + return (cx - px) * (ay - py) - (ax - px) * (cy - py) >= 0 && + (ax - px) * (by - py) - (bx - px) * (ay - py) >= 0 && + (bx - px) * (cy - py) - (cx - px) * (by - py) >= 0; +} + +// check if a diagonal between two polygon nodes is valid (lies in polygon interior) +template +bool Earcut::isValidDiagonal(Node* a, Node* b) { + return a->next->i != b->i && a->prev->i != b->i && !intersectsPolygon(a, b) && // dones't intersect other edges + ((locallyInside(a, b) && locallyInside(b, a) && middleInside(a, b) && // locally visible + (area(a->prev, a, b->prev) != 0.0 || area(a, b->prev, b) != 0.0)) || // does not create opposite-facing sectors + (equals(a, b) && area(a->prev, a, a->next) > 0 && area(b->prev, b, b->next) > 0)); // special zero-length case +} + +// signed area of a triangle +template +double Earcut::area(const Node* p, const Node* q, const Node* r) const { + return (q->y - p->y) * (r->x - q->x) - (q->x - p->x) * (r->y - q->y); +} + +// check if two points are equal +template +bool Earcut::equals(const Node* p1, const Node* p2) { + return p1->x == p2->x && p1->y == p2->y; +} + +// check if two segments intersect +template +bool Earcut::intersects(const Node* p1, const Node* q1, const Node* p2, const Node* q2) { + int o1 = sign(area(p1, q1, p2)); + int o2 = sign(area(p1, q1, q2)); + int o3 = sign(area(p2, q2, p1)); + int o4 = sign(area(p2, q2, q1)); + + if (o1 != o2 && o3 != o4) return true; // general case + + if (o1 == 0 && onSegment(p1, p2, q1)) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1 + if (o2 == 0 && onSegment(p1, q2, q1)) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1 + if (o3 == 0 && onSegment(p2, p1, q2)) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2 + if (o4 == 0 && onSegment(p2, q1, q2)) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2 + + return false; +} + +// for collinear points p, q, r, check if point q lies on segment pr +template +bool Earcut::onSegment(const Node* p, const Node* q, const Node* r) { + return q->x <= std::max(p->x, r->x) && + q->x >= std::min(p->x, r->x) && + q->y <= std::max(p->y, r->y) && + q->y >= std::min(p->y, r->y); +} + +template +int Earcut::sign(double val) { + return (0.0 < val) - (val < 0.0); +} + +// check if a polygon diagonal intersects any polygon segments +template +bool Earcut::intersectsPolygon(const Node* a, const Node* b) { + const Node* p = a; + do { + if (p->i != a->i && p->next->i != a->i && p->i != b->i && p->next->i != b->i && + intersects(p, p->next, a, b)) return true; + p = p->next; + } while (p != a); + + return false; +} + +// check if a polygon diagonal is locally inside the polygon +template +bool Earcut::locallyInside(const Node* a, const Node* b) { + return area(a->prev, a, a->next) < 0 ? + area(a, b, a->next) >= 0 && area(a, a->prev, b) >= 0 : + area(a, b, a->prev) < 0 || area(a, a->next, b) < 0; +} + +// check if the middle Vertex of a polygon diagonal is inside the polygon +template +bool Earcut::middleInside(const Node* a, const Node* b) { + const Node* p = a; + bool inside = false; + double px = (a->x + b->x) / 2; + double py = (a->y + b->y) / 2; + do { + if (((p->y > py) != (p->next->y > py)) && p->next->y != p->y && + (px < (p->next->x - p->x) * (py - p->y) / (p->next->y - p->y) + p->x)) + inside = !inside; + p = p->next; + } while (p != a); + + return inside; +} + +// link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits +// polygon into two; if one belongs to the outer ring and another to a hole, it merges it into a +// single ring +template +typename Earcut::Node* +Earcut::splitPolygon(Node* a, Node* b) { + Node* a2 = nodes.construct(a->i, a->x, a->y); + Node* b2 = nodes.construct(b->i, b->x, b->y); + Node* an = a->next; + Node* bp = b->prev; + + a->next = b; + b->prev = a; + + a2->next = an; + an->prev = a2; + + b2->next = a2; + a2->prev = b2; + + bp->next = b2; + b2->prev = bp; + + return b2; +} + +// create a node and util::optionally link it with previous one (in a circular doubly linked list) +template template +typename Earcut::Node* +Earcut::insertNode(std::size_t i, const Point& pt, Node* last) { + Node* p = nodes.construct(static_cast(i), util::nth<0, Point>::get(pt), util::nth<1, Point>::get(pt)); + + if (!last) { + p->prev = p; + p->next = p; + + } else { + assert(last); + p->next = last->next; + p->prev = last; + last->next->prev = p; + last->next = p; + } + return p; +} + +template +void Earcut::removeNode(Node* p) { + p->next->prev = p->prev; + p->prev->next = p->next; + + if (p->prevZ) p->prevZ->nextZ = p->nextZ; + if (p->nextZ) p->nextZ->prevZ = p->prevZ; +} +} + +template +std::vector earcut(const Polygon& poly) { + mapbox::detail::Earcut earcut; + earcut(poly); + return std::move(earcut.indices); +} +} \ No newline at end of file diff --git a/external/include/tiny_obj_loader.h b/external/include/tiny_obj_loader.h new file mode 100644 index 00000000..707f67b8 --- /dev/null +++ b/external/include/tiny_obj_loader.h @@ -0,0 +1,3366 @@ +/* +The MIT License (MIT) +Copyright (c) 2012-Present, Syoyo Fujita and many contributors. +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +// +// version 2.0.0 : Add new object oriented API. 1.x API is still provided. +// * Support line primitive. +// * Support points primitive. +// * Support multiple search path for .mtl(v1 API). +// * Support vertex weight `vw`(as an tinyobj extension) +// * Support escaped whitespece in mtllib +// * Add robust triangulation using Mapbox earcut(TINYOBJLOADER_USE_MAPBOX_EARCUT). +// version 1.4.0 : Modifed ParseTextureNameAndOption API +// version 1.3.1 : Make ParseTextureNameAndOption API public +// version 1.3.0 : Separate warning and error message(breaking API of LoadObj) +// version 1.2.3 : Added color space extension('-colorspace') to tex opts. +// version 1.2.2 : Parse multiple group names. +// version 1.2.1 : Added initial support for line('l') primitive(PR #178) +// version 1.2.0 : Hardened implementation(#175) +// version 1.1.1 : Support smoothing groups(#162) +// version 1.1.0 : Support parsing vertex color(#144) +// version 1.0.8 : Fix parsing `g` tag just after `usemtl`(#138) +// version 1.0.7 : Support multiple tex options(#126) +// version 1.0.6 : Add TINYOBJLOADER_USE_DOUBLE option(#124) +// version 1.0.5 : Ignore `Tr` when `d` exists in MTL(#43) +// version 1.0.4 : Support multiple filenames for 'mtllib'(#112) +// version 1.0.3 : Support parsing texture options(#85) +// version 1.0.2 : Improve parsing speed by about a factor of 2 for large +// files(#105) +// version 1.0.1 : Fixes a shape is lost if obj ends with a 'usemtl'(#104) +// version 1.0.0 : Change data structure. Change license from BSD to MIT. +// + +// +// Use this in *one* .cc +// #define TINYOBJLOADER_IMPLEMENTATION +// #include "tiny_obj_loader.h" +// + +#ifndef TINY_OBJ_LOADER_H_ +#define TINY_OBJ_LOADER_H_ + +#include +#include +#include + +namespace tinyobj { + + // TODO(syoyo): Better C++11 detection for older compiler +#if __cplusplus > 199711L +#define TINYOBJ_OVERRIDE override +#else +#define TINYOBJ_OVERRIDE +#endif + +#ifdef __clang__ +#pragma clang diagnostic push +#if __has_warning("-Wzero-as-null-pointer-constant") +#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant" +#endif + +#pragma clang diagnostic ignored "-Wpadded" + +#endif + +// https://en.wikipedia.org/wiki/Wavefront_.obj_file says ... +// +// -blendu on | off # set horizontal texture blending +// (default on) +// -blendv on | off # set vertical texture blending +// (default on) +// -boost real_value # boost mip-map sharpness +// -mm base_value gain_value # modify texture map values (default +// 0 1) +// # base_value = brightness, +// gain_value = contrast +// -o u [v [w]] # Origin offset (default +// 0 0 0) +// -s u [v [w]] # Scale (default +// 1 1 1) +// -t u [v [w]] # Turbulence (default +// 0 0 0) +// -texres resolution # texture resolution to create +// -clamp on | off # only render texels in the clamped +// 0-1 range (default off) +// # When unclamped, textures are +// repeated across a surface, +// # when clamped, only texels which +// fall within the 0-1 +// # range are rendered. +// -bm mult_value # bump multiplier (for bump maps +// only) +// +// -imfchan r | g | b | m | l | z # specifies which channel of the file +// is used to +// # create a scalar or bump texture. +// r:red, g:green, +// # b:blue, m:matte, l:luminance, +// z:z-depth.. +// # (the default for bump is 'l' and +// for decal is 'm') +// bump -imfchan r bumpmap.tga # says to use the red channel of +// bumpmap.tga as the bumpmap +// +// For reflection maps... +// +// -type sphere # specifies a sphere for a "refl" +// reflection map +// -type cube_top | cube_bottom | # when using a cube map, the texture +// file for each +// cube_front | cube_back | # side of the cube is specified +// separately +// cube_left | cube_right +// +// TinyObjLoader extension. +// +// -colorspace SPACE # Color space of the texture. e.g. +// 'sRGB` or 'linear' +// + +#ifdef TINYOBJLOADER_USE_DOUBLE +//#pragma message "using double" + typedef double real_t; +#else +//#pragma message "using float" + typedef float real_t; +#endif + + typedef enum { + TEXTURE_TYPE_NONE, // default + TEXTURE_TYPE_SPHERE, + TEXTURE_TYPE_CUBE_TOP, + TEXTURE_TYPE_CUBE_BOTTOM, + TEXTURE_TYPE_CUBE_FRONT, + TEXTURE_TYPE_CUBE_BACK, + TEXTURE_TYPE_CUBE_LEFT, + TEXTURE_TYPE_CUBE_RIGHT + } texture_type_t; + + struct texture_option_t { + texture_type_t type; // -type (default TEXTURE_TYPE_NONE) + real_t sharpness; // -boost (default 1.0?) + real_t brightness; // base_value in -mm option (default 0) + real_t contrast; // gain_value in -mm option (default 1) + real_t origin_offset[3]; // -o u [v [w]] (default 0 0 0) + real_t scale[3]; // -s u [v [w]] (default 1 1 1) + real_t turbulence[3]; // -t u [v [w]] (default 0 0 0) + int texture_resolution; // -texres resolution (No default value in the spec. + // We'll use -1) + bool clamp; // -clamp (default false) + char imfchan; // -imfchan (the default for bump is 'l' and for decal is 'm') + bool blendu; // -blendu (default on) + bool blendv; // -blendv (default on) + real_t bump_multiplier; // -bm (for bump maps only, default 1.0) + + // extension + std::string colorspace; // Explicitly specify color space of stored texel + // value. Usually `sRGB` or `linear` (default empty). + }; + + struct material_t { + std::string name; + + real_t ambient[3]; + real_t diffuse[3]; + real_t specular[3]; + real_t transmittance[3]; + real_t emission[3]; + real_t shininess; + real_t ior; // index of refraction + real_t dissolve; // 1 == opaque; 0 == fully transparent + // illumination model (see http://www.fileformat.info/format/material/) + int illum; + + int dummy; // Suppress padding warning. + + std::string ambient_texname; // map_Ka + std::string diffuse_texname; // map_Kd + std::string specular_texname; // map_Ks + std::string specular_highlight_texname; // map_Ns + std::string bump_texname; // map_bump, map_Bump, bump + std::string displacement_texname; // disp + std::string alpha_texname; // map_d + std::string reflection_texname; // refl + + texture_option_t ambient_texopt; + texture_option_t diffuse_texopt; + texture_option_t specular_texopt; + texture_option_t specular_highlight_texopt; + texture_option_t bump_texopt; + texture_option_t displacement_texopt; + texture_option_t alpha_texopt; + texture_option_t reflection_texopt; + + // PBR extension + // http://exocortex.com/blog/extending_wavefront_mtl_to_support_pbr + real_t roughness; // [0, 1] default 0 + real_t metallic; // [0, 1] default 0 + real_t sheen; // [0, 1] default 0 + real_t clearcoat_thickness; // [0, 1] default 0 + real_t clearcoat_roughness; // [0, 1] default 0 + real_t anisotropy; // aniso. [0, 1] default 0 + real_t anisotropy_rotation; // anisor. [0, 1] default 0 + real_t pad0; + std::string roughness_texname; // map_Pr + std::string metallic_texname; // map_Pm + std::string sheen_texname; // map_Ps + std::string emissive_texname; // map_Ke + std::string normal_texname; // norm. For normal mapping. + + texture_option_t roughness_texopt; + texture_option_t metallic_texopt; + texture_option_t sheen_texopt; + texture_option_t emissive_texopt; + texture_option_t normal_texopt; + + int pad2; + + std::map unknown_parameter; + +#ifdef TINY_OBJ_LOADER_PYTHON_BINDING + // For pybind11 + std::array GetDiffuse() { + std::array values; + values[0] = double(diffuse[0]); + values[1] = double(diffuse[1]); + values[2] = double(diffuse[2]); + + return values; + } + + std::array GetSpecular() { + std::array values; + values[0] = double(specular[0]); + values[1] = double(specular[1]); + values[2] = double(specular[2]); + + return values; + } + + std::array GetTransmittance() { + std::array values; + values[0] = double(transmittance[0]); + values[1] = double(transmittance[1]); + values[2] = double(transmittance[2]); + + return values; + } + + std::array GetEmission() { + std::array values; + values[0] = double(emission[0]); + values[1] = double(emission[1]); + values[2] = double(emission[2]); + + return values; + } + + std::array GetAmbient() { + std::array values; + values[0] = double(ambient[0]); + values[1] = double(ambient[1]); + values[2] = double(ambient[2]); + + return values; + } + + void SetDiffuse(std::array& a) { + diffuse[0] = real_t(a[0]); + diffuse[1] = real_t(a[1]); + diffuse[2] = real_t(a[2]); + } + + void SetAmbient(std::array& a) { + ambient[0] = real_t(a[0]); + ambient[1] = real_t(a[1]); + ambient[2] = real_t(a[2]); + } + + void SetSpecular(std::array& a) { + specular[0] = real_t(a[0]); + specular[1] = real_t(a[1]); + specular[2] = real_t(a[2]); + } + + void SetTransmittance(std::array& a) { + transmittance[0] = real_t(a[0]); + transmittance[1] = real_t(a[1]); + transmittance[2] = real_t(a[2]); + } + + std::string GetCustomParameter(const std::string& key) { + std::map::const_iterator it = + unknown_parameter.find(key); + + if (it != unknown_parameter.end()) { + return it->second; + } + return std::string(); + } + +#endif + }; + + struct tag_t { + std::string name; + + std::vector intValues; + std::vector floatValues; + std::vector stringValues; + }; + + struct joint_and_weight_t { + int joint_id; + real_t weight; + }; + + struct skin_weight_t { + int vertex_id; // Corresponding vertex index in `attrib_t::vertices`. + // Compared to `index_t`, this index must be positive and + // start with 0(does not allow relative indexing) + std::vector weightValues; + }; + + // Index struct to support different indices for vtx/normal/texcoord. + // -1 means not used. + struct index_t { + int vertex_index; + int normal_index; + int texcoord_index; + }; + + struct mesh_t { + std::vector indices; + std::vector + num_face_vertices; // The number of vertices per + // face. 3 = triangle, 4 = quad, + // ... Up to 255 vertices per face. + std::vector material_ids; // per-face material ID + std::vector smoothing_group_ids; // per-face smoothing group + // ID(0 = off. positive value + // = group id) + std::vector tags; // SubD tag + }; + + // struct path_t { + // std::vector indices; // pairs of indices for lines + //}; + + struct lines_t { + // Linear flattened indices. + std::vector indices; // indices for vertices(poly lines) + std::vector num_line_vertices; // The number of vertices per line. + }; + + struct points_t { + std::vector indices; // indices for points + }; + + struct shape_t { + std::string name; + mesh_t mesh; + lines_t lines; + points_t points; + }; + + // Vertex attributes + struct attrib_t { + std::vector vertices; // 'v'(xyz) + + // For backward compatibility, we store vertex weight in separate array. + std::vector vertex_weights; // 'v'(w) + std::vector normals; // 'vn' + std::vector texcoords; // 'vt'(uv) + + // For backward compatibility, we store texture coordinate 'w' in separate + // array. + std::vector texcoord_ws; // 'vt'(w) + std::vector colors; // extension: vertex colors + + // + // TinyObj extension. + // + + // NOTE(syoyo): array index is based on the appearance order. + // To get a corresponding skin weight for a specific vertex id `vid`, + // Need to reconstruct a look up table: `skin_weight_t::vertex_id` == `vid` + // (e.g. using std::map, std::unordered_map) + std::vector skin_weights; + + attrib_t() {} + + // + // For pybind11 + // + const std::vector& GetVertices() const { return vertices; } + + const std::vector& GetVertexWeights() const { return vertex_weights; } + }; + + struct callback_t { + // W is optional and set to 1 if there is no `w` item in `v` line + void (*vertex_cb)(void* user_data, real_t x, real_t y, real_t z, real_t w); + void (*normal_cb)(void* user_data, real_t x, real_t y, real_t z); + + // y and z are optional and set to 0 if there is no `y` and/or `z` item(s) in + // `vt` line. + void (*texcoord_cb)(void* user_data, real_t x, real_t y, real_t z); + + // called per 'f' line. num_indices is the number of face indices(e.g. 3 for + // triangle, 4 for quad) + // 0 will be passed for undefined index in index_t members. + void (*index_cb)(void* user_data, index_t* indices, int num_indices); + // `name` material name, `material_id` = the array index of material_t[]. -1 + // if + // a material not found in .mtl + void (*usemtl_cb)(void* user_data, const char* name, int material_id); + // `materials` = parsed material data. + void (*mtllib_cb)(void* user_data, const material_t* materials, + int num_materials); + // There may be multiple group names + void (*group_cb)(void* user_data, const char** names, int num_names); + void (*object_cb)(void* user_data, const char* name); + + callback_t() + : vertex_cb(NULL), + normal_cb(NULL), + texcoord_cb(NULL), + index_cb(NULL), + usemtl_cb(NULL), + mtllib_cb(NULL), + group_cb(NULL), + object_cb(NULL) {} + }; + + class MaterialReader { + public: + MaterialReader() {} + virtual ~MaterialReader(); + + virtual bool operator()(const std::string& matId, + std::vector* materials, + std::map* matMap, std::string* warn, + std::string* err) = 0; + }; + + /// + /// Read .mtl from a file. + /// + class MaterialFileReader : public MaterialReader { + public: + // Path could contain separator(';' in Windows, ':' in Posix) + explicit MaterialFileReader(const std::string& mtl_basedir) + : m_mtlBaseDir(mtl_basedir) {} + virtual ~MaterialFileReader() TINYOBJ_OVERRIDE {} + virtual bool operator()(const std::string& matId, + std::vector* materials, + std::map* matMap, std::string* warn, + std::string* err) TINYOBJ_OVERRIDE; + + private: + std::string m_mtlBaseDir; + }; + + /// + /// Read .mtl from a stream. + /// + class MaterialStreamReader : public MaterialReader { + public: + explicit MaterialStreamReader(std::istream& inStream) + : m_inStream(inStream) {} + virtual ~MaterialStreamReader() TINYOBJ_OVERRIDE {} + virtual bool operator()(const std::string& matId, + std::vector* materials, + std::map* matMap, std::string* warn, + std::string* err) TINYOBJ_OVERRIDE; + + private: + std::istream& m_inStream; + }; + + // v2 API + struct ObjReaderConfig { + bool triangulate; // triangulate polygon? + + // Currently not used. + // "simple" or empty: Create triangle fan + // "earcut": Use the algorithm based on Ear clipping + std::string triangulation_method; + + /// Parse vertex color. + /// If vertex color is not present, its filled with default value. + /// false = no vertex color + /// This will increase memory of parsed .obj + bool vertex_color; + + /// + /// Search path to .mtl file. + /// Default = "" = search from the same directory of .obj file. + /// Valid only when loading .obj from a file. + /// + std::string mtl_search_path; + + ObjReaderConfig() + : triangulate(true), triangulation_method("simple"), vertex_color(true) {} + }; + + /// + /// Wavefront .obj reader class(v2 API) + /// + class ObjReader { + public: + ObjReader() : valid_(false) {} + ~ObjReader() {} + + /// + /// Load .obj and .mtl from a file. + /// + /// @param[in] filename wavefront .obj filename + /// @param[in] config Reader configuration + /// + bool ParseFromFile(const std::string& filename, + const ObjReaderConfig& config = ObjReaderConfig()); + + /// + /// Parse .obj from a text string. + /// Need to supply .mtl text string by `mtl_text`. + /// This function ignores `mtllib` line in .obj text. + /// + /// @param[in] obj_text wavefront .obj filename + /// @param[in] mtl_text wavefront .mtl filename + /// @param[in] config Reader configuration + /// + bool ParseFromString(const std::string& obj_text, const std::string& mtl_text, + const ObjReaderConfig& config = ObjReaderConfig()); + + /// + /// .obj was loaded or parsed correctly. + /// + bool Valid() const { return valid_; } + + const attrib_t& GetAttrib() const { return attrib_; } + + const std::vector& GetShapes() const { return shapes_; } + + const std::vector& GetMaterials() const { return materials_; } + + /// + /// Warning message(may be filled after `Load` or `Parse`) + /// + const std::string& Warning() const { return warning_; } + + /// + /// Error message(filled when `Load` or `Parse` failed) + /// + const std::string& Error() const { return error_; } + + private: + bool valid_; + + attrib_t attrib_; + std::vector shapes_; + std::vector materials_; + + std::string warning_; + std::string error_; + }; + + /// ==>>========= Legacy v1 API ============================================= + + /// Loads .obj from a file. + /// 'attrib', 'shapes' and 'materials' will be filled with parsed shape data + /// 'shapes' will be filled with parsed shape data + /// Returns true when loading .obj become success. + /// Returns warning message into `warn`, and error message into `err` + /// 'mtl_basedir' is optional, and used for base directory for .mtl file. + /// In default(`NULL'), .mtl file is searched from an application's working + /// directory. + /// 'triangulate' is optional, and used whether triangulate polygon face in .obj + /// or not. + /// Option 'default_vcols_fallback' specifies whether vertex colors should + /// always be defined, even if no colors are given (fallback to white). + bool LoadObj(attrib_t* attrib, std::vector* shapes, + std::vector* materials, std::string* warn, + std::string* err, const char* filename, + const char* mtl_basedir = NULL, bool triangulate = true, + bool default_vcols_fallback = true); + + /// Loads .obj from a file with custom user callback. + /// .mtl is loaded as usual and parsed material_t data will be passed to + /// `callback.mtllib_cb`. + /// Returns true when loading .obj/.mtl become success. + /// Returns warning message into `warn`, and error message into `err` + /// See `examples/callback_api/` for how to use this function. + bool LoadObjWithCallback(std::istream& inStream, const callback_t& callback, + void* user_data = NULL, + MaterialReader* readMatFn = NULL, + std::string* warn = NULL, std::string* err = NULL); + + /// Loads object from a std::istream, uses `readMatFn` to retrieve + /// std::istream for materials. + /// Returns true when loading .obj become success. + /// Returns warning and error message into `err` + bool LoadObj(attrib_t* attrib, std::vector* shapes, + std::vector* materials, std::string* warn, + std::string* err, std::istream* inStream, + MaterialReader* readMatFn = NULL, bool triangulate = true, + bool default_vcols_fallback = true); + + /// Loads materials into std::map + void LoadMtl(std::map* material_map, + std::vector* materials, std::istream* inStream, + std::string* warning, std::string* err); + + /// + /// Parse texture name and texture option for custom texture parameter through + /// material::unknown_parameter + /// + /// @param[out] texname Parsed texture name + /// @param[out] texopt Parsed texopt + /// @param[in] linebuf Input string + /// + bool ParseTextureNameAndOption(std::string* texname, texture_option_t* texopt, + const char* linebuf); + + /// =<<========== Legacy v1 API ============================================= + +} // namespace tinyobj + +#endif // TINY_OBJ_LOADER_H_ + +#ifdef TINYOBJLOADER_IMPLEMENTATION +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef TINYOBJLOADER_USE_MAPBOX_EARCUT + +#ifdef TINYOBJLOADER_DONOT_INCLUDE_MAPBOX_EARCUT +// Assume earcut.hpp is included outside of tiny_obj_loader.h +#else + +#ifdef __clang__ +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Weverything" +#endif + +#include +#include "mapbox/earcut.hpp" + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif + +#endif + +#endif // TINYOBJLOADER_USE_MAPBOX_EARCUT + +namespace tinyobj { + + MaterialReader::~MaterialReader() {} + + struct vertex_index_t { + int v_idx, vt_idx, vn_idx; + vertex_index_t() : v_idx(-1), vt_idx(-1), vn_idx(-1) {} + explicit vertex_index_t(int idx) : v_idx(idx), vt_idx(idx), vn_idx(idx) {} + vertex_index_t(int vidx, int vtidx, int vnidx) + : v_idx(vidx), vt_idx(vtidx), vn_idx(vnidx) {} + }; + + // Internal data structure for face representation + // index + smoothing group. + struct face_t { + unsigned int + smoothing_group_id; // smoothing group id. 0 = smoothing groupd is off. + int pad_; + std::vector vertex_indices; // face vertex indices. + + face_t() : smoothing_group_id(0), pad_(0) {} + }; + + // Internal data structure for line representation + struct __line_t { + // l v1/vt1 v2/vt2 ... + // In the specification, line primitrive does not have normal index, but + // TinyObjLoader allow it + std::vector vertex_indices; + }; + + // Internal data structure for points representation + struct __points_t { + // p v1 v2 ... + // In the specification, point primitrive does not have normal index and + // texture coord index, but TinyObjLoader allow it. + std::vector vertex_indices; + }; + + struct tag_sizes { + tag_sizes() : num_ints(0), num_reals(0), num_strings(0) {} + int num_ints; + int num_reals; + int num_strings; + }; + + struct obj_shape { + std::vector v; + std::vector vn; + std::vector vt; + }; + + // + // Manages group of primitives(face, line, points, ...) + struct PrimGroup { + std::vector faceGroup; + std::vector<__line_t> lineGroup; + std::vector<__points_t> pointsGroup; + + void clear() { + faceGroup.clear(); + lineGroup.clear(); + pointsGroup.clear(); + } + + bool IsEmpty() const { + return faceGroup.empty() && lineGroup.empty() && pointsGroup.empty(); + } + + // TODO(syoyo): bspline, surface, ... + }; + + // See + // http://stackoverflow.com/questions/6089231/getting-std-ifstream-to-handle-lf-cr-and-crlf + static std::istream& safeGetline(std::istream& is, std::string& t) { + t.clear(); + + // The characters in the stream are read one-by-one using a std::streambuf. + // That is faster than reading them one-by-one using the std::istream. + // Code that uses streambuf this way must be guarded by a sentry object. + // The sentry object performs various tasks, + // such as thread synchronization and updating the stream state. + + std::istream::sentry se(is, true); + std::streambuf* sb = is.rdbuf(); + + if (se) { + for (;;) { + int c = sb->sbumpc(); + switch (c) { + case '\n': + return is; + case '\r': + if (sb->sgetc() == '\n') sb->sbumpc(); + return is; + case EOF: + // Also handle the case when the last line has no line ending + if (t.empty()) is.setstate(std::ios::eofbit); + return is; + default: + t += static_cast(c); + } + } + } + + return is; + } + +#define IS_SPACE(x) (((x) == ' ') || ((x) == '\t')) +#define IS_DIGIT(x) \ + (static_cast((x) - '0') < static_cast(10)) +#define IS_NEW_LINE(x) (((x) == '\r') || ((x) == '\n') || ((x) == '\0')) + + // Make index zero-base, and also support relative index. + static inline bool fixIndex(int idx, int n, int* ret) { + if (!ret) { + return false; + } + + if (idx > 0) { + (*ret) = idx - 1; + return true; + } + + if (idx == 0) { + // zero is not allowed according to the spec. + return false; + } + + if (idx < 0) { + (*ret) = n + idx; // negative value = relative + return true; + } + + return false; // never reach here. + } + + 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; + bool leading_decimal_dots = false; + + /* + BEGIN PARSING. + */ + + // Find out what sign we've got. + if (*curr == '+' || *curr == '-') { + sign = *curr; + curr++; + if ((curr != s_end) && (*curr == '.')) { + // accept. Somethig like `.7e+2`, `-.5234` + leading_decimal_dots = true; + } + } + else if (IS_DIGIT(*curr)) { /* Pass through. */ + } + else if (*curr == '.') { + // accept. Somethig like `.7e+2`, `-.5234` + leading_decimal_dots = true; + } + else { + goto fail; + } + + // Read the integer part. + end_not_reached = (curr != s_end); + if (!leading_decimal_dots) { + while (end_not_reached && IS_DIGIT(*curr)) { + mantissa *= 10; + mantissa += static_cast(*curr - 0x30); + curr++; + read++; + end_not_reached = (curr != s_end); + } + + // 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; + end_not_reached = (curr != s_end); + while (end_not_reached && IS_DIGIT(*curr)) { + static const double pow_lut[] = { + 1.0, 0.1, 0.01, 0.001, 0.0001, 0.00001, 0.000001, 0.0000001, + }; + const int lut_entries = sizeof pow_lut / sizeof pow_lut[0]; + + // NOTE: Don't use powf here, it will absolutely murder precision. + mantissa += static_cast(*curr - 0x30) * + (read < lut_entries ? pow_lut[read] : std::pow(10.0, -read)); + read++; + curr++; + end_not_reached = (curr != s_end); + } + } + 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. + end_not_reached = (curr != s_end); + if (end_not_reached && (*curr == '+' || *curr == '-')) { + exp_sign = *curr; + curr++; + } + else if (IS_DIGIT(*curr)) { /* Pass through. */ + } + else { + // Empty E is not allowed. + goto fail; + } + + read = 0; + end_not_reached = (curr != s_end); + while (end_not_reached && IS_DIGIT(*curr)) { + // To avoid annoying MSVC's min/max macro definiton, + // Use hardcoded int max value + if (exponent > (2147483647 / 10)) { // 2147483647 = std::numeric_limits::max() + // Integer overflow + goto fail; + } + exponent *= 10; + exponent += static_cast(*curr - 0x30); + curr++; + read++; + end_not_reached = (curr != s_end); + } + exponent *= (exp_sign == '+' ? 1 : -1); + if (read == 0) goto fail; + } + + assemble: + *result = (sign == '+' ? 1 : -1) * + (exponent ? std::ldexp(mantissa * std::pow(5.0, exponent), exponent) + : mantissa); + return true; + fail: + return false; + } + + static inline real_t parseReal(const char** token, double default_value = 0.0) { + (*token) += strspn((*token), " \t"); + const char* end = (*token) + strcspn((*token), " \t\r"); + double val = default_value; + tryParseDouble((*token), end, &val); + real_t f = static_cast(val); + (*token) = end; + return f; + } + + static inline bool parseReal(const char** token, real_t* out) { + (*token) += strspn((*token), " \t"); + const char* end = (*token) + strcspn((*token), " \t\r"); + double val; + bool ret = tryParseDouble((*token), end, &val); + if (ret) { + real_t f = static_cast(val); + (*out) = f; + } + (*token) = end; + return ret; + } + + static inline void parseReal2(real_t* x, real_t* y, const char** token, + const double default_x = 0.0, + const double default_y = 0.0) { + (*x) = parseReal(token, default_x); + (*y) = parseReal(token, default_y); + } + + static inline void parseReal3(real_t* x, real_t* y, real_t* z, + const char** token, const double default_x = 0.0, + const double default_y = 0.0, + const double default_z = 0.0) { + (*x) = parseReal(token, default_x); + (*y) = parseReal(token, default_y); + (*z) = parseReal(token, default_z); + } + + static inline void parseV(real_t* x, real_t* y, real_t* z, real_t* w, + const char** token, const double default_x = 0.0, + const double default_y = 0.0, + const double default_z = 0.0, + const double default_w = 1.0) { + (*x) = parseReal(token, default_x); + (*y) = parseReal(token, default_y); + (*z) = parseReal(token, default_z); + (*w) = parseReal(token, default_w); + } + + // Extension: parse vertex with colors(6 items) + static inline bool parseVertexWithColor(real_t* x, real_t* y, real_t* z, + real_t* r, real_t* g, real_t* b, + const char** token, + const double default_x = 0.0, + const double default_y = 0.0, + const double default_z = 0.0) { + (*x) = parseReal(token, default_x); + (*y) = parseReal(token, default_y); + (*z) = parseReal(token, default_z); + + const bool found_color = + parseReal(token, r) && parseReal(token, g) && parseReal(token, b); + + if (!found_color) { + (*r) = (*g) = (*b) = 1.0; + } + + return found_color; + } + + static inline bool parseOnOff(const char** token, bool default_value = true) { + (*token) += strspn((*token), " \t"); + const char* end = (*token) + strcspn((*token), " \t\r"); + + bool ret = default_value; + if ((0 == strncmp((*token), "on", 2))) { + ret = true; + } + else if ((0 == strncmp((*token), "off", 3))) { + ret = false; + } + + (*token) = end; + return ret; + } + + static inline texture_type_t parseTextureType( + const char** token, texture_type_t default_value = TEXTURE_TYPE_NONE) { + (*token) += strspn((*token), " \t"); + const char* end = (*token) + strcspn((*token), " \t\r"); + texture_type_t ty = default_value; + + if ((0 == strncmp((*token), "cube_top", strlen("cube_top")))) { + ty = TEXTURE_TYPE_CUBE_TOP; + } + else if ((0 == strncmp((*token), "cube_bottom", strlen("cube_bottom")))) { + ty = TEXTURE_TYPE_CUBE_BOTTOM; + } + else if ((0 == strncmp((*token), "cube_left", strlen("cube_left")))) { + ty = TEXTURE_TYPE_CUBE_LEFT; + } + else if ((0 == strncmp((*token), "cube_right", strlen("cube_right")))) { + ty = TEXTURE_TYPE_CUBE_RIGHT; + } + else if ((0 == strncmp((*token), "cube_front", strlen("cube_front")))) { + ty = TEXTURE_TYPE_CUBE_FRONT; + } + else if ((0 == strncmp((*token), "cube_back", strlen("cube_back")))) { + ty = TEXTURE_TYPE_CUBE_BACK; + } + else if ((0 == strncmp((*token), "sphere", strlen("sphere")))) { + ty = TEXTURE_TYPE_SPHERE; + } + + (*token) = end; + return ty; + } + + static tag_sizes parseTagTriple(const char** token) { + tag_sizes ts; + + (*token) += strspn((*token), " \t"); + ts.num_ints = atoi((*token)); + (*token) += strcspn((*token), "/ \t\r"); + if ((*token)[0] != '/') { + return ts; + } + + (*token)++; // Skip '/' + + (*token) += strspn((*token), " \t"); + ts.num_reals = atoi((*token)); + (*token) += strcspn((*token), "/ \t\r"); + if ((*token)[0] != '/') { + return ts; + } + (*token)++; // Skip '/' + + ts.num_strings = parseInt(token); + + return ts; + } + + // Parse triples with index offsets: i, i/j/k, i//k, i/j + static bool parseTriple(const char** token, int vsize, int vnsize, int vtsize, + vertex_index_t* ret) { + if (!ret) { + return false; + } + + vertex_index_t vi(-1); + + if (!fixIndex(atoi((*token)), vsize, &(vi.v_idx))) { + return false; + } + + (*token) += strcspn((*token), "/ \t\r"); + if ((*token)[0] != '/') { + (*ret) = vi; + return true; + } + (*token)++; + + // i//k + if ((*token)[0] == '/') { + (*token)++; + if (!fixIndex(atoi((*token)), vnsize, &(vi.vn_idx))) { + return false; + } + (*token) += strcspn((*token), "/ \t\r"); + (*ret) = vi; + return true; + } + + // i/j/k or i/j + if (!fixIndex(atoi((*token)), vtsize, &(vi.vt_idx))) { + return false; + } + + (*token) += strcspn((*token), "/ \t\r"); + if ((*token)[0] != '/') { + (*ret) = vi; + return true; + } + + // i/j/k + (*token)++; // skip '/' + if (!fixIndex(atoi((*token)), vnsize, &(vi.vn_idx))) { + return false; + } + (*token) += strcspn((*token), "/ \t\r"); + + (*ret) = vi; + + return true; + } + + // Parse raw triples: i, i/j/k, i//k, i/j + static vertex_index_t parseRawTriple(const char** token) { + vertex_index_t vi(static_cast(0)); // 0 is an invalid index in OBJ + + vi.v_idx = atoi((*token)); + (*token) += strcspn((*token), "/ \t\r"); + if ((*token)[0] != '/') { + return vi; + } + (*token)++; + + // i//k + if ((*token)[0] == '/') { + (*token)++; + vi.vn_idx = atoi((*token)); + (*token) += strcspn((*token), "/ \t\r"); + return vi; + } + + // i/j/k or i/j + vi.vt_idx = atoi((*token)); + (*token) += strcspn((*token), "/ \t\r"); + if ((*token)[0] != '/') { + return vi; + } + + // i/j/k + (*token)++; // skip '/' + vi.vn_idx = atoi((*token)); + (*token) += strcspn((*token), "/ \t\r"); + return vi; + } + + bool ParseTextureNameAndOption(std::string* texname, texture_option_t* texopt, + const char* linebuf) { + // @todo { write more robust lexer and parser. } + bool found_texname = false; + std::string texture_name; + + const char* token = linebuf; // Assume line ends with NULL + + while (!IS_NEW_LINE((*token))) { + token += strspn(token, " \t"); // skip space + if ((0 == strncmp(token, "-blendu", 7)) && IS_SPACE((token[7]))) { + token += 8; + texopt->blendu = parseOnOff(&token, /* default */ true); + } + else if ((0 == strncmp(token, "-blendv", 7)) && IS_SPACE((token[7]))) { + token += 8; + texopt->blendv = parseOnOff(&token, /* default */ true); + } + else if ((0 == strncmp(token, "-clamp", 6)) && IS_SPACE((token[6]))) { + token += 7; + texopt->clamp = parseOnOff(&token, /* default */ true); + } + else if ((0 == strncmp(token, "-boost", 6)) && IS_SPACE((token[6]))) { + token += 7; + texopt->sharpness = parseReal(&token, 1.0); + } + else if ((0 == strncmp(token, "-bm", 3)) && IS_SPACE((token[3]))) { + token += 4; + texopt->bump_multiplier = parseReal(&token, 1.0); + } + else if ((0 == strncmp(token, "-o", 2)) && IS_SPACE((token[2]))) { + token += 3; + parseReal3(&(texopt->origin_offset[0]), &(texopt->origin_offset[1]), + &(texopt->origin_offset[2]), &token); + } + else if ((0 == strncmp(token, "-s", 2)) && IS_SPACE((token[2]))) { + token += 3; + parseReal3(&(texopt->scale[0]), &(texopt->scale[1]), &(texopt->scale[2]), + &token, 1.0, 1.0, 1.0); + } + else if ((0 == strncmp(token, "-t", 2)) && IS_SPACE((token[2]))) { + token += 3; + parseReal3(&(texopt->turbulence[0]), &(texopt->turbulence[1]), + &(texopt->turbulence[2]), &token); + } + else if ((0 == strncmp(token, "-type", 5)) && IS_SPACE((token[5]))) { + token += 5; + texopt->type = parseTextureType((&token), TEXTURE_TYPE_NONE); + } + else if ((0 == strncmp(token, "-texres", 7)) && IS_SPACE((token[7]))) { + token += 7; + // TODO(syoyo): Check if arg is int type. + texopt->texture_resolution = parseInt(&token); + } + else if ((0 == strncmp(token, "-imfchan", 8)) && IS_SPACE((token[8]))) { + token += 9; + token += strspn(token, " \t"); + const char* end = token + strcspn(token, " \t\r"); + if ((end - token) == 1) { // Assume one char for -imfchan + texopt->imfchan = (*token); + } + token = end; + } + else if ((0 == strncmp(token, "-mm", 3)) && IS_SPACE((token[3]))) { + token += 4; + parseReal2(&(texopt->brightness), &(texopt->contrast), &token, 0.0, 1.0); + } + else if ((0 == strncmp(token, "-colorspace", 11)) && + IS_SPACE((token[11]))) { + token += 12; + texopt->colorspace = parseString(&token); + } + else { + // Assume texture filename +#if 0 + size_t len = strcspn(token, " \t\r"); // untile next space + texture_name = std::string(token, token + len); + token += len; + + token += strspn(token, " \t"); // skip space +#else + // Read filename until line end to parse filename containing whitespace + // TODO(syoyo): Support parsing texture option flag after the filename. + texture_name = std::string(token); + token += texture_name.length(); +#endif + + found_texname = true; + } + } + + if (found_texname) { + (*texname) = texture_name; + return true; + } + else { + return false; + } + } + + static void InitTexOpt(texture_option_t* texopt, const bool is_bump) { + if (is_bump) { + texopt->imfchan = 'l'; + } + else { + texopt->imfchan = 'm'; + } + texopt->bump_multiplier = static_cast(1.0); + texopt->clamp = false; + texopt->blendu = true; + texopt->blendv = true; + texopt->sharpness = static_cast(1.0); + texopt->brightness = static_cast(0.0); + texopt->contrast = static_cast(1.0); + texopt->origin_offset[0] = static_cast(0.0); + texopt->origin_offset[1] = static_cast(0.0); + texopt->origin_offset[2] = static_cast(0.0); + texopt->scale[0] = static_cast(1.0); + texopt->scale[1] = static_cast(1.0); + texopt->scale[2] = static_cast(1.0); + texopt->turbulence[0] = static_cast(0.0); + texopt->turbulence[1] = static_cast(0.0); + texopt->turbulence[2] = static_cast(0.0); + texopt->texture_resolution = -1; + texopt->type = TEXTURE_TYPE_NONE; + } + + static void InitMaterial(material_t* material) { + InitTexOpt(&material->ambient_texopt, /* is_bump */ false); + InitTexOpt(&material->diffuse_texopt, /* is_bump */ false); + InitTexOpt(&material->specular_texopt, /* is_bump */ false); + InitTexOpt(&material->specular_highlight_texopt, /* is_bump */ false); + InitTexOpt(&material->bump_texopt, /* is_bump */ true); + InitTexOpt(&material->displacement_texopt, /* is_bump */ false); + InitTexOpt(&material->alpha_texopt, /* is_bump */ false); + InitTexOpt(&material->reflection_texopt, /* is_bump */ false); + InitTexOpt(&material->roughness_texopt, /* is_bump */ false); + InitTexOpt(&material->metallic_texopt, /* is_bump */ false); + InitTexOpt(&material->sheen_texopt, /* is_bump */ false); + InitTexOpt(&material->emissive_texopt, /* is_bump */ false); + InitTexOpt(&material->normal_texopt, + /* is_bump */ false); // @fixme { is_bump will be true? } + material->name = ""; + material->ambient_texname = ""; + material->diffuse_texname = ""; + material->specular_texname = ""; + material->specular_highlight_texname = ""; + material->bump_texname = ""; + material->displacement_texname = ""; + material->reflection_texname = ""; + material->alpha_texname = ""; + for (int i = 0; i < 3; i++) { + material->ambient[i] = static_cast(0.0); + material->diffuse[i] = static_cast(0.0); + material->specular[i] = static_cast(0.0); + material->transmittance[i] = static_cast(0.0); + material->emission[i] = static_cast(0.0); + } + material->illum = 0; + material->dissolve = static_cast(1.0); + material->shininess = static_cast(1.0); + material->ior = static_cast(1.0); + + material->roughness = static_cast(0.0); + material->metallic = static_cast(0.0); + material->sheen = static_cast(0.0); + material->clearcoat_thickness = static_cast(0.0); + material->clearcoat_roughness = static_cast(0.0); + material->anisotropy_rotation = static_cast(0.0); + material->anisotropy = static_cast(0.0); + material->roughness_texname = ""; + material->metallic_texname = ""; + material->sheen_texname = ""; + material->emissive_texname = ""; + material->normal_texname = ""; + + material->unknown_parameter.clear(); + } + + // code from https://wrf.ecse.rpi.edu//Research/Short_Notes/pnpoly.html + template + static int pnpoly(int nvert, T* vertx, T* verty, T testx, T testy) { + int i, j, c = 0; + for (i = 0, j = nvert - 1; i < nvert; j = i++) { + if (((verty[i] > testy) != (verty[j] > testy)) && + (testx < + (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + + vertx[i])) + c = !c; + } + return c; + } + + // TODO(syoyo): refactor function. + static bool exportGroupsToShape(shape_t* shape, const PrimGroup& prim_group, + const std::vector& tags, + const int material_id, const std::string& name, + bool triangulate, const std::vector& v, + std::string* warn) { + if (prim_group.IsEmpty()) { + return false; + } + + shape->name = name; + + // polygon + if (!prim_group.faceGroup.empty()) { + // Flatten vertices and indices + for (size_t i = 0; i < prim_group.faceGroup.size(); i++) { + const face_t& face = prim_group.faceGroup[i]; + + size_t npolys = face.vertex_indices.size(); + + if (npolys < 3) { + // Face must have 3+ vertices. + if (warn) { + (*warn) += "Degenerated face found\n."; + } + continue; + } + + if (triangulate) { + if (npolys == 4) { + vertex_index_t i0 = face.vertex_indices[0]; + vertex_index_t i1 = face.vertex_indices[1]; + vertex_index_t i2 = face.vertex_indices[2]; + vertex_index_t i3 = face.vertex_indices[3]; + + size_t vi0 = size_t(i0.v_idx); + size_t vi1 = size_t(i1.v_idx); + size_t vi2 = size_t(i2.v_idx); + size_t vi3 = size_t(i3.v_idx); + + if (((3 * vi0 + 2) >= v.size()) || ((3 * vi1 + 2) >= v.size()) || + ((3 * vi2 + 2) >= v.size()) || ((3 * vi3 + 2) >= v.size())) { + // Invalid triangle. + // FIXME(syoyo): Is it ok to simply skip this invalid triangle? + if (warn) { + (*warn) += "Face with invalid vertex index found.\n"; + } + continue; + } + + real_t v0x = v[vi0 * 3 + 0]; + real_t v0y = v[vi0 * 3 + 1]; + real_t v0z = v[vi0 * 3 + 2]; + real_t v1x = v[vi1 * 3 + 0]; + real_t v1y = v[vi1 * 3 + 1]; + real_t v1z = v[vi1 * 3 + 2]; + real_t v2x = v[vi2 * 3 + 0]; + real_t v2y = v[vi2 * 3 + 1]; + real_t v2z = v[vi2 * 3 + 2]; + real_t v3x = v[vi3 * 3 + 0]; + real_t v3y = v[vi3 * 3 + 1]; + real_t v3z = v[vi3 * 3 + 2]; + + // There are two candidates to split the quad into two triangles. + // + // Choose the shortest edge. + // TODO: Is it better to determine the edge to split by calculating + // the area of each triangle? + // + // +---+ + // |\ | + // | \ | + // | \| + // +---+ + // + // +---+ + // | /| + // | / | + // |/ | + // +---+ + + real_t e02x = v2x - v0x; + real_t e02y = v2y - v0y; + real_t e02z = v2z - v0z; + real_t e13x = v3x - v1x; + real_t e13y = v3y - v1y; + real_t e13z = v3z - v1z; + + real_t sqr02 = e02x * e02x + e02y * e02y + e02z * e02z; + real_t sqr13 = e13x * e13x + e13y * e13y + e13z * e13z; + + index_t idx0, idx1, idx2, idx3; + + idx0.vertex_index = i0.v_idx; + idx0.normal_index = i0.vn_idx; + idx0.texcoord_index = i0.vt_idx; + idx1.vertex_index = i1.v_idx; + idx1.normal_index = i1.vn_idx; + idx1.texcoord_index = i1.vt_idx; + idx2.vertex_index = i2.v_idx; + idx2.normal_index = i2.vn_idx; + idx2.texcoord_index = i2.vt_idx; + idx3.vertex_index = i3.v_idx; + idx3.normal_index = i3.vn_idx; + idx3.texcoord_index = i3.vt_idx; + + if (sqr02 < sqr13) { + // [0, 1, 2], [0, 2, 3] + shape->mesh.indices.push_back(idx0); + shape->mesh.indices.push_back(idx1); + shape->mesh.indices.push_back(idx2); + + shape->mesh.indices.push_back(idx0); + shape->mesh.indices.push_back(idx2); + shape->mesh.indices.push_back(idx3); + } + else { + // [0, 1, 3], [1, 2, 3] + shape->mesh.indices.push_back(idx0); + shape->mesh.indices.push_back(idx1); + shape->mesh.indices.push_back(idx3); + + shape->mesh.indices.push_back(idx1); + shape->mesh.indices.push_back(idx2); + shape->mesh.indices.push_back(idx3); + } + + // Two triangle faces + shape->mesh.num_face_vertices.push_back(3); + shape->mesh.num_face_vertices.push_back(3); + + shape->mesh.material_ids.push_back(material_id); + shape->mesh.material_ids.push_back(material_id); + + shape->mesh.smoothing_group_ids.push_back(face.smoothing_group_id); + shape->mesh.smoothing_group_ids.push_back(face.smoothing_group_id); + + } + else { + vertex_index_t i0 = face.vertex_indices[0]; + vertex_index_t i1(-1); + vertex_index_t i2 = face.vertex_indices[1]; + + // find the two axes to work in + size_t axes[2] = { 1, 2 }; + for (size_t k = 0; k < npolys; ++k) { + i0 = face.vertex_indices[(k + 0) % npolys]; + i1 = face.vertex_indices[(k + 1) % npolys]; + i2 = face.vertex_indices[(k + 2) % npolys]; + size_t vi0 = size_t(i0.v_idx); + size_t vi1 = size_t(i1.v_idx); + size_t vi2 = size_t(i2.v_idx); + + if (((3 * vi0 + 2) >= v.size()) || ((3 * vi1 + 2) >= v.size()) || + ((3 * vi2 + 2) >= v.size())) { + // Invalid triangle. + // FIXME(syoyo): Is it ok to simply skip this invalid triangle? + continue; + } + real_t v0x = v[vi0 * 3 + 0]; + real_t v0y = v[vi0 * 3 + 1]; + real_t v0z = v[vi0 * 3 + 2]; + real_t v1x = v[vi1 * 3 + 0]; + real_t v1y = v[vi1 * 3 + 1]; + real_t v1z = v[vi1 * 3 + 2]; + real_t v2x = v[vi2 * 3 + 0]; + real_t v2y = v[vi2 * 3 + 1]; + real_t v2z = v[vi2 * 3 + 2]; + real_t e0x = v1x - v0x; + real_t e0y = v1y - v0y; + real_t e0z = v1z - v0z; + real_t e1x = v2x - v1x; + real_t e1y = v2y - v1y; + real_t e1z = v2z - v1z; + real_t cx = std::fabs(e0y * e1z - e0z * e1y); + real_t cy = std::fabs(e0z * e1x - e0x * e1z); + real_t cz = std::fabs(e0x * e1y - e0y * e1x); + const real_t epsilon = std::numeric_limits::epsilon(); + // std::cout << "cx " << cx << ", cy " << cy << ", cz " << cz << + // "\n"; + if (cx > epsilon || cy > epsilon || cz > epsilon) { + // std::cout << "corner\n"; + // found a corner + if (cx > cy && cx > cz) { + // std::cout << "pattern0\n"; + } + else { + // std::cout << "axes[0] = 0\n"; + axes[0] = 0; + if (cz > cx && cz > cy) { + // std::cout << "axes[1] = 1\n"; + axes[1] = 1; + } + } + break; + } + } + +#ifdef TINYOBJLOADER_USE_MAPBOX_EARCUT + using Point = std::array; + + // first polyline define the main polygon. + // following polylines define holes(not used in tinyobj). + std::vector > polygon; + + std::vector polyline; + + // Fill polygon data(facevarying vertices). + for (size_t k = 0; k < npolys; k++) { + i0 = face.vertex_indices[k]; + size_t vi0 = size_t(i0.v_idx); + + assert(((3 * vi0 + 2) < v.size())); + + real_t v0x = v[vi0 * 3 + axes[0]]; + real_t v0y = v[vi0 * 3 + axes[1]]; + + polyline.push_back({ v0x, v0y }); + } + + polygon.push_back(polyline); + std::vector indices = mapbox::earcut(polygon); + // => result = 3 * faces, clockwise + + assert(indices.size() % 3 == 0); + + // Reconstruct vertex_index_t + for (size_t k = 0; k < indices.size() / 3; k++) { + { + index_t idx0, idx1, idx2; + idx0.vertex_index = face.vertex_indices[indices[3 * k + 0]].v_idx; + idx0.normal_index = + face.vertex_indices[indices[3 * k + 0]].vn_idx; + idx0.texcoord_index = + face.vertex_indices[indices[3 * k + 0]].vt_idx; + idx1.vertex_index = face.vertex_indices[indices[3 * k + 1]].v_idx; + idx1.normal_index = + face.vertex_indices[indices[3 * k + 1]].vn_idx; + idx1.texcoord_index = + face.vertex_indices[indices[3 * k + 1]].vt_idx; + idx2.vertex_index = face.vertex_indices[indices[3 * k + 2]].v_idx; + idx2.normal_index = + face.vertex_indices[indices[3 * k + 2]].vn_idx; + idx2.texcoord_index = + face.vertex_indices[indices[3 * k + 2]].vt_idx; + + shape->mesh.indices.push_back(idx0); + shape->mesh.indices.push_back(idx1); + shape->mesh.indices.push_back(idx2); + + shape->mesh.num_face_vertices.push_back(3); + shape->mesh.material_ids.push_back(material_id); + shape->mesh.smoothing_group_ids.push_back( + face.smoothing_group_id); + } + } + +#else // Built-in ear clipping triangulation + + + face_t remainingFace = face; // copy + size_t guess_vert = 0; + vertex_index_t ind[3]; + real_t vx[3]; + real_t vy[3]; + + // How many iterations can we do without decreasing the remaining + // vertices. + size_t remainingIterations = face.vertex_indices.size(); + size_t previousRemainingVertices = + remainingFace.vertex_indices.size(); + + while (remainingFace.vertex_indices.size() > 3 && + remainingIterations > 0) { + // std::cout << "remainingIterations " << remainingIterations << + // "\n"; + + npolys = remainingFace.vertex_indices.size(); + if (guess_vert >= npolys) { + guess_vert -= npolys; + } + + if (previousRemainingVertices != npolys) { + // The number of remaining vertices decreased. Reset counters. + previousRemainingVertices = npolys; + remainingIterations = npolys; + } + else { + // We didn't consume a vertex on previous iteration, reduce the + // available iterations. + remainingIterations--; + } + + for (size_t k = 0; k < 3; k++) { + ind[k] = remainingFace.vertex_indices[(guess_vert + k) % npolys]; + size_t vi = size_t(ind[k].v_idx); + if (((vi * 3 + axes[0]) >= v.size()) || + ((vi * 3 + axes[1]) >= v.size())) { + // ??? + vx[k] = static_cast(0.0); + vy[k] = static_cast(0.0); + } + else { + vx[k] = v[vi * 3 + axes[0]]; + vy[k] = v[vi * 3 + axes[1]]; + } + } + + // + // area is calculated per face + // + real_t e0x = vx[1] - vx[0]; + real_t e0y = vy[1] - vy[0]; + real_t e1x = vx[2] - vx[1]; + real_t e1y = vy[2] - vy[1]; + real_t cross = e0x * e1y - e0y * e1x; + // std::cout << "axes = " << axes[0] << ", " << axes[1] << "\n"; + // std::cout << "e0x, e0y, e1x, e1y " << e0x << ", " << e0y << ", " + // << e1x << ", " << e1y << "\n"; + + real_t area = (vx[0] * vy[1] - vy[0] * vx[1]) * static_cast(0.5); + // std::cout << "cross " << cross << ", area " << area << "\n"; + // if an internal angle + if (cross * area < static_cast(0.0)) { + // std::cout << "internal \n"; + guess_vert += 1; + // std::cout << "guess vert : " << guess_vert << "\n"; + continue; + } + + // check all other verts in case they are inside this triangle + bool overlap = false; + for (size_t otherVert = 3; otherVert < npolys; ++otherVert) { + size_t idx = (guess_vert + otherVert) % npolys; + + if (idx >= remainingFace.vertex_indices.size()) { + // std::cout << "???0\n"; + // ??? + continue; + } + + size_t ovi = size_t(remainingFace.vertex_indices[idx].v_idx); + + if (((ovi * 3 + axes[0]) >= v.size()) || + ((ovi * 3 + axes[1]) >= v.size())) { + // std::cout << "???1\n"; + // ??? + continue; + } + real_t tx = v[ovi * 3 + axes[0]]; + real_t ty = v[ovi * 3 + axes[1]]; + if (pnpoly(3, vx, vy, tx, ty)) { + // std::cout << "overlap\n"; + overlap = true; + break; + } + } + + if (overlap) { + // std::cout << "overlap2\n"; + guess_vert += 1; + continue; + } + + // this triangle is an ear + { + index_t idx0, idx1, idx2; + idx0.vertex_index = ind[0].v_idx; + idx0.normal_index = ind[0].vn_idx; + idx0.texcoord_index = ind[0].vt_idx; + idx1.vertex_index = ind[1].v_idx; + idx1.normal_index = ind[1].vn_idx; + idx1.texcoord_index = ind[1].vt_idx; + idx2.vertex_index = ind[2].v_idx; + idx2.normal_index = ind[2].vn_idx; + idx2.texcoord_index = ind[2].vt_idx; + + shape->mesh.indices.push_back(idx0); + shape->mesh.indices.push_back(idx1); + shape->mesh.indices.push_back(idx2); + + shape->mesh.num_face_vertices.push_back(3); + shape->mesh.material_ids.push_back(material_id); + shape->mesh.smoothing_group_ids.push_back( + face.smoothing_group_id); + } + + // remove v1 from the list + size_t removed_vert_index = (guess_vert + 1) % npolys; + while (removed_vert_index + 1 < npolys) { + remainingFace.vertex_indices[removed_vert_index] = + remainingFace.vertex_indices[removed_vert_index + 1]; + removed_vert_index += 1; + } + remainingFace.vertex_indices.pop_back(); + } + + // std::cout << "remainingFace.vi.size = " << + // remainingFace.vertex_indices.size() << "\n"; + if (remainingFace.vertex_indices.size() == 3) { + i0 = remainingFace.vertex_indices[0]; + i1 = remainingFace.vertex_indices[1]; + i2 = remainingFace.vertex_indices[2]; + { + index_t idx0, idx1, idx2; + idx0.vertex_index = i0.v_idx; + idx0.normal_index = i0.vn_idx; + idx0.texcoord_index = i0.vt_idx; + idx1.vertex_index = i1.v_idx; + idx1.normal_index = i1.vn_idx; + idx1.texcoord_index = i1.vt_idx; + idx2.vertex_index = i2.v_idx; + idx2.normal_index = i2.vn_idx; + idx2.texcoord_index = i2.vt_idx; + + shape->mesh.indices.push_back(idx0); + shape->mesh.indices.push_back(idx1); + shape->mesh.indices.push_back(idx2); + + shape->mesh.num_face_vertices.push_back(3); + shape->mesh.material_ids.push_back(material_id); + shape->mesh.smoothing_group_ids.push_back( + face.smoothing_group_id); + } + } +#endif + } // npolys + } + else { + for (size_t k = 0; k < npolys; k++) { + index_t idx; + idx.vertex_index = face.vertex_indices[k].v_idx; + idx.normal_index = face.vertex_indices[k].vn_idx; + idx.texcoord_index = face.vertex_indices[k].vt_idx; + shape->mesh.indices.push_back(idx); + } + + shape->mesh.num_face_vertices.push_back( + static_cast(npolys)); + shape->mesh.material_ids.push_back(material_id); // per face + shape->mesh.smoothing_group_ids.push_back( + face.smoothing_group_id); // per face + } + } + + shape->mesh.tags = tags; + } + + // line + if (!prim_group.lineGroup.empty()) { + // Flatten indices + for (size_t i = 0; i < prim_group.lineGroup.size(); i++) { + for (size_t j = 0; j < prim_group.lineGroup[i].vertex_indices.size(); + j++) { + const vertex_index_t& vi = prim_group.lineGroup[i].vertex_indices[j]; + + index_t idx; + idx.vertex_index = vi.v_idx; + idx.normal_index = vi.vn_idx; + idx.texcoord_index = vi.vt_idx; + + shape->lines.indices.push_back(idx); + } + + shape->lines.num_line_vertices.push_back( + int(prim_group.lineGroup[i].vertex_indices.size())); + } + } + + // points + if (!prim_group.pointsGroup.empty()) { + // Flatten & convert indices + for (size_t i = 0; i < prim_group.pointsGroup.size(); i++) { + for (size_t j = 0; j < prim_group.pointsGroup[i].vertex_indices.size(); + j++) { + const vertex_index_t& vi = prim_group.pointsGroup[i].vertex_indices[j]; + + index_t idx; + idx.vertex_index = vi.v_idx; + idx.normal_index = vi.vn_idx; + idx.texcoord_index = vi.vt_idx; + + shape->points.indices.push_back(idx); + } + } + } + + return true; + } + + // Split a string with specified delimiter character and escape character. + // https://rosettacode.org/wiki/Tokenize_a_string_with_escaping#C.2B.2B + static void SplitString(const std::string& s, char delim, char escape, + std::vector& elems) { + std::string token; + + bool escaping = false; + for (size_t i = 0; i < s.size(); ++i) { + char ch = s[i]; + if (escaping) { + escaping = false; + } + else if (ch == escape) { + escaping = true; + continue; + } + else if (ch == delim) { + if (!token.empty()) { + elems.push_back(token); + } + token.clear(); + continue; + } + token += ch; + } + + elems.push_back(token); + } + + static std::string JoinPath(const std::string& dir, + const std::string& filename) { + if (dir.empty()) { + return filename; + } + else { + // check '/' + char lastChar = *dir.rbegin(); + if (lastChar != '/') { + return dir + std::string("/") + filename; + } + else { + return dir + filename; + } + } + } + + void LoadMtl(std::map* material_map, + std::vector* materials, std::istream* inStream, + std::string* warning, std::string* err) { + (void)err; + + // Create a default material anyway. + material_t material; + InitMaterial(&material); + + // Issue 43. `d` wins against `Tr` since `Tr` is not in the MTL specification. + bool has_d = false; + bool has_tr = false; + + // has_kd is used to set a default diffuse value when map_Kd is present + // and Kd is not. + bool has_kd = false; + + std::stringstream warn_ss; + + size_t line_no = 0; + std::string linebuf; + while (inStream->peek() != -1) { + safeGetline(*inStream, linebuf); + line_no++; + + // Trim trailing whitespace. + if (linebuf.size() > 0) { + linebuf = linebuf.substr(0, linebuf.find_last_not_of(" \t") + 1); + } + + // 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)) && IS_SPACE((token[6]))) { + // flush previous material. + if (!material.name.empty()) { + material_map->insert(std::pair( + material.name, static_cast(materials->size()))); + materials->push_back(material); + } + + // initial temporary material + InitMaterial(&material); + + has_d = false; + has_tr = false; + + // set new mtl name + token += 7; + { + std::stringstream sstr; + sstr << token; + material.name = sstr.str(); + } + continue; + } + + // ambient + if (token[0] == 'K' && token[1] == 'a' && IS_SPACE((token[2]))) { + token += 2; + real_t r, g, b; + parseReal3(&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' && IS_SPACE((token[2]))) { + token += 2; + real_t r, g, b; + parseReal3(&r, &g, &b, &token); + material.diffuse[0] = r; + material.diffuse[1] = g; + material.diffuse[2] = b; + has_kd = true; + continue; + } + + // specular + if (token[0] == 'K' && token[1] == 's' && IS_SPACE((token[2]))) { + token += 2; + real_t r, g, b; + parseReal3(&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' && IS_SPACE((token[2]))) || + (token[0] == 'T' && token[1] == 'f' && IS_SPACE((token[2])))) { + token += 2; + real_t r, g, b; + parseReal3(&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' && IS_SPACE((token[2]))) { + token += 2; + material.ior = parseReal(&token); + continue; + } + + // emission + if (token[0] == 'K' && token[1] == 'e' && IS_SPACE(token[2])) { + token += 2; + real_t r, g, b; + parseReal3(&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' && IS_SPACE(token[2])) { + token += 2; + material.shininess = parseReal(&token); + continue; + } + + // illum model + if (0 == strncmp(token, "illum", 5) && IS_SPACE(token[5])) { + token += 6; + material.illum = parseInt(&token); + continue; + } + + // dissolve + if ((token[0] == 'd' && IS_SPACE(token[1]))) { + token += 1; + material.dissolve = parseReal(&token); + + if (has_tr) { + warn_ss << "Both `d` and `Tr` parameters defined for \"" + << material.name + << "\". Use the value of `d` for dissolve (line " << line_no + << " in .mtl.)\n"; + } + has_d = true; + continue; + } + if (token[0] == 'T' && token[1] == 'r' && IS_SPACE(token[2])) { + token += 2; + if (has_d) { + // `d` wins. Ignore `Tr` value. + warn_ss << "Both `d` and `Tr` parameters defined for \"" + << material.name + << "\". Use the value of `d` for dissolve (line " << line_no + << " in .mtl.)\n"; + } + else { + // We invert value of Tr(assume Tr is in range [0, 1]) + // NOTE: Interpretation of Tr is application(exporter) dependent. For + // some application(e.g. 3ds max obj exporter), Tr = d(Issue 43) + material.dissolve = static_cast(1.0) - parseReal(&token); + } + has_tr = true; + continue; + } + + // PBR: roughness + if (token[0] == 'P' && token[1] == 'r' && IS_SPACE(token[2])) { + token += 2; + material.roughness = parseReal(&token); + continue; + } + + // PBR: metallic + if (token[0] == 'P' && token[1] == 'm' && IS_SPACE(token[2])) { + token += 2; + material.metallic = parseReal(&token); + continue; + } + + // PBR: sheen + if (token[0] == 'P' && token[1] == 's' && IS_SPACE(token[2])) { + token += 2; + material.sheen = parseReal(&token); + continue; + } + + // PBR: clearcoat thickness + if (token[0] == 'P' && token[1] == 'c' && IS_SPACE(token[2])) { + token += 2; + material.clearcoat_thickness = parseReal(&token); + continue; + } + + // PBR: clearcoat roughness + if ((0 == strncmp(token, "Pcr", 3)) && IS_SPACE(token[3])) { + token += 4; + material.clearcoat_roughness = parseReal(&token); + continue; + } + + // PBR: anisotropy + if ((0 == strncmp(token, "aniso", 5)) && IS_SPACE(token[5])) { + token += 6; + material.anisotropy = parseReal(&token); + continue; + } + + // PBR: anisotropy rotation + if ((0 == strncmp(token, "anisor", 6)) && IS_SPACE(token[6])) { + token += 7; + material.anisotropy_rotation = parseReal(&token); + continue; + } + + // ambient texture + if ((0 == strncmp(token, "map_Ka", 6)) && IS_SPACE(token[6])) { + token += 7; + ParseTextureNameAndOption(&(material.ambient_texname), + &(material.ambient_texopt), token); + continue; + } + + // diffuse texture + if ((0 == strncmp(token, "map_Kd", 6)) && IS_SPACE(token[6])) { + token += 7; + ParseTextureNameAndOption(&(material.diffuse_texname), + &(material.diffuse_texopt), token); + + // Set a decent diffuse default value if a diffuse texture is specified + // without a matching Kd value. + if (!has_kd) { + material.diffuse[0] = static_cast(0.6); + material.diffuse[1] = static_cast(0.6); + material.diffuse[2] = static_cast(0.6); + } + + continue; + } + + // specular texture + if ((0 == strncmp(token, "map_Ks", 6)) && IS_SPACE(token[6])) { + token += 7; + ParseTextureNameAndOption(&(material.specular_texname), + &(material.specular_texopt), token); + continue; + } + + // specular highlight texture + if ((0 == strncmp(token, "map_Ns", 6)) && IS_SPACE(token[6])) { + token += 7; + ParseTextureNameAndOption(&(material.specular_highlight_texname), + &(material.specular_highlight_texopt), token); + continue; + } + + // bump texture + if ((0 == strncmp(token, "map_bump", 8)) && IS_SPACE(token[8])) { + token += 9; + ParseTextureNameAndOption(&(material.bump_texname), + &(material.bump_texopt), token); + continue; + } + + // bump texture + if ((0 == strncmp(token, "map_Bump", 8)) && IS_SPACE(token[8])) { + token += 9; + ParseTextureNameAndOption(&(material.bump_texname), + &(material.bump_texopt), token); + continue; + } + + // bump texture + if ((0 == strncmp(token, "bump", 4)) && IS_SPACE(token[4])) { + token += 5; + ParseTextureNameAndOption(&(material.bump_texname), + &(material.bump_texopt), token); + continue; + } + + // alpha texture + if ((0 == strncmp(token, "map_d", 5)) && IS_SPACE(token[5])) { + token += 6; + material.alpha_texname = token; + ParseTextureNameAndOption(&(material.alpha_texname), + &(material.alpha_texopt), token); + continue; + } + + // displacement texture + if ((0 == strncmp(token, "disp", 4)) && IS_SPACE(token[4])) { + token += 5; + ParseTextureNameAndOption(&(material.displacement_texname), + &(material.displacement_texopt), token); + continue; + } + + // reflection map + if ((0 == strncmp(token, "refl", 4)) && IS_SPACE(token[4])) { + token += 5; + ParseTextureNameAndOption(&(material.reflection_texname), + &(material.reflection_texopt), token); + continue; + } + + // PBR: roughness texture + if ((0 == strncmp(token, "map_Pr", 6)) && IS_SPACE(token[6])) { + token += 7; + ParseTextureNameAndOption(&(material.roughness_texname), + &(material.roughness_texopt), token); + continue; + } + + // PBR: metallic texture + if ((0 == strncmp(token, "map_Pm", 6)) && IS_SPACE(token[6])) { + token += 7; + ParseTextureNameAndOption(&(material.metallic_texname), + &(material.metallic_texopt), token); + continue; + } + + // PBR: sheen texture + if ((0 == strncmp(token, "map_Ps", 6)) && IS_SPACE(token[6])) { + token += 7; + ParseTextureNameAndOption(&(material.sheen_texname), + &(material.sheen_texopt), token); + continue; + } + + // PBR: emissive texture + if ((0 == strncmp(token, "map_Ke", 6)) && IS_SPACE(token[6])) { + token += 7; + ParseTextureNameAndOption(&(material.emissive_texname), + &(material.emissive_texopt), token); + continue; + } + + // PBR: normal map texture + if ((0 == strncmp(token, "norm", 4)) && IS_SPACE(token[4])) { + token += 5; + ParseTextureNameAndOption(&(material.normal_texname), + &(material.normal_texopt), 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, static_cast(len)); + std::string value = _space + 1; + material.unknown_parameter.insert( + std::pair(key, value)); + } + } + // flush last material. + material_map->insert(std::pair( + material.name, static_cast(materials->size()))); + materials->push_back(material); + + if (warning) { + (*warning) = warn_ss.str(); + } + } + + bool MaterialFileReader::operator()(const std::string& matId, + std::vector* materials, + std::map* matMap, + std::string* warn, std::string* err) { + if (!m_mtlBaseDir.empty()) { +#ifdef _WIN32 + char sep = ';'; +#else + char sep = ':'; +#endif + + // https://stackoverflow.com/questions/5167625/splitting-a-c-stdstring-using-tokens-e-g + std::vector paths; + std::istringstream f(m_mtlBaseDir); + + std::string s; + while (getline(f, s, sep)) { + paths.push_back(s); + } + + for (size_t i = 0; i < paths.size(); i++) { + std::string filepath = JoinPath(paths[i], matId); + + std::ifstream matIStream(filepath.c_str()); + if (matIStream) { + LoadMtl(matMap, materials, &matIStream, warn, err); + + return true; + } + } + + std::stringstream ss; + ss << "Material file [ " << matId + << " ] not found in a path : " << m_mtlBaseDir << "\n"; + if (warn) { + (*warn) += ss.str(); + } + return false; + + } + else { + std::string filepath = matId; + std::ifstream matIStream(filepath.c_str()); + if (matIStream) { + LoadMtl(matMap, materials, &matIStream, warn, err); + + return true; + } + + std::stringstream ss; + ss << "Material file [ " << filepath + << " ] not found in a path : " << m_mtlBaseDir << "\n"; + if (warn) { + (*warn) += ss.str(); + } + + return false; + } + } + + bool MaterialStreamReader::operator()(const std::string& matId, + std::vector* materials, + std::map* matMap, + std::string* warn, std::string* err) { + (void)err; + (void)matId; + if (!m_inStream) { + std::stringstream ss; + ss << "Material stream in error state. \n"; + if (warn) { + (*warn) += ss.str(); + } + return false; + } + + LoadMtl(matMap, materials, &m_inStream, warn, err); + + return true; + } + + bool LoadObj(attrib_t* attrib, std::vector* shapes, + std::vector* materials, std::string* warn, + std::string* err, const char* filename, const char* mtl_basedir, + bool triangulate, bool default_vcols_fallback) { + attrib->vertices.clear(); + attrib->normals.clear(); + attrib->texcoords.clear(); + attrib->colors.clear(); + shapes->clear(); + + std::stringstream errss; + + std::ifstream ifs(filename); + if (!ifs) { + errss << "Cannot open file [" << filename << "]\n"; + if (err) { + (*err) = errss.str(); + } + return false; + } + + std::string baseDir = mtl_basedir ? mtl_basedir : ""; + if (!baseDir.empty()) { +#ifndef _WIN32 + const char dirsep = '/'; +#else + const char dirsep = '\\'; +#endif + if (baseDir[baseDir.length() - 1] != dirsep) baseDir += dirsep; + } + MaterialFileReader matFileReader(baseDir); + + return LoadObj(attrib, shapes, materials, warn, err, &ifs, &matFileReader, + triangulate, default_vcols_fallback); + } + + bool LoadObj(attrib_t* attrib, std::vector* shapes, + std::vector* materials, std::string* warn, + std::string* err, std::istream* inStream, + MaterialReader* readMatFn /*= NULL*/, bool triangulate, + bool default_vcols_fallback) { + std::stringstream errss; + + std::vector v; + std::vector vn; + std::vector vt; + std::vector vc; + std::vector vw; + std::vector tags; + PrimGroup prim_group; + std::string name; + + // material + std::map material_map; + int material = -1; + + // smoothing group id + unsigned int current_smoothing_id = + 0; // Initial value. 0 means no smoothing. + + int greatest_v_idx = -1; + int greatest_vn_idx = -1; + int greatest_vt_idx = -1; + + shape_t shape; + + bool found_all_colors = true; + + size_t line_num = 0; + std::string linebuf; + while (inStream->peek() != -1) { + safeGetline(*inStream, linebuf); + + line_num++; + + // 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' && IS_SPACE((token[1]))) { + token += 2; + real_t x, y, z; + real_t r, g, b; + + found_all_colors &= parseVertexWithColor(&x, &y, &z, &r, &g, &b, &token); + + v.push_back(x); + v.push_back(y); + v.push_back(z); + + if (found_all_colors || default_vcols_fallback) { + vc.push_back(r); + vc.push_back(g); + vc.push_back(b); + } + + continue; + } + + // normal + if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) { + token += 3; + real_t x, y, z; + parseReal3(&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' && IS_SPACE((token[2]))) { + token += 3; + real_t x, y; + parseReal2(&x, &y, &token); + vt.push_back(x); + vt.push_back(y); + continue; + } + + // skin weight. tinyobj extension + if (token[0] == 'v' && token[1] == 'w' && IS_SPACE((token[2]))) { + token += 3; + + // vw ... + // example: + // vw 0 0 0.25 1 0.25 2 0.5 + + // TODO(syoyo): Add syntax check + int vid = 0; + vid = parseInt(&token); + + skin_weight_t sw; + + sw.vertex_id = vid; + + while (!IS_NEW_LINE(token[0])) { + real_t j, w; + // joint_id should not be negative, weight may be negative + // TODO(syoyo): # of elements check + parseReal2(&j, &w, &token, -1.0); + + if (j < static_cast(0)) { + if (err) { + std::stringstream ss; + ss << "Failed parse `vw' line. joint_id is negative. " + "line " + << line_num << ".)\n"; + (*err) += ss.str(); + } + return false; + } + + joint_and_weight_t jw; + + jw.joint_id = int(j); + jw.weight = w; + + sw.weightValues.push_back(jw); + + size_t n = strspn(token, " \t\r"); + token += n; + } + + vw.push_back(sw); + } + + // line + if (token[0] == 'l' && IS_SPACE((token[1]))) { + token += 2; + + __line_t line; + + while (!IS_NEW_LINE(token[0])) { + vertex_index_t vi; + if (!parseTriple(&token, static_cast(v.size() / 3), + static_cast(vn.size() / 3), + static_cast(vt.size() / 2), &vi)) { + if (err) { + std::stringstream ss; + ss << "Failed parse `l' line(e.g. zero value for vertex index. " + "line " + << line_num << ".)\n"; + (*err) += ss.str(); + } + return false; + } + + line.vertex_indices.push_back(vi); + + size_t n = strspn(token, " \t\r"); + token += n; + } + + prim_group.lineGroup.push_back(line); + + continue; + } + + // points + if (token[0] == 'p' && IS_SPACE((token[1]))) { + token += 2; + + __points_t pts; + + while (!IS_NEW_LINE(token[0])) { + vertex_index_t vi; + if (!parseTriple(&token, static_cast(v.size() / 3), + static_cast(vn.size() / 3), + static_cast(vt.size() / 2), &vi)) { + if (err) { + std::stringstream ss; + ss << "Failed parse `p' line(e.g. zero value for vertex index. " + "line " + << line_num << ".)\n"; + (*err) += ss.str(); + } + return false; + } + + pts.vertex_indices.push_back(vi); + + size_t n = strspn(token, " \t\r"); + token += n; + } + + prim_group.pointsGroup.push_back(pts); + + continue; + } + + // face + if (token[0] == 'f' && IS_SPACE((token[1]))) { + token += 2; + token += strspn(token, " \t"); + + face_t face; + + face.smoothing_group_id = current_smoothing_id; + face.vertex_indices.reserve(3); + + while (!IS_NEW_LINE(token[0])) { + vertex_index_t vi; + if (!parseTriple(&token, static_cast(v.size() / 3), + static_cast(vn.size() / 3), + static_cast(vt.size() / 2), &vi)) { + if (err) { + std::stringstream ss; + ss << "Failed parse `f' line(e.g. zero value for face index. line " + << line_num << ".)\n"; + (*err) += ss.str(); + } + return false; + } + + greatest_v_idx = greatest_v_idx > vi.v_idx ? greatest_v_idx : vi.v_idx; + greatest_vn_idx = + greatest_vn_idx > vi.vn_idx ? greatest_vn_idx : vi.vn_idx; + greatest_vt_idx = + greatest_vt_idx > vi.vt_idx ? greatest_vt_idx : vi.vt_idx; + + face.vertex_indices.push_back(vi); + size_t n = strspn(token, " \t\r"); + token += n; + } + + // replace with emplace_back + std::move on C++11 + prim_group.faceGroup.push_back(face); + + continue; + } + + // use mtl + if ((0 == strncmp(token, "usemtl", 6))) { + token += 6; + std::string namebuf = parseString(&token); + + int newMaterialId = -1; + std::map::const_iterator it = + material_map.find(namebuf); + if (it != material_map.end()) { + newMaterialId = it->second; + } + else { + // { error!! material not found } + if (warn) { + (*warn) += "material [ '" + namebuf + "' ] not found in .mtl\n"; + } + } + + if (newMaterialId != material) { + // Create per-face material. Thus we don't add `shape` to `shapes` at + // this time. + // just clear `faceGroup` after `exportGroupsToShape()` call. + exportGroupsToShape(&shape, prim_group, tags, material, name, + triangulate, v, warn); + prim_group.faceGroup.clear(); + material = newMaterialId; + } + + continue; + } + + // load mtl + if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) { + if (readMatFn) { + token += 7; + + std::vector filenames; + SplitString(std::string(token), ' ', '\\', filenames); + + if (filenames.empty()) { + if (warn) { + std::stringstream ss; + ss << "Looks like empty filename for mtllib. Use default " + "material (line " + << line_num << ".)\n"; + + (*warn) += ss.str(); + } + } + else { + bool found = false; + for (size_t s = 0; s < filenames.size(); s++) { + std::string warn_mtl; + std::string err_mtl; + bool ok = (*readMatFn)(filenames[s].c_str(), materials, + &material_map, &warn_mtl, &err_mtl); + if (warn && (!warn_mtl.empty())) { + (*warn) += warn_mtl; + } + + if (err && (!err_mtl.empty())) { + (*err) += err_mtl; + } + + if (ok) { + found = true; + break; + } + } + + if (!found) { + if (warn) { + (*warn) += + "Failed to load material file(s). Use default " + "material.\n"; + } + } + } + } + + continue; + } + + // group name + if (token[0] == 'g' && IS_SPACE((token[1]))) { + // flush previous face group. + bool ret = exportGroupsToShape(&shape, prim_group, tags, material, name, + triangulate, v, warn); + (void)ret; // return value not used. + + if (shape.mesh.indices.size() > 0) { + shapes->push_back(shape); + } + + shape = shape_t(); + + // material = -1; + prim_group.clear(); + + std::vector names; + + while (!IS_NEW_LINE(token[0])) { + std::string str = parseString(&token); + names.push_back(str); + token += strspn(token, " \t\r"); // skip tag + } + + // names[0] must be 'g' + + if (names.size() < 2) { + // 'g' with empty names + if (warn) { + std::stringstream ss; + ss << "Empty group name. line: " << line_num << "\n"; + (*warn) += ss.str(); + name = ""; + } + } + else { + std::stringstream ss; + ss << names[1]; + + // tinyobjloader does not support multiple groups for a primitive. + // Currently we concatinate multiple group names with a space to get + // single group name. + + for (size_t i = 2; i < names.size(); i++) { + ss << " " << names[i]; + } + + name = ss.str(); + } + + continue; + } + + // object name + if (token[0] == 'o' && IS_SPACE((token[1]))) { + // flush previous face group. + bool ret = exportGroupsToShape(&shape, prim_group, tags, material, name, + triangulate, v, warn); + (void)ret; // return value not used. + + if (shape.mesh.indices.size() > 0 || shape.lines.indices.size() > 0 || + shape.points.indices.size() > 0) { + shapes->push_back(shape); + } + + // material = -1; + prim_group.clear(); + shape = shape_t(); + + // @todo { multiple object name? } + token += 2; + std::stringstream ss; + ss << token; + name = ss.str(); + + continue; + } + + if (token[0] == 't' && IS_SPACE(token[1])) { + const int max_tag_nums = 8192; // FIXME(syoyo): Parameterize. + tag_t tag; + + token += 2; + + tag.name = parseString(&token); + + tag_sizes ts = parseTagTriple(&token); + + if (ts.num_ints < 0) { + ts.num_ints = 0; + } + if (ts.num_ints > max_tag_nums) { + ts.num_ints = max_tag_nums; + } + + if (ts.num_reals < 0) { + ts.num_reals = 0; + } + if (ts.num_reals > max_tag_nums) { + ts.num_reals = max_tag_nums; + } + + if (ts.num_strings < 0) { + ts.num_strings = 0; + } + if (ts.num_strings > max_tag_nums) { + ts.num_strings = max_tag_nums; + } + + tag.intValues.resize(static_cast(ts.num_ints)); + + for (size_t i = 0; i < static_cast(ts.num_ints); ++i) { + tag.intValues[i] = parseInt(&token); + } + + tag.floatValues.resize(static_cast(ts.num_reals)); + for (size_t i = 0; i < static_cast(ts.num_reals); ++i) { + tag.floatValues[i] = parseReal(&token); + } + + tag.stringValues.resize(static_cast(ts.num_strings)); + for (size_t i = 0; i < static_cast(ts.num_strings); ++i) { + tag.stringValues[i] = parseString(&token); + } + + tags.push_back(tag); + + continue; + } + + if (token[0] == 's' && IS_SPACE(token[1])) { + // smoothing group id + token += 2; + + // skip space. + token += strspn(token, " \t"); // skip space + + if (token[0] == '\0') { + continue; + } + + if (token[0] == '\r' || token[1] == '\n') { + continue; + } + + if (strlen(token) >= 3 && token[0] == 'o' && token[1] == 'f' && + token[2] == 'f') { + current_smoothing_id = 0; + } + else { + // assume number + int smGroupId = parseInt(&token); + if (smGroupId < 0) { + // parse error. force set to 0. + // FIXME(syoyo): Report warning. + current_smoothing_id = 0; + } + else { + current_smoothing_id = static_cast(smGroupId); + } + } + + continue; + } // smoothing group id + + // Ignore unknown command. + } + + // not all vertices have colors, no default colors desired? -> clear colors + if (!found_all_colors && !default_vcols_fallback) { + vc.clear(); + } + + if (greatest_v_idx >= static_cast(v.size() / 3)) { + if (warn) { + std::stringstream ss; + ss << "Vertex indices out of bounds (line " << line_num << ".)\n\n"; + (*warn) += ss.str(); + } + } + if (greatest_vn_idx >= static_cast(vn.size() / 3)) { + if (warn) { + std::stringstream ss; + ss << "Vertex normal indices out of bounds (line " << line_num << ".)\n\n"; + (*warn) += ss.str(); + } + } + if (greatest_vt_idx >= static_cast(vt.size() / 2)) { + if (warn) { + std::stringstream ss; + ss << "Vertex texcoord indices out of bounds (line " << line_num << ".)\n\n"; + (*warn) += ss.str(); + } + } + + bool ret = exportGroupsToShape(&shape, prim_group, tags, material, name, + triangulate, v, warn); + // exportGroupsToShape return false when `usemtl` is called in the last + // line. + // we also add `shape` to `shapes` when `shape.mesh` has already some + // faces(indices) + if (ret || shape.mesh.indices + .size()) { // FIXME(syoyo): Support other prims(e.g. lines) + shapes->push_back(shape); + } + prim_group.clear(); // for safety + + if (err) { + (*err) += errss.str(); + } + + attrib->vertices.swap(v); + attrib->vertex_weights.swap(v); + attrib->normals.swap(vn); + attrib->texcoords.swap(vt); + attrib->texcoord_ws.swap(vt); + attrib->colors.swap(vc); + attrib->skin_weights.swap(vw); + + return true; + } + + bool LoadObjWithCallback(std::istream& inStream, const callback_t& callback, + void* user_data /*= NULL*/, + MaterialReader* readMatFn /*= NULL*/, + std::string* warn, /* = NULL*/ + std::string* err /*= NULL*/) { + std::stringstream errss; + + // material + std::map material_map; + int material_id = -1; // -1 = invalid + + std::vector indices; + std::vector materials; + std::vector names; + names.reserve(2); + std::vector names_out; + + std::string linebuf; + while (inStream.peek() != -1) { + safeGetline(inStream, linebuf); + + // 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' && IS_SPACE((token[1]))) { + token += 2; + // TODO(syoyo): Support parsing vertex color extension. + real_t x, y, z, w; // w is optional. default = 1.0 + parseV(&x, &y, &z, &w, &token); + if (callback.vertex_cb) { + callback.vertex_cb(user_data, x, y, z, w); + } + continue; + } + + // normal + if (token[0] == 'v' && token[1] == 'n' && IS_SPACE((token[2]))) { + token += 3; + real_t x, y, z; + parseReal3(&x, &y, &z, &token); + if (callback.normal_cb) { + callback.normal_cb(user_data, x, y, z); + } + continue; + } + + // texcoord + if (token[0] == 'v' && token[1] == 't' && IS_SPACE((token[2]))) { + token += 3; + real_t x, y, z; // y and z are optional. default = 0.0 + parseReal3(&x, &y, &z, &token); + if (callback.texcoord_cb) { + callback.texcoord_cb(user_data, x, y, z); + } + continue; + } + + // face + if (token[0] == 'f' && IS_SPACE((token[1]))) { + token += 2; + token += strspn(token, " \t"); + + indices.clear(); + while (!IS_NEW_LINE(token[0])) { + vertex_index_t vi = parseRawTriple(&token); + + index_t idx; + idx.vertex_index = vi.v_idx; + idx.normal_index = vi.vn_idx; + idx.texcoord_index = vi.vt_idx; + + indices.push_back(idx); + size_t n = strspn(token, " \t\r"); + token += n; + } + + if (callback.index_cb && indices.size() > 0) { + callback.index_cb(user_data, &indices.at(0), + static_cast(indices.size())); + } + + continue; + } + + // use mtl + if ((0 == strncmp(token, "usemtl", 6)) && IS_SPACE((token[6]))) { + token += 7; + std::stringstream ss; + ss << token; + std::string namebuf = ss.str(); + + int newMaterialId = -1; + std::map::const_iterator it = + material_map.find(namebuf); + if (it != material_map.end()) { + newMaterialId = it->second; + } + else { + // { warn!! material not found } + if (warn && (!callback.usemtl_cb)) { + (*warn) += "material [ " + namebuf + " ] not found in .mtl\n"; + } + } + + if (newMaterialId != material_id) { + material_id = newMaterialId; + } + + if (callback.usemtl_cb) { + callback.usemtl_cb(user_data, namebuf.c_str(), material_id); + } + + continue; + } + + // load mtl + if ((0 == strncmp(token, "mtllib", 6)) && IS_SPACE((token[6]))) { + if (readMatFn) { + token += 7; + + std::vector filenames; + SplitString(std::string(token), ' ', '\\', filenames); + + if (filenames.empty()) { + if (warn) { + (*warn) += + "Looks like empty filename for mtllib. Use default " + "material. \n"; + } + } + else { + bool found = false; + for (size_t s = 0; s < filenames.size(); s++) { + std::string warn_mtl; + std::string err_mtl; + bool ok = (*readMatFn)(filenames[s].c_str(), &materials, + &material_map, &warn_mtl, &err_mtl); + + if (warn && (!warn_mtl.empty())) { + (*warn) += warn_mtl; // This should be warn message. + } + + if (err && (!err_mtl.empty())) { + (*err) += err_mtl; + } + + if (ok) { + found = true; + break; + } + } + + if (!found) { + if (warn) { + (*warn) += + "Failed to load material file(s). Use default " + "material.\n"; + } + } + else { + if (callback.mtllib_cb) { + callback.mtllib_cb(user_data, &materials.at(0), + static_cast(materials.size())); + } + } + } + } + + continue; + } + + // group name + if (token[0] == 'g' && IS_SPACE((token[1]))) { + names.clear(); + + while (!IS_NEW_LINE(token[0])) { + std::string str = parseString(&token); + names.push_back(str); + token += strspn(token, " \t\r"); // skip tag + } + + assert(names.size() > 0); + + if (callback.group_cb) { + if (names.size() > 1) { + // create const char* array. + names_out.resize(names.size() - 1); + for (size_t j = 0; j < names_out.size(); j++) { + names_out[j] = names[j + 1].c_str(); + } + callback.group_cb(user_data, &names_out.at(0), + static_cast(names_out.size())); + + } + else { + callback.group_cb(user_data, NULL, 0); + } + } + + continue; + } + + // object name + if (token[0] == 'o' && IS_SPACE((token[1]))) { + // @todo { multiple object name? } + token += 2; + + std::stringstream ss; + ss << token; + std::string object_name = ss.str(); + + if (callback.object_cb) { + callback.object_cb(user_data, object_name.c_str()); + } + + continue; + } + +#if 0 // @todo + if (token[0] == 't' && IS_SPACE(token[1])) { + tag_t tag; + + token += 2; + std::stringstream ss; + ss << token; + tag.name = ss.str(); + + token += tag.name.size() + 1; + + tag_sizes ts = parseTagTriple(&token); + + tag.intValues.resize(static_cast(ts.num_ints)); + + for (size_t i = 0; i < static_cast(ts.num_ints); ++i) { + tag.intValues[i] = atoi(token); + token += strcspn(token, "/ \t\r") + 1; + } + + tag.floatValues.resize(static_cast(ts.num_reals)); + for (size_t i = 0; i < static_cast(ts.num_reals); ++i) { + tag.floatValues[i] = parseReal(&token); + token += strcspn(token, "/ \t\r") + 1; + } + + tag.stringValues.resize(static_cast(ts.num_strings)); + for (size_t i = 0; i < static_cast(ts.num_strings); ++i) { + std::stringstream ss; + ss << token; + tag.stringValues[i] = ss.str(); + token += tag.stringValues[i].size() + 1; + } + + tags.push_back(tag); + } +#endif + + // Ignore unknown command. + } + + if (err) { + (*err) += errss.str(); + } + + return true; + } + + bool ObjReader::ParseFromFile(const std::string& filename, + const ObjReaderConfig& config) { + std::string mtl_search_path; + + if (config.mtl_search_path.empty()) { + // + // split at last '/'(for unixish system) or '\\'(for windows) to get + // the base directory of .obj file + // + size_t pos = filename.find_last_of("/\\"); + if (pos != std::string::npos) { + mtl_search_path = filename.substr(0, pos); + } + } + else { + mtl_search_path = config.mtl_search_path; + } + + valid_ = LoadObj(&attrib_, &shapes_, &materials_, &warning_, &error_, + filename.c_str(), mtl_search_path.c_str(), + config.triangulate, config.vertex_color); + + return valid_; + } + + bool ObjReader::ParseFromString(const std::string& obj_text, + const std::string& mtl_text, + const ObjReaderConfig& config) { + std::stringbuf obj_buf(obj_text); + std::stringbuf mtl_buf(mtl_text); + + std::istream obj_ifs(&obj_buf); + std::istream mtl_ifs(&mtl_buf); + + MaterialStreamReader mtl_ss(mtl_ifs); + + valid_ = LoadObj(&attrib_, &shapes_, &materials_, &warning_, &error_, + &obj_ifs, &mtl_ss, config.triangulate, config.vertex_color); + + return valid_; + } + +#ifdef __clang__ +#pragma clang diagnostic pop +#endif +} // namespace tinyobj + +#endif#pragma once diff --git a/img/100_iter.png b/img/100_iter.png new file mode 100644 index 00000000..93009885 Binary files /dev/null and b/img/100_iter.png differ diff --git a/img/10_iter.png b/img/10_iter.png new file mode 100644 index 00000000..4bf9017c Binary files /dev/null and b/img/10_iter.png differ diff --git a/img/1_iter.png b/img/1_iter.png new file mode 100644 index 00000000..32d5770e Binary files /dev/null and b/img/1_iter.png differ diff --git a/img/abduction_1.png b/img/abduction_1.png new file mode 100644 index 00000000..dddbeb7d Binary files /dev/null and b/img/abduction_1.png differ diff --git a/img/abduction_2.png b/img/abduction_2.png new file mode 100644 index 00000000..47f2e6e7 Binary files /dev/null and b/img/abduction_2.png differ diff --git a/img/arb_mesh.png b/img/arb_mesh.png new file mode 100644 index 00000000..11cc71ed Binary files /dev/null and b/img/arb_mesh.png differ diff --git a/img/fres.png b/img/fres.png new file mode 100644 index 00000000..ec9ea922 Binary files /dev/null and b/img/fres.png differ diff --git a/img/its_over.jpg b/img/its_over.jpg new file mode 100644 index 00000000..3bdc301d Binary files /dev/null and b/img/its_over.jpg differ diff --git a/img/material_sorting_chart.png b/img/material_sorting_chart.png new file mode 100644 index 00000000..1880c369 Binary files /dev/null and b/img/material_sorting_chart.png differ diff --git a/img/oct_chart.png b/img/oct_chart.png new file mode 100644 index 00000000..32f5250c Binary files /dev/null and b/img/oct_chart.png differ diff --git a/img/octree.png b/img/octree.png new file mode 100644 index 00000000..cbc04d1a Binary files /dev/null and b/img/octree.png differ diff --git a/img/octree_glitch.png b/img/octree_glitch.png new file mode 100644 index 00000000..ab4ea7da Binary files /dev/null and b/img/octree_glitch.png differ diff --git a/img/octree_ref.png b/img/octree_ref.png new file mode 100644 index 00000000..a615208f Binary files /dev/null and b/img/octree_ref.png differ diff --git a/img/overview.png b/img/overview.png new file mode 100644 index 00000000..4f4dcd67 Binary files /dev/null and b/img/overview.png differ diff --git a/img/overview.pptx b/img/overview.pptx new file mode 100644 index 00000000..82912e81 Binary files /dev/null and b/img/overview.pptx differ diff --git a/img/sc_chart.png b/img/sc_chart.png new file mode 100644 index 00000000..1f0d8e5e Binary files /dev/null and b/img/sc_chart.png differ diff --git a/img/sc_num_rays.png b/img/sc_num_rays.png new file mode 100644 index 00000000..9e017a13 Binary files /dev/null and b/img/sc_num_rays.png differ diff --git a/img/simple_ref.png b/img/simple_ref.png new file mode 100644 index 00000000..d341d495 Binary files /dev/null and b/img/simple_ref.png differ diff --git a/notes.txt b/notes.txt new file mode 100644 index 00000000..7504a94b --- /dev/null +++ b/notes.txt @@ -0,0 +1,117 @@ +1. For each pixel, shoot a ray into the scene. + Questions: + - Direction computed using eye point to frustum near clip plane? + +2. For each ray, check what surface it hits. + Questions: + - Compute intersection with objects (e.g. sphere/cube) or with individual triangles in the mesh? + A) has it intersected + B) where has it intersected + (with sphere and cube, if you compute one, you get the other) + For mesh, + bounding cube or square + test, then next level of granularity + CLOSEST INTERSECTION + +3. + If surface is diffuse, cast shadow feeler towards light source(s) to determine if this part of the surface is in shadow. Return color based on surface color, material properties, and lit/occluded status. + + If surface is reflective/refractive, generate bounce ray with a new start position and direction and repeat from step 2 with new ray. + + Questions: + - All light sources? + - Treat light source as a point? Or a square? + + + +Ray Tracing - one ray per pixel, terminal. +Path Tracing - one ray per pixel, keep bouncing, using BSDF, go until maybe hit the light (depth or samples per pixel) + +time (iterations) & bounces +8 samples per pixel (depth) +iteration 0 -> bounces up to 8 times +iteration 1 -> same thing +. +. +. + + + + + + + + + + + + + + +Big confusion: Is this vanilla "Ray Tracing"? Only reflective/refractive seem to be producing additional rays. Is the addition of Bidirectional Scattering Distribution Functions (BSDFs) what enables global illumination? + +- Shoot multiple rays at each pixel and let bounce randomly? Or have each bounce spawn multiple new rays? + +- Is starter code working properly? +- Review Questions +- When are assignments graded + + +- Installing dependencies in c++ + - JS: npm install ... + - Python: pip install ... + - Golang: go get ... + - C++: + - Add git submodule to project + - Add to cmake + +Cuda-opengl interop + ->> Search for cudagl +OpenGL higher level +Vulcan lower level + + + + + + + + + + + + + + + +- Path tracing is a type of ray tracing + +RAY TRACING VS PATH TRACING +Ray tracing - Each ray can spawn multiple new rays at every bounce +Path tracing - Each ray only spawns one new ray at every bounce + +Parallelizing by rays vs pixels? Why is this different? + +At each iteration, advance each ray path by one bounce + - check against scene geometry + - remove terminated rays from ray pool with stream compaction + +Perform intersection testing and shading/bsdf evaluation in separate kernels +"wavefront" of intersection testing + +- Use parallel radix sort to batch by material type + + + +glm::vec3* device_image -> pixelcount + These are the colors in the image to be rendered + +PathSegment* device_paths -> pixelcount + These are the ray origin, direction, color, pixelIndex, and remainingBounces. It makes sense to store the pixelIndex because as some PathSegments reach 0 remainingBounces, we will do stream compaction to remove them from device_paths and so the indices in this array will not line up with pixel indices. At each timestep, this array holds all in-flight rays. + +ShadeableIntersection* device_intersections -> pixelcount + For every PathSegment in device_paths, there may or may not be a corresponding ShadeableIntersection (maybe the ray shoots out of the scene). But for every ShadeableIntersection in device_intersections, there must be a corresponding PathSegment. + + +Geom* device_geoms -> scene->geoms.size() +Material* device_materials scene->materials.size() \ No newline at end of file diff --git a/scenes/__pycache__/scenegenerator.cpython-39.pyc b/scenes/__pycache__/scenegenerator.cpython-39.pyc new file mode 100644 index 00000000..942ef34f Binary files /dev/null and b/scenes/__pycache__/scenegenerator.cpython-39.pyc differ diff --git a/scenes/cornell.txt b/scenes/cornell.txt index 83ff8202..4ceba709 100644 --- a/scenes/cornell.txt +++ b/scenes/cornell.txt @@ -6,7 +6,7 @@ SPECRGB 0 0 0 REFL 0 REFR 0 REFRIOR 0 -EMITTANCE 5 +EMITTANCE 10 // Diffuse white MATERIAL 1 @@ -48,12 +48,22 @@ REFR 0 REFRIOR 0 EMITTANCE 0 +// Refractive white +MATERIAL 5 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB .98 .98 .98 +REFL 0 +REFR 1 +REFRIOR 1.5 +EMITTANCE 0 + // Camera CAMERA RES 800 800 FOVY 45 ITERATIONS 5000 -DEPTH 8 +DEPTH 6 FILE cornell EYE 0.0 5 10.5 LOOKAT 0 5 0 @@ -109,9 +119,33 @@ ROTAT 0 0 0 SCALE .01 10 10 // Sphere +// OBJECT 6 +// sphere +// material 4 +// TRANS 2 4 0 +// ROTAT 0 0 0 +// SCALE 3 3 3 + +// Sphere +// OBJECT 6 +// sphere +// material 5 +// TRANS -2 4 0 +// ROTAT 0 0 0 +// SCALE 3 3 3 + +// Cow OBJECT 6 -sphere -material 4 -TRANS -1 4 -1 +cow.obj +material 5 +TRANS 0 2 0 ROTAT 0 0 0 -SCALE 3 3 3 +SCALE 0.5 0.5 0.5 + +// Cow BOX +// OBJECT 6 +// cow.obj +// material 5 +// TRANS 0 5 0 +// ROTAT 0 0 0 +// SCALE 5 5 5 \ No newline at end of file diff --git a/scenes/cover.txt b/scenes/cover.txt new file mode 100644 index 00000000..1e1fbe01 --- /dev/null +++ b/scenes/cover.txt @@ -0,0 +1,153 @@ +// light +MATERIAL 0 +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 10 + +// Diffuse white +MATERIAL 1 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse red +MATERIAL 2 +RGB 0.85 0.35 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse green +MATERIAL 3 +RGB 0.35 0.85 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse cyan +MATERIAL 4 +RGB 0 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Specular white +MATERIAL 5 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 1 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Refractive white +MATERIAL 6 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 0 +REFR 1 +REFRIOR 1.5 +EMITTANCE 0 + +// Camera +CAMERA +RES 1200 800 +FOVY 45 +ITERATIONS 4000 +DEPTH 6 +FILE cornell +EYE 0 5 10.5 +LOOKAT 0 5 0 +UP 0 1 0 + +// Ceiling light L +OBJECT 0 +cube +material 0 +TRANS -3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Ceiling light R +OBJECT 1 +cube +material 0 +TRANS 3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Floor +OBJECT 2 +cube +material 1 +TRANS 0 0 0 +ROTAT 0 0 0 +SCALE 10 0.01 10 + +// Ceiling +OBJECT 3 +cube +material 1 +TRANS 0 10 0 +ROTAT 0 0 90 +SCALE 0.01 10 10 + +// Back wall +OBJECT 4 +cube +material 1 +TRANS 0 5 -5 +ROTAT 0 90 0 +SCALE 0.01 10 10 + +// Left wall +OBJECT 5 +cube +material 2 +TRANS -5 5 0 +ROTAT 0 0 0 +SCALE 0.01 10 10 + +// Right wall +OBJECT 6 +cube +material 3 +TRANS 5 5 0 +ROTAT 0 0 0 +SCALE 0.01 10 10 + +// Cow +OBJECT 7 +cow.obj +material 6 +TRANS -0.5 2.7 -2 +ROTAT 0 -140 0 +SCALE 0.75 0.75 0.75 + +// Sphere +OBJECT 8 +sphere +material 6 +TRANS 2 4 2 +ROTAT 0 0 0 +SCALE 4 4 4 + diff --git a/scenes/cow.txt b/scenes/cow.txt new file mode 100644 index 00000000..7d102872 --- /dev/null +++ b/scenes/cow.txt @@ -0,0 +1,155 @@ +// light +MATERIAL 0 +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 10 + +// Diffuse white +MATERIAL 1 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse red +MATERIAL 2 +RGB 0.85 0.35 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse green +MATERIAL 3 +RGB 0.35 0.85 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse cyan +MATERIAL 4 +RGB 0 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse fuchsia +MATERIAL 5 +RGB 0.98 0 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Specular white +MATERIAL 6 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 1 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Refractive white +MATERIAL 7 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 0 +REFR 1 +REFRIOR 1.5 +EMITTANCE 0 + +// Camera +CAMERA +RES 800 800 +FOVY 45 +ITERATIONS 1000 +DEPTH 4 +FILE cornell +EYE 0 5 10.5 +LOOKAT 0 5 0 +UP 0 1 0 + +// Ceiling light L +OBJECT 0 +cube +material 0 +TRANS -3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Ceiling light R +OBJECT 1 +cube +material 0 +TRANS 3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Floor +OBJECT 2 +cube +material 1 +TRANS 0 0 0 +ROTAT 0 0 0 +SCALE 10 0.01 10 + +// Ceiling +OBJECT 3 +cube +material 1 +TRANS 0 10 0 +ROTAT 0 0 90 +SCALE 0.01 10 10 + +// Back wall +OBJECT 4 +cube +material 1 +TRANS 0 5 -5 +ROTAT 0 90 0 +SCALE 0.01 10 10 + +// Left wall +OBJECT 5 +cube +material 2 +TRANS -5 5 0 +ROTAT 0 0 0 +SCALE 0.01 10 10 + +// Right wall +OBJECT 6 +cube +material 3 +TRANS 5 5 0 +ROTAT 0 0 0 +SCALE 0.01 10 10 + +// Cow +OBJECT 7 +cow.obj +material 4 +TRANS -0.2 2.8 0 +ROTAT 0 0 0 +SCALE 0.8 0.8 0.8 + diff --git a/scenes/epic.txt b/scenes/epic.txt new file mode 100644 index 00000000..fe4e7fe0 --- /dev/null +++ b/scenes/epic.txt @@ -0,0 +1,165 @@ +// light +MATERIAL 0 +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 50 + +// cow light +MATERIAL 1 +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 10 + +// Diffuse white +MATERIAL 2 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse red +MATERIAL 3 +RGB 0.85 0.35 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse green +MATERIAL 4 +RGB 0.35 0.85 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse cyan +MATERIAL 5 +RGB 0 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse fuchsia +MATERIAL 6 +RGB 0.98 0 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Specular white +MATERIAL 7 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 1 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Refractive white +MATERIAL 8 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 0 +REFR 1 +REFRIOR 1.5 +EMITTANCE 0 + +// Camera +CAMERA +RES 1200 800 +FOVY 45 +ITERATIONS 3000 +DEPTH 8 +FILE cornell +EYE 0 5 10.5 +LOOKAT 0 5 0 +UP 0 1 0 + +// Floor +OBJECT 0 +cube +material 4 +TRANS 0 0 0 +ROTAT 0 0 0 +SCALE 100 0.01 100 + +// Ceiling +OBJECT 1 +cube +material 2 +TRANS 0 100 0 +ROTAT 0 0 90 +SCALE 0.01 10 10 + +// Cow +OBJECT 2 +cow.obj +material 6 +TRANS -3 2.7 2 +ROTAT 0 -25 0 +SCALE 0.75 0.75 0.75 + +// Cow +OBJECT 3 +cow.obj +material 6 +TRANS -1 2.7 -4 +ROTAT 0 25 0 +SCALE 0.75 0.75 0.75 + +// Cow +OBJECT 4 +cow.obj +material 6 +TRANS 3 2.7 -1 +ROTAT 0 0 0 +SCALE 0.75 0.75 0.75 + +// Cow +OBJECT 5 +cow.obj +material 6 +TRANS 4 2.7 6 +ROTAT 0 30 0 +SCALE 0.75 0.75 0.75 + +// Cow +OBJECT 6 +cow.obj +material 1 +TRANS 3 10 4 +ROTAT 0 0 -120 +SCALE 0.75 0.75 0.75 + +// Ufo +OBJECT 7 +ufo.obj +material 7 +TRANS 4 14 4 +ROTAT 0 0 -20 +SCALE 0.75 0.75 0.75 + diff --git a/scenes/epics.txt b/scenes/epics.txt new file mode 100644 index 00000000..79113a13 --- /dev/null +++ b/scenes/epics.txt @@ -0,0 +1,147 @@ +// light +MATERIAL 0 +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 10 + +// Diffuse white +MATERIAL 1 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse red +MATERIAL 2 +RGB 0.85 0.35 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse green +MATERIAL 3 +RGB 0.35 0.85 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse cyan +MATERIAL 4 +RGB 0 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse fuchsia +MATERIAL 5 +RGB 0.98 0 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Specular white +MATERIAL 6 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 1 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Refractive white +MATERIAL 7 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 0 +REFR 1 +REFRIOR 1.5 +EMITTANCE 0 + +// Camera +CAMERA +RES 800 800 +FOVY 45 +ITERATIONS 4000 +DEPTH 8 +FILE cornell +EYE 0 5 10.5 +LOOKAT 0 5 0 +UP 0 1 0 + +// Ceiling light L +OBJECT 0 +cube +material 0 +TRANS -3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Ceiling light R +OBJECT 1 +cube +material 0 +TRANS 3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Floor +OBJECT 2 +cube +material 1 +TRANS 0 0 0 +ROTAT 0 0 0 +SCALE 10 0.01 10 + +// Ceiling +OBJECT 3 +cube +material 1 +TRANS 0 10 0 +ROTAT 0 0 90 +SCALE 0.01 10 10 + +// Cow +OBJECT 4 +cow.obj +material 4 +TRANS -3 2.7 2 +ROTAT 0 0 0 +SCALE 0.75 0.75 0.75 + +// Cow +OBJECT 5 +cow.obj +material 4 +TRANS -1 2.7 -2 +ROTAT 0 0 0 +SCALE 0.75 0.75 0.75 + +// Cow +OBJECT 6 +cow.obj +material 4 +TRANS 3 2.7 -1 +ROTAT 0 0 0 +SCALE 0.75 0.75 0.75 + diff --git a/scenes/gen_cover.py b/scenes/gen_cover.py new file mode 100644 index 00000000..065863bc --- /dev/null +++ b/scenes/gen_cover.py @@ -0,0 +1,98 @@ +from scenegenerator import Camera, Object, Material, SceneFile + +sceneFile = SceneFile() +sceneFile.addMaterial(Material( + tag='// light', + RGB=[1, 1, 1], + EMITTANCE=10)) +sceneFile.addMaterial(Material( + tag='// Diffuse white', + RGB=[0.98, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Diffuse red', + RGB=[0.85, 0.35, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse green', + RGB=[0.35, 0.85, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse cyan', + RGB=[0, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Specular white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFL=1)) +sceneFile.addMaterial(Material( + tag='// Refractive white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFR=1, + REFRIOR=1.5)) + +sceneFile.addCamera(Camera( + DEPTH=6, + RES=[1200, 800], + FOVY=45, + ITERATIONS=4000)) + +sceneFile.addObject(Object( + tag='// Ceiling light L', + flavor='cube', + material='// light', + TRANS=[-3, 10, 0], + SCALE=[2, 0.3, 2])) +sceneFile.addObject(Object( + tag='// Ceiling light R', + flavor='cube', + material='// light', + TRANS=[3, 10, 0], + SCALE=[2, 0.3, 2])) +sceneFile.addObject(Object( + tag='// Floor', + flavor='cube', + material='// Diffuse white', + SCALE=[10, 0.01, 10])) +sceneFile.addObject(Object( + tag='// Ceiling', + flavor='cube', + material='// Diffuse white', + TRANS=[0, 10, 0], + ROTAT=[0, 0, 90], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Back wall', + flavor='cube', + material='// Diffuse white', + TRANS=[0, 5, -5], + ROTAT=[0, 90, 0], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Left wall', + flavor='cube', + material='// Diffuse red', + TRANS=[-5, 5, 0], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Right wall', + flavor='cube', + material='// Diffuse green', + TRANS=[5, 5, 0], + SCALE=[0.01, 10, 10])) + + +sceneFile.addObject(Object( + tag='// Cow', + flavor='cow.obj', + material='// Refractive white', + TRANS=[-0.5, 2.7, -2], + ROTAT=[0, -140, 0], + SCALE=[0.75, 0.75, 0.75])) +sceneFile.addObject(Object( + tag='// Sphere', + flavor='sphere', + material='// Refractive white', + TRANS=[2, 4, 2], + ROTAT=[0, 0, 0], + SCALE=[4, 4, 4])) + +sceneFile.generate('cover') \ No newline at end of file diff --git a/scenes/gen_cow.py b/scenes/gen_cow.py new file mode 100644 index 00000000..3bb6bffa --- /dev/null +++ b/scenes/gen_cow.py @@ -0,0 +1,93 @@ +from scenegenerator import Camera, Object, Material, SceneFile + +sceneFile = SceneFile() +sceneFile.addMaterial(Material( + tag='// light', + RGB=[1, 1, 1], + EMITTANCE=10)) +sceneFile.addMaterial(Material( + tag='// Diffuse white', + RGB=[0.98, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Diffuse red', + RGB=[0.85, 0.35, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse green', + RGB=[0.35, 0.85, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse cyan', + RGB=[0, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Diffuse fuchsia', + RGB=[0.98, 0, 0.98])) +sceneFile.addMaterial(Material( + tag='// Specular white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFL=1)) +sceneFile.addMaterial(Material( + tag='// Refractive white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFR=1, + REFRIOR=1.5)) + +sceneFile.addCamera(Camera( + DEPTH=4, + RES=[800, 800], + FOVY=45, + ITERATIONS=1000)) + +sceneFile.addObject(Object( + tag='// Ceiling light L', + flavor='cube', + material='// light', + TRANS=[-3, 10, 0], + SCALE=[2, 0.3, 2])) +sceneFile.addObject(Object( + tag='// Ceiling light R', + flavor='cube', + material='// light', + TRANS=[3, 10, 0], + SCALE=[2, 0.3, 2])) +sceneFile.addObject(Object( + tag='// Floor', + flavor='cube', + material='// Diffuse white', + SCALE=[10, 0.01, 10])) +sceneFile.addObject(Object( + tag='// Ceiling', + flavor='cube', + material='// Diffuse white', + TRANS=[0, 10, 0], + ROTAT=[0, 0, 90], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Back wall', + flavor='cube', + material='// Diffuse white', + TRANS=[0, 5, -5], + ROTAT=[0, 90, 0], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Left wall', + flavor='cube', + material='// Diffuse red', + TRANS=[-5, 5, 0], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Right wall', + flavor='cube', + material='// Diffuse green', + TRANS=[5, 5, 0], + SCALE=[0.01, 10, 10])) + +sceneFile.addObject(Object( + tag='// Cow', + flavor='cow.obj', + material='// Diffuse cyan', + TRANS=[-0.2, 2.8, 0], + ROTAT=[0, 0, 0], + SCALE=[0.8, 0.8, 0.8])) + +sceneFile.generate('cow') \ No newline at end of file diff --git a/scenes/gen_epic.py b/scenes/gen_epic.py new file mode 100644 index 00000000..5ce40e6f --- /dev/null +++ b/scenes/gen_epic.py @@ -0,0 +1,128 @@ +from scenegenerator import Camera, Object, Material, SceneFile + +sceneFile = SceneFile() +sceneFile.addMaterial(Material( + tag='// light', + RGB=[1, 1, 1], + EMITTANCE=50)) +sceneFile.addMaterial(Material( + tag='// cow light', + RGB=[1, 1, 1], + EMITTANCE=10)) +sceneFile.addMaterial(Material( + tag='// Diffuse white', + RGB=[0.98, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Diffuse red', + RGB=[0.85, 0.35, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse green', + RGB=[0.35, 0.85, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse cyan', + RGB=[0, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Diffuse fuchsia', + RGB=[0.98, 0, 0.98])) +sceneFile.addMaterial(Material( + tag='// Specular white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFL=1)) +sceneFile.addMaterial(Material( + tag='// Refractive white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFR=1, + REFRIOR=1.5)) + +sceneFile.addCamera(Camera( + DEPTH=8, + RES=[1200, 800], + FOVY=45, + ITERATIONS=3000)) + +# sceneFile.addObject(Object( +# tag='// Ceiling light L', +# flavor='sphere', +# material='// light', +# TRANS=[10, 50, 0], +# SCALE=[1, 1, 1])) +# sceneFile.addObject(Object( +# tag='// Ceiling light R', +# flavor='sphere', +# material='// light', +# TRANS=[-10, 50, 0], +# SCALE=[1, 1, 1])) +sceneFile.addObject(Object( + tag='// Floor', + flavor='cube', + material='// Diffuse green', + SCALE=[100, 0.01, 100])) +sceneFile.addObject(Object( + tag='// Ceiling', + flavor='cube', + material='// Diffuse white', + TRANS=[0, 100, 0], + ROTAT=[0, 0, 90], + SCALE=[0.01, 10, 10])) + +sceneFile.addObject(Object( + tag='// Cow', + flavor='cow.obj', + material='// Diffuse fuchsia', + TRANS=[-3, 2.7, 2], + ROTAT=[0, -25, 0], + SCALE=[0.75, 0.75, 0.75])) + +sceneFile.addObject(Object( + tag='// Cow', + flavor='cow.obj', + material='// Diffuse fuchsia', + TRANS=[-1, 2.7, -4], + ROTAT=[0, 25, 0], + SCALE=[0.75, 0.75, 0.75])) + +sceneFile.addObject(Object( + tag='// Cow', + flavor='cow.obj', + material='// Diffuse fuchsia', + TRANS=[3, 2.7, -1], + ROTAT=[0, 0, 0], + SCALE=[0.75, 0.75, 0.75])) + +sceneFile.addObject(Object( + tag='// Cow', + flavor='cow.obj', + material='// Diffuse fuchsia', + TRANS=[4, 2.7, 6], + ROTAT=[0, 30, 0], + SCALE=[0.75, 0.75, 0.75])) + +sceneFile.addObject(Object( + tag='// Cow', + flavor='cow.obj', + material='// cow light', + TRANS=[3, 10, 4], + ROTAT=[0, 0, -120], + SCALE=[0.75, 0.75, 0.75])) + +sceneFile.addObject(Object( + tag='// Ufo', + flavor='ufo.obj', + material='// Specular white', + TRANS=[4, 14, 4], + ROTAT=[0, 0, -20], + SCALE=[0.75, 0.75, 0.75])) + + +# sceneFile.addObject(Object( +# tag='// Cow', +# flavor='cow.obj', +# material='// Diffuse cyan', +# TRANS=[0, 2.7, 0], +# ROTAT=[0, 0, 0], +# SCALE=[0.75, 0.75, 0.75])) + + +sceneFile.generate('epic') \ No newline at end of file diff --git a/scenes/gen_spheres copy.py b/scenes/gen_spheres copy.py new file mode 100644 index 00000000..aadd9e6b --- /dev/null +++ b/scenes/gen_spheres copy.py @@ -0,0 +1,114 @@ +from scenegenerator import Camera, Object, Material, SceneFile + +sceneFile = SceneFile() +sceneFile.addMaterial(Material( + tag='// light', + RGB=[1, 1, 1], + EMITTANCE=10)) +sceneFile.addMaterial(Material( + tag='// Diffuse white', + RGB=[0.98, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Diffuse red', + RGB=[0.85, 0.35, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse green', + RGB=[0.35, 0.85, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse cyan', + RGB=[0, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Diffuse fuchsia', + RGB=[0.98, 0, 0.98])) +sceneFile.addMaterial(Material( + tag='// Specular white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFL=1)) +sceneFile.addMaterial(Material( + tag='// Refractive white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFR=1, + REFRIOR=1.5)) + +sceneFile.addCamera(Camera( + DEPTH=8, + RES=[800, 800], + FOVY=45, + ITERATIONS=4000)) + +sceneFile.addObject(Object( + tag='// Ceiling light L', + flavor='cube', + material='// light', + TRANS=[-3, 10, 0], + SCALE=[2, 0.3, 2])) +sceneFile.addObject(Object( + tag='// Ceiling light R', + flavor='cube', + material='// light', + TRANS=[3, 10, 0], + SCALE=[2, 0.3, 2])) +sceneFile.addObject(Object( + tag='// Floor', + flavor='cube', + material='// Diffuse white', + SCALE=[10, 0.01, 10])) +sceneFile.addObject(Object( + tag='// Ceiling', + flavor='cube', + material='// Diffuse white', + TRANS=[0, 10, 0], + ROTAT=[0, 0, 90], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Back wall', + flavor='cube', + material='// Diffuse white', + TRANS=[0, 5, -5], + ROTAT=[0, 90, 0], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Left wall', + flavor='cube', + material='// Diffuse red', + TRANS=[-5, 5, 0], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Right wall', + flavor='cube', + material='// Diffuse green', + TRANS=[5, 5, 0], + SCALE=[0.01, 10, 10])) + +sceneFile.addObject(Object( + tag='// L sphere', + flavor='sphere', + material='// Specular white', + TRANS=[-3.5, 2, 2], + SCALE=[3, 3, 3])) + +sceneFile.addObject(Object( + tag='// C sphere', + flavor='sphere', + material='// Refractive white', + TRANS=[0, 2, 2], + SCALE=[3, 3, 3])) + +sceneFile.addObject(Object( + tag='// R sphere', + flavor='sphere', + material='// Diffuse fuchsia', + TRANS=[3.5, 2, 2], + SCALE=[3, 3, 3])) + +# sceneFile.addObject(Object( +# tag='// Cow', +# flavor='cow.obj', +# material='// Diffuse cyan', +# TRANS=[-1, 2.7, 0], +# ROTAT=[0, 0, 0], +# SCALE=[0.75, 0.75, 0.75])) + +sceneFile.generate('spheres') \ No newline at end of file diff --git a/scenes/gen_spheres.py b/scenes/gen_spheres.py new file mode 100644 index 00000000..aadd9e6b --- /dev/null +++ b/scenes/gen_spheres.py @@ -0,0 +1,114 @@ +from scenegenerator import Camera, Object, Material, SceneFile + +sceneFile = SceneFile() +sceneFile.addMaterial(Material( + tag='// light', + RGB=[1, 1, 1], + EMITTANCE=10)) +sceneFile.addMaterial(Material( + tag='// Diffuse white', + RGB=[0.98, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Diffuse red', + RGB=[0.85, 0.35, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse green', + RGB=[0.35, 0.85, 0.35])) +sceneFile.addMaterial(Material( + tag='// Diffuse cyan', + RGB=[0, 0.98, 0.98])) +sceneFile.addMaterial(Material( + tag='// Diffuse fuchsia', + RGB=[0.98, 0, 0.98])) +sceneFile.addMaterial(Material( + tag='// Specular white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFL=1)) +sceneFile.addMaterial(Material( + tag='// Refractive white', + RGB=[0.98, 0.98, 0.98], + SPECRGB=[0.98, 0.98, 0.98], + REFR=1, + REFRIOR=1.5)) + +sceneFile.addCamera(Camera( + DEPTH=8, + RES=[800, 800], + FOVY=45, + ITERATIONS=4000)) + +sceneFile.addObject(Object( + tag='// Ceiling light L', + flavor='cube', + material='// light', + TRANS=[-3, 10, 0], + SCALE=[2, 0.3, 2])) +sceneFile.addObject(Object( + tag='// Ceiling light R', + flavor='cube', + material='// light', + TRANS=[3, 10, 0], + SCALE=[2, 0.3, 2])) +sceneFile.addObject(Object( + tag='// Floor', + flavor='cube', + material='// Diffuse white', + SCALE=[10, 0.01, 10])) +sceneFile.addObject(Object( + tag='// Ceiling', + flavor='cube', + material='// Diffuse white', + TRANS=[0, 10, 0], + ROTAT=[0, 0, 90], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Back wall', + flavor='cube', + material='// Diffuse white', + TRANS=[0, 5, -5], + ROTAT=[0, 90, 0], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Left wall', + flavor='cube', + material='// Diffuse red', + TRANS=[-5, 5, 0], + SCALE=[0.01, 10, 10])) +sceneFile.addObject(Object( + tag='// Right wall', + flavor='cube', + material='// Diffuse green', + TRANS=[5, 5, 0], + SCALE=[0.01, 10, 10])) + +sceneFile.addObject(Object( + tag='// L sphere', + flavor='sphere', + material='// Specular white', + TRANS=[-3.5, 2, 2], + SCALE=[3, 3, 3])) + +sceneFile.addObject(Object( + tag='// C sphere', + flavor='sphere', + material='// Refractive white', + TRANS=[0, 2, 2], + SCALE=[3, 3, 3])) + +sceneFile.addObject(Object( + tag='// R sphere', + flavor='sphere', + material='// Diffuse fuchsia', + TRANS=[3.5, 2, 2], + SCALE=[3, 3, 3])) + +# sceneFile.addObject(Object( +# tag='// Cow', +# flavor='cow.obj', +# material='// Diffuse cyan', +# TRANS=[-1, 2.7, 0], +# ROTAT=[0, 0, 0], +# SCALE=[0.75, 0.75, 0.75])) + +sceneFile.generate('spheres') \ No newline at end of file diff --git a/scenes/jonas.txt b/scenes/jonas.txt new file mode 100644 index 00000000..002fe064 --- /dev/null +++ b/scenes/jonas.txt @@ -0,0 +1,144 @@ +// Specular white +MATERIAL 0 +RGB .98 .98 .98 +SPECEX 0 +SPECRGB .98 .98 .98 +REFL 1 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse white +MATERIAL 1 +RGB 0.98 0.98 .98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse red +MATERIAL 2 +RGB 1 0 0 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse green +MATERIAL 3 +RGB 0 1 0 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse blue +MATERIAL 4 +RGB 0 0 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Emissive material (light) +MATERIAL 5 +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 30 + +// Refractive white +MATERIAL 6 +RGB .98 .98 .98 +SPECEX 0 +SPECRGB .98 .98 .98 +REFL 0 +REFR 1 +REFRIOR 1.5 +EMITTANCE 0 + +// Camera +CAMERA +RES 800 800 +FOVY 45 +ITERATIONS 5000 +DEPTH 6 +FILE cornell +EYE 0.0 5 10.5 +LOOKAT 0 5 0 +UP 0 1 0 + +// Large ceiling light +OBJECT 0 +cube +material 5 +TRANS 0 50 0 +ROTAT 0 0 0 +SCALE 10 .3 10 + +// Floor +OBJECT 1 +cube +material 1 +TRANS 0 -3.6 0 +ROTAT 0 0 0 +SCALE 200 .01 200 + +// Cow object +OBJECT 2 +cow.obj +material 2 +TRANS 5 0 5 +ROTAT 0 30 0 +SCALE 1 1 1 + +// Cow object +OBJECT 3 +cow.obj +material 6 +TRANS -5 0 -5 +ROTAT 0 -30 0 +SCALE 1 1 1 + +// Cow object +OBJECT 4 +cow.obj +material 4 +TRANS 0 0 0 +ROTAT 0 0 0 +SCALE 1 1 1 + +// Ufo object +OBJECT 5 +ufo.obj +material 2 +TRANS 5 20 0 +ROTAT 0 0 -30 +SCALE 1 1 1 + +// ceiling +OBJECT 6 +cube +material 3 +TRANS 0 50 0 +ROTAT 0 0 0 +SCALE 100 .01 100 + +// ball +OBJECT 7 +sphere +material 5 +TRANS 5 20 0 +ROTAT 0 0 0 +SCALE 1 1 1 \ No newline at end of file diff --git a/scenes/jonas2.txt b/scenes/jonas2.txt new file mode 100644 index 00000000..783424fa --- /dev/null +++ b/scenes/jonas2.txt @@ -0,0 +1,82 @@ +// Emissive material (light) +MATERIAL 0 +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 20 + +// Diffuse white +MATERIAL 1 +RGB 0.98 0.98 .98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Specular white +MATERIAL 2 +RGB .98 .98 .98 +SPECEX 0 +SPECRGB .98 .98 .98 +REFL 1 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse ceiling +MATERIAL 3 +RGB 0.2 0 .98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Camera +CAMERA +RES 800 800 +FOVY 45 +ITERATIONS 5000 +DEPTH 3 +FILE cornell +EYE 0.0 5 10.5 +LOOKAT 0 5 0 +UP 0 1 0 + +// Ceiling light +OBJECT 0 +cube +material 0 +TRANS 0 10 0 +ROTAT 0 0 0 +SCALE 5 .3 5 + +// Floor +OBJECT 1 +cube +material 1 +TRANS 0 -5 0 +ROTAT 0 0 0 +SCALE 30 .01 30 + +// Cow object +OBJECT 2 +cow.obj +material 1 +TRANS 0 0 0 +ROTAT 0 0 90 +SCALE 1 1 1 + +// ceiling +OBJECT 3 +cube +material 3 +TRANS 0 10 0 +ROTAT 0 0 0 +SCALE 100 .01 100 \ No newline at end of file diff --git a/scenes/scenegenerator.py b/scenes/scenegenerator.py new file mode 100644 index 00000000..5aeea553 --- /dev/null +++ b/scenes/scenegenerator.py @@ -0,0 +1,113 @@ +spread_array = lambda array: ' '.join([str(e) for e in array]) + +class Material(object): + def __init__(self, tag='// asdf', RGB=[1, 0, 0], SPECEX=0, SPECRGB=[0, 0, 0], REFL=0, REFR=0, REFRIOR=0, EMITTANCE=0): + self.tag = tag + self.rgb = RGB + self.specex = SPECEX + self.specrgb = SPECRGB + self.refl = REFL + self.refr = REFR + self.refrior = REFRIOR + self.emittance = EMITTANCE + + def get_lines(self, i): + return [ + self.tag, + f'MATERIAL {i}', + f'RGB\t\t{spread_array(self.rgb)}', + f'SPECEX\t\t{self.specex}', + f'SPECRGB\t\t{spread_array(self.specrgb)}', + f'REFL\t\t{self.refl}', + f'REFR\t\t{self.refr}', + f'REFRIOR\t\t{self.refrior}', + f'EMITTANCE\t\t{self.emittance}' + ] + +class Object(object): + def __init__(self, tag='// asdf', flavor='sphere', material=0, TRANS=[0, 0, 0], ROTAT=[0, 0, 0], SCALE=[1, 1, 1]): + self.tag = tag + self.flavor = flavor + self.material = material + self.trans = TRANS + self.rotat = ROTAT + self.scale = SCALE + + def get_lines(self, i): + return [ + self.tag, + f'OBJECT {i}', + self.flavor, + f'material {self.material}', + f'TRANS\t\t{spread_array(self.trans)}', + f'ROTAT\t\t{spread_array(self.rotat)}', + f'SCALE\t\t{spread_array(self.scale)}', + ] + +class Camera(object): + def __init__(self, tag='// Camera', RES=[800, 800], FOVY=45, ITERATIONS=5000, DEPTH=6, FILE='cornell', EYE=[0, 5, 10.5], LOOKAT=[0, 5, 0], UP=[0, 1, 0]): + self.tag = tag + self.res = RES + self.fovy = FOVY + self.iterations = ITERATIONS + self.depth = DEPTH + self.file = FILE + self.eye = EYE + self.lookat = LOOKAT + self.up = UP + + def get_lines(self): + return [ + self.tag, + 'CAMERA', + f'RES\t\t{spread_array(self.res)}', + f'FOVY\t\t{self.fovy}', + f'ITERATIONS\t\t{self.iterations}', + f'DEPTH\t\t{self.depth}', + f'FILE\t\t{self.file}', + f'EYE\t\t{spread_array(self.eye)}', + f'LOOKAT\t\t{spread_array(self.lookat)}', + f'UP\t\t{spread_array(self.up)}' + ] + +class SceneFile(object): + def __init__(self): + self.objects = [] + self.materials = [] + + def addCamera(self, camera): + self.camera = camera + + def addObject(self, object): + self.objects.append(object) + + def addMaterial(self, material): + self.materials.append(material) + + def generate(self, filename): + f = open(f"{filename}.txt", "w") + + matTag2Num = {} + # MATERIALS + for i, material in enumerate(self.materials): + for j, line in enumerate(material.get_lines(i)): + if j == 0: + matTag2Num[line] = i + f.write(line + '\n') + f.write('\n') + + # CAMERA + for line in self.camera.get_lines(): + f.write(line + '\n') + f.write('\n') + + # OBJECTS + for i, object in enumerate(self.objects): + # convert string label to number + object.material = matTag2Num[object.material] + for j, line in enumerate(object.get_lines(i)): + f.write(line + '\n') + f.write('\n') + + f.close() + diff --git a/scenes/spheres.txt b/scenes/spheres.txt new file mode 100644 index 00000000..86f40523 --- /dev/null +++ b/scenes/spheres.txt @@ -0,0 +1,171 @@ +// light +MATERIAL 0 +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 10 + +// Diffuse white +MATERIAL 1 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse red +MATERIAL 2 +RGB 0.85 0.35 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse green +MATERIAL 3 +RGB 0.35 0.85 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse cyan +MATERIAL 4 +RGB 0 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse fuchsia +MATERIAL 5 +RGB 0.98 0 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Specular white +MATERIAL 6 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 1 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Refractive white +MATERIAL 7 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 0 +REFR 1 +REFRIOR 1.5 +EMITTANCE 0 + +// Camera +CAMERA +RES 800 800 +FOVY 45 +ITERATIONS 4000 +DEPTH 8 +FILE cornell +EYE 0 5 10.5 +LOOKAT 0 5 0 +UP 0 1 0 + +// Ceiling light L +OBJECT 0 +cube +material 0 +TRANS -3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Ceiling light R +OBJECT 1 +cube +material 0 +TRANS 3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Floor +OBJECT 2 +cube +material 1 +TRANS 0 0 0 +ROTAT 0 0 0 +SCALE 10 0.01 10 + +// Ceiling +OBJECT 3 +cube +material 1 +TRANS 0 10 0 +ROTAT 0 0 90 +SCALE 0.01 10 10 + +// Back wall +OBJECT 4 +cube +material 1 +TRANS 0 5 -5 +ROTAT 0 90 0 +SCALE 0.01 10 10 + +// Left wall +OBJECT 5 +cube +material 2 +TRANS -5 5 0 +ROTAT 0 0 0 +SCALE 0.01 10 10 + +// Right wall +OBJECT 6 +cube +material 3 +TRANS 5 5 0 +ROTAT 0 0 0 +SCALE 0.01 10 10 + +// L sphere +OBJECT 7 +sphere +material 6 +TRANS -3.5 2 2 +ROTAT 0 0 0 +SCALE 3 3 3 + +// C sphere +OBJECT 8 +sphere +material 7 +TRANS 0 2 2 +ROTAT 0 0 0 +SCALE 3 3 3 + +// R sphere +OBJECT 9 +sphere +material 5 +TRANS 3.5 2 2 +ROTAT 0 0 0 +SCALE 3 3 3 + diff --git a/scenes/test_scene.txt b/scenes/test_scene.txt new file mode 100644 index 00000000..2ae6f7d5 --- /dev/null +++ b/scenes/test_scene.txt @@ -0,0 +1,153 @@ +// light +MATERIAL 0 +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 10 + +// Diffuse white +MATERIAL 1 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse red +MATERIAL 2 +RGB 0.85 0.35 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse green +MATERIAL 3 +RGB 0.35 0.85 0.35 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Diffuse cyan +MATERIAL 4 +RGB 0 0.98 0.98 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Specular white +MATERIAL 5 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 1 +REFR 0 +REFRIOR 0 +EMITTANCE 0 + +// Refractive white +MATERIAL 6 +RGB 0.98 0.98 0.98 +SPECEX 0 +SPECRGB 0.98 0.98 0.98 +REFL 0 +REFR 1 +REFRIOR 1.5 +EMITTANCE 0 + +// Camera +CAMERA +RES 1200 800 +FOVY 45 +ITERATIONS 4000 +DEPTH 6 +FILE cornell +EYE 0 5 10.5 +LOOKAT 0 5 0 +UP 0 1 0 + +// Ceiling light L +OBJECT 0 +cube +material 0 +TRANS -3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Ceiling light R +OBJECT 1 +cube +material 0 +TRANS 3 10 0 +ROTAT 0 0 0 +SCALE 2 0.3 2 + +// Floor +OBJECT 2 +cube +material 1 +TRANS 0 0 0 +ROTAT 0 0 0 +SCALE 10 0.01 10 + +// Ceiling +OBJECT 3 +cube +material 1 +TRANS 0 10 0 +ROTAT 0 0 90 +SCALE 0.01 10 10 + +// Back wall +OBJECT 4 +cube +material 1 +TRANS 0 5 -5 +ROTAT 0 90 0 +SCALE 0.01 10 10 + +// Left wall +OBJECT 5 +cube +material 2 +TRANS -5 5 0 +ROTAT 0 0 0 +SCALE 0.01 10 10 + +// Right wall +OBJECT 6 +cube +material 3 +TRANS 5 5 0 +ROTAT 0 0 0 +SCALE 0.01 10 10 + +// Cow +OBJECT 7 +cow.obj +material 6 +TRANS -1.5 2.7 0 +ROTAT 0 -135 0 +SCALE 0.75 0.75 0.75 + +// Ufo +OBJECT 8 +ufo.obj +material 5 +TRANS 1.5 4 0 +ROTAT 0 0 0 +SCALE 0.35 0.35 0.35 + diff --git a/src/interactions.h b/src/interactions.h index f969e458..7b4ff5f4 100644 --- a/src/interactions.h +++ b/src/interactions.h @@ -1,15 +1,14 @@ #pragma once #include "intersections.h" +#include -// CHECKITOUT /** * Computes a cosine-weighted random direction in a hemisphere. * Used for diffuse lighting. */ __host__ __device__ -glm::vec3 calculateRandomDirectionInHemisphere( - glm::vec3 normal, thrust::default_random_engine &rng) { +glm::vec3 calculateRandomDirectionInHemisphere(glm::vec3 normal, thrust::default_random_engine &rng) { thrust::uniform_real_distribution u01(0, 1); float up = sqrt(u01(rng)); // cos(theta) @@ -31,16 +30,42 @@ glm::vec3 calculateRandomDirectionInHemisphere( } // Use not-normal direction to generate two perpendicular directions - glm::vec3 perpendicularDirection1 = - glm::normalize(glm::cross(normal, directionNotNormal)); - glm::vec3 perpendicularDirection2 = - glm::normalize(glm::cross(normal, perpendicularDirection1)); + glm::vec3 perpendicularDirection1 = glm::normalize(glm::cross(normal, directionNotNormal)); + glm::vec3 perpendicularDirection2 = glm::normalize(glm::cross(normal, perpendicularDirection1)); return up * normal + cos(around) * over * perpendicularDirection1 + sin(around) * over * perpendicularDirection2; } +// https://blog.demofox.org/2017/01/09/raytracing-reflection-refraction-fresnel-total-internal-reflection-and-beers-law/ +__host__ __device__ +float FresnelReflectAmount(float n1, float n2, glm::vec3 normal, glm::vec3 incident) { + // Schlick aproximation + float r0 = (n1 - n2) / (n1 + n2); + r0 *= r0; + float cosX = -glm::dot(normal, incident); + + if (n1 > n2) { + float n = n1 / n2; + float sinT2 = n * n * (1.0 - cosX * cosX); + + // Total internal reflection + if (sinT2 > 1.0) { + return 1.0; + } + cosX = glm::sqrt(1.0 - sinT2); + } + + float x = 1.0 - cosX; + float ret = r0 + (1.0 - r0) * x * x * x * x * x; + + // adjust reflect multiplier for object reflectivity + //ret = (OBJECT_REFLECTIVITY + (1.0 - OBJECT_REFLECTIVITY) * ret); + + return ret; +} + /** * Scatter a ray with some probabilities according to the material properties. * For example, a diffuse surface scatters in a cosine-weighted hemisphere. @@ -50,7 +75,7 @@ glm::vec3 calculateRandomDirectionInHemisphere( * * The visual effect you want is to straight-up add the diffuse and specular * components. You can do this in a few ways. This logic also applies to - * combining other types of materias (such as refractive). + * combining other types of materials (such as refractive). * * - Always take an even (50/50) split between a each effect (a diffuse bounce * and a specular bounce), but divide the resulting color of either branch @@ -68,12 +93,39 @@ glm::vec3 calculateRandomDirectionInHemisphere( */ __host__ __device__ void scatterRay( - PathSegment & pathSegment, - glm::vec3 intersect, - glm::vec3 normal, - const Material &m, - thrust::default_random_engine &rng) { - // TODO: implement this. - // A basic implementation of pure-diffuse shading will just call the - // calculateRandomDirectionInHemisphere defined above. + PathSegment &pathSegment, + glm::vec3 intersect, + glm::vec3 normal, + const Material &material, + thrust::default_random_engine &rng, + bool outside +) { + if (material.emittance > 0.0f) { + pathSegment.color *= material.emittance; + pathSegment.remainingBounces = 0; + } else { + pathSegment.ray.origin = intersect; + pathSegment.remainingBounces -= 1; + pathSegment.color *= material.color; + + if (material.hasReflective) { + pathSegment.ray.direction = pathSegment.ray.direction - 2.f * (glm::dot(pathSegment.ray.direction, normal) * normal); + } else if (material.hasRefractive) { + float n1 = outside ? 1.0 : material.indexOfRefraction; + float n2 = outside ? material.indexOfRefraction : 1.0; + + float r = FresnelReflectAmount(n1, n2, normal, pathSegment.ray.direction); + thrust::uniform_real_distribution u01(0, 1); + + if (u01(rng) < r) { + pathSegment.ray.direction = pathSegment.ray.direction - 2.f * (glm::dot(pathSegment.ray.direction, normal) * normal); + } else { + float eta = outside ? 1.0 / material.indexOfRefraction : material.indexOfRefraction; + pathSegment.ray.direction = glm::normalize(glm::refract(pathSegment.ray.direction, normal, eta)); + } + } else { + pathSegment.ray.direction = calculateRandomDirectionInHemisphere(normal, rng); + } + } } + diff --git a/src/intersections.h b/src/intersections.h index b1504071..668a09a2 100644 --- a/src/intersections.h +++ b/src/intersections.h @@ -24,8 +24,8 @@ __host__ __device__ inline unsigned int utilhash(unsigned int a) { * Compute a point at parameter value `t` on ray `r`. * Falls slightly short so that it doesn't intersect the object it's hitting. */ -__host__ __device__ glm::vec3 getPointOnRay(Ray r, float t) { - return r.origin + (t - .0001f) * glm::normalize(r.direction); +__host__ __device__ glm::vec3 getPointOnRay(Ray r, float t, float gap) { + return r.origin + (t + gap) * glm::normalize(r.direction); } /** @@ -45,8 +45,7 @@ __host__ __device__ glm::vec3 multiplyMV(glm::mat4 m, glm::vec4 v) { * @param outside Output param for whether the ray came from outside. * @return Ray parameter `t` value. -1 if no intersection. */ -__host__ __device__ float boxIntersectionTest(Geom box, Ray r, - glm::vec3 &intersectionPoint, glm::vec3 &normal, bool &outside) { +__host__ __device__ float boxIntersectionTest(Geom box, Ray r, glm::vec3 &intersectionPoint, glm::vec3 &normal, bool &outside) { Ray q; q.origin = multiplyMV(box.inverseTransform, glm::vec4(r.origin , 1.0f)); q.direction = glm::normalize(multiplyMV(box.inverseTransform, glm::vec4(r.direction, 0.0f))); @@ -82,7 +81,7 @@ __host__ __device__ float boxIntersectionTest(Geom box, Ray r, tmin_n = tmax_n; outside = false; } - intersectionPoint = multiplyMV(box.transform, glm::vec4(getPointOnRay(q, tmin), 1.0f)); + intersectionPoint = multiplyMV(box.transform, glm::vec4(getPointOnRay(q, tmin, 0), 1.0f)); normal = glm::normalize(multiplyMV(box.invTranspose, glm::vec4(tmin_n, 0.0f))); return glm::length(r.origin - intersectionPoint); } @@ -99,9 +98,8 @@ __host__ __device__ float boxIntersectionTest(Geom box, Ray r, * @param outside Output param for whether the ray came from outside. * @return Ray parameter `t` value. -1 if no intersection. */ -__host__ __device__ float sphereIntersectionTest(Geom sphere, Ray r, - glm::vec3 &intersectionPoint, glm::vec3 &normal, bool &outside) { - float radius = .5; +__host__ __device__ float sphereIntersectionTest(Geom sphere, Ray r, glm::vec3 &intersectionPoint, glm::vec3 &normal, bool &outside) { + float radius = 0.5; glm::vec3 ro = multiplyMV(sphere.inverseTransform, glm::vec4(r.origin, 1.0f)); glm::vec3 rd = glm::normalize(multiplyMV(sphere.inverseTransform, glm::vec4(r.direction, 0.0f))); @@ -132,7 +130,7 @@ __host__ __device__ float sphereIntersectionTest(Geom sphere, Ray r, outside = false; } - glm::vec3 objspaceIntersection = getPointOnRay(rt, t); + glm::vec3 objspaceIntersection = getPointOnRay(rt, t, 0); intersectionPoint = multiplyMV(sphere.transform, glm::vec4(objspaceIntersection, 1.f)); normal = glm::normalize(multiplyMV(sphere.invTranspose, glm::vec4(objspaceIntersection, 0.f))); @@ -142,3 +140,163 @@ __host__ __device__ float sphereIntersectionTest(Geom sphere, Ray r, return glm::length(r.origin - intersectionPoint); } + +// https://en.wikipedia.org/wiki/M%C3%B6ller%E2%80%93Trumbore_intersection_algorithm +__host__ __device__ +bool rayIntersectsTriangle(const Ray r, const Triangle triangle, glm::vec3 &intersection, float &t) { + glm::vec3 vertex0 = triangle.points[0]; + glm::vec3 vertex1 = triangle.points[1]; + glm::vec3 vertex2 = triangle.points[2]; + + glm::vec3 edge1, edge2, h, s, q; + float a, f, u, v; + + edge1 = vertex1 - vertex0; + edge2 = vertex2 - vertex0; + + h = glm::cross(r.direction, edge2); + a = glm::dot(edge1, h); + if (a > -EPSILON && a < EPSILON) { + return false; // This ray is parallel to this triangle. + } + + f = 1.0 / a; + s = r.origin - vertex0; + u = f * glm::dot(s, h); + if (u < 0.0 || u > 1.0) { + return false; + } + + q = glm::cross(s, edge1); + v = f * glm::dot(r.direction, q); + if (v < 0.0 || u + v > 1.0) { + return false; + } + + // At this stage we can compute t to find out where the intersection point is on the line. + t = f * glm::dot(edge2, q); + if (t > EPSILON) { + // ray intersection + intersection = getPointOnRay(r, t, 0); + return true; + } else { + // This means that there is a line intersection but not a ray intersection. + return false; + } +} + +__host__ __device__ +float trianglesIntersectionTest(Geom bb, Ray r, glm::vec3 &intersectionPoint, glm::vec3 &normal, bool &outside) { + glm::vec3 tmp_intersect; + glm::vec3 tmp_normal; + float tmp_t; + + float min_t = FLT_MAX; + glm::vec3 min_intersect; + glm::vec3 min_normal; + glm::vec3 min_color; + + for (int i = 0; i < bb.numTriangles; i++) { + if (rayIntersectsTriangle(r, bb.device_triangles[i], tmp_intersect, tmp_t)) { + if (tmp_t < min_t) { + min_t = tmp_t; + min_intersect = tmp_intersect; + min_normal = bb.device_triangles[i].normal; + } + } + } + + intersectionPoint = min_intersect; + normal = min_normal; + //outside = true; + if (min_t < 1000000) { + float ang = glm::acos(glm::dot(r.direction, normal) / (glm::length(r.direction) * glm::length(normal))); + outside = glm::degrees(ang) > 90; + } + + return min_t > 10000 ? -1.0 : min_t; +} + +__host__ __device__ +bool rayIntersectsCube(const Ray r, const Node n) { + Ray q; + q.origin = multiplyMV(n.inverseTransform, glm::vec4(r.origin, 1.0f)); + q.direction = glm::normalize(multiplyMV(n.inverseTransform, glm::vec4(r.direction, 0.0f))); + + float tmin = -1e38f; + float tmax = 1e38f; + glm::vec3 tmin_n; + glm::vec3 tmax_n; + for (int xyz = 0; xyz < 3; ++xyz) { + float qdxyz = q.direction[xyz]; + //if (glm::abs(qdxyz) > 0.00001f) { + float t1 = (-0.5f - q.origin[xyz]) / qdxyz; + float t2 = (+0.5f - q.origin[xyz]) / qdxyz; + float ta = glm::min(t1, t2); + float tb = glm::max(t1, t2); + glm::vec3 n; + n[xyz] = t2 < t1 ? +1 : -1; + if (ta > 0 && ta > tmin) { + tmin = ta; + tmin_n = n; + } + if (tb < tmax) { + tmax = tb; + tmax_n = n; + } + //} + } + + return tmax >= tmin && tmax > 0; +} + +__host__ __device__ +void recurse(const Node* nodes, const int i, const Ray r, float& minT, glm::vec3& minIntersect, glm::vec3& minNormal, glm::vec3& minColor) { + Node n = nodes[i]; + + if (rayIntersectsCube(r, n)) { + if (n.leaf) { + float tmp_t; + glm::vec3 tmp_intersect; + + for (int j = 0; j < n.numTriangles; j++) { + Triangle t = n.device_triangles[j]; + + if (rayIntersectsTriangle(r, t, tmp_intersect, tmp_t)) { + if (tmp_t < minT) { + minT = tmp_t; + minIntersect = tmp_intersect; + minNormal = t.normal; + } + } + } + } else { + for (int j = n.childrenStartIndex; j < n.childrenStartIndex + 8; j++) { + recurse(nodes, j, r, minT, minIntersect, minNormal, minColor); + } + } + } +} + +__host__ __device__ +float octreeIntersectionTest(Geom bb, Ray r, glm::vec3& intersectionPoint, glm::vec3& normal, bool& outside) { + float minT = FLT_MAX; + glm::vec3 minIntersect; + glm::vec3 minNormal; + glm::vec3 minColor; + + recurse(bb.device_tree, 0, r, minT, minIntersect, minNormal, minColor); + + intersectionPoint = minIntersect; + normal = minNormal; + + if (minT < FLT_MAX) { + float ang = glm::acos(glm::dot(r.direction, normal) / (glm::length(r.direction) * glm::length(normal))); + outside = glm::degrees(ang) > 90; + if (!outside) { + normal = -normal; + } + } + + return minT == FLT_MAX ? -1.0 : minT; +} diff --git a/src/main.cpp b/src/main.cpp index fe8e85ec..bd0e3cbb 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,10 +26,6 @@ int iteration; int width; int height; -//------------------------------- -//-------------MAIN-------------- -//------------------------------- - int main(int argc, char** argv) { startTimeString = currentTimeString(); @@ -182,7 +178,7 @@ void mousePositionCallback(GLFWwindow* window, double xpos, double ypos) { camchanged = true; } else if (rightMousePressed) { - zoom += (ypos - lastY) / height; + zoom += (ypos - lastY) / height * 4; zoom = std::fmax(0.1f, zoom); camchanged = true; } diff --git a/src/pathtrace.cu b/src/pathtrace.cu index 056e1467..c2cb26c4 100644 --- a/src/pathtrace.cu +++ b/src/pathtrace.cu @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #include "sceneStructs.h" #include "scene.h" @@ -13,6 +16,7 @@ #include "pathtrace.h" #include "intersections.h" #include "interactions.h" +#include #define ERRORCHECK 1 @@ -41,12 +45,12 @@ void checkCUDAErrorFn(const char *msg, const char *file, int line) { __host__ __device__ thrust::default_random_engine makeSeededRandomEngine(int iter, int index, int depth) { int h = utilhash((1 << 31) | (depth << 22) | iter) ^ utilhash(index); + return thrust::default_random_engine(h); } //Kernel that writes the image to the OpenGL PBO directly. -__global__ void sendImageToPBO(uchar4* pbo, glm::ivec2 resolution, - int iter, glm::vec3* image) { +__global__ void sendImageToPBO(uchar4* pbo, glm::ivec2 resolution, int iter, glm::vec3* image) { int x = (blockIdx.x * blockDim.x) + threadIdx.x; int y = (blockIdx.y * blockDim.y) + threadIdx.y; @@ -67,46 +71,175 @@ __global__ void sendImageToPBO(uchar4* pbo, glm::ivec2 resolution, } } -static Scene * hst_scene = NULL; -static glm::vec3 * dev_image = NULL; -static Geom * dev_geoms = NULL; -static Material * dev_materials = NULL; -static PathSegment * dev_paths = NULL; -static ShadeableIntersection * dev_intersections = NULL; -// TODO: static variables for device memory, any extra info you need, etc -// ... +static Scene* hst_scene = NULL; +static glm::vec3* device_image = NULL; +static Geom* device_geoms = NULL; +static Material* device_materials = NULL; +static PathSegment* device_paths = NULL; +static ShadeableIntersection* device_intersections = NULL; +static ShadeableIntersection* device_intersections_first_bounce = NULL; + +const bool STREAM_COMPACTION = true; +const bool SORT_MATERIALS = false; +const bool CACHE_FIRST_BOUNCE = true; +static bool cached = false; + +const int DEPTH = 3; + +std::vector iterationTimes; +std::vector computeIntersectionsTimes; +std::vector shadeFakeMaterialTimes; +std::vector sortMaterialsTimes; +std::vector streamCompactionTimes; +std::vector numPaths; + +std::vector firstComputeIntersectionsTimes; +std::vector cacheWriteTimes; +std::vector cacheReadTimes; + +chrono::steady_clock::time_point iterationStart; +chrono::steady_clock::time_point iterationEnd; + +chrono::steady_clock::time_point computeIntersectionsStart; +chrono::steady_clock::time_point computeIntersectionsEnd; + +chrono::steady_clock::time_point shadeFakeMaterialStart; +chrono::steady_clock::time_point shadeFakeMaterialEnd; + +chrono::steady_clock::time_point sortMaterialsStart; +chrono::steady_clock::time_point sortMaterialsEnd; + +chrono::steady_clock::time_point streamCompactionStart; +chrono::steady_clock::time_point streamCompactionEnd; + +chrono::steady_clock::time_point firstComputeIntersectionsStart; +chrono::steady_clock::time_point firstComputeIntersectionsEnd; + +chrono::steady_clock::time_point cacheWriteStart; +chrono::steady_clock::time_point cacheWriteEnd; + +chrono::steady_clock::time_point cacheReadStart; +chrono::steady_clock::time_point cacheReadEnd; + +struct NoRemainingBounces { + __host__ __device__ + bool operator()(const PathSegment& pathSegment) { + return pathSegment.remainingBounces; + } +}; + +struct MaterialSort { + __host__ __device__ + bool operator()(const ShadeableIntersection& i1, const ShadeableIntersection& i2) { + return i1.materialId > i2.materialId; + } +}; + +void allocateTriangleMemory(Geom& geom) { + int bytes; + int startIndex = (pow(8, DEPTH) - 1) / 7; + int endIndex = (pow(8, DEPTH + 1) - 1) / 7; + + for (int i = startIndex; i < endIndex; i++) { + if ((*geom.host_tree)[i].numTriangles) { + cudaMalloc( + &(*geom.host_tree)[i].device_triangles, + (*geom.host_tree)[i].host_triangles->size() * sizeof(Triangle)); + cudaMemcpy( + (*geom.host_tree)[i].device_triangles, + (*geom.host_tree)[i].host_triangles->data(), + (*geom.host_tree)[i].host_triangles->size() * sizeof(Triangle), + cudaMemcpyHostToDevice); + + bytes += (*geom.host_tree)[i].host_triangles->size() * sizeof(Triangle); + } + } + + std::cout << "Allocated triangle memory\t(" << bytes << " bytes)" << std::endl; +} + +void allocateTreeMemory(Geom& geom) { + cudaMalloc(&geom.device_tree, geom.host_tree->size() * sizeof(Node)); + cudaMemcpy(geom.device_tree, geom.host_tree->data(), geom.host_tree->size() * sizeof(Node), cudaMemcpyHostToDevice); + + std::cout << "Allocated tree memory\t\t(" << geom.host_tree->size() * sizeof(Node) << " bytes)" << std::endl; + +} void pathtraceInit(Scene *scene) { + + //// https://www.cs.cmu.edu/afs/cs/academic/class/15668-s11/www/cuda-doc/html/group__CUDART__THREAD_gfd87d16d2bbf4bc41a892f3f75bac5e0.html#gfd87d16d2bbf4bc41a892f3f75bac5e0 + //size_t lim; + //cudaThreadGetLimit(&lim, cudaLimitMallocHeapSize); + //std::cout << "OLD MallocHeapSize " << lim << std::endl; + + //size_t new_lim = 74299064 * 4; + //cudaThreadSetLimit(cudaLimitMallocHeapSize, new_lim); + //cudaThreadGetLimit(&lim, cudaLimitMallocHeapSize); + //std::cout << "NEW MallocHeapSize " << lim << std::endl; + + //cudaThreadGetLimit(&lim, cudaLimitStackSize); + //std::cout << "OLD StackSize " << lim << std::endl; + + //new_lim = 1024 * 64; + //cudaThreadSetLimit(cudaLimitStackSize, new_lim); + //cudaThreadGetLimit(&lim, cudaLimitStackSize); + //std::cout << "NEW StackSize " << lim << std::endl; + hst_scene = scene; const Camera &cam = hst_scene->state.camera; const int pixelcount = cam.resolution.x * cam.resolution.y; - cudaMalloc(&dev_image, pixelcount * sizeof(glm::vec3)); - cudaMemset(dev_image, 0, pixelcount * sizeof(glm::vec3)); + // For each pixel, store a + // - vec3 (the color) + // - ray (PathSegment) + // - intersection (ShadeableIntersection) + cudaMalloc(&device_image, pixelcount * sizeof(glm::vec3)); + cudaMemset(device_image, 0, pixelcount * sizeof(glm::vec3)); - cudaMalloc(&dev_paths, pixelcount * sizeof(PathSegment)); + cudaMalloc(&device_paths, pixelcount * sizeof(PathSegment)); - cudaMalloc(&dev_geoms, scene->geoms.size() * sizeof(Geom)); - cudaMemcpy(dev_geoms, scene->geoms.data(), scene->geoms.size() * sizeof(Geom), cudaMemcpyHostToDevice); + cudaMalloc(&device_intersections, pixelcount * sizeof(ShadeableIntersection)); + cudaMemset(device_intersections, 0, pixelcount * sizeof(ShadeableIntersection)); - cudaMalloc(&dev_materials, scene->materials.size() * sizeof(Material)); - cudaMemcpy(dev_materials, scene->materials.data(), scene->materials.size() * sizeof(Material), cudaMemcpyHostToDevice); + if (CACHE_FIRST_BOUNCE) { + cudaMalloc(&device_intersections_first_bounce, pixelcount * sizeof(ShadeableIntersection)); + } + + for (int i = 0; i < scene->geoms.size(); i++) { + if (scene->geoms[i].type == BB) { + //cudaMalloc( + // &scene->geoms[i].device_triangles, + // scene->geoms[i].host_triangles->size() * sizeof(Triangle)); + //cudaMemcpy( + // scene->geoms[i].device_triangles, + // scene->geoms[i].host_triangles->data(), + // scene->geoms[i].host_triangles->size() * sizeof(Triangle), + // cudaMemcpyHostToDevice); + + allocateTriangleMemory(scene->geoms[i]); + allocateTreeMemory(scene->geoms[i]); + } + } - cudaMalloc(&dev_intersections, pixelcount * sizeof(ShadeableIntersection)); - cudaMemset(dev_intersections, 0, pixelcount * sizeof(ShadeableIntersection)); + // Also copy over geometries (Geom) and materials (Material) + cudaMalloc(&device_geoms, scene->geoms.size() * sizeof(Geom)); + cudaMemcpy(device_geoms, scene->geoms.data(), scene->geoms.size() * sizeof(Geom), cudaMemcpyHostToDevice); - // TODO: initialize any extra device memeory you need + cudaMalloc(&device_materials, scene->materials.size() * sizeof(Material)); + cudaMemcpy(device_materials, scene->materials.data(), scene->materials.size() * sizeof(Material), cudaMemcpyHostToDevice); + cached = false; checkCUDAError("pathtraceInit"); } void pathtraceFree() { - cudaFree(dev_image); // no-op if dev_image is null - cudaFree(dev_paths); - cudaFree(dev_geoms); - cudaFree(dev_materials); - cudaFree(dev_intersections); - // TODO: clean up any extra device memory you created + cudaFree(device_image); // no-op if device_image is null + cudaFree(device_paths); + cudaFree(device_geoms); + cudaFree(device_materials); + cudaFree(device_intersections); + cudaFree(device_intersections_first_bounce); checkCUDAError("pathtraceFree"); } @@ -119,78 +252,65 @@ void pathtraceFree() { * motion blur - jitter rays "in time" * lens effect - jitter ray origin positions based on a lens */ -__global__ void generateRayFromCamera(Camera cam, int iter, int traceDepth, PathSegment* pathSegments) -{ +__global__ void generateRaysFromCamera(Camera camera, int iter, int traceDepth, PathSegment* pathSegments) { int x = (blockIdx.x * blockDim.x) + threadIdx.x; int y = (blockIdx.y * blockDim.y) + threadIdx.y; - if (x < cam.resolution.x && y < cam.resolution.y) { - int index = x + (y * cam.resolution.x); - PathSegment & segment = pathSegments[index]; + if (x < camera.resolution.x && y < camera.resolution.y) { + int index = x + (y * camera.resolution.x); + // consider using shared memory here, since all of these operations are on global memory + PathSegment& segment = pathSegments[index]; - segment.ray.origin = cam.position; - segment.color = glm::vec3(1.0f, 1.0f, 1.0f); + segment.ray.origin = camera.position; + segment.color = glm::vec3(1.0f, 1.0f, 1.0f); // TODO: implement antialiasing by jittering the ray - segment.ray.direction = glm::normalize(cam.view - - cam.right * cam.pixelLength.x * ((float)x - (float)cam.resolution.x * 0.5f) - - cam.up * cam.pixelLength.y * ((float)y - (float)cam.resolution.y * 0.5f) - ); + segment.ray.direction = glm::normalize(camera.view + - camera.right * camera.pixelLength.x * ((float)x - (float)camera.resolution.x * 0.5f) + - camera.up * camera.pixelLength.y * ((float)y - (float)camera.resolution.y * 0.5f)); segment.pixelIndex = index; segment.remainingBounces = traceDepth; } } -// TODO: -// computeIntersections handles generating ray intersections ONLY. -// Generating new rays is handled in your shader(s). -// Feel free to modify the code below. __global__ void computeIntersections( - int depth - , int num_paths - , PathSegment * pathSegments - , Geom * geoms - , int geoms_size - , ShadeableIntersection * intersections - ) -{ + int depth, int num_paths, + PathSegment* pathSegments, + Geom* geoms, int geoms_size, + ShadeableIntersection* intersections +) { int path_index = blockIdx.x * blockDim.x + threadIdx.x; - if (path_index < num_paths) - { + if (path_index < num_paths && pathSegments[path_index].remainingBounces) { PathSegment pathSegment = pathSegments[path_index]; float t; - glm::vec3 intersect_point; - glm::vec3 normal; float t_min = FLT_MAX; - int hit_geom_index = -1; - bool outside = true; + glm::vec3 intersect_point; + glm::vec3 normal; glm::vec3 tmp_intersect; glm::vec3 tmp_normal; + + int hit_geom_index = -1; + bool outside; // naive parse through global geoms + for (int i = 0; i < geoms_size; i++) { + Geom& geom = geoms[i]; - for (int i = 0; i < geoms_size; i++) - { - Geom & geom = geoms[i]; - - if (geom.type == CUBE) - { + if (geom.type == CUBE) { t = boxIntersectionTest(geom, pathSegment.ray, tmp_intersect, tmp_normal, outside); - } - else if (geom.type == SPHERE) - { + } else if (geom.type == SPHERE) { t = sphereIntersectionTest(geom, pathSegment.ray, tmp_intersect, tmp_normal, outside); + } else if (geom.type == BB) { + t = octreeIntersectionTest(geom, pathSegment.ray, tmp_intersect, tmp_normal, outside); } - // TODO: add more intersection tests here... triangle? metaball? CSG? - // Compute the minimum t from the intersection tests to determine what + // Compute the minimum t from the intersection tests to determine which // scene geometry object was hit first. - if (t > 0.0f && t_min > t) - { + if (t > 0.0f && t_min > t) { t_min = t; hit_geom_index = i; intersect_point = tmp_intersect; @@ -198,13 +318,12 @@ __global__ void computeIntersections( } } - if (hit_geom_index == -1) - { + if (hit_geom_index == -1) { intersections[path_index].t = -1.0f; - } - else - { + } else { //The ray hits something + intersections[path_index].outside = outside; + intersections[path_index].intersectionPt = intersect_point; intersections[path_index].t = t_min; intersections[path_index].materialId = geoms[hit_geom_index].materialid; intersections[path_index].surfaceNormal = normal; @@ -222,172 +341,275 @@ __global__ void computeIntersections( // Your shaders should handle that - this can allow techniques such as // bump mapping. __global__ void shadeFakeMaterial ( - int iter - , int num_paths - , ShadeableIntersection * shadeableIntersections - , PathSegment * pathSegments - , Material * materials - ) -{ - int idx = blockIdx.x * blockDim.x + threadIdx.x; - if (idx < num_paths) - { - ShadeableIntersection intersection = shadeableIntersections[idx]; - if (intersection.t > 0.0f) { // if the intersection exists... - // Set up the RNG - // LOOK: this is how you use thrust's RNG! Please look at - // makeSeededRandomEngine as well. - thrust::default_random_engine rng = makeSeededRandomEngine(iter, idx, 0); - thrust::uniform_real_distribution u01(0, 1); - - Material material = materials[intersection.materialId]; - glm::vec3 materialColor = material.color; - - // If the material indicates that the object was a light, "light" the ray - if (material.emittance > 0.0f) { - pathSegments[idx].color *= (materialColor * material.emittance); - } - // Otherwise, do some pseudo-lighting computation. This is actually more - // like what you would expect from shading in a rasterizer like OpenGL. - // TODO: replace this! you should be able to start with basically a one-liner - else { - float lightTerm = glm::dot(intersection.surfaceNormal, glm::vec3(0.0f, 1.0f, 0.0f)); - pathSegments[idx].color *= (materialColor * lightTerm) * 0.3f + ((1.0f - intersection.t * 0.02f) * materialColor) * 0.7f; - pathSegments[idx].color *= u01(rng); // apply some noise because why not - } - // If there was no intersection, color the ray black. - // Lots of renderers use 4 channel color, RGBA, where A = alpha, often - // used for opacity, in which case they can indicate "no opacity". - // This can be useful for post-processing and image compositing. - } else { - pathSegments[idx].color = glm::vec3(0.0f); + int iter, int num_paths, + ShadeableIntersection* shadeableIntersections, + PathSegment* pathSegments, + Material* materials, + int currentDepth +) { + int idx = blockIdx.x * blockDim.x + threadIdx.x; + + if (idx < num_paths && pathSegments[idx].remainingBounces) { + ShadeableIntersection intersection = shadeableIntersections[idx]; + + if (intersection.t > 0.0f) { + thrust::default_random_engine rng = makeSeededRandomEngine(iter, idx, currentDepth); + Material material = materials[intersection.materialId]; + float gap = material.hasRefractive ? 0.0001f : -0.0001f; + + scatterRay( + pathSegments[idx], + getPointOnRay(pathSegments[idx].ray, intersection.t, gap), + intersection.surfaceNormal, + material, rng, + shadeableIntersections[idx].outside); + } else { + pathSegments[idx].color = glm::vec3(0.0f); + pathSegments[idx].remainingBounces = 0; + } } - } } // Add the current iteration's output to the overall image -__global__ void finalGather(int nPaths, glm::vec3 * image, PathSegment * iterationPaths) -{ +__global__ void finalGather(int nPaths, glm::vec3 * image, PathSegment * iterationPaths) { int index = (blockIdx.x * blockDim.x) + threadIdx.x; - if (index < nPaths) - { + if (index < nPaths) { PathSegment iterationPath = iterationPaths[index]; image[iterationPath.pixelIndex] += iterationPath.color; } } -/** - * Wrapper for the __global__ call that sets up the kernel calls and does a ton - * of memory management - */ +// Wrapper for the __global__ call +// Sets up the kernel calls and does memory management void pathtrace(uchar4 *pbo, int frame, int iter) { - const int traceDepth = hst_scene->state.traceDepth; - const Camera &cam = hst_scene->state.camera; - const int pixelcount = cam.resolution.x * cam.resolution.y; + const int maxTraceDepth = hst_scene->state.traceDepth; + const Camera &camera = hst_scene->state.camera; + const int pixelcount = camera.resolution.x * camera.resolution.y; + + const dim3 blockSizeRayGen(8, 8); + const dim3 gridSizeRayGen( + (camera.resolution.x + blockSizeRayGen.x - 1) / blockSizeRayGen.x, + (camera.resolution.y + blockSizeRayGen.y - 1) / blockSizeRayGen.y); + + const int blockSizeCompute = 128; + + // INITIALIZE device_paths (PathSegment structs) + generateRaysFromCamera <<>>(camera, iter, maxTraceDepth, device_paths); + checkCUDAError("generate camera rays"); + + int currentTraceDepth = 0; + PathSegment* device_path_end = device_paths + pixelcount; + int num_paths = device_path_end - device_paths; + + bool iterationComplete = false; - // 2D block for generating ray from camera - const dim3 blockSize2d(8, 8); - const dim3 blocksPerGrid2d( - (cam.resolution.x + blockSize2d.x - 1) / blockSize2d.x, - (cam.resolution.y + blockSize2d.y - 1) / blockSize2d.y); - - // 1D block for path tracing - const int blockSize1d = 128; - - /////////////////////////////////////////////////////////////////////////// - - // Recap: - // * Initialize array of path rays (using rays that come out of the camera) - // * You can pass the Camera object to that kernel. - // * Each path ray must carry at minimum a (ray, color) pair, - // * where color starts as the multiplicative identity, white = (1, 1, 1). - // * This has already been done for you. - // * For each depth: - // * Compute an intersection in the scene for each path ray. - // A very naive version of this has been implemented for you, but feel - // free to add more primitives and/or a better algorithm. - // Currently, intersection distance is recorded as a parametric distance, - // t, or a "distance along the ray." t = -1.0 indicates no intersection. - // * Color is attenuated (multiplied) by reflections off of any object - // * TODO: Stream compact away all of the terminated paths. - // You may use either your implementation or `thrust::remove_if` or its - // cousins. - // * Note that you can't really use a 2D kernel launch any more - switch - // to 1D. - // * TODO: Shade the rays that intersected something or didn't bottom out. - // That is, color the ray by performing a color computation according - // to the shader, then generate a new ray to continue the ray path. - // We recommend just updating the ray's PathSegment in place. - // Note that this step may come before or after stream compaction, - // since some shaders you write may also cause a path to terminate. - // * Finally, add this iteration's results to the image. This has been done - // for you. - - // TODO: perform one iteration of path tracing - - generateRayFromCamera <<>>(cam, iter, traceDepth, dev_paths); - checkCUDAError("generate camera ray"); - - int depth = 0; - PathSegment* dev_path_end = dev_paths + pixelcount; - int num_paths = dev_path_end - dev_paths; - - // --- PathSegment Tracing Stage --- - // Shoot ray into scene, bounce between objects, push shading chunks - - bool iterationComplete = false; while (!iterationComplete) { + if (iter >= 30 && iter < 50) { + if (iter == 30) { + numPaths.push_back(num_paths); + } else { + numPaths[currentTraceDepth] += num_paths; + } + } - // clean shading chunks - cudaMemset(dev_intersections, 0, pixelcount * sizeof(ShadeableIntersection)); - - // tracing - dim3 numblocksPathSegmentTracing = (num_paths + blockSize1d - 1) / blockSize1d; - computeIntersections <<>> ( - depth - , num_paths - , dev_paths - , dev_geoms - , hst_scene->geoms.size() - , dev_intersections - ); - checkCUDAError("trace one bounce"); - cudaDeviceSynchronize(); - depth++; - - - // TODO: - // --- Shading Stage --- - // Shade path segments based on intersections and generate new rays by - // evaluating the BSDF. - // Start off with just a big kernel that handles all the different - // materials you have in the scenefile. - // TODO: compare between directly shading the path segments and shading - // path segments that have been reshuffled to be contiguous in memory. - - shadeFakeMaterial<<>> ( - iter, - num_paths, - dev_intersections, - dev_paths, - dev_materials - ); - iterationComplete = true; // TODO: should be based off stream compaction results. + iterationStart = chrono::high_resolution_clock::now(); + // RESET device_intersections (ShadeableIntersection structs) + cudaMemset(device_intersections, 0, pixelcount * sizeof(ShadeableIntersection)); + + dim3 gridSizePathSegmentTracing = (num_paths + blockSizeCompute - 1) / blockSizeCompute; + + computeIntersectionsStart = chrono::high_resolution_clock::now(); + if (currentTraceDepth == 0 && CACHE_FIRST_BOUNCE) { + if (!cached) { + cached = true; + // POPULATE device_intersections (ShadeableIntersection structs) + firstComputeIntersectionsStart = chrono::high_resolution_clock::now(); + computeIntersections << > > ( + currentTraceDepth, num_paths, device_paths, device_geoms, hst_scene->geoms.size(), device_intersections); + checkCUDAError("Error with computeIntersections()"); + cudaDeviceSynchronize(); + firstComputeIntersectionsEnd = chrono::high_resolution_clock::now(); + + // POPULATE device_intersections_first_bounce (cache of ShadeableIntersection structs) + cacheWriteStart = chrono::high_resolution_clock::now(); + cudaMemcpy( + device_intersections_first_bounce, + device_intersections, + pixelcount * sizeof(ShadeableIntersection), + cudaMemcpyDeviceToDevice); + checkCUDAError("Error with populating cache"); + cudaDeviceSynchronize(); + cacheWriteEnd = chrono::high_resolution_clock::now(); + + } else { + // POPULATE device_intersections using cache + cacheReadStart = chrono::high_resolution_clock::now(); + cudaMemcpy( + device_intersections, + device_intersections_first_bounce, + pixelcount * sizeof(ShadeableIntersection), + cudaMemcpyDeviceToDevice); + checkCUDAError("Error with reading from cache"); + cudaDeviceSynchronize(); + cacheReadEnd = chrono::high_resolution_clock::now(); + } + } else { + // POPULATE device_intersections (ShadeableIntersection structs) + computeIntersections << > > ( + currentTraceDepth, num_paths, device_paths, device_geoms, hst_scene->geoms.size(), device_intersections); + checkCUDAError("Error with computeIntersections()"); + cudaDeviceSynchronize(); + } + computeIntersectionsEnd = chrono::high_resolution_clock::now(); + + if (SORT_MATERIALS) { + sortMaterialsStart = chrono::high_resolution_clock::now(); + thrust::stable_sort_by_key( + thrust::device, + device_intersections, + device_intersections + num_paths, + device_paths, + MaterialSort()); + sortMaterialsEnd = chrono::high_resolution_clock::now(); + } + + shadeFakeMaterialStart = chrono::high_resolution_clock::now(); + shadeFakeMaterial<<>> ( + iter, num_paths, device_intersections, device_paths, device_materials, currentTraceDepth); + checkCUDAError("Error with shadeFakeMaterial()"); + cudaDeviceSynchronize(); + shadeFakeMaterialEnd = chrono::high_resolution_clock::now(); + + if (STREAM_COMPACTION) { + streamCompactionStart = chrono::high_resolution_clock::now(); + device_path_end = thrust::stable_partition(thrust::device, device_paths, device_path_end, NoRemainingBounces()); + num_paths = device_path_end - device_paths; + streamCompactionEnd = chrono::high_resolution_clock::now(); + } + + iterationEnd = chrono::high_resolution_clock::now(); + if (iter >= 30 && iter < 50) { + if (iter == 30) { + iterationTimes.push_back(chrono::duration_cast(iterationEnd - iterationStart).count()); + computeIntersectionsTimes.push_back(chrono::duration_cast(computeIntersectionsEnd - computeIntersectionsStart).count()); + shadeFakeMaterialTimes.push_back(chrono::duration_cast(shadeFakeMaterialEnd - shadeFakeMaterialStart).count()); + sortMaterialsTimes.push_back(chrono::duration_cast(sortMaterialsEnd - sortMaterialsStart).count()); + streamCompactionTimes.push_back(chrono::duration_cast(streamCompactionEnd - streamCompactionStart).count()); + } else { + iterationTimes[currentTraceDepth] += chrono::duration_cast(iterationEnd - iterationStart).count(); + computeIntersectionsTimes[currentTraceDepth] += chrono::duration_cast(computeIntersectionsEnd - computeIntersectionsStart).count(); + shadeFakeMaterialTimes[currentTraceDepth] += chrono::duration_cast(shadeFakeMaterialEnd - shadeFakeMaterialStart).count(); + sortMaterialsTimes[currentTraceDepth] += chrono::duration_cast(sortMaterialsEnd - sortMaterialsStart).count(); + streamCompactionTimes[currentTraceDepth] += chrono::duration_cast(streamCompactionEnd - streamCompactionStart).count(); + } + } + + if (iter >= 30 && iter < 50 && currentTraceDepth == 0) { + firstComputeIntersectionsTimes.push_back(chrono::duration_cast(firstComputeIntersectionsEnd - firstComputeIntersectionsStart).count()); + cacheReadTimes.push_back(chrono::duration_cast(cacheReadEnd - cacheReadStart).count()); + cacheWriteTimes.push_back(chrono::duration_cast(cacheWriteEnd - cacheWriteStart).count()); + } + + currentTraceDepth++; + iterationComplete = num_paths == 0 || currentTraceDepth == maxTraceDepth; } - // Assemble this iteration and apply it to the image - dim3 numBlocksPixels = (pixelcount + blockSize1d - 1) / blockSize1d; - finalGather<<>>(num_paths, dev_image, dev_paths); - /////////////////////////////////////////////////////////////////////////// + + if (iter == 51) { + long total; + + std::cout << "firstComputeIntersectons" << std::endl; + for (int i = 0; i < 20; i++) { + std::cout << i << " " << firstComputeIntersectionsTimes[i] << std::endl; + total += firstComputeIntersectionsTimes[i]; + } + std::cout << "TOTAL " << total / 20 << std::endl; + total = 0; + + std::cout << "cacheRead" << std::endl; + for (int i = 0; i < 20; i++) { + std::cout << i << " " << cacheReadTimes[i] << std::endl; + total += cacheReadTimes[i]; + } + std::cout << "TOTAL " << total / 20 << std::endl; + total = 0; + + std::cout << "cacheWrite" << std::endl; + for (int i = 0; i < 20; i++) { + std::cout << i << " " << cacheWriteTimes[i] << std::endl; + total += cacheWriteTimes[i]; + } + std::cout << "TOTAL " << total / 20 << std::endl; + total = 0; + + + std::cout << "numPaths" << std::endl; + for (int i = 0; i < maxTraceDepth; i++) { + std::cout << i << " " << numPaths[i]/20 << std::endl; + total += numPaths[i]; + } + std::cout << "TOTAL " << total / 20 << std::endl; + total = 0; + + + std::cout << "iteration times" << std::endl; + for (int i = 0; i < maxTraceDepth; i++) { + std::cout << i << " " << iterationTimes[i] / 20 << std::endl; + total += iterationTimes[i]; + } + std::cout << "TOTAL " << total / 20 << std::endl; + total = 0; + + + std::cout << "computeIntersections times" << std::endl; + for (int i = 0; i < maxTraceDepth; i++) { + std::cout << i << " " << computeIntersectionsTimes[i] / 20 << std::endl; + total += computeIntersectionsTimes[i]; + } + std::cout << "TOTAL " << total / 20 << std::endl; + total = 0; + + + std::cout << "shadeFakeMaterial times" << std::endl; + for (int i = 0; i < maxTraceDepth; i++) { + std::cout << i << " " << shadeFakeMaterialTimes[i] / 20 << std::endl; + total += shadeFakeMaterialTimes[i]; + } + std::cout << "TOTAL " << total / 20 << std::endl; + total = 0; + + + std::cout << "sortMaterials times" << std::endl; + for (int i = 0; i < maxTraceDepth; i++) { + std::cout << i << " " << sortMaterialsTimes[i] / 20 << std::endl; + total += sortMaterialsTimes[i]; + } + std::cout << "TOTAL " << total / 20 << std::endl; + total = 0; + + + std::cout << "streamCompaction times" << std::endl; + for (int i = 0; i < maxTraceDepth; i++) { + std::cout << i << " " << streamCompactionTimes[i] / 20 << std::endl; + total += streamCompactionTimes[i]; + } + std::cout << "TOTAL " << total / 20 << std::endl; + total = 0; + } + + // Assemble this iteration and apply it to the image + dim3 gridSizePixels = (pixelcount + blockSizeCompute - 1) / blockSizeCompute; + finalGather<<>>(pixelcount, device_image, device_paths); + checkCUDAError("Error with finalGather()"); + cudaDeviceSynchronize(); // Send results to OpenGL buffer for rendering - sendImageToPBO<<>>(pbo, cam.resolution, iter, dev_image); + sendImageToPBO<<>>(pbo, camera.resolution, iter, device_image); // Retrieve image from GPU - cudaMemcpy(hst_scene->state.image.data(), dev_image, - pixelcount * sizeof(glm::vec3), cudaMemcpyDeviceToHost); + cudaMemcpy(hst_scene->state.image.data(), device_image, pixelcount * sizeof(glm::vec3), cudaMemcpyDeviceToHost); checkCUDAError("pathtrace"); } diff --git a/src/scene.cpp b/src/scene.cpp index 3fb6239a..94bb2d2b 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -3,6 +3,15 @@ #include #include #include +#include + + +#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc +// Optional. define TINYOBJLOADER_USE_MAPBOX_EARCUT gives robust trinagulation. Requires C++11 +//#define TINYOBJLOADER_USE_MAPBOX_EARCUT +#include "tiny_obj_loader.h" + +const int DEPTH = 3; Scene::Scene(string filename) { cout << "Reading scene from " << filename << " ..." << endl; @@ -32,7 +41,331 @@ Scene::Scene(string filename) { } } +int countTriangles(const std::vector shapes) { + int numTriangles = 0; + + for (size_t s = 0; s < shapes.size(); s++) { + numTriangles += shapes[s].mesh.num_face_vertices.size(); + //std::cout << "shape " << s << " has " << shapes[s].mesh.num_face_vertices.size() << " triangles" << std::endl; + } + + return numTriangles; +} + +void buildTriangles(std::string filename, Geom& geom) { + tinyobj::ObjReaderConfig reader_config; + reader_config.mtl_search_path = "./"; // Path to material files + + tinyobj::ObjReader reader; + + if (!reader.ParseFromFile(filename, reader_config)) { + if (!reader.Error().empty()) { + std::cerr << "TinyObjReader: " << reader.Error(); + } + exit(1); + } + + //if (!reader.Warning().empty()) { + // std::cout << "TinyObjReader: " << reader.Warning(); + //} + + auto& attrib = reader.GetAttrib(); + auto& shapes = reader.GetShapes(); + auto& materials = reader.GetMaterials(); + + + geom.maxDims = glm::vec3(-FLT_MAX, -FLT_MAX, -FLT_MAX); + geom.minDims = glm::vec3(FLT_MAX, FLT_MAX, FLT_MAX); + geom.host_triangles = new std::vector; + + // Loop over shapes + for (size_t s = 0; s < shapes.size(); s++) { + // Loop over faces(polygon/triangles) + size_t index_offset = 0; + for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++) { + Triangle t; + size_t fv = size_t(shapes[s].mesh.num_face_vertices[f]); + + // Loop over vertices in the face. + for (size_t v = 0; v < fv; v++) { + // access to vertex + tinyobj::index_t idx = shapes[s].mesh.indices[index_offset + v]; + tinyobj::real_t vx = attrib.vertices[3 * size_t(idx.vertex_index) + 0]; + tinyobj::real_t vy = attrib.vertices[3 * size_t(idx.vertex_index) + 1]; + tinyobj::real_t vz = attrib.vertices[3 * size_t(idx.vertex_index) + 2]; + + glm::vec3 vertex(vx, vy, vz); + vertex = glm::vec3(geom.transform * glm::vec4(vertex, 1.f)); + + for (int i = 0; i < 3; i++) { + if (vertex[i] < geom.minDims[i]) { + geom.minDims[i] = vertex[i]; + } + + if (geom.maxDims[i] < vertex[i]) { + geom.maxDims[i] = vertex[i]; + } + } + + t.points[v] = vertex; + + // Check if `normal_index` is zero or positive. negative = no normal data + if (idx.normal_index >= 0) { + tinyobj::real_t nx = attrib.normals[3 * size_t(idx.normal_index) + 0]; + tinyobj::real_t ny = attrib.normals[3 * size_t(idx.normal_index) + 1]; + tinyobj::real_t nz = attrib.normals[3 * size_t(idx.normal_index) + 2]; + + // don't need to do this thrice + t.normal = glm::vec3(nx, ny, nz); + } + + // Check if `texcoord_index` is zero or positive. negative = no texcoord data + if (idx.texcoord_index >= 0) { + tinyobj::real_t tx = attrib.texcoords[2 * size_t(idx.texcoord_index) + 0]; + tinyobj::real_t ty = attrib.texcoords[2 * size_t(idx.texcoord_index) + 1]; + } + + // Optional: vertex colors + // tinyobj::real_t red = attrib.colors[3*size_t(idx.vertex_index)+0]; + // tinyobj::real_t green = attrib.colors[3*size_t(idx.vertex_index)+1]; + // tinyobj::real_t blue = attrib.colors[3*size_t(idx.vertex_index)+2]; + t.color = glm::vec3(0.0, 1.0, 0.0); + } + index_offset += fv; + // per-face material + shapes[s].mesh.material_ids[f]; + geom.host_triangles->push_back(t); + } + } + + geom.numTriangles = geom.host_triangles->size(); //countTriangles(shapes); + + geom.maxDims += 0.1; + geom.minDims -= 0.1; + std::cout << glm::to_string(geom.minDims) << " " << glm::to_string(geom.maxDims) << std::endl; +} + +void Node::computeChildLocations(glm::vec3* childLocations) { + int i = 0; + + for (int x = 0; x < 2; x++) { + for (int y = 0; y < 2; y++) { + for (int z = 0; z < 2; z++) { + float childX = this->position.x + (x ? this->dimension.x / 2.f : 0); + float childY = this->position.y + (y ? this->dimension.y / 2.f : 0); + float childZ = this->position.z + (z ? this->dimension.z / 2.f : 0); + + childLocations[i++] = glm::vec3(childX, childY, childZ); + } + } + } +} + +// https://github.com/juj/MathGeoLib/blob/master/src/Geometry/Triangle.cpp#L697 +bool Node::triangleIntersectionTest(Triangle& triangle) { + glm::vec3 a = triangle.points[0]; + glm::vec3 b = triangle.points[1]; + glm::vec3 c = triangle.points[2]; + + glm::vec3 tMin = glm::min(a, glm::min(b, c)); + glm::vec3 tMax = glm::max(a, glm::max(b, c)); + + glm::vec3 maxPoint = this->position + this->dimension; + glm::vec3 minPoint = this->position; + if (tMin.x >= maxPoint.x || tMax.x <= minPoint.x + || tMin.y >= maxPoint.y || tMax.y <= minPoint.y + || tMin.z >= maxPoint.z || tMax.z <= minPoint.z) + return false; + + glm::vec3 center = (minPoint + maxPoint) * 0.5f; + glm::vec3 h = maxPoint - center; + + const glm::vec3 t[3] = { b - a, c - a, c - b }; + + glm::vec3 ac = a - center; + + glm::vec3 n = glm::cross(t[0], t[1]); + float s = glm::dot(n, ac); + float r = glm::abs(glm::dot(h, glm::abs(n))); // Abs(h.Dot(n.Abs())); + if (glm::abs(s) >= r) + return false; + + const glm::vec3 at[3] = { glm::abs(t[0]), glm::abs(t[1]), glm::abs(t[2]) }; + + glm::vec3 bc = b - center; + glm::vec3 cc = c - center; + + // SAT test all cross-axes. + // The following is a fully unrolled loop of this code, stored here for reference: + + // eX t[0] + float d1 = t[0].y * ac.z - t[0].z * ac.y; + float d2 = t[0].y * cc.z - t[0].z * cc.y; + float tc = (d1 + d2) * 0.5f; + r = glm::abs(h.y * at[0].z + h.z * at[0].y); + if (r + glm::abs(tc - d1) < glm::abs(tc)) + return false; + + // eX t[1] + d1 = t[1].y * ac.z - t[1].z * ac.y; + d2 = t[1].y * bc.z - t[1].z * bc.y; + tc = (d1 + d2) * 0.5f; + r = glm::abs(h.y * at[1].z + h.z * at[1].y); + if (r + glm::abs(tc - d1) < glm::abs(tc)) + return false; + + // eX t[2] + d1 = t[2].y * ac.z - t[2].z * ac.y; + d2 = t[2].y * bc.z - t[2].z * bc.y; + tc = (d1 + d2) * 0.5f; + r = glm::abs(h.y * at[2].z + h.z * at[2].y); + if (r + glm::abs(tc - d1) < glm::abs(tc)) + return false; + + // eY t[0] + d1 = t[0].z * ac.x - t[0].x * ac.z; + d2 = t[0].z * cc.x - t[0].x * cc.z; + tc = (d1 + d2) * 0.5f; + r = glm::abs(h.x * at[0].z + h.z * at[0].x); + if (r + glm::abs(tc - d1) < glm::abs(tc)) + return false; + + // eY t[1] + d1 = t[1].z * ac.x - t[1].x * ac.z; + d2 = t[1].z * bc.x - t[1].x * bc.z; + tc = (d1 + d2) * 0.5f; + r = glm::abs(h.x * at[1].z + h.z * at[1].x); + if (r + glm::abs(tc - d1) < glm::abs(tc)) + return false; + + // eY t[2] + d1 = t[2].z * ac.x - t[2].x * ac.z; + d2 = t[2].z * bc.x - t[2].x * bc.z; + tc = (d1 + d2) * 0.5f; + r = glm::abs(h.x * at[2].z + h.z * at[2].x); + if (r + glm::abs(tc - d1) < glm::abs(tc)) + return false; + + // eZ t[0] + d1 = t[0].x * ac.y - t[0].y * ac.x; + d2 = t[0].x * cc.y - t[0].y * cc.x; + tc = (d1 + d2) * 0.5f; + r = glm::abs(h.y * at[0].x + h.x * at[0].y); + if (r + glm::abs(tc - d1) < glm::abs(tc)) + return false; + + // eZ t[1] + d1 = t[1].x * ac.y - t[1].y * ac.x; + d2 = t[1].x * bc.y - t[1].y * bc.x; + tc = (d1 + d2) * 0.5f; + r = glm::abs(h.y * at[1].x + h.x * at[1].y); + if (r + glm::abs(tc - d1) < glm::abs(tc)) + return false; + + // eZ t[2] + d1 = t[2].x * ac.y - t[2].y * ac.x; + d2 = t[2].x * bc.y - t[2].y * bc.x; + tc = (d1 + d2) * 0.5f; + r = glm::abs(h.y * at[2].x + h.x * at[2].y); + if (r + glm::abs(tc - d1) < glm::abs(tc)) + return false; + + // No separating axis exists, the AABB and triangle intersect. + return true; +} + +bool Node::contains(Triangle& triangle) { + for (int i = 0; i < 3; i++) { + glm::vec3 p = triangle.points[i]; + + bool greaterThanMin = this->position.x <= p.x && this->position.y <= p.y && this->position.z <= p.z; + bool lessThanMax = p.x <= this->position.x + this->dimension.x && p.y <= this->position.y + this->dimension.y && p.z <= this->position.z + this->dimension.z; + + if (greaterThanMin && lessThanMax) { + return true; + } + } + + return false; +} + +void initializeTree(Geom& geom) { + std::cout << "Building octree of size " << DEPTH << std::endl; + + Node root(geom.minDims, geom.maxDims - geom.minDims, 1, DEPTH == 0); + glm::vec3 scale = root.dimension; + glm::vec3 translation = root.position + scale / 2.f; + glm::vec3 rotation(0, 0, 0); + root.transform = utilityCore::buildTransformationMatrix(translation, rotation, scale); + root.inverseTransform = glm::inverse(root.transform); + root.invTranspose = glm::inverseTranspose(root.transform); + + std::vector* tree = new std::vector(); + (*tree).push_back(root); + + for (int d = 0; d < DEPTH; d++) { + int startIndex = (pow(8, d) - 1) / 7; + int endIndex = (pow(8, d + 1) - 1) / 7; + bool leaf = d == (DEPTH - 1); + std::cout << "Depth " << d << " from " << startIndex << " to " << endIndex << " and leaf " << leaf << std::endl; + + glm::vec3 newDim = (*tree)[startIndex].dimension / 2.f; + for (int i = startIndex; i < endIndex; i++) { + glm::vec3 childLocations[8]; + (*tree)[i].computeChildLocations(childLocations); + + for (int j = 0; j < 8; j++) { + int childI = leaf ? 0 : ((*tree).size() * 8 + 1); + Node node(childLocations[j], newDim, childI, leaf); + //std::cout << "Created node " << (*tree).size() << " with pos " << glm::to_string(node.position) << " and dim " << glm::to_string(node.dimension) << std::endl; + + glm::vec3 scale = node.dimension; + glm::vec3 translation = node.position + scale / 2.f; + + node.transform = utilityCore::buildTransformationMatrix(translation, rotation, scale); + node.inverseTransform = glm::inverse(node.transform); + node.invTranspose = glm::inverseTranspose(node.transform); + + (*tree).push_back(node); + } + } + } + + geom.host_tree = tree; +} + +void insertTriangle(Geom& geom, int nodeIndex, int triangleIndex) { + if ((*geom.host_tree)[nodeIndex].contains((*geom.host_triangles)[triangleIndex])) { + //if ((*geom.host_tree)[nodeIndex].triangleIntersectionTest((*geom.host_triangles)[triangleIndex])) { + if ((*geom.host_tree)[nodeIndex].leaf) { + (*geom.host_tree)[nodeIndex].host_triangles->push_back((*geom.host_triangles)[triangleIndex]); + (*geom.host_tree)[nodeIndex].numTriangles++; + } else { + for (int i = (*geom.host_tree)[nodeIndex].childrenStartIndex; i < (*geom.host_tree)[nodeIndex].childrenStartIndex + 8; i++) { + insertTriangle(geom, i, triangleIndex); + } + } + } +} + +void insertTriangles(Geom& geom) { + for (int i = 0; i < geom.numTriangles; i++) { + //std::cout << "Inserting triangle " << i << std::endl; + insertTriangle(geom, 0, i); + } +} + +void buildOctree(Geom& geom) { + initializeTree(geom); + insertTriangles(geom); +} + int Scene::loadGeom(string objectid) { + std::cout << "Size of Triangle " << sizeof(Triangle) << std::endl; + std::cout << "Size of Node " << sizeof(Node) << std::endl; + glm::vec3 a(0, 1, 0); + glm::vec3 b(0, 0, -1); + int id = atoi(objectid.c_str()); if (id != geoms.size()) { cout << "ERROR: OBJECT ID does not match expected number of geoms" << endl; @@ -42,6 +375,7 @@ int Scene::loadGeom(string objectid) { Geom newGeom; string line; + std::string filename; //load object type utilityCore::safeGetline(fp_in, line); if (!line.empty() && fp_in.good()) { @@ -52,6 +386,12 @@ int Scene::loadGeom(string objectid) { cout << "Creating new cube..." << endl; newGeom.type = CUBE; } + else { + cout << "Creating new obj..." << endl; + cout << line.c_str() << endl; + newGeom.type = BB; + filename = line.c_str(); + } } //link material @@ -80,10 +420,18 @@ int Scene::loadGeom(string objectid) { } newGeom.transform = utilityCore::buildTransformationMatrix( - newGeom.translation, newGeom.rotation, newGeom.scale); + newGeom.translation, newGeom.rotation, newGeom.scale); newGeom.inverseTransform = glm::inverse(newGeom.transform); newGeom.invTranspose = glm::inverseTranspose(newGeom.transform); + if (newGeom.type == BB) { + //newGeom.scale = newGeom.maxDims - newGeom.minDims; + //newGeom.translation = newGeom.minDims + newGeom.scale / 2.f; + + buildTriangles(filename, newGeom); + buildOctree(newGeom); + } + geoms.push_back(newGeom); return 1; } @@ -186,3 +534,7 @@ int Scene::loadMaterial(string materialid) { return 1; } } + +int Scene::loadObjFile() { + return -1; +} \ No newline at end of file diff --git a/src/scene.h b/src/scene.h index f29a9171..fa444e22 100644 --- a/src/scene.h +++ b/src/scene.h @@ -16,6 +16,7 @@ class Scene { int loadMaterial(string materialid); int loadGeom(string objectid); int loadCamera(); + int loadObjFile(); public: Scene(string filename); ~Scene(); diff --git a/src/sceneStructs.h b/src/sceneStructs.h index da4dbf30..37a11785 100644 --- a/src/sceneStructs.h +++ b/src/sceneStructs.h @@ -4,28 +4,74 @@ #include #include #include "glm/glm.hpp" - #define BACKGROUND_COLOR (glm::vec3(0.0f)) -enum GeomType { - SPHERE, - CUBE, -}; - struct Ray { glm::vec3 origin; glm::vec3 direction; }; +struct Triangle { + glm::vec3 points[3]; + glm::vec3 normal; + glm::vec3 color; +}; + +struct Node { + Node(glm::vec3 pos, glm::vec3 dim, int i, bool leaf) : + position(pos), dimension(dim), childrenStartIndex(i), leaf(leaf), + host_triangles(new std::vector), + device_triangles(NULL), numTriangles(0) {}; + glm::vec3 position; + glm::vec3 dimension; + bool contains(Triangle& triangle); + void computeChildLocations(glm::vec3* childLocations); + bool triangleIntersectionTest(Triangle& triangle); + + // If false, then childrenStartIndex points to first of 8 Nodes in tree array. + // If true, then numTriangles, host_triangles, and device_triangles are populated. + bool leaf; + + int childrenStartIndex; + std::vector* host_triangles; + Triangle* device_triangles; + int numTriangles; + + glm::mat4 transform; + glm::mat4 inverseTransform; + glm::mat4 invTranspose; +}; + +enum GeomType { + SPHERE, + CUBE, + BB +}; + struct Geom { + Geom() : host_tree(new std::vector) {} enum GeomType type; int materialid; + glm::vec3 translation; glm::vec3 rotation; glm::vec3 scale; glm::mat4 transform; glm::mat4 inverseTransform; glm::mat4 invTranspose; + + glm::vec3 minDims; + glm::vec3 maxDims; + + char filename; + + std::vector* host_triangles; + Triangle* device_triangles; + int numTriangles; + + std::vector* host_tree; + Node* device_tree; + int treeSize; }; struct Material { @@ -40,25 +86,6 @@ struct Material { float emittance; }; -struct Camera { - glm::ivec2 resolution; - glm::vec3 position; - glm::vec3 lookAt; - glm::vec3 view; - glm::vec3 up; - glm::vec3 right; - glm::vec2 fov; - glm::vec2 pixelLength; -}; - -struct RenderState { - Camera camera; - unsigned int iterations; - int traceDepth; - std::vector image; - std::string imageName; -}; - struct PathSegment { Ray ray; glm::vec3 color; @@ -71,6 +98,30 @@ struct PathSegment { // 2) BSDF evaluation: generate a new ray struct ShadeableIntersection { float t; + bool outside; glm::vec3 surfaceNormal; + glm::vec3 intersectionPt; int materialId; + __host__ __device__ bool operator < (const ShadeableIntersection& si) const { + return materialId < si.materialId; + } }; + +struct Camera { + glm::ivec2 resolution; + glm::vec3 position; + glm::vec3 lookAt; + glm::vec3 view; + glm::vec3 up; + glm::vec3 right; + glm::vec2 fov; + glm::vec2 pixelLength; +}; + +struct RenderState { + Camera camera; + unsigned int iterations; + int traceDepth; + std::vector image; + std::string imageName; +}; \ No newline at end of file