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

Submitting Project 2 #7

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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added PROJ1_WIN/565Raytracer/README_images/Thumbs.db
Binary file not shown.
Binary file not shown.
Binary file added PROJ1_WIN/565Raytracer/README_images/diffuse.bmp
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
335 changes: 238 additions & 97 deletions README.md

Large diffs are not rendered by default.

100 changes: 36 additions & 64 deletions scenes/sampleScene.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
MATERIAL 0 //white diffuse
RGB 0.9 0.9 0.9
MATERIAL 0 //yellow diffuse
RGB 1 1 1
SPECEX 0
SPECRGB 1 1 1
REFL 0
Expand Down Expand Up @@ -34,11 +34,11 @@ ABSCOEFF 0 0 0
RSCTCOEFF 0
EMITTANCE 0

MATERIAL 3 //red glossy
RGB .63 .26 .24
SPECEX 0
SPECRGB 1 1 1
REFL 0
MATERIAL 3 //yellow glossy
RGB 1 1 0
SPECEX 0.99999
SPECRGB .6 .6 .6
REFL 1
REFR 0
REFRIOR 2
SCATTER 0
Expand All @@ -48,9 +48,9 @@ EMITTANCE 0

MATERIAL 4 //white glossy
RGB 1 1 1
SPECEX 0
SPECEX 0.9
SPECRGB 1 1 1
REFL 0
REFL 1
REFR 0
REFRIOR 2
SCATTER 0
Expand All @@ -59,7 +59,7 @@ RSCTCOEFF 0
EMITTANCE 0

MATERIAL 5 //glass
RGB 0 0 0
RGB 1 1 1
SPECEX 0
SPECRGB 1 1 1
REFL 0
Expand All @@ -70,9 +70,9 @@ ABSCOEFF .02 5.1 5.7
RSCTCOEFF 13
EMITTANCE 0

MATERIAL 6 //green glossy
RGB .35 .48 .29
SPECEX 0
MATERIAL 6 //purple glossy
RGB .5 0 .5
SPECEX 0
SPECRGB 1 1 1
REFL 0
REFR 0
Expand Down Expand Up @@ -106,19 +106,27 @@ ABSCOEFF 0 0 0
RSCTCOEFF 0
EMITTANCE 15

MATERIAL 9 //yellow diffuse
RGB 1 1 0
SPECEX 0
SPECRGB 1 1 1
REFL 0
REFR 0
REFRIOR 0
SCATTER 0
ABSCOEFF 0 0 0
RSCTCOEFF 0
EMITTANCE 0

CAMERA
RES 800 800
FOVY 25
ITERATIONS 5000
RES 400 400
FOVY 25 25
ITERATIONS 300
FILE test.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
Expand All @@ -127,10 +135,6 @@ 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
Expand All @@ -139,10 +143,6 @@ 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
Expand All @@ -151,10 +151,6 @@ 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
Expand All @@ -163,10 +159,6 @@ 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
Expand All @@ -175,44 +167,28 @@ 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
material 5
frame 0
TRANS 0 2 0
TRANS 3.5 5 1.5
ROTAT 0 180 0
SCALE 3 3 3
frame 1
TRANS 0 2 0
ROTAT 0 180 0
SCALE 3 3 3
SCALE 2.5 2.5 2.5

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
TRANS -2 5 -2
ROTAT 0 180 0
SCALE 2.5 2.5 2.5
SCALE 3 3 3

OBJECT 7
sphere
material 6
material 9
frame 0
TRANS -2 5 -2
ROTAT 0 180 0
SCALE 3 3 3
frame 1
TRANS -2 5 -2
TRANS 2 4 -2.5
ROTAT 0 180 0
SCALE 3 3 3

Expand All @@ -222,8 +198,4 @@ 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
SCALE .3 3 3
2 changes: 1 addition & 1 deletion src/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ image::~image(){
//------------------------

float image::applyGamma(float f){
//apply gamma correction, use simple power law gamma for now.
//apply gamma correction, use simple power law gamma for now. TODO: sRGB
return pow(f/float(gamma.divisor), gamma.gamma);
}

Expand Down
125 changes: 123 additions & 2 deletions src/interactions.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,20 @@

#include "intersections.h"

#define DIFFUSE 0
#define SPECULAR 1
#define TRANSMIT 2

struct Fresnel {
float reflectionCoefficient;
float transmissionCoefficient;
};

struct AbsorptionAndScatteringProperties{
glm::vec3 absorptionCoefficient;
float reducedScatteringCoefficient;
};

//forward declaration
__host__ __device__ bool calculateScatterAndAbsorption(ray& r, float& depth, AbsorptionAndScatteringProperties& currentAbsorptionAndScattering, glm::vec3& unabsorbedColor, material m, float randomFloatForScatteringDistance, float randomFloat2, float randomFloat3);
__host__ __device__ glm::vec3 getRandomDirectionInSphere(float xi1, float xi2);
Expand All @@ -16,6 +30,71 @@ __host__ __device__ glm::vec3 calculateTransmissionDirection(glm::vec3 normal, g
__host__ __device__ glm::vec3 calculateReflectionDirection(glm::vec3 normal, glm::vec3 incident);
__host__ __device__ Fresnel calculateFresnel(glm::vec3 normal, glm::vec3 incident, float incidentIOR, float transmittedIOR, glm::vec3 reflectionDirection, glm::vec3 transmissionDirection);
__host__ __device__ glm::vec3 calculateRandomDirectionInHemisphere(glm::vec3 normal, float xi1, float xi2);
__host__ __device__ glm::vec3 computePhongTotal(ray& r, glm::vec3 intersection_point, glm::vec3 intersection_normal, material intersection_mtl, staticGeom* lights, int numberOfLights, staticGeom* geoms, int numberOfGeoms, material* materials, float time);
__host__ __device__ float computeShadowCoefficient(glm::vec3 intersection_point, staticGeom light, staticGeom* geoms, int numberOfGeoms, float time);

//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ glm::vec3 calculateTransmission(glm::vec3 absorptionCoefficient, float distance) {
return glm::vec3(0,0,0);
}

//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ bool calculateScatterAndAbsorption(ray& r, float& depth, AbsorptionAndScatteringProperties& currentAbsorptionAndScattering,
glm::vec3& unabsorbedColor, material m, float randomFloatForScatteringDistance, float randomFloat2, float randomFloat3){
return false;
}

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

float n1 = incidentIOR;
float n2 = transmittedIOR;
float n = n1 / n2;

float c1 = glm::dot(-incident, normal);
float c2 = sqrt(1 - (n*n)*(1 - c1*c1));

if (c1 > 0.0f) {
normal = -normal;
c1 = -c1;
}

glm::vec3 transmitDirection = (n*incident) + (n*c1 + c2) * normal;
return transmitDirection;
}

//TODO (OPTIONAL): IMPLEMENT THIS FUNCTION
__host__ __device__ glm::vec3 calculateReflectionDirection(glm::vec3 normal, glm::vec3 incident) {
float IdotN = glm::dot(-incident,normal);
glm::vec3 I;
if (IdotN < 0.0f) { I = incident; }
else { I = -incident; }
glm::vec3 R = glm::normalize(2*IdotN*normal - I);
return R;
}

//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;

float n1 = incidentIOR;
float n2 = transmittedIOR;
float n = n1 / n2;

float c1 = glm::dot(-incident, normal);
float c2 = sqrt(1 - (n*n)*(1 - c1*c1));

float R1 = glm::abs( (n1*c1 - n2*c2) / (n1*c1 + n2*c2) ) * glm::abs( (n1*c1 - n2*c2) / (n1*c1 + n2*c2) );
float R2 = glm::abs( (n1*c2 - n2*c1) / (n1*c2 + n2*c1) ) * glm::abs( (n1*c2 - n2*c1) / (n1*c2 + n2*c1) );

float R = (R1 + R2) / 2.0f;
float T = 1.0 - R;

fresnel.reflectionCoefficient = R;
fresnel.transmissionCoefficient = T;

return fresnel;
}

//LOOK: This function demonstrates cosine weighted random direction generation in a sphere!
__host__ __device__ glm::vec3 calculateRandomDirectionInHemisphere(glm::vec3 normal, float xi1, float xi2) {
Expand All @@ -26,7 +105,8 @@ __host__ __device__ glm::vec3 calculateRandomDirectionInHemisphere(glm::vec3 nor
float over = sqrt(1 - up * up); // sin(theta)
float around = xi2 * TWO_PI;

//Find a direction that is not the normal based off of whether or not the normal's components are all equal to sqrt(1/3) or whether or not at least one component is less than sqrt(1/3). Learned this trick from Peter Kutz.
//Find a direction that is not the normal based off of whether or not the normal's components are all equal to sqrt(1/3)
//or whether or not at least one component is less than sqrt(1/3). Learned this trick from Peter Kutz.

glm::vec3 directionNotNormal;
if (abs(normal.x) < SQRT_OF_ONE_THIRD) {
Expand All @@ -49,7 +129,48 @@ __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 = xi1;
float theta = xi2 * TWO_PI;

float r = sqrt(1-z*z);
float x = r*cos(theta);
float y = r*sin(theta);

return glm::vec3(x,y,z);
}

//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, material* m, float randomSeed){
//AbsorptionAndScatteringProperties& currentAbsorptionAndScattering

if (!m->hasReflective && !m->hasRefractive) { return DIFFUSE; }

float incidentIOR = r.currentIOR;
float transmittedIOR = m->indexOfRefraction;

glm::vec3 incident = r.direction;
glm::vec3 reflectionDirection = calculateReflectionDirection(normal, incident);
glm::vec3 transmittedDirection = calculateTransmissionDirection(normal, incident, incidentIOR, transmittedIOR);

Fresnel fresnel = calculateFresnel(normal, r.direction, incidentIOR, transmittedIOR, reflectionDirection, transmittedDirection);

double diffuse_range, specular_range;
diffuse_range = 0.2;
if (!m->hasRefractive) { specular_range = 1.0; }
else { specular_range = diffuse_range + (1.0 - diffuse_range) * fresnel.reflectionCoefficient * m->hasReflective; }

thrust::default_random_engine rng(hash(randomSeed));
thrust::uniform_real_distribution<float> u01(0,1);
float sample = (float)u01(rng);

if (sample < diffuse_range)
return DIFFUSE;
else if (sample < specular_range)
return SPECULAR;
else
return TRANSMIT;
};

#endif
Loading