Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes for Ray Tracer #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions MyReadme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
*** Ray Tracer ***

Implementation Details
The goal was to create a ray tracer on the GPU using CUDA.

I initially thought of implrmenting parallelism using pixels directly. But then I realized then it would be a simple loop within the kernel function executing the recursion. So I tried packing up the rays and creating threads based on how many rays were currently available. This greatly decreased the number of threads being used as most of the diffuse surfaces just returned color without any reflections or refraction. This also helped me implement refraction as each ray could now give rise to 2 rays.

The bad part was that I did the compaction of the list on the CPU and transferred it back. So I was gaining memory by releasing threads, but I was loosing time as I was going back to the CPU. I did not get time to implement the stream compaction on the GPU as that would have been the ideal solution. Maybe will try it in the path tracer.


Features implemented:
* Diffuse Shading
* Phong's specular shading
* Fresnel's equation to calculate transmittance and reflectance using the refractive index.
* Specular Reflection
* Refraction
* Anti-aliasing

Blog:
http://cudaraytracer.blogspot.com/

Binary file modified PROJ1_MAC/bin/565raytracer
Binary file not shown.
2 changes: 2 additions & 0 deletions PROJ1_WIN/565Raytracer/565Raytracer.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
<CudaCompile>
<CompileOut>$(ProjectDir)$(Platform)/$(Configuration)/%(Filename)%(Extension).obj</CompileOut>
<Include>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</Include>
<CodeGeneration>compute_20,sm_20</CodeGeneration>
</CudaCompile>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
Expand All @@ -117,6 +118,7 @@
<CudaCompile>
<CompileOut>$(ProjectDir)$(Platform)/$(Configuration)/%(Filename)%(Extension).obj</CompileOut>
<Include>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</Include>
<CodeGeneration>compute_20,sm_20</CodeGeneration>
</CudaCompile>
</ItemDefinitionGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
6 changes: 5 additions & 1 deletion PROJ1_WIN/565Raytracer/565Raytracer.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LocalDebuggerCommandArguments>scene="../../scenes/sampleScene.txt"</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>scene=../../scenes/sampleScene_1.txt</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>scene=../../scenes/sampleScene.txt</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
Binary file added PROJ1_WIN/565Raytracer/renders/AntiAlias.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PROJ1_WIN/565Raytracer/renders/AntiAliasing.bmp
Binary file not shown.
Binary file added PROJ1_WIN/565Raytracer/renders/Diffuse.bmp
Binary file not shown.
Binary file added PROJ1_WIN/565Raytracer/renders/DiffuseShading.bmp
Binary file not shown.
Binary file added PROJ1_WIN/565Raytracer/renders/Final.bmp
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added PROJ1_WIN/565Raytracer/renders/FlatShading.bmp
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added PROJ1_WIN/565Raytracer/renders/Refraction.bmp
Binary file not shown.
Binary file added PROJ1_WIN/565Raytracer/renders/Running.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added PROJ1_WIN/565Raytracer/renders/Specular.bmp
Binary file not shown.
Binary file not shown.
Binary file added PROJ1_WIN/565Raytracer/renders/sampleScene1.0.bmp
Binary file not shown.
Binary file not shown.
Binary file added ProjectRunning.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions scenes/sampleScene.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ EMITTANCE 0

MATERIAL 3 //red glossy
RGB .63 .06 .04
SPECEX 0
SPECEX 10
SPECRGB 1 1 1
REFL 0
REFR 0
Expand Down Expand Up @@ -109,8 +109,8 @@ EMITTANCE 15
CAMERA
RES 800 800
FOVY 25
ITERATIONS 5000
FILE renders/sampleScene.bmp
ITERATIONS 1
FILE renders/sampleScene1.bmp
frame 0
EYE 0 4.5 12
VIEW 0 0 -1
Expand Down
63 changes: 54 additions & 9 deletions src/interactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,56 @@ __host__ __device__ bool calculateScatterAndAbsorption(ray& r, float& depth, Ab

//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ glm::vec3 calculateTransmissionDirection(glm::vec3 normal, glm::vec3 incident, float incidentIOR, float transmittedIOR) {
return glm::vec3(0,0,0);
float n12 = incidentIOR/transmittedIOR;

float cos1 = glm::dot(normal, glm::vec3(-incident.x, -incident.y, -incident.z));
float rootValue = 1 - pow(n12,2)*(1.0f-pow(cos1,2));
if (rootValue < 0)
{
return calculateReflectionDirection(normal, incident);
}

if (cos1 > 0.0)
return glm::normalize(normal*(n12*cos1 - sqrt(rootValue)) + incident*n12);
else
return glm::normalize(normal*(-n12*cos1 + sqrt(rootValue)) + incident*n12);
}

//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ glm::vec3 calculateReflectionDirection(glm::vec3 normal, glm::vec3 incident) {
//nothing fancy here
return glm::vec3(0,0,0);
// Rr = Ri - 2N(Ri.N)
float dotProd = glm::dot(incident, normal);
return glm::normalize(incident - normal*2.0f*dotProd);
}

//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ Fresnel calculateFresnel(glm::vec3 normal, glm::vec3 incident, float incidentIOR, float transmittedIOR, glm::vec3 reflectionDirection, glm::vec3 transmissionDirection) {
Fresnel fresnel;

fresnel.reflectionCoefficient = 1;
fresnel.transmissionCoefficient = 0;
return fresnel;
__host__ __device__ Fresnel calculateFresnel(glm::vec3 normal, glm::vec3 incident, float incidentIOR, float transmittedIOR) {
Fresnel fresnel;

float cosIncidence = abs(glm::dot(incident, normal));
float sinIncidence = sqrt(1-pow(cosIncidence,2));

if (transmittedIOR > 0.0 && incidentIOR > 0)
{
float commonNumerator = sqrt(1-pow(((incidentIOR/transmittedIOR)*sinIncidence),2));
float RsNumerator = incidentIOR*cosIncidence-transmittedIOR*commonNumerator;
float RsDenominator = incidentIOR*cosIncidence+transmittedIOR*commonNumerator;
float Rs = pow((RsNumerator/RsDenominator),2);

float RpNumerator = (incidentIOR * commonNumerator) - (transmittedIOR * cosIncidence);
float RpDenominator = (incidentIOR * commonNumerator) + (transmittedIOR * cosIncidence);
float Rp = pow((RpNumerator/RpDenominator),2);

fresnel.reflectionCoefficient = (Rs + Rp)/2.0;
fresnel.transmissionCoefficient = 1 - fresnel.reflectionCoefficient;
}
else
{
fresnel.reflectionCoefficient = 1;
fresnel.transmissionCoefficient = 0;
}
return fresnel;
}

//LOOK: This function demonstrates cosine weighted random direction generation in a sphere!
Expand Down Expand Up @@ -90,7 +124,18 @@ __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);
float z = 1.0f - 2.0f*xi1;
float temp = 1.0f - z*z;
float r;
if (temp < 0.0f)
r = 0.0f;
else
r = sqrtf(temp);

float phi = 2.f * PI * xi2;
float x = r * cosf(phi);
float y = r * sinf(phi);
return glm::normalize(glm::vec3(x, y, z));
}

//TODO (PARTIALLY OPTIONAL): IMPLEMENT THIS FUNCTION
Expand Down
Loading