diff --git a/PROJ1_WIN/565Raytracer/565Raytracer.vcxproj b/PROJ1_WIN/565Raytracer/565Raytracer.vcxproj index fcc853d..987df92 100755 --- a/PROJ1_WIN/565Raytracer/565Raytracer.vcxproj +++ b/PROJ1_WIN/565Raytracer/565Raytracer.vcxproj @@ -56,7 +56,7 @@ - + @@ -78,7 +78,7 @@ Level3 Disabled WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) - C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\include;C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\common\inc;../shared/glew/include;../shared/freeglut/include;%(AdditionalIncludeDirectories) + C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\include;C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.0\C\common\inc;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\include;C:\ProgramData\NVIDIA Corporation\NVIDIA GPU Computing SDK 4.2\C\common\inc;../shared/glew/include;../shared/freeglut/include;%(AdditionalIncludeDirectories) Console @@ -91,7 +91,7 @@ $(ProjectDir)$(Platform)/$(Configuration)/%(Filename)%(Extension).obj - C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\include;C:/ProgramData/NVIDIA Corporation/NVIDIA GPU Computing SDK 4.0/C/common/inc;../shared/glew/includes;../shared/freeglut/includes + C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.0\include;C:/ProgramData/NVIDIA Corporation/NVIDIA GPU Computing SDK 4.0/C/common/inc;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v4.2\include;C:/ProgramData/NVIDIA Corporation/NVIDIA GPU Computing SDK 4.2/C/common/inc;../shared/glew/includes;../shared/freeglut/includes @@ -121,6 +121,6 @@ - + \ No newline at end of file diff --git a/PROJ1_WIN/565Raytracer/565Raytracer.vcxproj.user b/PROJ1_WIN/565Raytracer/565Raytracer.vcxproj.user index d7ca222..ca60186 100755 --- a/PROJ1_WIN/565Raytracer/565Raytracer.vcxproj.user +++ b/PROJ1_WIN/565Raytracer/565Raytracer.vcxproj.user @@ -4,4 +4,8 @@ scene="../../scenes/sampleScene.txt" WindowsLocalDebugger + + frame=0 scene="C:\Users\wjboone\Documents\GitHub\Project1-Raytracer\scenes\sampleScene.txt" + WindowsLocalDebugger + \ No newline at end of file diff --git a/PROJ1_WIN/Debug/565Raytracer.exe b/PROJ1_WIN/Debug/565Raytracer.exe new file mode 100644 index 0000000..d33209e Binary files /dev/null and b/PROJ1_WIN/Debug/565Raytracer.exe differ diff --git a/PROJ1_WIN/Debug/freeglut.dll b/PROJ1_WIN/Debug/freeglut.dll new file mode 100644 index 0000000..4ec8893 Binary files /dev/null and b/PROJ1_WIN/Debug/freeglut.dll differ diff --git a/PROJ1_WIN/Debug/glew32.dll b/PROJ1_WIN/Debug/glew32.dll new file mode 100644 index 0000000..fc4d8d8 Binary files /dev/null and b/PROJ1_WIN/Debug/glew32.dll differ diff --git a/PROJ1_WIN/Debug/glut32.dll b/PROJ1_WIN/Debug/glut32.dll new file mode 100644 index 0000000..106646f Binary files /dev/null and b/PROJ1_WIN/Debug/glut32.dll differ diff --git a/README.md b/README.md index 4a65ba6..8088e94 100644 --- a/README.md +++ b/README.md @@ -7,168 +7,29 @@ Due Sunday, 09/30/2012 ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- -NOTE: +Raytracer ------------------------------------------------------------------------------- -This project requires an NVIDIA graphics card with CUDA capability! Any card after the Geforce 8xxx series will work. If you do not have an NVIDIA graphics card in the machine you are working on, feel free to use any machine in the SIG Lab or in Moore100 labs. All machines in the SIG Lab and Moore100 are equipped with CUDA capable NVIDIA graphics cards. If this too proves to be a problem, please contact Patrick or Karl as soon as possible. +For the ray tracer project I've implemented all of the base features for the project in an accumulation approach. Each iteration of the ray tracer renders the scene as a standard ray tracer would and then the output of this operation is accumulated and then averaged to produce one mean image at the end. I've chosen to implement in addition to the base set of features, soft shadows and depth of field. Since none of my features incorporate the bouncing of rays (no reflection, no refraction, etc.) I have implemented the ray tracer in a ray parallel format. Unfortunately, due to hardware constraints, my project runs considerably slower than I would have liked. This will be looked into more in depth in the future. ------------------------------------------------------------------------------- -INTRODUCTION: +Soft shadows ------------------------------------------------------------------------------- -In this project, you will implement a CUDA based raytracer capable of generating raytraced rendered images extremely quickly. For those of you who have taken CIS460/560, building a raytracer should not be anything new to you from a conceptual point of you. For those of you that have not taken CIS460/560, raytracing is a technique for generating images by tracing rays of light through pixels in an image plane out into a scene and following the way the rays of light bounce and interact with objects in the scene. More information can be found here: http://en.wikipedia.org/wiki/Ray_tracing_(graphics). - -The ultimate purpose of this project is to serve as the foundation for your next project: a full CUDA based global illumination pathtracer. Raytracing can be thought of as a way to generate an isolated version of the direct light contribution in a global illumination scenario. - -Since in this class we are concerned with working in generating actual images and less so with mundane tasks like file I/O, this project includes basecode for loading a scene description file format, described below, and various other things that generally make up the render "harness" that takes care of everything up to the rendering itself. The core renderer is left for you to implement. -Finally, note that while this basecode is meant to serve as a strong starting point for a CUDA raytracer, you are not required to use this basecode if you wish, and you may also change any part of the basecode specification as you please, so long as the final rendered result is correct. - -------------------------------------------------------------------------------- -CONTENTS: -------------------------------------------------------------------------------- -The Project1 root directory contains the following subdirectories: - -* src/ contains the source code for the project. Both the Windows Visual Studio solution and the OSX makefile reference this folder for all source; the base source code compiles on OSX and Windows without modification. -* scenes/ contains an example scene description file. -* renders/ contains an example render of the given example scene file. -* PROJ1_WIN/ contains a Windows Visual Studio 2010 project and all dependencies needed for building and running on Windows 7. -* PROJ1_OSX/ contains a OSX makefile, run script, and all dependencies needed for building and running on Mac OSX 10.8. - -The Windows and OSX versions of the project build and run exactly the same way as in Project0. - -------------------------------------------------------------------------------- -REQUIREMENTS: -------------------------------------------------------------------------------- -In this project, you are given code for: - -* Loading, reading, and storing the TAKUAscene scene description format -* Example functions that can run on both the CPU and GPU for generating random numbers, spherical intersection testing, and surface point sampling on cubes -* A class for handling image operations and saving images -* Working code for CUDA-GL interop - -You will need to implement the following features: - -* Raycasting from a camera into a scene through a pixel grid -* Phong lighting for one point light source -* Diffuse lambertian surfaces -* Raytraced shadows -* Cube intersection testing -* Sphere surface point sampling - -You are also required to implement at least 2 of the following features: - -* Specular reflection -* Soft shadows and area lights -* Texture mapping -* Bump mapping -* Depth of field -* Supersampled antialiasing -* Refraction, i.e. glass -* OBJ Mesh loading and renderin -* Interactive camera - -------------------------------------------------------------------------------- -BASE CODE TOUR: -------------------------------------------------------------------------------- -You will be working in three files: raytraceKernel.cu, intersections.h, and interactions.h. Within these files, areas that you need to complete are marked with a TODO comment. Areas that are useful to and serve as hints for optional features are marked with TODO (Optional). Functions that are useful for reference are marked with the comment LOOK. - -* raytraceKernel.cu contains the core raytracing CUDA kernel. You will need to complete: - * cudaRaytraceCore() handles kernel launches and memory management; this function already contains example code for launching kernels, transferring geometry and cameras from the host to the device, and transferring image buffers from the host to the device and back. You will have to complete this function to support passing materials and lights to CUDA. - * raycastFromCameraKernel() is a function that you need to implement. This function once correctly implemented should handle camera raycasting. - * raytraceRay() is the core raytracing CUDA kernel; all of your raytracing logic should be implemented in this CUDA kernel. raytraceRay() should take in a camera, image buffer, geometry, materials, and lights, and should trace a ray through the scene and write the resultant color to a pixel in the image buffer. - -* intersections.h contains functions for geometry intersection testing and point generation. You will need to complete: - * boxIntersectionTest(), which takes in a box and a ray and performs an intersection test. This function should work in the same way as sphereIntersectionTest(). - * getRandomPointOnSphere(), which takes in a sphere and returns a random point on the surface of the sphere with an even probability distribution. This function should work in the same way as getRandomPointOnCube(). You can (although do not necessarily have to) use this to generate points on a sphere to use a point lights, or can use this for area lighting. - -* interactions.h contains functions for ray-object interactions that define how rays behave upon hitting materials and objects. You will need to complete: - * getRandomDirectionInSphere(), which generates a random direction in a sphere with a uniform probability. This function works in a fashion similar to that of calculateRandomDirectionInHemisphere(), which generates a random cosine-weighted direction in a hemisphere. - * calculateBSDF(), which takes in an incoming ray, normal, material, and other information, and returns an outgoing ray. You can either implement this function for ray-surface interactions, or you can replace it with your own function(s). - -You will also want to familiarize yourself with: - -* sceneStructs.h, which contains definitions for how geometry, materials, lights, cameras, and animation frames are stored in the renderer. -* utilities.h, which serves as a kitchen-sink of useful functions - -------------------------------------------------------------------------------- -NOTES ON GLM: -------------------------------------------------------------------------------- -This project uses GLM, the GL Math library, for linear algebra. You need to know two important points on how GLM is used in this project: - -* In this project, indices in GLM vectors (such as vec3, vec4), are accessed via swizzling. So, instead of v[0], v.x is used, and instead of v[1], v.y is used, and so on and so forth. -* GLM Matrix operations work fine on NVIDIA Fermi cards and later, but pre-Fermi cards do not play nice with GLM matrices. As such, in this project, GLM matrices are replaced with a custom matrix struct, called a cudaMat4, found in cudaMat4.h. A custom function for multiplying glm::vec4s and cudaMat4s is provided as multiplyMV() in intersections.h. - -------------------------------------------------------------------------------- -TAKUAscene FORMAT: -------------------------------------------------------------------------------- -This project uses a custom scene description format, called TAKUAscene. TAKUAscene files are flat text files that describe all geometry, materials, lights, cameras, render settings, and animation frames inside of the scene. Items in the format are delimited by new lines, and comments can be added at the end of each line preceded with a double-slash. - -Materials are defined in the following fashion: - -* MATERIAL (material ID) //material header -* RGB (float r) (float g) (float b) //diffuse color -* SPECX (float specx) //specular exponent -* SPECRGB (float r) (float g) (float b) //specular color -* REFL (bool refl) //reflectivity flag, 0 for no, 1 for yes -* REFR (bool refr) //refractivity flag, 0 for no, 1 for yes -* REFRIOR (float ior) //index of refraction for Fresnel effects -* SCATTER (float scatter) //scatter flag, 0 for no, 1 for yes -* ABSCOEFF (float r) (float b) (float g) //absorption coefficient for scattering -* RSCTCOEFF (float rsctcoeff) //reduced scattering coefficient -* EMITTANCE (float emittance) //the emittance of the material. Anything >0 makes the material a light source. - -Cameras are defined in the following fashion: - -* CAMERA //camera header -* RES (float x) (float y) //resolution -* FOVY (float fovy) //vertical field of view half-angle. the horizonal angle is calculated from this and the reslution -* ITERATIONS (float interations) //how many iterations to refine the image, only relevant for supersampled antialiasing, depth of field, area lights, and other distributed raytracing applications -* FILE (string filename) //file to output render to upon completion -* frame (frame number) //start of a frame -* EYE (float x) (float y) (float z) //camera's position in worldspace -* VIEW (float x) (float y) (float z) //camera's view direction -* UP (float x) (float y) (float z) //camera's up vector - -Objects are defined in the following fashion: -* OBJECT (object ID) //object header -* (cube OR sphere OR mesh) //type of object, can be either "cube", "sphere", or "mesh". Note that cubes and spheres are unit sized and centered at the origin. -* material (material ID) //material to assign this object -* frame (frame number) //start of a frame -* TRANS (float transx) (float transy) (float transz) //translation -* ROTAT (float rotationx) (float rotationy) (float rotationz) //rotation -* SCALE (float scalex) (float scaley) (float scalez) //scale - -An example TAKUAscene file setting up two frames inside of a Cornell Box can be found in the scenes/ directory. +To render my scene with soft shadows, I model the light source as being a collection of point light sources along its surface. In the code this is reflected by choosing a random point on the surface of the geometry emitting light when calculating the light ray from the intersection on the scene geometry to the light source instead of using the center of mass as a single point source. I use the iteration number as the seed for the random value and ignore the thread index. Calculating the seed this way gives the image (in my opinion) a more interesting and less grainy feel as it is being created. ------------------------------------------------------------------------------- -BLOG +Depth of Field ------------------------------------------------------------------------------- -As mentioned in class, all students should have student blogs detailing progress on projects. If you already have a blog, you can use it; otherwise, please create a blog using www.blogger.com or any other tool, such as www.wordpress.org. Blog posts on your project are due on the SAME DAY as the project, and should include: - -* A brief description of the project and the specific features you implemented. -* A link to your github repo if the code is open source. -* At least one screenshot of your project running. -* A 30 second or longer video of your project running. To create the video use http://www.microsoft.com/expression/products/Encoder4_Overview.aspx +Giving the scene depth of field was a relatively straight forward task. When calculating the ray to cast into the scene I calculate it as normal, then I pivot by a small random amount about a point some distance (the focal distance, in my program it is hard coded in for simplicity to be 15 scene units). As in the soft shadows I only use the iteration number as the seed to give it a more interesting feel. ------------------------------------------------------------------------------- -THIRD PARTY CODE POLICY +Blog ------------------------------------------------------------------------------- -* Use of any third-party code must be approved by asking on Piazza. If it is approved, all students are welcome to use it. Generally, we approve use of third-party code that is not a core part of the project. For example, for the ray tracer, we would approve using a third-party library for loading models, but would not approve copying and pasting a CUDA function for doing refraction. -* Third-party code must be credited in README.md. -* Using third-party code without its approval, including using another student's code, is an academic integrity violation, and will result in you receiving an F for the semester. +http://liamboone.blogspot.com/2012/09/project-1-raytracer.html ------------------------------------------------------------------------------- -SELF-GRADING -------------------------------------------------------------------------------- -* On the submission date, email your grade, on a scale of 0 to 100, to Karl, yiningli@seas.upenn.edu, with a one paragraph explanation. Be concise and realistic. Recall that we reserve 30 points as a sanity check to adjust your grade. Your actual grade will be (0.7 * your grade) + (0.3 * our grade). We hope to only use this in extreme cases when your grade does not realistically reflect your work - it is either too high or too low. In most cases, we plan to give you the exact grade you suggest. -* Projects are not weighted evenly, e.g., Project 0 doesn't count as much as the path tracer. We will determine the weighting at the end of the semester based on the size of each project. - +Building & Running ------------------------------------------------------------------------------- -SUBMISSION -------------------------------------------------------------------------------- -As with the previous project, you should fork this project and work inside of your fork. Upon completion, commit your finished project back to your fork, and make a pull request to the master repository. -You should include a README.md file in the root directory detailing the following - -* A brief description of the project and specific features you implemented -* At least one screenshot of your project running, and at least one screenshot of the final rendered output of your raytracer -* Instructions for building and running your project if they differ from the base code -* A link to your blog post detailing the project -* A list of all third-party code used \ No newline at end of file +This code uses CUDA 4.2 +I've tried to include all dll dependencies in the repo. +I've left the 'Release' mode in visual studio untouched to allow for ease of testing on 4.0 machines +If there are any problems please let me know, I'll be happy to do a fresh clone and run on my laptop. diff --git a/scenes/sampleScene.old.txt b/scenes/sampleScene.old.txt new file mode 100644 index 0000000..936135b --- /dev/null +++ b/scenes/sampleScene.old.txt @@ -0,0 +1,229 @@ +MATERIAL 0 //white diffuse +RGB 1 1 1 +SPECEX 0 +SPECRGB 1 1 1 +REFL 0 +REFR 0 +REFRIOR 0 +SCATTER 0 +ABSCOEFF 0 0 0 +RSCTCOEFF 0 +EMITTANCE 0 + +MATERIAL 1 //red diffuse +RGB .63 .06 .04 +SPECEX 0 +SPECRGB 1 1 1 +REFL 0 +REFR 0 +REFRIOR 0 +SCATTER 0 +ABSCOEFF 0 0 0 +RSCTCOEFF 0 +EMITTANCE 0 + +MATERIAL 2 //green diffuse +RGB .15 .48 .09 +SPECEX 0 +SPECRGB 1 1 1 +REFL 0 +REFR 0 +REFRIOR 0 +SCATTER 0 +ABSCOEFF 0 0 0 +RSCTCOEFF 0 +EMITTANCE 0 + +MATERIAL 3 //red glossy +RGB .63 .06 .04 +SPECEX 0 +SPECRGB 1 1 1 +REFL 0 +REFR 0 +REFRIOR 2 +SCATTER 0 +ABSCOEFF 0 0 0 +RSCTCOEFF 0 +EMITTANCE 0 + +MATERIAL 4 //white glossy +RGB 1 1 1 +SPECEX 0 +SPECRGB 1 1 1 +REFL 0 +REFR 0 +REFRIOR 2 +SCATTER 0 +ABSCOEFF 0 0 0 +RSCTCOEFF 0 +EMITTANCE 0 + +MATERIAL 5 //glass +RGB 0 0 0 +SPECEX 0 +SPECRGB 1 1 1 +REFL 0 +REFR 1 +REFRIOR 2.2 +SCATTER 0 +ABSCOEFF .02 5.1 5.7 +RSCTCOEFF 13 +EMITTANCE 0 + +MATERIAL 6 //green glossy +RGB .15 .48 .09 +SPECEX 0 +SPECRGB 1 1 1 +REFL 0 +REFR 0 +REFRIOR 2.6 +SCATTER 0 +ABSCOEFF 0 0 0 +RSCTCOEFF 0 +EMITTANCE 0 + +MATERIAL 7 //light +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +SCATTER 0 +ABSCOEFF 0 0 0 +RSCTCOEFF 0 +EMITTANCE 1 + +MATERIAL 8 //light +RGB 1 1 1 +SPECEX 0 +SPECRGB 0 0 0 +REFL 0 +REFR 0 +REFRIOR 0 +SCATTER 0 +ABSCOEFF 0 0 0 +RSCTCOEFF 0 +EMITTANCE 15 + +CAMERA +RES 800 800 +FOVY 25 +ITERATIONS 5000 +FILE renders/sampleScene.bmp +frame 0 +EYE 0 4.5 12 +VIEW 0 0 -1 +UP 0 1 0 +frame 1 +EYE 0 4.5 12 +VIEW 0 0 -1 +UP 0 1 0 + +OBJECT 0 +cube +material 0 +frame 0 +TRANS 0 0 0 +ROTAT 0 0 90 +SCALE .01 10 10 +frame 1 +TRANS 0 0 0 +ROTAT 0 0 90 +SCALE .01 10 10 + +OBJECT 1 +cube +material 0 +frame 0 +TRANS 0 5 -5 +ROTAT 0 90 0 +SCALE .01 10 10 +frame 1 +TRANS 0 5 -5 +ROTAT 0 90 0 +SCALE .01 10 10 + +OBJECT 2 +cube +material 0 +frame 0 +TRANS 0 10 0 +ROTAT 0 0 90 +SCALE .01 10 10 +frame 1 +TRANS 0 10 0 +ROTAT 0 0 90 +SCALE .01 10 10 + +OBJECT 3 +cube +material 1 +frame 0 +TRANS -5 5 0 +ROTAT 0 0 0 +SCALE .01 10 10 +frame 1 +TRANS -5 5 0 +ROTAT 0 0 0 +SCALE .01 10 10 + +OBJECT 4 +cube +material 2 +frame 0 +TRANS 5 5 0 +ROTAT 0 0 0 +SCALE .01 10 10 +frame 1 +TRANS 5 5 0 +ROTAT 0 0 0 +SCALE .01 10 10 + +OBJECT 5 +sphere +material 4 +frame 0 +TRANS 0 2 0 +ROTAT 0 180 0 +SCALE 3 3 3 +frame 1 +TRANS 0 2 0 +ROTAT 0 180 0 +SCALE 3 3 3 + +OBJECT 6 +sphere +material 3 +frame 0 +TRANS 2 5 2 +ROTAT 0 180 0 +SCALE 2.5 2.5 2.5 +frame 1 +TRANS 2 5 2 +ROTAT 0 180 0 +SCALE 2.5 2.5 2.5 + +OBJECT 7 +sphere +material 6 +frame 0 +TRANS -2 5 -2 +ROTAT 0 180 0 +SCALE 3 3 3 +frame 1 +TRANS -2 5 -2 +ROTAT 0 180 0 +SCALE 3 3 3 + +OBJECT 8 +cube +material 8 +frame 0 +TRANS 0 10 0 +ROTAT 0 0 90 +SCALE .3 3 3 +frame 1 +TRANS 0 10 0 +ROTAT 0 0 90 +SCALE .3 3 3 \ No newline at end of file diff --git a/scenes/sampleScene.txt b/scenes/sampleScene.txt index 936135b..67b510e 100755 --- a/scenes/sampleScene.txt +++ b/scenes/sampleScene.txt @@ -107,16 +107,16 @@ RSCTCOEFF 0 EMITTANCE 15 CAMERA -RES 800 800 +RES 400 400 FOVY 25 ITERATIONS 5000 -FILE renders/sampleScene.bmp +FILE sampleScene.bmp frame 0 EYE 0 4.5 12 VIEW 0 0 -1 UP 0 1 0 frame 1 -EYE 0 4.5 12 +EYE 0 4.5 15 VIEW 0 0 -1 UP 0 1 0 @@ -182,11 +182,11 @@ SCALE .01 10 10 OBJECT 5 sphere -material 4 +material 8 frame 0 -TRANS 0 2 0 +TRANS 0 2 3 ROTAT 0 180 0 -SCALE 3 3 3 +SCALE 2 2 2 frame 1 TRANS 0 2 0 ROTAT 0 180 0 @@ -220,7 +220,7 @@ OBJECT 8 cube material 8 frame 0 -TRANS 0 10 0 +TRANS 0 9.84 0 ROTAT 0 0 90 SCALE .3 3 3 frame 1 diff --git a/src/interactions.h b/src/interactions.h index e18cfff..4d75b91 100755 --- a/src/interactions.h +++ b/src/interactions.h @@ -90,16 +90,23 @@ __host__ __device__ glm::vec3 calculateRandomDirectionInHemisphere(glm::vec3 nor //Now that you know how cosine weighted direction generation works, try implementing non-cosine (uniform) weighted random direction generation. //This should be much easier than if you had to implement calculateRandomDirectionInHemisphere. __host__ __device__ glm::vec3 getRandomDirectionInSphere(float xi1, float xi2) { - return glm::vec3(0,0,0); + + //using Archimedes theorem (thanks to a paper from '96 published bu our own Norm Badler (http://repository.upenn.edu/cgi/viewcontent.cgi?article=1188&context=cis_reports) ) + xi1 *= 2; + xi1 -= 1; + float r = sin( acos( xi1 ) ); + xi2 *= TWO_PI; + return glm::vec3(r*cos(xi2),r*sin(xi2),xi1); } //TODO (PARTIALLY OPTIONAL): IMPLEMENT THIS FUNCTION //returns 0 if diffuse scatter, 1 if reflected, 2 if transmitted. __host__ __device__ int calculateBSDF(ray& r, glm::vec3 intersect, glm::vec3 normal, glm::vec3 emittedColor, AbsorptionAndScatteringProperties& currentAbsorptionAndScattering, - glm::vec3& color, glm::vec3& unabsorbedColor, material m){ + glm::vec3& color, glm::vec3& unabsorbedColor, material m) +{ - return 1; + return 1; }; #endif diff --git a/src/intersections.h b/src/intersections.h index 2d9dcc8..950fe09 100755 --- a/src/intersections.h +++ b/src/intersections.h @@ -17,7 +17,7 @@ __host__ __device__ glm::vec3 getPointOnRay(ray r, float t); __host__ __device__ glm::vec3 multiplyMV(cudaMat4 m, glm::vec4 v); __host__ __device__ glm::vec3 getSignOfRay(ray r); __host__ __device__ glm::vec3 getInverseDirectionOfRay(ray r); -__host__ __device__ float boxIntersectionTest(staticGeom sphere, ray r, glm::vec3& intersectionPoint, glm::vec3& normal); +__host__ __device__ float boxIntersectionTest(staticGeom sphere, ray r, glm::vec3& intersectionPoint, glm::vec3& normal, glm::vec3 * debugcolor); __host__ __device__ float sphereIntersectionTest(staticGeom sphere, ray r, glm::vec3& intersectionPoint, glm::vec3& normal); __host__ __device__ glm::vec3 getRandomPointOnCube(staticGeom cube, float randomSeed); @@ -70,9 +70,57 @@ __host__ __device__ glm::vec3 getSignOfRay(ray r){ //TODO: IMPLEMENT THIS FUNCTION //Cube intersection test, return -1 if no intersection, otherwise, distance to intersection -__host__ __device__ float boxIntersectionTest(staticGeom box, ray r, glm::vec3& intersectionPoint, glm::vec3& normal){ - - return -1; +__host__ __device__ float boxIntersectionTest(staticGeom box, ray r, glm::vec3& intersectionPoint, glm::vec3& normal, glm::vec3 * debugcolor = NULL) +{ + glm::vec3 ro = multiplyMV(box.inverseTransform, glm::vec4(r.origin,1.0f)); + glm::vec3 rd = glm::normalize(multiplyMV(box.inverseTransform, glm::vec4(r.direction,0.0f))); + + ray rt; rt.origin = ro; rt.direction = rd; + + glm::vec3 tlfb = ( glm::vec3(-0.5,-0.5,-0.5) - ro ) / rd; + glm::vec3 trbt = ( glm::vec3(0.5,0.5,0.5) - ro ) / rd; + + glm::vec3 tmin = glm::min( tlfb, trbt ); + glm::vec3 tmax = glm::max( tlfb, trbt ); + + if( tmax.x < tmin.y || tmax.x < tmin.z || tmax.y < tmin.x || + tmax.y < tmin.z || tmax.z < tmin.x || tmax.z < tmin.y ) + { + return -1; + } + + float t = ( tmin.x > tmin.y ? tmin.x : tmin.y ); + t = ( t > tmin.z ? t : tmin.z ); + + if( t < 0 ) return -1; + + glm::vec3 realNormal = glm::vec3( 1, 1, 1 ); + + //glm::vec4 point = glm::vec4( getPointOnRay(rt, t), 1.0f ); + glm::vec4 point = glm::vec4( rt.origin + rt.direction*t, 1.0f ); + + if( debugcolor ) *debugcolor = glm::vec3( point.x + 0.5, point.y + 0.5, point.z + 0.5 ); + + if( fabs( point.x - 0.5 ) < 2e-4 ) + realNormal = glm::vec3( 1, 0, 0 ); + else if( fabs( point.x + 0.5 ) < 2e-4 ) + realNormal = glm::vec3( -1, 0, 0 ); + else if( fabs( point.y - 0.5 ) < 2e-4 ) + realNormal = glm::vec3( 0, 1, 0 ); + else if( fabs( point.y + 0.5 ) < 2e-4 ) + realNormal = glm::vec3( 0, -1, 0 ); + else if( fabs( point.z - 0.5 ) < 2e-4 ) + realNormal = glm::vec3( 0, 0, 1 ); + else if( fabs( point.z + 0.5 ) < 2e-4 ) + realNormal = glm::vec3( 0, 0, -1 ); + + glm::vec3 realIntersectionPoint = multiplyMV(box.transform, point); + glm::vec3 realOrigin = multiplyMV(box.transform, glm::vec4(0,0,0,1)); + + normal = glm::normalize(multiplyMV(box.transform, glm::vec4(realNormal,0.0f))); + intersectionPoint = realIntersectionPoint; + + return glm::length(r.origin - realIntersectionPoint); } //LOOK: Here's an intersection test example from a sphere. Now you just need to figure out cube and, optionally, triangle. @@ -176,8 +224,13 @@ __host__ __device__ glm::vec3 getRandomPointOnCube(staticGeom cube, float random //TODO: IMPLEMENT THIS FUNCTION //Generates a random point on a given sphere __host__ __device__ glm::vec3 getRandomPointOnSphere(staticGeom sphere, float randomSeed){ - - return glm::vec3(0,0,0); + thrust::default_random_engine rng(hash(randomSeed)); + thrust::uniform_real_distribution u01(-1,1); + thrust::uniform_real_distribution u02(0,TWO_PI); + float xi1 = (float)u01(rng); + float r = sin( acos( xi1 ) ); + float xi2 = (float)u02(rng); + return multiplyMV( sphere.transform, glm::vec4( glm::vec3(r*cos(xi2),r*sin(xi2),xi1), 1.0f ) ); } #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 4e94892..473a755 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -141,7 +141,7 @@ void runCuda(){ } gammaSettings gamma; - gamma.applyGamma = true; + gamma.applyGamma = false; gamma.gamma = 1.0/2.2; gamma.divisor = renderCam->iterations; outputImage.setGammaSettings(gamma); diff --git a/src/raytraceKernel.cu b/src/raytraceKernel.cu index dae7b03..cf9e6a0 100755 --- a/src/raytraceKernel.cu +++ b/src/raytraceKernel.cu @@ -38,11 +38,34 @@ __host__ __device__ glm::vec3 generateRandomNumberFromThread(glm::vec2 resolutio //TODO: IMPLEMENT THIS FUNCTION //Function that does the initial raycast from the camera -__host__ __device__ ray raycastFromCameraKernel(glm::vec2 resolution, float time, int x, int y, glm::vec3 eye, glm::vec3 view, glm::vec3 up, glm::vec2 fov){ - ray r; - r.origin = glm::vec3(0,0,0); - r.direction = glm::vec3(0,0,-1); - return r; +__host__ __device__ ray raycastFromCameraKernel(glm::vec2 resolution, float time, int index, int x, int y, glm::vec3 eye, glm::vec3 view, glm::vec3 up, glm::vec2 fov){ + ray r; + float xstep = 2 * tan( PI / 180.0f * fov.x ) / resolution.x; + float ystep = 2 * tan( PI / 180.0f * fov.y ) / resolution.y; + + glm::vec3 right = -glm::cross( view, up ); + up = glm::cross( right, view ); + + glm::vec3 botleft = view - (xstep*resolution.x/2)*right - (ystep*resolution.y/2)*up; + + glm::vec3 raycast = botleft + (float)x*xstep*right + (float)y*ystep*up; + + raycast = glm::normalize(raycast); + + //right = glm::cross(raycast, up); + thrust::default_random_engine rng(hash(time/**resolution.x*resolution.y+index*/)); + thrust::uniform_real_distribution X(0,2*PI); + thrust::uniform_real_distribution Y(0,PI); + + float theta = (float)X(rng); + float R = 0.3*sin( (float)Y(rng) ); + + glm::vec3 ref = eye + raycast*15.0f; + eye = eye + cos(theta)*up*R + sin(theta)*right*R; + raycast = glm::normalize(ref-eye); + + r.origin = eye; r.direction = raycast; + return r; } //Kernel that blacks out a given image buffer @@ -66,8 +89,8 @@ __global__ void sendImageToPBO(uchar4* PBOpos, glm::vec2 resolution, glm::vec3* glm::vec3 color; color.x = image[index].x*255.0; - color.y = image[index].x*255.0; - color.z = image[index].x*255.0; + color.y = image[index].y*255.0; + color.z = image[index].z*255.0; if(color.x>255){ color.x = 255; @@ -92,16 +115,125 @@ __global__ void sendImageToPBO(uchar4* PBOpos, glm::vec2 resolution, glm::vec3* //TODO: IMPLEMENT THIS FUNCTION //Core raytracer kernel __global__ void raytraceRay(glm::vec2 resolution, float time, cameraData cam, int rayDepth, glm::vec3* colors, - staticGeom* geoms, int numberOfGeoms){ + staticGeom* geoms, int numberOfGeoms, int numberOfCubes, material* materials, int numberOfMaterials, + int* lights, int numberOfLights) +{ + int x = (blockIdx.x * blockDim.x) + threadIdx.x; + int y = (blockIdx.y * blockDim.y) + threadIdx.y; + int index = x + (y * resolution.x); - int x = (blockIdx.x * blockDim.x) + threadIdx.x; - int y = (blockIdx.y * blockDim.y) + threadIdx.y; - int index = x + (y * resolution.x); + if((x<=resolution.x && y<=resolution.y)){ + ray cast = raycastFromCameraKernel( resolution, time, index, x, y, cam.position, cam.view, cam.up, cam.fov); + + glm::vec3 interPoint, tmpInterPoint; + glm::vec3 normal, tmpNormal; + glm::vec3 dcol( 0 ); + float len = 999999.99999f, tmpLen; + + int object = -1; + + for( int i = 0; i < numberOfCubes; i ++ ) + { + tmpLen = boxIntersectionTest( geoms[i], cast, tmpInterPoint, tmpNormal ); + if( tmpLen < len && tmpLen > 0 ) + { + len = tmpLen; + normal = tmpNormal; + interPoint = tmpInterPoint; + object = i; + } + } + + for( int i = numberOfCubes; i < numberOfGeoms; i ++ ) + { + tmpLen = sphereIntersectionTest( geoms[i], cast, tmpInterPoint, tmpNormal ); + if( tmpLen < len && tmpLen > 0 ) + { + len = tmpLen; + normal = tmpNormal; + interPoint = tmpInterPoint; + object = i; + } + } - if((x<=resolution.x && y<=resolution.y)){ + if( object == -1 ) + return; + + glm::vec3 light = glm::vec3( 0 ); + int matid = geoms[object].materialid; - colors[index] = generateRandomNumberFromThread(resolution, time, x, y); - } + glm::vec3 color = glm::vec3( 0 );// * materials[matid].color; + + if( materials[matid].emittance > 0 ) + { + color = materials[matid].color; + + } + else + { + ray shadowcast; + for( int j = 0; j < numberOfLights; j ++ ) + { + bool hasLight = true; + //get the point to use + glm::vec3 lightPos; + if( geoms[lights[j]].type == CUBE ) + { + lightPos = getRandomPointOnCube(geoms[lights[j]], time/* * resolution.x * resolution.y + index*/); + } + else + { + lightPos = getRandomPointOnSphere(geoms[lights[j]], time/* * resolution.x * resolution.y + index*/); + } + glm::vec3 lnorm = lightPos - interPoint; + + + float lDist = glm::length( lnorm ); + lnorm = glm::normalize( lnorm ); + + shadowcast.direction = lnorm; + shadowcast.origin = interPoint; + + for( int i = 0; i < numberOfCubes; i ++ ) + { + if( i != lights[j] ) + { + tmpLen = boxIntersectionTest( geoms[i], shadowcast, tmpInterPoint, tmpNormal ); + if( tmpLen > 0 && tmpLen < lDist ) + { + hasLight = false; + break; + } + } + } + for( int i = numberOfCubes; i < numberOfGeoms; i ++ ) + { + if( i != lights[j] && hasLight ) + { + tmpLen = sphereIntersectionTest( geoms[i], shadowcast, tmpInterPoint, tmpNormal ); + if( tmpLen > 0 && tmpLen < lDist ) + { + hasLight = false; + break; + } + } + } + float diffuseC = 0.0; + if( hasLight ) + { + diffuseC = 1; + } + float diffuse = max( (float) glm::dot( lnorm, normal ), 0.0f ) * diffuseC; + glm::vec3 H = glm::normalize( lnorm - cast.direction ); + float specular = 0; + if( materials[matid].indexOfRefraction > 0 ) + specular = pow( max( (float) glm::dot( H, normal ), 0.0f ), materials[matid].specularExponent+1000 ); + color += materials[matid].color * materials[geoms[lights[j]].materialid].color * diffuse / (float) numberOfLights; + color += materials[matid].specularColor * materials[geoms[lights[j]].materialid].color * materials[geoms[lights[j]].materialid].emittance * specular / (float) numberOfLights; + } + } + colors[index] = colors[index]*(time-1)/time + color/time; + } } @@ -121,8 +253,21 @@ void cudaRaytraceCore(uchar4* PBOpos, camera* renderCam, int frame, int iteratio cudaMalloc((void**)&cudaimage, (int)renderCam->resolution.x*(int)renderCam->resolution.y*sizeof(glm::vec3)); cudaMemcpy( cudaimage, renderCam->image, (int)renderCam->resolution.x*(int)renderCam->resolution.y*sizeof(glm::vec3), cudaMemcpyHostToDevice); + int numberOfLights = 0; + int* lights = new int[numberOfGeoms]; + //package geometry and materials and sent to GPU staticGeom* geomList = new staticGeom[numberOfGeoms]; + int nCubes = 0; + for(int i=0; i 0 ) + { + lights[numberOfLights] = c; + numberOfLights ++; + } + c++; + } + else + { + geomList[s] = newStaticGeom; + if( materials[newStaticGeom.materialid].emittance > 0 ) + { + lights[numberOfLights] = s; + numberOfLights ++; + } + s++; + } } staticGeom* cudageoms = NULL; cudaMalloc((void**)&cudageoms, numberOfGeoms*sizeof(staticGeom)); cudaMemcpy( cudageoms, geomList, numberOfGeoms*sizeof(staticGeom), cudaMemcpyHostToDevice); + material* cudamater = NULL; + cudaMalloc((void**)&cudamater, numberOfMaterials*sizeof(material)); + cudaMemcpy( cudamater, materials, numberOfMaterials*sizeof(material), cudaMemcpyHostToDevice); + + int* cudalights = NULL; + cudaMalloc((void**)&cudalights, numberOfLights*sizeof(int)); + cudaMemcpy( cudalights, lights, numberOfLights*sizeof(int), cudaMemcpyHostToDevice); + //package camera cameraData cam; cam.resolution = renderCam->resolution; @@ -148,7 +320,9 @@ void cudaRaytraceCore(uchar4* PBOpos, camera* renderCam, int frame, int iteratio cam.fov = renderCam->fov; //kernel launches - raytraceRay<<>>(renderCam->resolution, (float)iterations, cam, traceDepth, cudaimage, cudageoms, numberOfGeoms); + if( iterations == 0 ) clearImage<<>>( renderCam->resolution, cudaimage ); + + raytraceRay<<>>(renderCam->resolution, (float)iterations, cam, traceDepth, cudaimage, cudageoms, numberOfGeoms, nCubes, cudamater, numberOfMaterials, cudalights, numberOfLights); sendImageToPBO<<>>(PBOpos, renderCam->resolution, cudaimage); @@ -158,7 +332,10 @@ void cudaRaytraceCore(uchar4* PBOpos, camera* renderCam, int frame, int iteratio //free up stuff, or else we'll leak memory like a madman cudaFree( cudaimage ); cudaFree( cudageoms ); + cudaFree( cudamater ); + cudaFree( cudalights ); delete geomList; + delete lights; // make certain the kernel has completed cudaThreadSynchronize();