diff --git a/CMakeLists.txt b/CMakeLists.txt
index c473e2c0..9f51f4d7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -74,6 +74,7 @@ set(headers
src/preview.h
src/utilities.h
src/ImGui/imconfig.h
+ src/tiny_obj_loader.h
src/ImGui/imgui.h
src/ImGui/imconfig.h
diff --git a/README.md b/README.md
index 110697ce..963e8141 100644
--- a/README.md
+++ b/README.md
@@ -3,11 +3,169 @@ CUDA Path Tracer
**University of Pennsylvania, CIS 565: GPU Programming and Architecture, Project 3**
-* (TODO) YOUR NAME HERE
-* Tested on: (TODO) Windows 22, i7-2222 @ 2.22GHz 22GB, GTX 222 222MB (Moore 2222 Lab)
+* Haoquan Liang
+ * [LinkedIn](https://www.linkedin.com/in/leohaoquanliang/)
+* Tested on: Windows 10, Ryzen 7 5800X 8 Core 3.80 GHz, NVIDIA GeForce RTX 3080 Ti 12 GB
-### (TODO: Your README)
+# Overview
+![Overview](img/overview.png)
+This project is a CUDA-based GPU path tracer capable of rendering globally-illuminated images very quickly. Path tracing is a ray tracing algorithm that sends rays from the Camera and, when a ray hits a reflective or refractive surface, recurses the process util it reaches a light source. Fundamentally, the algorithm is integrating over all the illuminance arriving to a single point on the surface of an object to achieve the effect of physically accurate rendering.
-*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.
+# Table of Contents
+[Features](#features)
+[Feature Showcase and Analysis](#showcase)
+[Bloopers](#bloopers)
+[Reference](#reference)
+# Features
+## Core features
+* [Ideal Diffuse surfaces](#diffuse)
+* [Perfectly specular-reflective](#perf-specular)
+* [Imperfect specular-reflective](#imperf-specular)
+* [Path termination with Stream Compaction](#stream_compaction_term)
+* [Sorting rays by materials](#ray_sorting)
+* [Caching the first bounce intersections](#cache)
+## Additional features
+* [Refraction](#refract)
+* [Physically-based depth-of-field](#dof)
+* [Direct lighting](#directlighting)
+* [Motion blur](#motionblur)
+* [Stochastic Sampled Antialiasing](#ssaa)
+* [Arbitrary obj mesh loading](#mesh)
+* [Texture mapping with a basic procedural texture](#texture)
+* [Post-processing shaders (greyscale, sepia, inverted, and high-contrast filters)](#post)
+
+
+# Features Showcase and Analysis
+
+## BSDF Evaluation
+
+### Ideal Diffuse
+
+Diffuse is the most basic BSDF. It samples a cosine-weighted hemisphere on the intersection points, and evaluate the sampled rays from the surface. It gives a rough look on the surface.
+![ideal diffuse](img/diffuse.png)
+
+### Perfectly Specular Reflective
+A perfectly specular surface will reflect all the rays at the angle of incidence. It gives the surface a mirror look.
+![perfectly specular](img/perf-specular.png)
+
+### Imperfect Specular Reflective
+Instead of always reflecting the rays in the angle of incidence, the imperfect specular surface blends the result of perfect specular and perfect diffuse to give it a more natural metalic look. To enable imperfect specular, set `IMPERFECT_SPECULAR` to 1 in `interactions.h`
+![imperfect specular](img/imperf-specular.png)
+
+## Path Termination using Stream Compaction
+The code for path termination using stream compaction is as followed. The idea is to use the ray terminated function to determine which rays have terminated (no longer bounce around), and mark the terminated rays. Then, use the stream compaction algorithm to partition the running rays and the terminated rays, in order to achieve better performance by improving the threads locality.
+Unfortunately, there isn't a baseline to compare the performance with. But path-termination using stream compaction is expected to improve the performance.
+
+```
+struct rayTerminated
+{
+ __host__ __device__
+ bool operator()(const PathSegment& pathSegment)
+ {
+ return pathSegment.remainingBounces;
+ }
+};
+PathSegment* path_end = thrust::stable_partition(thrust::device, dev_paths, dev_paths + num_paths, rayTerminated());
+num_paths = path_end - dev_paths;
+iterationComplete = (num_paths == 0);
+```
+
+
+## Sorting Rays
+To enable ray sorting, set `SORT_RAYS` to 1 in `pathtrace.cu`.
+Since each ray can hit a material of different BSDF computation, some rays might terminate early, while others can be in flight for a long time. This results in threads finishing at different times. To solve this problem, we can use radix sort to sort the rays by the material types. By making the rays that hit the same material contiguous in memory, we can improve the performance by terminating the idling warps early so they can be utilized again. However, since sorting adds a significant amount of computation overheads, this algorithm only helps improve the performance when there are many different material types in the scene. In a simple scene like Cornell box, toggling sorting rays on will actually decrease the performance.
+I'm getting **19.2** fps for ray sorting on vs. **19.7** fps for ray sorting off for the refraction scene (higher is better), where there are 10 materials.
+Then I added 8 more materials and 8 more spheres. I'm getting **18.6** fps for ray sorting on vs. **16.9** fps for ray soring off. It clearly proves that when there are less materials/objects in the scene, ray sorting may actually be slower than without it. But as the scene gets more complicated, sorting rays will increase the performance significantly.
+
+
+## Caching the First Bounce Intersections
+To enable first bounce intersections caching, set `CACHE_FIRST_BOUNCE` to 1 in `pathtrace.cu`.
+One small improvement is to cache the first bounce intersections so it can be re-used for all the subsequent iterations. According to my test result, this provides a very small performance improvement.
+From my tests on different scenes, caching the first bounce intersection generally gives a roughly **1/60 performance boost**.
+
+## Refraction
+This uses the Schlick's approximation to implement the refraction effect.
+From the left to the right, the refraction indices are 1.33 (water), 1.77 (sapphire), and 2.42 (diamond)
+![Refraction](img/refract.png)
+
+## Depth of Field
+Depth of Field in a path tracer is done by jittering rays within an aperture. It will add noises to the rays that does hit objects on the focal length, and create a blurry effect.
+To enable DOF, set `DEPTH_OF_FIELD` to 1 in `pathtrace.cu`, and use `FOCAL_LENGTH` and `APERTURE` to adjust the effect.
+The following scene has an aperture of 1.2 and a focal length of 10. It is easy to see that only the sphere in the center is clear, while eveything else seems blurred.
+![DOF](img/dof.png)
+
+## Direct Lighting
+In a path tracer, light intensity is the most important information, and ideally we want each bounce and iteration to carry as much information as possible. One way to achieve this is by always taking the final ray directly to a random point on the light source.
+To enable direct lighting, set `DIRECT_LIGHTING` to 1 in `pathtrace.cu`.
+Comparing the same scene with direct lighting on (left) and off (right), direct lighting makes the scene brighter. Additionally, it also converges faster when the iteration count is still low.
+
+## Motion Blur
+Motion blur is achieved by averaging samples at different times in the animation.
+To enable direct lighting, set `MOTION_BLUR` to 1 in `pathtrace.cu`, and define the motion of the objects in the scene by setting its "ENDPOS". The object will then move from its starting postion to the endpos.
+In the following example, the sphere is moving down by 2 unit
+![motionblur](img/motionblur.png)
+
+## Stochastic Sampled Anti-aliasing
+Stochastic sampled anti-aliasing is a relatively low-cost anti-aliasing technique ahieved by jittering the sample locations that are spaced out regularly. This increase in low frequency noise would cause an image convoluted with this filter to scatter the high frequencies into low frequencies. Since the human visual system is more sensitive to low frequencies, this "tricks" people into thinking the there are less aliasing in the scene.
+To enable antialiasing, set `ANTI_ALIASING` to 1. Note that in my current implementation, DOF can't be turned on with AA simultaneously.
+Below is the comparison of the same scene with anti-aliasing on (left) and off (right).
+
+The following maginified image shows that with anti-aliasing (below), the image indeed look less "noisy".
+![no anti-aliasing](img/aa_compare.png)
+
+## Mesh Loading
+My pathtracer supports loading any number of arbitrary meshes in .obj format into the scene.
+It uses the [tinyObjLoader](https://github.com/tinyobjloader/tinyobjloader) to parse the data in the .obj file. With the vert/face/norm/texture data, it assembles the triangles in the scene, and test ray-triangle intersection to render the mesh.
+It also does bounding volume intersection culling by first checking rays against a volume that completely bounds the mesh.
+Please see the sample scene files to learn how to load a mesh into the scene.
+![mesh](img/mesh.png)
+### Performance Analysis - Bounding Volume Check On vs. Off
+I'm getting **1.8 fps** for the bounding culling on vs. **1.6 fps** for the bounding culling off (higher is better).
+Bounding volume culling does give a slight performance boost. It is not a big difference, but that's mostly because I'm only testing the cow mesh. If the mesh is more complicated and has more out-of-bound vertices, the benefits are expected to be significantly bigger.
+
+## Texture Mapping with Simple Procedural Texture
+Loading a mesh without its texture is no fun! Fortunately, my pathtracer also loads the texture and maps it on the mesh according to the given uv coordinates.
+When there is no texture file, it will generate a simple procedural texture.
+In the following scene, Mario is using a image texture, and the cow is using a procedural texture.
+Please see the sample scene files to learn how to load a texture into the scene.
+ ![Texture-mapping](img/texture.png)
+### Performance Analysis - Image Texture vs. Procedural Texture
+I'm getting **1.8 fps** for the image texture vs. **1.7 fps** for the procedural texture (higher is better).
+It's not a huge difference, and it makes sense because image texture is simply reading data from an image, while procedural texture involves computing noises and generating a new color. Additionally, I'm only generating the noise from a simple noise function. If a fansy procedural texture is desired, it will require multiple layers of noises, which is expected to slow down the frame rate even more.
+
+## Final Rays Post-processing
+Final rays post-processing is basically taking the final color of the scene, and apply interesting filters to it by tuning its rgb value.
+I implemented 3 simple post-proessing filters: greyscale, sepia, inverted, and high-contrast.
+To enable direct lighting, set `POST_PROCESS` to 1 in `pathtrace.cu`, and set the desired filter (like `GREYSCALE`) to 1, while setting the other filter types to 0.
+### No Filter
+![nofilter](img/nofilter.png)
+### Greyscale
+Greyscale filter converts the color information to only light intensity information. The image will have many shades of gray in between. Greyscale color = 0.21R + 0.72G + 0.07B
+![Greyscale](img/greyscale.png)
+### Sepia
+Sepia filter is a reddish-brown tone, and it is a chemical process used in photography. Sepia adjust each color channel with a certain value to add shades of brown to the image.
+![Sepia](img/sepia.png)
+### Inverted
+Inverted filter is simply inverting the color of the scene. It makes the original dark pixels bright and bright pixels dark. It can be simply achieved by new color = 1 - original color.
+![Inverted](img/inverted.png)
+### High-contrast
+High-contrast filter makes bright pixels brighter and dark pixels darker by multiplying the color according to its brightness by a small number.
+![High-contrast](img/high-contrast.png)
+
+# Bloopers
+### Wahoo! Toxic mushrooms!
+![mario_toxic](img/Bloopers/mario_toxic.png)
+This is caused by normalizing the interpolated UV coords
+### Cow-rio
+![cow-rio](img/Bloopers/cow-rio.png)
+This is caused by forgetting to clear the triangle buffer (one is meant to be just the cow and the other is meant to be just the Mario)
+### Motion refraction?
+![motion-refraction](img/Bloopers/motion-refraction.png)
+This is caused by unknown computation bugs when implementing motion blur. Instead of looking blurred, the supposedly blurry part looks like it's refracting.
+
+
+# Refrence
+* Meshes: [titanic](https://sketchfab.com/3d-models/lego-sinking-titanic-8048d606fcb54bcc9b3e0b52c5d6a394), [train](https://sketchfab.com/3d-models/train-fb2d125c67fe44c69c4238a9f5a12d5a#download), [Mario and Cow](https://www.cis.upenn.edu/~cis4600/22fa/hw/hw04/openglFun.html)
+* [Formulas reference](https://pbr-book.org/)
+* [Base structure reference](https://onedrive.live.com/view.aspx?resid=A6B78147D66DD722!96250&ithint=file%2cpptx&authkey=!AHM5o0OIig5tENc)
diff --git a/img/Bloopers/cow-rio.png b/img/Bloopers/cow-rio.png
new file mode 100644
index 00000000..bf84e26f
Binary files /dev/null and b/img/Bloopers/cow-rio.png differ
diff --git a/img/Bloopers/mario_toxic.png b/img/Bloopers/mario_toxic.png
new file mode 100644
index 00000000..c0253b77
Binary files /dev/null and b/img/Bloopers/mario_toxic.png differ
diff --git a/img/Bloopers/motion-refraction.png b/img/Bloopers/motion-refraction.png
new file mode 100644
index 00000000..8b282a09
Binary files /dev/null and b/img/Bloopers/motion-refraction.png differ
diff --git a/img/aa_compare.png b/img/aa_compare.png
new file mode 100644
index 00000000..d8d7b4b7
Binary files /dev/null and b/img/aa_compare.png differ
diff --git a/img/antialiasing.png b/img/antialiasing.png
new file mode 100644
index 00000000..c1ee96f2
Binary files /dev/null and b/img/antialiasing.png differ
diff --git a/img/diffuse.png b/img/diffuse.png
new file mode 100644
index 00000000..48d754b2
Binary files /dev/null and b/img/diffuse.png differ
diff --git a/img/directlighting.png b/img/directlighting.png
new file mode 100644
index 00000000..04f49bfc
Binary files /dev/null and b/img/directlighting.png differ
diff --git a/img/dof.png b/img/dof.png
new file mode 100644
index 00000000..6fa5aa7a
Binary files /dev/null and b/img/dof.png differ
diff --git a/img/greyscale.png b/img/greyscale.png
new file mode 100644
index 00000000..2880e5e1
Binary files /dev/null and b/img/greyscale.png differ
diff --git a/img/high-contrast.png b/img/high-contrast.png
new file mode 100644
index 00000000..8200f2c2
Binary files /dev/null and b/img/high-contrast.png differ
diff --git a/img/imperf-specular.png b/img/imperf-specular.png
new file mode 100644
index 00000000..ccf9847f
Binary files /dev/null and b/img/imperf-specular.png differ
diff --git a/img/inverted.png b/img/inverted.png
new file mode 100644
index 00000000..f9823e7e
Binary files /dev/null and b/img/inverted.png differ
diff --git a/img/mesh.png b/img/mesh.png
new file mode 100644
index 00000000..2ef4326c
Binary files /dev/null and b/img/mesh.png differ
diff --git a/img/motionblur.png b/img/motionblur.png
new file mode 100644
index 00000000..abad87cb
Binary files /dev/null and b/img/motionblur.png differ
diff --git a/img/no-antialiasing.png b/img/no-antialiasing.png
new file mode 100644
index 00000000..520383a4
Binary files /dev/null and b/img/no-antialiasing.png differ
diff --git a/img/nodirectlighting.png b/img/nodirectlighting.png
new file mode 100644
index 00000000..c5d27862
Binary files /dev/null and b/img/nodirectlighting.png differ
diff --git a/img/nofilter.png b/img/nofilter.png
new file mode 100644
index 00000000..fe849561
Binary files /dev/null and b/img/nofilter.png differ
diff --git a/img/overview.png b/img/overview.png
new file mode 100644
index 00000000..1484f47c
Binary files /dev/null and b/img/overview.png differ
diff --git a/img/perf-specular.png b/img/perf-specular.png
new file mode 100644
index 00000000..b5909abf
Binary files /dev/null and b/img/perf-specular.png differ
diff --git a/img/refract.png b/img/refract.png
new file mode 100644
index 00000000..d9faf3fd
Binary files /dev/null and b/img/refract.png differ
diff --git a/img/sepia.png b/img/sepia.png
new file mode 100644
index 00000000..96e40764
Binary files /dev/null and b/img/sepia.png differ
diff --git a/img/texture.png b/img/texture.png
new file mode 100644
index 00000000..9d08bee3
Binary files /dev/null and b/img/texture.png differ
diff --git a/scenes/antialiasing.txt b/scenes/antialiasing.txt
new file mode 100644
index 00000000..8e606a21
--- /dev/null
+++ b/scenes/antialiasing.txt
@@ -0,0 +1,185 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Water
+MATERIAL 8
+RGB .8314 .9451 .9765
+SPECEX 0
+SPECRGB .8314 .9451 .9765
+REFL 1
+REFR 1
+REFRIOR 1.33
+EMITTANCE 0
+
+// Sapphire
+MATERIAL 9
+RGB .059 .322 .729
+SPECEX 0
+SPECRGB .059 .322 .729
+REFL 1
+REFR 1
+REFRIOR 1.77
+EMITTANCE 0
+
+// Diamond
+MATERIAL 10
+RGB .7255 .949 .99
+SPECEX 0
+SPECRGB .7255 .949 .99
+REFL 1
+REFR 1
+REFRIOR 2.42
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 5000
+DEPTH 8
+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 6 .3 6
+
+// Floor
+OBJECT 1
+cube
+material 1
+TRANS 0 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 6
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 3
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 2
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 5
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Sphere
+OBJECT 6
+sphere
+material 8
+TRANS -2 4 2
+ROTAT 0 0 0
+SCALE 6 6 6
+
+// cube
+OBJECT 7
+cube
+material 6
+TRANS 3 4 2
+ROTAT 0 0 0
+SCALE 2 2 2
diff --git a/scenes/diffuse.txt b/scenes/diffuse.txt
new file mode 100644
index 00000000..4dfd1d36
--- /dev/null
+++ b/scenes/diffuse.txt
@@ -0,0 +1,147 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 5000
+DEPTH 8
+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 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 1
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 3
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 2
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 5
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Sphere
+OBJECT 6
+sphere
+material 1
+TRANS 0 4 0
+ROTAT 0 0 0
+SCALE 5 5 5
diff --git a/scenes/directlighting.txt b/scenes/directlighting.txt
new file mode 100644
index 00000000..2e7694db
--- /dev/null
+++ b/scenes/directlighting.txt
@@ -0,0 +1,154 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 5000
+DEPTH 8
+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 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 1
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 3
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 2
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 5
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Sphere
+OBJECT 6
+sphere
+material 4
+TRANS 0 6 0
+ROTAT 0 0 0
+SCALE 4 4 4
+
+OBJECT 7
+sphere
+material 6
+TRANS 0 2 0
+ROTAT 0 0 0
+SCALE 2 2 2
diff --git a/scenes/dof.txt b/scenes/dof.txt
new file mode 100644
index 00000000..699ccc46
--- /dev/null
+++ b/scenes/dof.txt
@@ -0,0 +1,211 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Water
+MATERIAL 8
+RGB .8314 .9451 .9765
+SPECEX 0
+SPECRGB .8314 .9451 .9765
+REFL 1
+REFR 1
+REFRIOR 1.33
+EMITTANCE 0
+
+// Sapphire
+MATERIAL 9
+RGB .059 .322 .729
+SPECEX 0
+SPECRGB .059 .322 .729
+REFL 1
+REFR 1
+REFRIOR 1.77
+EMITTANCE 0
+
+// Diamond
+MATERIAL 10
+RGB .7255 .949 .99
+SPECEX 0
+SPECRGB .7255 .949 .99
+REFL 1
+REFR 1
+REFRIOR 2.42
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 5000
+DEPTH 8
+FILE cornell
+EYE 0.0 3 10.5
+LOOKAT 0 3 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 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 1
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 3
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 2
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 5
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Sphere
+OBJECT 6
+cube
+material 9
+TRANS -3 2 -4
+ROTAT 0 45 0
+SCALE 2 2 2
+
+// Sphere
+OBJECT 7
+sphere
+material 8
+TRANS 0 5 0
+ROTAT 0 45 0
+SCALE 3 3 3
+
+// Sphere
+OBJECT 8
+cube
+material 9
+TRANS 3 6 4
+ROTAT 0 45 0
+SCALE 2 2 2
+
+
+// Sphere
+OBJECT 9
+cube
+material 10
+TRANS -3 6 4
+ROTAT 0 45 0
+SCALE 2 2 2
+
+// Sphere
+OBJECT 10
+cube
+material 10
+TRANS 3 2 -4
+ROTAT 0 45 0
+SCALE 2 2 2
+
diff --git a/scenes/filters.txt b/scenes/filters.txt
new file mode 100644
index 00000000..8bfc6665
--- /dev/null
+++ b/scenes/filters.txt
@@ -0,0 +1,158 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Water
+MATERIAL 8
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 1
+REFRIOR 1.33
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 5000
+DEPTH 8
+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 5
+ROTAT 0 0 0
+SCALE 8 .3 8
+
+// Floor
+OBJECT 1
+cube
+material 1
+TRANS 0 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 6
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 3
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 2
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 5
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+
+// cow
+OBJECT 6
+obj_mesh
+../scenes/objs/cow.obj
+material 8
+TRANS 0 4 5
+ROTAT 0 -60 0
+SCALE 0.5 0.5 0.5
\ No newline at end of file
diff --git a/scenes/mesh.txt b/scenes/mesh.txt
new file mode 100644
index 00000000..2d1d88f2
--- /dev/null
+++ b/scenes/mesh.txt
@@ -0,0 +1,162 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 5000
+DEPTH 8
+FILE cornell
+EYE 0.0 5 10.5
+LOOKAT 0 5 0
+UP 0 1 0
+
+TEXTURE 0
+PATH ../scenes/textures/wahoo.bmp
+
+TEXTURE 1
+PROCEDURAL
+
+// Ceiling light
+OBJECT 0
+cube
+material 0
+TRANS 0 10 5
+ROTAT 0 0 0
+SCALE 8 .3 8
+
+// Floor
+OBJECT 1
+cube
+material 6
+TRANS 0 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 6
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 6
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 6
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 6
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Mario
+OBJECT 6
+obj_mesh
+../scenes/objs/wahoo.obj
+material 1
+TRANS -2 4 5
+ROTAT 0 15 0
+SCALE 0.5 0.5 0.5
+
+// cow
+OBJECT 7
+obj_mesh
+../scenes/objs/cow.obj
+material 1
+TRANS 1.5 4 5
+ROTAT 0 -75 0
+SCALE 0.5 0.5 0.5
\ No newline at end of file
diff --git a/scenes/motionblur.txt b/scenes/motionblur.txt
new file mode 100644
index 00000000..66541a49
--- /dev/null
+++ b/scenes/motionblur.txt
@@ -0,0 +1,148 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 500
+DEPTH 8
+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 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 1
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 3
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 2
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 5
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Sphere
+OBJECT 6
+cube
+material 6
+TRANS 0 6 0
+ROTAT 0 0 0
+SCALE 4 4 4
+ENDPOS 0 5 0
\ No newline at end of file
diff --git a/scenes/objs/Mineways2Skfb.mtl b/scenes/objs/Mineways2Skfb.mtl
new file mode 100644
index 00000000..9d99c2c0
--- /dev/null
+++ b/scenes/objs/Mineways2Skfb.mtl
@@ -0,0 +1,108 @@
+Wavefront OBJ material file
+# Contains 8 materials
+
+newmtl Glass
+Ns 0
+Ka 0.2 0.2 0.2
+Kd 1 1 1
+Ks 0.03 0.03 0.03
+map_Ka Mineways2Skfb-RGBA.png
+# for G3D, to make textures look blocky:
+interpolateMode NEAREST_MAGNIFICATION_TRILINEAR_MIPMAP_MINIFICATION
+map_Kd Mineways2Skfb-RGBA.png
+map_d Mineways2Skfb-RGBA.png
+illum 2
+# d 1
+# Tr 1
+
+newmtl Block_of_Iron
+Ns 0
+Ka 0.2 0.2 0.2
+Kd 1 1 1
+Ks 0 0 0
+map_Ka Mineways2Skfb-RGBA.png
+# for G3D, to make textures look blocky:
+interpolateMode NEAREST_MAGNIFICATION_TRILINEAR_MIPMAP_MINIFICATION
+map_Kd Mineways2Skfb-RGBA.png
+illum 2
+# d 1
+# Tr 1
+
+newmtl Double_Stone_Slab
+Ns 0
+Ka 0.2 0.2 0.2
+Kd 1 1 1
+Ks 0 0 0
+map_Ka Mineways2Skfb-RGBA.png
+# for G3D, to make textures look blocky:
+interpolateMode NEAREST_MAGNIFICATION_TRILINEAR_MIPMAP_MINIFICATION
+map_Kd Mineways2Skfb-RGBA.png
+illum 2
+# d 1
+# Tr 1
+
+newmtl Stone_Slab
+Ns 0
+Ka 0.2 0.2 0.2
+Kd 1 1 1
+Ks 0 0 0
+map_Ka Mineways2Skfb-RGBA.png
+# for G3D, to make textures look blocky:
+interpolateMode NEAREST_MAGNIFICATION_TRILINEAR_MIPMAP_MINIFICATION
+map_Kd Mineways2Skfb-RGBA.png
+illum 2
+# d 1
+# Tr 1
+
+newmtl Iron_Door
+Ns 0
+Ka 0.2 0.2 0.2
+Kd 1 1 1
+Ks 0 0 0
+map_Ka Mineways2Skfb-RGBA.png
+# for G3D, to make textures look blocky:
+interpolateMode NEAREST_MAGNIFICATION_TRILINEAR_MIPMAP_MINIFICATION
+map_Kd Mineways2Skfb-RGBA.png
+map_d Mineways2Skfb-RGBA.png
+illum 2
+# d 1
+# Tr 1
+
+newmtl Stone_Button
+Ns 0
+Ka 0.2 0.2 0.2
+Kd 1 1 1
+Ks 0 0 0
+map_Ka Mineways2Skfb-RGBA.png
+# for G3D, to make textures look blocky:
+interpolateMode NEAREST_MAGNIFICATION_TRILINEAR_MIPMAP_MINIFICATION
+map_Kd Mineways2Skfb-RGBA.png
+illum 2
+# d 1
+# Tr 1
+
+newmtl Stone_Brick_Stairs
+Ns 0
+Ka 0.2 0.2 0.2
+Kd 1 1 1
+Ks 0 0 0
+map_Ka Mineways2Skfb-RGBA.png
+# for G3D, to make textures look blocky:
+interpolateMode NEAREST_MAGNIFICATION_TRILINEAR_MIPMAP_MINIFICATION
+map_Kd Mineways2Skfb-RGBA.png
+illum 2
+# d 1
+# Tr 1
+
+newmtl Anvil
+Ns 0
+Ka 0.2 0.2 0.2
+Kd 1 1 1
+Ks 0 0 0
+map_Ka Mineways2Skfb-RGBA.png
+# for G3D, to make textures look blocky:
+interpolateMode NEAREST_MAGNIFICATION_TRILINEAR_MIPMAP_MINIFICATION
+map_Kd Mineways2Skfb-RGBA.png
+illum 2
+# d 1
+# Tr 1
diff --git a/scenes/objs/titanic.mtl b/scenes/objs/titanic.mtl
new file mode 100644
index 00000000..d7af4e89
--- /dev/null
+++ b/scenes/objs/titanic.mtl
@@ -0,0 +1,8 @@
+newmtl material_0
+Ka 0.100000 0.100000 0.100000
+Kd 1.000000 1.000000 1.000000
+Ks 0.000000 0.000000 0.000000
+Tr 0.000000
+illum 1
+Ns 1.000000
+map_Kd textured_output.jpg
\ No newline at end of file
diff --git a/scenes/objs/train.mtl b/scenes/objs/train.mtl
new file mode 100644
index 00000000..d7af4e89
--- /dev/null
+++ b/scenes/objs/train.mtl
@@ -0,0 +1,8 @@
+newmtl material_0
+Ka 0.100000 0.100000 0.100000
+Kd 1.000000 1.000000 1.000000
+Ks 0.000000 0.000000 0.000000
+Tr 0.000000
+illum 1
+Ns 1.000000
+map_Kd textured_output.jpg
\ No newline at end of file
diff --git a/scenes/refract.txt b/scenes/refract.txt
new file mode 100644
index 00000000..3a4a6fef
--- /dev/null
+++ b/scenes/refract.txt
@@ -0,0 +1,193 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Water
+MATERIAL 8
+RGB .8314 .9451 .9765
+SPECEX 0
+SPECRGB .8314 .9451 .9765
+REFL 1
+REFR 1
+REFRIOR 1.33
+EMITTANCE 0
+
+// Sapphire
+MATERIAL 9
+RGB .059 .322 .729
+SPECEX 0
+SPECRGB .059 .322 .729
+REFL 1
+REFR 1
+REFRIOR 1.77
+EMITTANCE 0
+
+// Diamond
+MATERIAL 10
+RGB .7255 .949 .99
+SPECEX 0
+SPECRGB .7255 .949 .99
+REFL 1
+REFR 1
+REFRIOR 2.42
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 5000
+DEPTH 8
+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 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 1
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 3
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 2
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 5
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Sphere
+OBJECT 6
+sphere
+material 8
+TRANS -3 4 3
+ROTAT 0 0 0
+SCALE 3 3 3
+
+// Sphere
+OBJECT 7
+sphere
+material 9
+TRANS 0 4 3
+ROTAT 0 0 0
+SCALE 3 3 3
+
+// Sphere
+OBJECT 8
+sphere
+material 10
+TRANS 3 4 3
+ROTAT 0 0 0
+SCALE 3 3 3
diff --git a/scenes/specular.txt b/scenes/specular.txt
new file mode 100644
index 00000000..83f425d2
--- /dev/null
+++ b/scenes/specular.txt
@@ -0,0 +1,147 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 5000
+DEPTH 8
+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 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 1
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 3
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 2
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 5
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Sphere
+OBJECT 6
+sphere
+material 4
+TRANS 0 4 0
+ROTAT 0 0 0
+SCALE 5 5 5
diff --git a/scenes/texture.txt b/scenes/texture.txt
new file mode 100644
index 00000000..21d37e38
--- /dev/null
+++ b/scenes/texture.txt
@@ -0,0 +1,164 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 5
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .98 .0 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .0 .98 .0
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Blue
+MATERIAL 5
+RGB .0 .0 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Grey
+MATERIAL 6
+RGB .5 .5 .5
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse Black
+MATERIAL 7
+RGB .02 .02 .02
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 5000
+DEPTH 8
+FILE cornell
+EYE 0.0 5 10.5
+LOOKAT 0 5 0
+UP 0 1 0
+
+TEXTURE 0
+PATH ../scenes/textures/wahoo.bmp
+
+TEXTURE 1
+PROCEDURAL
+
+// Ceiling light
+OBJECT 0
+cube
+material 0
+TRANS 0 10 5
+ROTAT 0 0 0
+SCALE 8 .3 8
+
+// Floor
+OBJECT 1
+cube
+material 6
+TRANS 0 0 0
+ROTAT 0 0 0
+SCALE 10 .01 10
+
+// Ceiling
+OBJECT 2
+cube
+material 6
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 10 10
+
+// Back wall
+OBJECT 3
+cube
+material 6
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 10 10
+
+// Left wall
+OBJECT 4
+cube
+material 6
+TRANS -5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Right wall
+OBJECT 5
+cube
+material 6
+TRANS 5 5 0
+ROTAT 0 0 0
+SCALE .01 10 10
+
+// Mario
+OBJECT 6
+obj_mesh
+../scenes/objs/wahoo.obj
+material 1
+TRANS -2 4 5
+ROTAT 0 15 0
+SCALE 0.5 0.5 0.5
+TEXTURE 0
+
+// cow
+OBJECT 7
+obj_mesh
+../scenes/objs/cow.obj
+material 1
+TRANS 1.5 4 5
+ROTAT 0 -75 0
+SCALE 0.5 0.5 0.5
+TEXTURE 1
\ No newline at end of file
diff --git a/scenes/textures/Mineways2Skfb-RGBA.png b/scenes/textures/Mineways2Skfb-RGBA.png
new file mode 100644
index 00000000..41f8515a
Binary files /dev/null and b/scenes/textures/Mineways2Skfb-RGBA.png differ
diff --git a/scenes/textures/robot_steampunk_color.tga.png b/scenes/textures/robot_steampunk_color.tga.png
new file mode 100644
index 00000000..3279ebb0
Binary files /dev/null and b/scenes/textures/robot_steampunk_color.tga.png differ
diff --git a/scenes/textures/titanic.jpg b/scenes/textures/titanic.jpg
new file mode 100644
index 00000000..4f324191
Binary files /dev/null and b/scenes/textures/titanic.jpg differ
diff --git a/scenes/textures/train.jpg b/scenes/textures/train.jpg
new file mode 100644
index 00000000..92849fc0
Binary files /dev/null and b/scenes/textures/train.jpg differ
diff --git a/scenes/textures/wahoo.bmp b/scenes/textures/wahoo.bmp
new file mode 100644
index 00000000..bf1598d9
Binary files /dev/null and b/scenes/textures/wahoo.bmp differ
diff --git a/scenes/textures/winxp.jpg b/scenes/textures/winxp.jpg
new file mode 100644
index 00000000..6a2687de
Binary files /dev/null and b/scenes/textures/winxp.jpg differ
diff --git a/scenes/titanic.txt b/scenes/titanic.txt
new file mode 100644
index 00000000..77a5991b
--- /dev/null
+++ b/scenes/titanic.txt
@@ -0,0 +1,201 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 6
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .85 .35 .35
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .35 .85 .35
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Warm Light
+MATERIAL 5
+RGB 1 0.96 0.713
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 3
+
+// Refraction blue
+MATERIAL 6
+RGB .6705 .8588 .8901
+SPECEX 0
+SPECRGB .6705 .8588 .8901
+REFL 1
+REFR 1
+REFRIOR 1.325
+EMITTANCE 0
+
+// Blue floor
+MATERIAL 7
+RGB .0392 .4705 .9686
+SPECEX 0
+SPECRGB .0392 .4705 .9686
+REFL 1
+REFR 1
+REFRIOR 1.5
+EMITTANCE 0
+
+// Dark blue wall
+MATERIAL 8
+RGB .098 .1725 .3882
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Crystal
+MATERIAL 9
+RGB .6549 .847 .8705
+SPECEX 0
+SPECRGB .6549 .847 .8705
+REFL 1
+REFR 1
+REFRIOR 2.0
+EMITTANCE 0
+
+TEXTURE 0
+PATH ../scenes/textures/titanic.jpg
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 10000
+DEPTH 10
+FILE cornell
+EYE 0.0 3 10.5
+LOOKAT 0 3 0
+UP 0 1 0
+
+// Ceiling light
+OBJECT 0
+cube
+material 0
+TRANS 0 10 0
+ROTAT 0 135 0
+SCALE 20 .3 4
+
+// Floor
+OBJECT 1
+cube
+material 7
+TRANS 0 -4 0
+ROTAT 0 0 0
+SCALE 30 .01 30
+
+// Ceiling
+OBJECT 2
+cube
+material 1
+TRANS 0 10 0
+ROTAT 0 0 90
+SCALE .01 20 20
+
+// Back wall
+OBJECT 3
+cube
+material 8
+TRANS 0 5 -5
+ROTAT 0 90 0
+SCALE .01 20 20
+
+// Left wall
+OBJECT 4
+cube
+material 8
+TRANS -15 5 0
+ROTAT 0 -45 0
+SCALE .01 20 20
+
+// Right wall
+OBJECT 5
+cube
+material 8
+TRANS 15 5 0
+ROTAT 0 45 0
+SCALE .01 20 20
+
+// Sphere
+OBJECT 6
+sphere
+material 6
+TRANS -0.5 6 0
+ROTAT 0 0 0
+SCALE 3 3 3
+
+// Crystal
+OBJECT 7
+cube
+material 9
+TRANS 5 1 0
+ROTAT 0 0 -45
+SCALE 2 6 20
+
+// Mirror
+OBJECT 8
+cube
+material 4
+TRANS -10 5 -5
+ROTAT 0 30 0
+SCALE 4 20 4
+
+OBJECT 9
+sphere
+material 5
+TRANS 4 8 18
+ROTAT 0 0 0
+SCALE 6 6 0.1
+
+OBJECT 10
+obj_mesh
+../scenes/objs/titanic.obj
+material 2
+TRANS -1 -0.3 0
+ROTAT 0 -45 0
+SCALE 5 5 5
+TEXTURE 0
\ No newline at end of file
diff --git a/scenes/train.txt b/scenes/train.txt
new file mode 100644
index 00000000..e6e4960b
--- /dev/null
+++ b/scenes/train.txt
@@ -0,0 +1,169 @@
+// Emissive material (light)
+MATERIAL 0
+RGB 1 1 1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 4
+
+// Diffuse white
+MATERIAL 1
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse red
+MATERIAL 2
+RGB .85 .35 .35
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Diffuse green
+MATERIAL 3
+RGB .35 .85 .35
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Specular white
+MATERIAL 4
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+// Refract white
+MATERIAL 5
+RGB .98 .98 .98
+SPECEX 0
+SPECRGB .98 .98 .98
+REFL 1
+REFR 1
+REFRIOR 1.5
+EMITTANCE 0
+
+MATERIAL 6
+RGB 0.125 0.164 .2667
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+MATERIAL 7
+RGB 0.1 0.1 0.1
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+MATERIAL 8
+RGB 0.085 0.244 .22
+SPECEX 0
+SPECRGB 0 0 0
+REFL 0
+REFR 0
+REFRIOR 0
+EMITTANCE 0
+
+TEXTURE 0
+PATH ../scenes/textures/titanic.jpg
+
+TEXTURE 1
+PATH ../scenes/textures/train.jpg
+
+// Camera
+CAMERA
+RES 1600 1600
+FOVY 45
+ITERATIONS 10000
+DEPTH 8
+FILE cornell
+EYE 0.0 2 10
+LOOKAT 0 2 0
+UP 0 1 0
+
+// Floor
+OBJECT 0
+cube
+material 6
+TRANS 0 0 0
+ROTAT 0 0 0
+SCALE 30 .01 30
+
+// Ceiling
+OBJECT 1
+cube
+material 8
+TRANS 0 5 0
+ROTAT 0 0 90
+SCALE .01 20 20
+
+// Back wall
+OBJECT 2
+cube
+material 5
+TRANS 0 10 -15
+ROTAT 45 90 0
+SCALE .01 30 30
+
+// light
+OBJECT 3
+cube
+material 0
+TRANS -6 5 0
+ROTAT 0 0 -45
+SCALE .01 5 30
+
+// Left wall
+OBJECT 4
+cube
+material 7
+TRANS -10 5 0
+ROTAT 0 0 20
+SCALE .01 20 30
+
+// Right wall
+OBJECT 5
+cube
+material 4
+TRANS 3 5 0
+ROTAT 0 0 15
+SCALE .01 20 30
+
+// Front light
+OBJECT 6
+cube
+material 0
+TRANS 0 5 12
+ROTAT 0 90 0
+SCALE .01 20 20
+
+OBJECT 7
+obj_mesh
+../scenes/objs/train.obj
+material 2
+TRANS -1 0 0
+ROTAT 0 20 0
+SCALE 25 25 25
+TEXTURE 1
+
diff --git a/src/interactions.h b/src/interactions.h
index f969e458..7f1f2f51 100644
--- a/src/interactions.h
+++ b/src/interactions.h
@@ -2,6 +2,8 @@
#include "intersections.h"
+#define IMPERFECT_SPECULAR 1
+
// CHECKITOUT
/**
* Computes a cosine-weighted random direction in a hemisphere.
@@ -72,8 +74,103 @@ void scatterRay(
glm::vec3 intersect,
glm::vec3 normal,
const Material &m,
- thrust::default_random_engine &rng) {
+ thrust::default_random_engine &rng
+) {
// TODO: implement this.
// A basic implementation of pure-diffuse shading will just call the
// calculateRandomDirectionInHemisphere defined above.
+
+ if (pathSegment.remainingBounces == 0) {
+ return;
+ }
+
+ thrust::uniform_real_distribution u01(0, 1);
+ float rand = u01(rng);
+
+ if (m.hasRefractive && m.hasReflective)
+ {
+ float R1 = m.indexOfRefraction;
+ float R2 = 1.;
+
+ float cosThetaI = glm::dot(-1.f * pathSegment.ray.direction, normal);
+
+ bool entering = cosThetaI > 0.f;
+ if (!entering)
+ {
+ normal = -normal;
+ R2 = R1;
+ R1 = 1.;
+ }
+
+ float R0 = pow((R1 - R2) / (R1 + R2), 2);
+ float R = R0 + (1 - R0) * pow(1 - cosThetaI, 5);
+
+ glm::vec3 newDir = glm::refract(pathSegment.ray.direction, normal, R2 / R1);
+
+ if (R > rand)
+ {
+ pathSegment.ray.direction = glm::reflect(pathSegment.ray.direction, normal);
+ pathSegment.color *= m.specular.color * m.color;
+ }
+ else
+ {
+ pathSegment.ray.direction = newDir;
+ pathSegment.color *= m.specular.color * m.color;
+ }
+
+ }
+ // Even split between specular and diffuse
+ else if (m.hasReflective)
+ {
+
+
+#if IMPERFECT_SPECULAR
+ // imperfect specular
+ if (rand > 0.5f)
+ {
+ pathSegment.ray.direction = calculateRandomDirectionInHemisphere(normal, rng);
+ }
+ // Perfect specular
+ else
+ {
+ pathSegment.ray.direction = glm::reflect(pathSegment.ray.direction, normal);
+ pathSegment.color *= m.specular.color;
+ }
+ pathSegment.color *= 2.f * m.color;
+#else
+ // perfect specular
+ pathSegment.ray.direction = glm::reflect(pathSegment.ray.direction, normal);
+ pathSegment.color *= m.specular.color * m.color;
+#endif
+
+ }
+ else if (m.hasRefractive)
+ {
+ // perfect refraction
+ float R1 = m.indexOfRefraction;
+ float R2 = 1.;
+
+ float cosThetaI = glm::dot(-1.f * pathSegment.ray.direction, normal);
+
+ bool entering = cosThetaI > 0.f;
+ if (!entering)
+ {
+ normal = -normal;
+ R2 = R1;
+ R1 = 1.;
+ }
+
+ pathSegment.ray.direction = glm::refract(pathSegment.ray.direction, normal, R2 / R1);
+ pathSegment.color *= m.specular.color * m.color;
+ }
+
+ else
+ {
+ pathSegment.ray.direction = calculateRandomDirectionInHemisphere(normal, rng);
+ pathSegment.color *= m.color;
+ }
+
+ pathSegment.ray.origin = intersect + pathSegment.ray.direction * EPSILON;
+ pathSegment.remainingBounces--;
+
}
diff --git a/src/intersections.h b/src/intersections.h
index b1504071..9a439931 100644
--- a/src/intersections.h
+++ b/src/intersections.h
@@ -142,3 +142,86 @@ __host__ __device__ float sphereIntersectionTest(Geom sphere, Ray r,
return glm::length(r.origin - intersectionPoint);
}
+
+
+__host__ __device__ float triangleIntersectionTest(Geom custom_obj, Ray r,
+ glm::vec3& intersectionPoint, Triangle* triangles, int triangles_start, int triangles_end, glm::vec3& normal, bool& outside,
+ glm::vec2& uv)
+{
+
+ // get the Ray in local space
+ Ray ray_inversed;
+ ray_inversed.origin = multiplyMV(custom_obj.inverseTransform, glm::vec4(r.origin, 1.0f));
+ ray_inversed.direction = glm::normalize(multiplyMV(custom_obj.inverseTransform, glm::vec4(r.direction, 0.0f)));
+
+ float min_t = FLT_MAX;
+
+ for (int i = triangles_start; i < triangles_end; i++)
+ {
+ Triangle& triangle = triangles[i];
+ glm::vec3 vertex1 = triangle.v1;
+ glm::vec3 vertex2 = triangle.v2;
+ glm::vec3 vertex3 = triangle.v3;
+ glm::vec3 normal1 = triangle.n1;
+ glm::vec3 normal2 = triangle.n2;
+ glm::vec3 normal3 = triangle.n3;
+ glm::vec2 t1 = triangle.t1;
+ glm::vec2 t2 = triangle.t2;
+ glm::vec2 t3 = triangle.t3;
+ glm::vec3 baryPos;
+
+
+ // Not intersected
+ if (glm::intersectRayTriangle(ray_inversed.origin, ray_inversed.direction, vertex1, vertex2, vertex3, baryPos))
+ {
+
+ // Smooth interpolate normals
+ glm::vec3 n0;
+ glm::vec3 n1;
+ glm::vec3 n2;
+
+ glm::vec3 isect_pos = (1.f - baryPos.x - baryPos.y) * vertex1 + baryPos.x * vertex2 + baryPos.y * vertex3;
+ intersectionPoint = multiplyMV(custom_obj.transform, glm::vec4(isect_pos, 1.f));
+ float t = glm::length(r.origin - intersectionPoint);
+ if (t > min_t)
+ {
+ continue;
+ }
+ min_t = t;
+
+ if ((glm::length(normal1) != 0) && (glm::length(normal2) != 0) && (glm::length(normal3) != 0))
+ {
+ n0 = normal1;
+ n1 = normal2;
+ n2 = normal3;
+ }
+ else
+ {
+ n0 = glm::normalize(glm::cross(vertex2 - vertex1, vertex3 - vertex1));
+ n1 = glm::normalize(glm::cross(vertex1 - vertex2, vertex3 - vertex2));
+ n2 = glm::normalize(glm::cross(vertex1 - vertex3, vertex2 - vertex3));
+ }
+
+ // Barycentric Interpolation
+ float S = 0.5f * glm::length(glm::cross(vertex1 - vertex2, vertex3 - vertex2));
+ float S0 = 0.5f * glm::length(glm::cross(vertex2 - isect_pos, vertex3 - isect_pos));
+ float S1 = 0.5f * glm::length(glm::cross(vertex1 - isect_pos, vertex3 - isect_pos));
+ float S2 = 0.5f * glm::length(glm::cross(vertex1 - isect_pos, vertex2 - isect_pos));
+ glm::vec3 newNormal = glm::normalize(n0 * S0 / S + n1 * S1 / S + n2 * S2 / S);
+
+ if ((glm::length(t1) != 0) && (glm::length(t2) != 0) && (glm::length(t3) != 0))
+ {
+ uv = t1 * S0 / S + t2 * S1 / S + t3 * S2 / S;
+ }
+
+ normal = glm::normalize(multiplyMV(custom_obj.invTranspose, glm::vec4(newNormal, 0.f)));
+ outside = glm::dot(normal, ray_inversed.direction) < 0;
+ isect_pos = multiplyMV(custom_obj.transform, glm::vec4(isect_pos, 1.f));
+ }
+ }
+ if (!outside)
+ {
+ normal = -normal;
+ }
+ return min_t;
+}
\ No newline at end of file
diff --git a/src/pathtrace.cu b/src/pathtrace.cu
index fd2a4641..93fd971c 100644
--- a/src/pathtrace.cu
+++ b/src/pathtrace.cu
@@ -4,17 +4,44 @@
#include
#include
#include
+#include
+#include
+
#include "sceneStructs.h"
#include "scene.h"
#include "glm/glm.hpp"
#include "glm/gtx/norm.hpp"
+#include
+#include
#include "utilities.h"
#include "pathtrace.h"
#include "intersections.h"
#include "interactions.h"
-#define ERRORCHECK 1
+#include "../stream_compaction/efficient.h"
+
+
+#define ERRORCHECK 0
+#define SORT_RAYS 0
+#define CACHE_FIRST_BOUNCE 0
+#define ANTI_ALIASING 0
+
+#define DEPTH_OF_FIELD 0
+#define FOCAL_LENGTH 10.0f
+#define APERTURE 0.3f
+
+#define MESH_BOUNDING_BOX 1
+
+#define POST_PROCESS 0
+#define GREYSCALE 0
+#define SEPIA 0
+#define INVERTED 0
+#define CONTRAST 1
+
+#define MOTION_BLUR 0
+
+#define DIRECT_LIGHTING 0
#define FILENAME (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
#define checkCUDAError(msg) checkCUDAErrorFn(msg, FILENAME, __LINE__)
@@ -76,6 +103,14 @@ static PathSegment* dev_paths = NULL;
static ShadeableIntersection* dev_intersections = NULL;
// TODO: static variables for device memory, any extra info you need, etc
// ...
+static ShadeableIntersection* dev_intersection_cache = NULL;
+static Triangle* dev_triangles = NULL;
+static Texture* dev_textures = NULL;
+static glm::vec3* dev_texColors = NULL;
+
+static Geom* dev_lights = NULL;
+
+
void InitDataContainer(GuiDataContainer* imGuiData)
{
@@ -103,6 +138,20 @@ void pathtraceInit(Scene* scene) {
cudaMemset(dev_intersections, 0, pixelcount * sizeof(ShadeableIntersection));
// TODO: initialize any extra device memeory you need
+ cudaMalloc(&dev_intersection_cache, pixelcount * sizeof(ShadeableIntersection));
+ cudaMemset(dev_intersection_cache, 0, pixelcount * sizeof(ShadeableIntersection));
+
+ cudaMalloc(&dev_triangles, scene->triangles.size() * sizeof(Triangle));
+ cudaMemcpy(dev_triangles, scene->triangles.data(), scene->triangles.size() * sizeof(Triangle), cudaMemcpyHostToDevice);
+
+ cudaMalloc(&dev_textures, scene->textures.size() * sizeof(Texture));
+ cudaMemcpy(dev_textures, scene->textures.data(), scene->textures.size() * sizeof(Texture), cudaMemcpyHostToDevice);
+
+ cudaMalloc(&dev_texColors, scene->textureColors.size() * sizeof(glm::vec3));
+ cudaMemcpy(dev_texColors, scene->textureColors.data(), scene->textureColors.size() * sizeof(glm::vec3), cudaMemcpyHostToDevice);
+
+ cudaMalloc(&dev_lights, scene->lights.size() * sizeof(Geom));
+ cudaMemcpy(dev_lights, scene->lights.data(), scene->lights.size() * sizeof(Geom), cudaMemcpyHostToDevice);
checkCUDAError("pathtraceInit");
}
@@ -114,6 +163,11 @@ void pathtraceFree() {
cudaFree(dev_materials);
cudaFree(dev_intersections);
// TODO: clean up any extra device memory you created
+ cudaFree(dev_intersection_cache);
+ cudaFree(dev_triangles);
+ cudaFree(dev_textures);
+ cudaFree(dev_texColors);
+ cudaFree(dev_lights);
checkCUDAError("pathtraceFree");
}
@@ -130,6 +184,11 @@ __global__ void generateRayFromCamera(Camera cam, int iter, int traceDepth, Path
{
int x = (blockIdx.x * blockDim.x) + threadIdx.x;
int y = (blockIdx.y * blockDim.y) + threadIdx.y;
+ int index = x + (y * cam.resolution.x);
+
+ thrust::default_random_engine rng = makeSeededRandomEngine(iter, index , pathSegments[index].remainingBounces);
+ thrust::uniform_real_distribution u01(-0.5, 0.5);
+
if (x < cam.resolution.x && y < cam.resolution.y) {
int index = x + (y * cam.resolution.x);
@@ -138,17 +197,75 @@ __global__ void generateRayFromCamera(Camera cam, int iter, int traceDepth, Path
segment.ray.origin = cam.position;
segment.color = glm::vec3(1.0f, 1.0f, 1.0f);
+
+
// TODO: implement antialiasing by jittering the ray
+#if ANTI_ALIASING
+ float jX = u01(rng);
+ float jY = u01(rng);
+ segment.ray.direction = glm::normalize(cam.view
+ - cam.right * cam.pixelLength.x * ((float)x + jX - (float)cam.resolution.x * 0.5f)
+ - cam.up * cam.pixelLength.y * ((float)y + jY - (float)cam.resolution.y * 0.5f)
+ );
+#else
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)
);
+#endif
+
+#if DEPTH_OF_FIELD
+ float jXd = u01(rng);
+ float jYd = u01(rng);
+
+ glm::vec3 focalPoint = segment.ray.direction * FOCAL_LENGTH;
+ glm::vec3 shift = glm::vec3(jXd, jYd, 0.0f) * APERTURE;
+
+ segment.ray.origin += shift;
+ segment.ray.direction = glm::normalize(focalPoint - shift);
+#endif
segment.pixelIndex = index;
segment.remainingBounces = traceDepth;
}
}
+
+__host__ __device__ bool checkMeshhBoundingBox(Geom& geom, Ray& ray) {
+ Ray q;
+ q.origin = multiplyMV(geom.inverseTransform, glm::vec4(ray.origin, 1.0f));
+ q.direction = glm::normalize(multiplyMV(geom.inverseTransform, glm::vec4(ray.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 = (geom.boundingBoxMin[xyz] - q.origin[xyz]) / qdxyz;
+ float t2 = (geom.boundingBoxMax[xyz] - 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;
+ }
+ }
+ }
+
+ if (tmax >= tmin && tmax > 0) {
+ return true;
+ }
+ return false;
+}
+
// TODO:
// computeIntersections handles generating ray intersections ONLY.
// Generating new rays is handled in your shader(s).
@@ -160,6 +277,9 @@ __global__ void computeIntersections(
, Geom* geoms
, int geoms_size
, ShadeableIntersection* intersections
+ // for mesh loading
+ ,Triangle* triangles
+ ,int iter
)
{
int path_index = blockIdx.x * blockDim.x + threadIdx.x;
@@ -178,12 +298,12 @@ __global__ void computeIntersections(
glm::vec3 tmp_intersect;
glm::vec3 tmp_normal;
- // naive parse through global geoms
+ glm::vec2 uv(0.f);
+ // naive parse through global geoms
for (int i = 0; i < geoms_size; i++)
{
Geom& geom = geoms[i];
-
if (geom.type == CUBE)
{
t = boxIntersectionTest(geom, pathSegment.ray, tmp_intersect, tmp_normal, outside);
@@ -193,7 +313,19 @@ __global__ void computeIntersections(
t = sphereIntersectionTest(geom, pathSegment.ray, tmp_intersect, tmp_normal, outside);
}
// TODO: add more intersection tests here... triangle? metaball? CSG?
-
+ else if (geom.type == OBJ_MESH)
+ {
+#if MESH_BOUNDING_BOX
+ if (checkMeshhBoundingBox(geom, pathSegment.ray))
+ {
+ t = triangleIntersectionTest(geom, pathSegment.ray,
+ tmp_intersect, triangles, geom.triangleStart, geom.triangleEnd, tmp_normal, outside, uv);
+ }
+#else
+ t = triangleIntersectionTest(geom, pathSegment.ray,
+ tmp_intersect, triangles + geom.triangleIndex, triangles_size, tmp_normal, outside, uv);
+#endif
+ }
// Compute the minimum t from the intersection tests to determine what
// scene geometry object was hit first.
if (t > 0.0f && t_min > t)
@@ -208,6 +340,8 @@ __global__ void computeIntersections(
if (hit_geom_index == -1)
{
intersections[path_index].t = -1.0f;
+ // Suggested by Rhuta
+ pathSegments[path_index].remainingBounces = 0;
}
else
{
@@ -215,6 +349,8 @@ __global__ void computeIntersections(
intersections[path_index].t = t_min;
intersections[path_index].materialId = geoms[hit_geom_index].materialid;
intersections[path_index].surfaceNormal = normal;
+ intersections[path_index].uv = uv;
+ intersections[path_index].textureId = geoms[hit_geom_index].textureId;
}
}
}
@@ -273,6 +409,163 @@ __global__ void shadeFakeMaterial(
}
}
+__global__ void shadeMaterial(
+ int iter
+ , int num_paths
+ , ShadeableIntersection* shadeableIntersections
+ , PathSegment* pathSegments
+ , Material* materials
+ , Texture* textures
+ , glm::vec3* texColors
+)
+{
+ int idx = blockIdx.x * blockDim.x + threadIdx.x;
+ if (idx < num_paths && pathSegments[idx].remainingBounces >= 0)
+ {
+ ShadeableIntersection intersection = shadeableIntersections[idx];
+ // No intersection then return black and no more bounce
+ if (intersection.t <= 0.0f)
+ {
+ pathSegments[idx].color = glm::vec3(0.0f);
+ pathSegments[idx].remainingBounces = 0;
+ return;
+ }
+ thrust::default_random_engine rng = makeSeededRandomEngine(iter, idx, 0);
+
+ Material material = materials[intersection.materialId];
+ glm::vec3 materialColor = material.color;
+
+ if (intersection.textureId != -1)
+ {
+ Texture tex = textures[intersection.textureId];
+ int w = tex.width * intersection.uv[0] - 0.5;
+ int h = tex.height * (1 - intersection.uv[1]) - 0.5;
+ int colIdx = h * tex.width + w + tex.idx;
+ material.color = texColors[colIdx];
+ }
+
+ // Light source then return light color
+ if (material.emittance > 0.0f)
+ {
+ pathSegments[idx].color *= (materialColor * material.emittance);
+ pathSegments[idx].remainingBounces = 0;
+ return;
+ }
+ // ScatterRay and accumulate color
+ else
+ {
+ glm::vec3 i = getPointOnRay(pathSegments[idx].ray, intersection.t);
+
+
+ scatterRay(pathSegments[idx], i, intersection.surfaceNormal, material, rng);
+ return;
+ }
+ }
+}
+
+__global__ void shadeMaterialWithDirectLighting(
+ int iter
+ , int num_paths
+ , ShadeableIntersection* shadeableIntersections
+ , PathSegment* pathSegments
+ , Material* materials
+ , Geom* lights
+ , int lightCount
+ ,Texture* textures
+ ,glm::vec3* texColors
+)
+{
+ int idx = blockIdx.x * blockDim.x + threadIdx.x;
+ if (idx < num_paths && pathSegments[idx].remainingBounces >= 0)
+ {
+ ShadeableIntersection intersection = shadeableIntersections[idx];
+ // No intersection then return black and no more bounce
+ if (intersection.t <= 0.0f)
+ {
+ pathSegments[idx].color = glm::vec3(0.0f);
+ pathSegments[idx].remainingBounces = 0;
+ return;
+ }
+ thrust::default_random_engine rng = makeSeededRandomEngine(iter, idx, 0);
+
+ Material material = materials[intersection.materialId];
+ glm::vec3 materialColor = material.color;
+
+ glm::vec3 textureColor = glm::vec3(0.f);
+ if (intersection.textureId != -1)
+ {
+ Texture tex = textures[intersection.textureId];
+ int w = tex.width * intersection.uv[0] - 0.5;
+ int h = tex.height * (1 - intersection.uv[1]) - 0.5;
+ int colIdx = h * tex.width + w;
+ material.color = texColors[colIdx];
+ }
+
+
+ // Light source then return light color
+ if (material.emittance > 0.0f)
+ {
+ pathSegments[idx].color *= (materialColor * material.emittance);
+ pathSegments[idx].remainingBounces = 0;
+ return;
+ }
+ // ScatterRay and accumulate color
+ else
+ {
+ glm::vec3 i = getPointOnRay(pathSegments[idx].ray, intersection.t);
+
+ scatterRay(pathSegments[idx], i, intersection.surfaceNormal, material, rng);
+
+ if (pathSegments[idx].remainingBounces == 1)
+ {
+ thrust::uniform_real_distribution u01(0, 1);
+ thrust::uniform_real_distribution u02(0, lightCount-1);
+ glm::vec3 sampledLight = glm::vec3(lights[u02(rng)].transform * glm::vec4(u01(rng), u01(rng), u01(rng), 1.f));
+ pathSegments[idx].ray.direction = glm::normalize(sampledLight - pathSegments[idx].ray.origin);
+ }
+
+ return;
+ }
+ }
+}
+__global__ void postShade(
+ int iter
+ , int num_paths
+ , ShadeableIntersection* shadeableIntersections
+ , PathSegment* pathSegments)
+{
+ int idx = blockIdx.x * blockDim.x + threadIdx.x;
+ if (idx < num_paths)
+ {
+ glm::vec3 postProcessColor = glm::vec3(0.f);
+
+ glm::vec3 currentColor = pathSegments[idx].color;
+
+ // Greyscale filter
+#if GREYSCALE
+ postProcessColor = glm::vec3(0.21 * currentColor.x + 0.72 * currentColor.y + 0.07 * currentColor.z);
+#elif SEPIA
+ // Sepia
+ float adjust = 0.4f;
+ postProcessColor.r = glm::min(1.0, (currentColor.r * (1.0 - (0.607 * adjust))) + (currentColor.g * (0.769 * adjust)) + (currentColor.b * (0.189 * adjust)));
+ postProcessColor.g = glm::min(1.0, (currentColor.r * (0.349 * adjust)) + (currentColor.g * (1.0 - (0.314 * adjust))) + (currentColor.b * (0.168 * adjust)));
+ postProcessColor.b = glm::min(1.0, (currentColor.r * (0.272 * adjust)) + (currentColor.g * (0.534 * adjust)) + (currentColor.b * (1.0 - (0.869 * adjust))));
+#elif INVERTED
+ // Inverted
+ postProcessColor = glm::vec3(1.0) - currentColor;
+ // High Contrast
+#elif CONTRAST
+ postProcessColor = (currentColor - glm::vec3(0.5f)) * 1.1f + glm::vec3(0.5f);
+
+#endif
+
+ ShadeableIntersection intersection = shadeableIntersections[idx];
+ if (intersection.t > 0.0f) {
+ pathSegments[idx].color = postProcessColor;
+ }
+ }
+}
+
// Add the current iteration's output to the overall image
__global__ void finalGather(int nPaths, glm::vec3* image, PathSegment* iterationPaths)
{
@@ -341,17 +634,71 @@ void pathtrace(uchar4* pbo, int frame, int iter) {
PathSegment* dev_path_end = dev_paths + pixelcount;
int num_paths = dev_path_end - dev_paths;
+ int ori_num_paths = num_paths;
+
// --- PathSegment Tracing Stage ---
// Shoot ray into scene, bounce between objects, push shading chunks
+#if MOTION_BLUR
+ for (int i = 0; i < hst_scene->geoms.size(); i++) {
+ Geom& geom = hst_scene->geoms[i];
+ geom.translation = geom.translation + (geom.endpos - geom.translation) * (float)iter / (float)hst_scene->state.iterations;
+ geom.transform[3] = glm::vec4(geom.translation, geom.transform[3][3]);
+ geom.inverseTransform = glm::inverse(geom.transform);
+ geom.invTranspose = glm::inverseTranspose(geom.transform);
+ }
+ cudaMemcpy(dev_geoms, hst_scene->geoms.data(), hst_scene->geoms.size() * sizeof(Geom), cudaMemcpyHostToDevice);
+#endif
+
bool iterationComplete = false;
while (!iterationComplete) {
// clean shading chunks
cudaMemset(dev_intersections, 0, pixelcount * sizeof(ShadeableIntersection));
+
// tracing
dim3 numblocksPathSegmentTracing = (num_paths + blockSize1d - 1) / blockSize1d;
+
+
+ // Caching the first intersection
+#if CACHE_FIRST_BOUNCE
+ if (depth == 0 && iter == 1)
+ {
+ computeIntersections << > > (
+ depth
+ , num_paths
+ , dev_paths
+ , dev_geoms
+ , hst_scene->geoms.size()
+ , dev_intersections
+ , dev_triangles
+ ,iter
+ );
+ checkCUDAError("trace one bounce");
+ cudaDeviceSynchronize();
+ cudaMemcpy(dev_intersection_cache, dev_intersections, pixelcount * sizeof(ShadeableIntersection), cudaMemcpyDeviceToDevice);
+ }
+ else if (depth == 0)
+ {
+ cudaMemcpy(dev_intersections, dev_intersection_cache, pixelcount * sizeof(ShadeableIntersection), cudaMemcpyDeviceToDevice);
+ }
+ else
+ {
+ computeIntersections << > > (
+ depth
+ , num_paths
+ , dev_paths
+ , dev_geoms
+ , hst_scene->geoms.size()
+ , dev_intersections
+ , dev_triangles
+ , iter
+ );
+ checkCUDAError("trace one bounce");
+ cudaDeviceSynchronize();
+ }
+#else
computeIntersections << > > (
depth
, num_paths
@@ -359,28 +706,79 @@ void pathtrace(uchar4* pbo, int frame, int iter) {
, dev_geoms
, hst_scene->geoms.size()
, dev_intersections
+ , dev_triangles
+ , iter
);
checkCUDAError("trace one bounce");
cudaDeviceSynchronize();
+#endif
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 << > > (
+ //Sort rays by material
+ #if SORT_RAY
+ thrust::sort_by_key(thrust::device, dev_intersections, dev_intersections + num_paths, dev_paths, compareIntersections())
+#endif
+
+
+ // 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
+ // );
+
+#if DIRECT_LIGHTING
+ shadeMaterialWithDirectLighting << > > (
+ iter,
+ num_paths,
+ dev_intersections,
+ dev_paths,
+ dev_materials,
+ dev_lights,
+ hst_scene->lightCount,
+ dev_textures,
+ dev_texColors
+ );
+#else
+ shadeMaterial << > > (
iter,
num_paths,
dev_intersections,
dev_paths,
- dev_materials
+ dev_materials,
+ dev_textures,
+ dev_texColors
);
- iterationComplete = true; // TODO: should be based off stream compaction results.
+#endif
+
+#if POST_PROCESS
+
+ postShade << > > (
+ iter,
+ num_paths,
+ dev_intersections,
+ dev_paths
+ );
+#endif
+
+ //Stream compact
+ PathSegment* path_end = thrust::stable_partition(thrust::device, dev_paths, dev_paths + num_paths, rayTerminated());
+ num_paths = path_end - dev_paths;
+
+
+ iterationComplete = (num_paths == 0);
+
if (guiData != NULL)
{
@@ -390,7 +788,7 @@ void pathtrace(uchar4* pbo, int frame, int iter) {
// Assemble this iteration and apply it to the image
dim3 numBlocksPixels = (pixelcount + blockSize1d - 1) / blockSize1d;
- finalGather << > > (num_paths, dev_image, dev_paths);
+ finalGather << > > (ori_num_paths, dev_image, dev_paths);
///////////////////////////////////////////////////////////////////////////
diff --git a/src/scene.cpp b/src/scene.cpp
index 3fb6239a..a976315e 100644
--- a/src/scene.cpp
+++ b/src/scene.cpp
@@ -4,6 +4,10 @@
#include
#include
+#define TINYOBJLOADER_IMPLEMENTATION
+#include "tiny_obj_loader.h"
+#include
+
Scene::Scene(string filename) {
cout << "Reading scene from " << filename << " ..." << endl;
cout << " " << endl;
@@ -28,6 +32,11 @@ Scene::Scene(string filename) {
loadCamera();
cout << " " << endl;
}
+ else if (strcmp(tokens[0].c_str(), "TEXTURE") == 0)
+ {
+ loadTexture(tokens[1]);
+ cout << " " << endl;
+ }
}
}
}
@@ -37,7 +46,8 @@ int Scene::loadGeom(string objectid) {
if (id != geoms.size()) {
cout << "ERROR: OBJECT ID does not match expected number of geoms" << endl;
return -1;
- } else {
+ }
+ else {
cout << "Loading Geom " << id << "..." << endl;
Geom newGeom;
string line;
@@ -52,6 +62,16 @@ int Scene::loadGeom(string objectid) {
cout << "Creating new cube..." << endl;
newGeom.type = CUBE;
}
+ else if (strcmp(line.c_str(), "obj_mesh") == 0)
+ {
+ cout << "Creating new obj mesh..." << endl;
+ newGeom.type = OBJ_MESH;
+ utilityCore::safeGetline(fp_in, line);
+ if (!line.empty() && fp_in.good()) {
+ const char* filename = line.c_str();
+ loadObj(newGeom, filename);
+ }
+ }
}
//link material
@@ -70,10 +90,16 @@ int Scene::loadGeom(string objectid) {
//load tranformations
if (strcmp(tokens[0].c_str(), "TRANS") == 0) {
newGeom.translation = glm::vec3(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()));
+ newGeom.endpos = newGeom.translation;
} else if (strcmp(tokens[0].c_str(), "ROTAT") == 0) {
newGeom.rotation = glm::vec3(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()));
} else if (strcmp(tokens[0].c_str(), "SCALE") == 0) {
newGeom.scale = glm::vec3(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()));
+ } else if (strcmp(tokens[0].c_str(), "ENDPOS") == 0) {
+ newGeom.endpos = glm::vec3(atof(tokens[1].c_str()), atof(tokens[2].c_str()), atof(tokens[3].c_str()));
+ }
+ else if (strcmp(tokens[0].c_str(), "TEXTURE") == 0) {
+ newGeom.textureId = atoi(tokens[1].c_str());
}
utilityCore::safeGetline(fp_in, line);
@@ -84,6 +110,13 @@ int Scene::loadGeom(string objectid) {
newGeom.inverseTransform = glm::inverse(newGeom.transform);
newGeom.invTranspose = glm::inverseTranspose(newGeom.transform);
+ // Store the light sources
+ if (newGeom.materialid == 0)
+ {
+ this->lights.push_back(newGeom);
+ this->lightCount++;
+ }
+
geoms.push_back(newGeom);
return 1;
}
@@ -186,3 +219,183 @@ int Scene::loadMaterial(string materialid) {
return 1;
}
}
+
+// Reference: https://github.com/tinyobjloader/tinyobjloader
+int Scene::loadObj(Geom& geo, const char* inputfile)
+{
+ tinyobj::ObjReader reader;
+ tinyobj::ObjReaderConfig reader_config;
+
+
+ if (!reader.ParseFromFile(inputfile, reader_config)) {
+ if (!reader.Error().empty()) {
+ std::cerr << "TinyObjReader: " << reader.Error();
+ }
+ exit(1);
+ }
+ if (!reader.Warning().empty()) {
+ std::cout << "TinyObjReader: " << reader.Warning();
+ }
+
+ tinyobj::attrib_t attrib = reader.GetAttrib();
+ std::vector shapes = reader.GetShapes();
+
+ geo.triangleStart = triangles.size();
+
+ // Loop over shapes and attributes
+ for (size_t s = 0; s < shapes.size(); s++) {
+ Triangle face; // current face to be loaded
+
+ size_t idx = 0;
+ float maxX = FLT_MAX, maxY = FLT_MAX, maxZ = FLT_MAX;
+ float minX = FLT_MIN, minY = FLT_MIN, minZ = FLT_MIN;
+
+ // Loop over faces
+ for (size_t f = 0; f < shapes[s].mesh.num_face_vertices.size(); f++)
+ {
+ size_t num_v = size_t(shapes[s].mesh.num_face_vertices[f]);
+
+ glm::vec3 vert;
+ glm::vec3 norm;
+ glm::vec2 texture;
+
+ // Loop over vertices
+ for (size_t v = 0; v < num_v; v++)
+ {
+
+ tinyobj::index_t v_idx = shapes[s].mesh.indices[idx + v];
+ tinyobj::real_t vx = attrib.vertices[3 * size_t(v_idx.vertex_index) + 0];
+ tinyobj::real_t vy = attrib.vertices[3 * size_t(v_idx.vertex_index) + 1];
+ tinyobj::real_t vz = attrib.vertices[3 * size_t(v_idx.vertex_index) + 2];
+
+ vert = glm::vec3(vx, vy, vz);
+
+ // Compute the bounding box
+ if (vx > maxX)
+ {
+ maxX = vx;
+ }
+ if (vy > maxY)
+ {
+ maxY = vy;
+ }
+ if (vz > maxZ)
+ {
+ maxZ = vz;
+ }
+ if (vx < minX)
+ {
+ minX = vx;
+ }
+ if (vy < minY)
+ {
+ minY = vy;
+ }
+ if (vz < minZ)
+ {
+ minZ = vz;
+ }
+
+ if (v_idx.normal_index >= 0) {
+ tinyobj::real_t nx = attrib.normals[3 * size_t(v_idx.normal_index) + 0];
+ tinyobj::real_t ny = attrib.normals[3 * size_t(v_idx.normal_index) + 1];
+ tinyobj::real_t nz = attrib.normals[3 * size_t(v_idx.normal_index) + 2];
+ norm = glm::vec3(nx, ny, nz);
+ }
+
+ if (v_idx.texcoord_index >= 0) {
+ tinyobj::real_t tx = attrib.texcoords[2 * size_t(v_idx.texcoord_index) + 0];
+ tinyobj::real_t ty = attrib.texcoords[2 * size_t(v_idx.texcoord_index) + 1];
+ texture = glm::vec2(tx, ty);
+ }
+
+ if (v == 0)
+ {
+ face.v1 = vert;
+ face.n1 = norm;
+ face.t1 = texture;
+ }
+ else if (v == 1)
+ {
+ face.v2 = vert;
+ face.n2 = norm;
+ face.t2 = texture;
+ }
+ else if (v == 2)
+ {
+ face.v3 = vert;
+ face.n3= norm;
+ face.t3 = texture;
+ }
+ else
+ {
+ std::cout << "Quad face detected" << reader.Warning();
+ }
+ }
+ idx += num_v;
+ triangles.push_back(face);
+ geo.boundingBoxMax = glm::vec3(maxX, maxY, maxZ);
+ geo.boundingBoxMin = glm::vec3(minX, minY, minZ);
+ }
+ }
+ geo.triangleEnd = triangles.size();
+
+ return 1;
+}
+
+float noise(float x)
+{
+ float r = (sin(x * 127.1) * 43758.5453);
+ return r - (long)r;
+}
+
+int Scene::loadTexture(string textureID)
+{
+ int id = atoi(textureID.c_str());
+ std::cout << "Loading texture file: " << id <<" starting index: "<< textureColors.size() < tokens = utilityCore::tokenizeString(line);
+
+ if (strcmp(tokens[0].c_str(), "PATH") == 0) {
+ const char* filepath = tokens[1].c_str();
+ unsigned char* img = stbi_load(filepath, &width, &height, &channels, 0);
+ if (img != nullptr && width > 0 && height > 0)
+ {
+ texture.width = width;
+ texture.height = height;
+ texture.channel = channels;
+
+ for (int i = 0; i < width * height; ++i)
+ {
+ glm::vec3 col = glm::vec3(img[3 * i + 0], img[3 * i + 1] , img[3 * i + 2]) / 255.f;
+ textureColors.emplace_back(col);
+ }
+ }
+ stbi_image_free(img);
+ textures.push_back(texture);
+ return 1;
+ }
+ else if (strcmp(tokens[0].c_str(), "PROCEDURAL") == 0)
+ {
+ texture.width = 1000;
+ texture.height = 1;
+ texture.channel = 3;
+ for (float i = 0.f; i < 1000.f; ++i)
+ {
+ glm::vec3 col = glm::vec3(noise(i), noise(i * 2.f), noise(i * 3.f));
+ textureColors.emplace_back(col);
+ }
+ textures.push_back(texture);
+ return 1;
+ }
+ std::cout << "Texture path does not exist" << endl;
+ return -1;
+
+}
\ No newline at end of file
diff --git a/src/scene.h b/src/scene.h
index f29a9171..7ae3b6ea 100644
--- a/src/scene.h
+++ b/src/scene.h
@@ -16,11 +16,20 @@ class Scene {
int loadMaterial(string materialid);
int loadGeom(string objectid);
int loadCamera();
+ int loadObj(Geom&, const char*);
+ int loadTexture(string textureID);
public:
Scene(string filename);
~Scene();
std::vector geoms;
+ std::vector triangles;
+ std::vector textures;
+ std::vector textureColors;
std::vector materials;
RenderState state;
+
+ std::vector lights;
+ int lightCount = 0;
};
+
diff --git a/src/sceneStructs.h b/src/sceneStructs.h
index da4dbf30..d3e052ed 100644
--- a/src/sceneStructs.h
+++ b/src/sceneStructs.h
@@ -10,6 +10,7 @@
enum GeomType {
SPHERE,
CUBE,
+ OBJ_MESH,
};
struct Ray {
@@ -24,8 +25,37 @@ struct Geom {
glm::vec3 rotation;
glm::vec3 scale;
glm::mat4 transform;
+ glm::vec3 endpos;
glm::mat4 inverseTransform;
glm::mat4 invTranspose;
+ glm::vec3 boundingBoxMin;
+ glm::vec3 boundingBoxMax;
+ int triangleStart;
+ int triangleEnd;
+ int textureId = -1;
+};
+
+struct Triangle {
+ // Vertices
+ glm::vec3 v1;
+ glm::vec3 v2;
+ glm::vec3 v3;
+ // Normals
+ glm::vec3 n1;
+ glm::vec3 n2;
+ glm::vec3 n3;
+ // Texcoords
+ glm::vec2 t1;
+ glm::vec2 t2;
+ glm::vec2 t3;
+};
+
+struct Texture {
+ int id;
+ int channel;
+ int width;
+ int height;
+ int idx;
};
struct Material {
@@ -73,4 +103,24 @@ struct ShadeableIntersection {
float t;
glm::vec3 surfaceNormal;
int materialId;
+ glm::vec2 uv;
+ int textureId;
};
+
+struct compareIntersections
+{
+ __host__ __device__
+ bool operator()(const ShadeableIntersection& a, const ShadeableIntersection& b)
+ {
+ return a.materialId < b.materialId;
+ }
+};
+
+struct rayTerminated
+{
+ __host__ __device__
+ bool operator()(const PathSegment& pathSegment)
+ {
+ return pathSegment.remainingBounces;
+ }
+};
\ No newline at end of file
diff --git a/src/tiny_obj_loader.h b/src/tiny_obj_loader.h
new file mode 100644
index 00000000..7d0c3844
--- /dev/null
+++ b/src/tiny_obj_loader.h
@@ -0,0 +1,3333 @@
+/*
+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