diff --git a/examples.c b/examples.c index 2a1696f..f14c2a3 100644 --- a/examples.c +++ b/examples.c @@ -84,8 +84,8 @@ void exampleSphere( int width, int height, int channels, unsigned char *image) { free(lightRay); image[step] = (int)(pixelColor.r * 255.0); - image[step+1] = 0; - image[step+2] = 0; + image[step+1] = (int)(pixelColor.g * 255.0); + image[step+2] = (int)(pixelColor.b * 255.0); if (channels == 4) { image[step+3] = 255; } diff --git a/raytrace.c b/raytrace.c index 411dd87..7b1818f 100644 --- a/raytrace.c +++ b/raytrace.c @@ -8,28 +8,44 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) { RGBA pixel = (RGBA){0,0,0,1}; const int lightBounces = 1; sphere **spheresHitList = (sphere **)calloc( (lightBounces + 1),sizeof(sphere*) ); + int hitLightIndex = -1; + vec3 sphereNormal; sphere *nearestSphere = nullptr; vec3 nearestCollisionPoint = {10000,10000,10000}; - for (int i = 0; i < 100 && objectList[i] != nullptr; i++) { - vec3 collisionPoint = collisionPointSphere(lightray, objectList[i]); - if (collisionPoint.x == -10000000) { + for (int j = 0; j <= lightBounces; j++) { + nearestSphere = nullptr; + nearestCollisionPoint = (vec3){10000,10000,10000}; + for (int i = 0; i < 100 && objectList[i] != nullptr; i++) { + vec3 collisionPoint = collisionPointSphere(lightray, objectList[i]); + if (collisionPoint.x == -10000000) { - } - else{ - if (vec3Length(collisionPoint)< vec3Length(nearestCollisionPoint)) { - nearestCollisionPoint = collisionPoint; - nearestSphere = objectList[i]; } + else{ + if (vec3Length(collisionPoint)< vec3Length(nearestCollisionPoint)) { + nearestCollisionPoint = collisionPoint; + nearestSphere = objectList[i]; + } + } + } + + + if (nearestSphere != nullptr) { + sphereNormal = vec3Normalize(vec3Subtract(nearestCollisionPoint, nearestSphere->origin));////////////////////////////// optimize divide by sphere radius instead + lightray->direction = vec3Add(lightray->direction, vec3Scale(sphereNormal, 2)); + lightray->origin = nearestCollisionPoint; + } + else { + sphereNormal = (vec3){0,0,0}; } } - if (nearestSphere != nullptr) { - vec3 sphereNormal = vec3Normalize(vec3Subtract(nearestCollisionPoint, nearestSphere->origin)); - pixel.r = -vec3Product(sphereNormal, lightray->direction); + + if (nearestSphere != nullptr) + { + //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}; } - - free(spheresHitList); return pixel; diff --git a/test.png b/test.png index 8d55da7..116c575 100644 Binary files a/test.png and b/test.png differ