diff --git a/1GoodResult.png b/1GoodResult.png new file mode 100644 index 0000000..4d38794 Binary files /dev/null and b/1GoodResult.png differ diff --git a/Collision.c b/Collision.c index 5fcd321..d561f8f 100644 --- a/Collision.c +++ b/Collision.c @@ -1,5 +1,6 @@ #include "structs.h" #include "math.h" +#include "helper.h" vec3 collisionPointSphere(ray *lightray, sphere *object) { @@ -21,6 +22,10 @@ vec3 collisionPointSphere(ray *lightray, sphere *object) collisionPoint.x = S * lightray->direction.x + lightray->origin.x; collisionPoint.y = S * lightray->direction.y + lightray->origin.y; collisionPoint.z = S * lightray->direction.z + lightray->origin.z; + if (!epsilonCheck(lightray->origin, collisionPoint)) { + return (vec3){-10000000, -10000000, -10000000}; + } + return collisionPoint; } double S = (-b)/(2*a); diff --git a/examples.c b/examples.c index 2b48f77..d17a5aa 100644 --- a/examples.c +++ b/examples.c @@ -36,10 +36,11 @@ void exampleSphere( int width, int height, int channels, unsigned char **image) cam1.screenDistance = 1; cam1.width = 1.920; cam1.height = 1.080; - vec3 sphereOrigin = (vec3){0,-2,5}; + vec3 sphereOrigin = (vec3){0,-1.5,5}; double sphereRadius = 1.0; + int objectCount = 7; sphere obj1 = (sphere){sphereOrigin, sphereRadius,(RGBA){1,0.5,1,1},0}; - sphere **objectList = (sphere **) calloc(5+1, sizeof(sphere*));; + sphere **objectList = (sphere **) calloc(objectCount+1, sizeof(sphere*));; objectList[0] = &obj1; sphere obj2 = (sphere){vec3Add(sphereOrigin, (vec3){3,0,0}), sphereRadius, (RGBA){1,1,0.5,1},0}; objectList[1] = &obj2; @@ -49,7 +50,11 @@ void exampleSphere( int width, int height, int channels, unsigned char **image) objectList[3] = &obj4; sphere light1 = (sphere){(vec3){1,1,4.5}, sphereRadius, (RGBA){1,1,1,1},1}; objectList[4] = &light1; - int objectCount = 5; + sphere obj5 = (sphere){(vec3){0, -6, 6.5}, 5, (RGBA){1,0.2,0.6,1},0}; + objectList[5] = &obj5; + sphere light2 = (sphere){(vec3){1,-2,4}, sphereRadius, (RGBA){1,1,1,1},1}; + objectList[6] = &light2; + displaySzene(objectList, objectCount, cam1, width, height, channels, image); diff --git a/helper.c b/helper.c index f21d2d2..2f47373 100644 --- a/helper.c +++ b/helper.c @@ -6,6 +6,8 @@ #include #include #include "structs.h" +#include "vectorOp.h" +#define EPSILON 1e-8 vec3 randUnitVector() { double x1,x2,lengthSquared; @@ -22,3 +24,11 @@ vec3 randUnitVector() { 1.0 - 2.0 * lengthSquared}; } +int epsilonCheck(vec3 p1, vec3 p2) { //if distance is to small, the test fails (return 0) + vec3 distanceVector = vec3Subtract(p1, p2); + double sumVector = fabs(distanceVector.x) + fabs(distanceVector.y) + fabs(distanceVector.z); + if (sumVector <= EPSILON) { + return 0; + } + return 1; +} diff --git a/helper.h b/helper.h index 3f516c2..ed1cb11 100644 --- a/helper.h +++ b/helper.h @@ -2,3 +2,5 @@ #include "structs.h" vec3 randUnitVector(); + +int epsilonCheck(vec3, vec3); diff --git a/raytrace.c b/raytrace.c index 8024d0a..1c07901 100644 --- a/raytrace.c +++ b/raytrace.c @@ -4,6 +4,8 @@ #include "vectorOp.h" #define MAXSPHERES 100 #include "helper.h" +#include +#define epsilon 1e-8 RGBA raytraceSphere(sphere **objectList, ray *lightray) { RGBA pixel = (RGBA){0,0,0,0}; @@ -12,7 +14,7 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) { //vec3 *spheresNormalList = (vec3 *)calloc( lightBounces + 1,sizeof(vec3) ); int hitLightIndex = -1; vec3 sphereNormal; - int raysPerPixel = 2000; + int raysPerPixel = 20; ray originalRay = *lightray; for (int k = 0; k < raysPerPixel; k++) { @@ -46,6 +48,9 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) { spheresNormalList[j] = sphereNormal; //diffus: lightray->direction = vec3Add(sphereNormal, randUnitVector()); + if (fabs(lightray->direction.x ) <= epsilon || fabs(lightray->direction.y ) <= epsilon || fabs(lightray->direction.z) <= epsilon) { + lightray->direction = sphereNormal; + } //reflektion: //lightray->direction = vec3Add(lightray->direction, vec3Scale(sphereNormal, 2)); lightray->origin = nearestCollisionPoint; diff --git a/test.png b/test.png index 4d38794..d172143 100644 Binary files a/test.png and b/test.png differ