diff --git a/raytrace.c b/raytrace.c index 889d7ef..001fd37 100644 --- a/raytrace.c +++ b/raytrace.c @@ -6,8 +6,9 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) { RGBA pixel = (RGBA){0,0,0,1}; - const int lightBounces = 1; + const int lightBounces = 5; sphere **spheresHitList = (sphere **)calloc( (lightBounces + 1),sizeof(sphere*) ); + vec3 *spheresNormalList = (vec3 *)calloc( lightBounces + 1,sizeof(vec3) ); int hitLightIndex = -1; vec3 sphereNormal; @@ -32,7 +33,9 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) { if (nearestSphere != nullptr) { - sphereNormal = vec3Normalize(vec3Subtract(nearestCollisionPoint, nearestSphere->origin));////////////////////////////// optimize divide by sphere radius instead + spheresHitList[j] = nearestSphere; + sphereNormal = vec3Scale(vec3Subtract(nearestCollisionPoint, nearestSphere->origin), 1/(nearestSphere->radius));////////////////////////////// optimize, schreibe divide vector funktion + spheresNormalList[j] = sphereNormal; lightray->direction = vec3Add(lightray->direction, vec3Scale(sphereNormal, 2)); lightray->origin = nearestCollisionPoint; } @@ -41,11 +44,32 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) { } } + int firstLightHitIndex = -1; + for (int i = 0; i <= lightBounces && spheresHitList[i] != nullptr; i++) { + if (spheresHitList[i]->isLight == 1) { + firstLightHitIndex = i; + break; + } + } + if (firstLightHitIndex == -1) { + pixel = (RGBA){0,0,0,1}; + } + else + { pixel = spheresHitList[firstLightHitIndex]->color; + for (int i = firstLightHitIndex - 1; i >= 0; i--) { + pixel.r = pixel.r * spheresHitList[i]->color.r; + pixel.g = pixel.g * spheresHitList[i]->color.g; + pixel.b = pixel.b * spheresHitList[i]->color.b; + pixel.a = pixel.a * spheresHitList[i]->color.a; + } + //pixel.r = -vec3Product(sphereNormal, lightray->direction); - pixel = (RGBA){1 - 0.5 *( sphereNormal.x + 1), 1 - 0.5 * (sphereNormal.y +1), 1 -0.5* (sphereNormal.z + 1),1}; + //pixel = (RGBA){1 - 0.5 *( sphereNormal.x + 1), 1 - 0.5 * (sphereNormal.y +1), 1 -0.5* (sphereNormal.z + 1),1}; + } free(spheresHitList); + free(spheresNormalList); return pixel; } \ No newline at end of file diff --git a/test.png b/test.png index 10e6154..cae9d98 100644 Binary files a/test.png and b/test.png differ