diff --git a/Collision.c b/Collision.c index 6f4666f..d3df9d5 100644 --- a/Collision.c +++ b/Collision.c @@ -4,9 +4,9 @@ vec3 collisionPointSphere(ray *lightray, sphere *object) { vec3 collisionPoint; - collisionPoint.x = -1; - collisionPoint.y = -1; - collisionPoint.z = -1; + collisionPoint.x = -10000000; + collisionPoint.y = -10000000; + collisionPoint.z = -10000000; double a = lightray->direction.x * lightray->direction.x + lightray->direction.y * lightray->direction.y + lightray->direction.z * lightray->direction.z; double b = 2 * ((lightray->direction.x * (lightray->origin.x - object->origin.x)) + (lightray->direction.y * (lightray->origin.y - object->origin.y)) + (lightray->direction.z * (lightray->origin.z - object->origin.z))); double c = (lightray->origin.x * lightray->origin.x + object->origin.x * object->origin.x - 2 * lightray->origin.x * object->origin.x) diff --git a/examples.c b/examples.c index f4b7d9c..2a1696f 100644 --- a/examples.c +++ b/examples.c @@ -36,8 +36,16 @@ void exampleSphere( int width, int height, int channels, unsigned char *image) { cam1.width = 1.920; cam1.height = 1.080; vec3 sphereOrigin = (vec3){0,0,5}; - double sphereRadius = 2.0; - sphere obj = (sphere){sphereOrigin, sphereRadius}; + double sphereRadius = 1.0; + sphere obj1 = (sphere){sphereOrigin, sphereRadius}; + sphere **objectList = (sphere **) calloc(4+1, sizeof(sphere*));; + objectList[0] = &obj1; + sphere obj2 = (sphere){vec3Add(sphereOrigin, (vec3){3,0,0}), sphereRadius}; + objectList[1] = &obj2; + sphere obj3 = (sphere){vec3Add(sphereOrigin, (vec3){-3,0,0}), sphereRadius}; + objectList[2] = &obj3; + sphere obj4 = (sphere){vec3Add(sphereOrigin, (vec3){1,1,3}), sphereRadius}; + objectList[3] = &obj4; vec3 worldUp = (vec3){0, 1, 0}; vec3 screenX = vec3Cross(worldUp, cam1.direction); @@ -45,7 +53,7 @@ void exampleSphere( int width, int height, int channels, unsigned char *image) { vec3 screenMiddlePoint = vec3Add(cam1.origin, vec3Scale(cam1.direction, cam1.screenDistance)); vec3 screenTopLeft = vec3Add( vec3Add(screenMiddlePoint, - vec3Scale(screenY, -cam1.height/2.0)), + vec3Scale(screenY, -cam1.height/2.0)),///////////////////////////////////////////////////////////////////////////////////////////////////////////////aendern minus weg vec3Scale(screenX, -cam1.width/2.0) ); double pixelStepX = cam1.width / cam1.resolutionX; @@ -71,7 +79,7 @@ void exampleSphere( int width, int height, int channels, unsigned char *image) { lightRay->direction = vec3Normalize(vec3Subtract(*pixelOrigin, cam1.origin)); - pixelColor = raytraceSphere(&obj, lightRay); + pixelColor = raytraceSphere(objectList, lightRay); free(pixelOrigin); free(lightRay); @@ -82,4 +90,5 @@ void exampleSphere( int width, int height, int channels, unsigned char *image) { image[step+3] = 255; } } + free(objectList); } diff --git a/raytrace.c b/raytrace.c index c54c0fe..411dd87 100644 --- a/raytrace.c +++ b/raytrace.c @@ -1,18 +1,36 @@ +#include #include "structs.h" #include "collision.h" #include "vectorOp.h" +#define MAXSPHERES 100 -RGBA raytraceSphere(sphere *obj, ray *lightray) { - RGBA pixel; - pixel.r = 0; - pixel.g = 0; - pixel.b = 0; - pixel.a = 1; +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*) ); - vec3 collisionPoint = collisionPointSphere(lightray, obj); - if (collisionPoint.x == -1) {return pixel;} - vec3 sphereNormal = vec3Normalize(vec3Subtract(collisionPoint, obj->origin)); - pixel.r = -vec3Product(sphereNormal, lightray->direction); + 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) { + + } + else{ + if (vec3Length(collisionPoint)< vec3Length(nearestCollisionPoint)) { + nearestCollisionPoint = collisionPoint; + nearestSphere = objectList[i]; + } + + } + } + if (nearestSphere != nullptr) { + vec3 sphereNormal = vec3Normalize(vec3Subtract(nearestCollisionPoint, nearestSphere->origin)); + pixel.r = -vec3Product(sphereNormal, lightray->direction); + } + + + free(spheresHitList); return pixel; } \ No newline at end of file diff --git a/raytrace.h b/raytrace.h index 471bef8..cd49199 100644 --- a/raytrace.h +++ b/raytrace.h @@ -1,4 +1,4 @@ #pragma once #include "structs.h" -RGBA raytraceSphere(sphere *, ray *); +RGBA raytraceSphere(sphere **, ray *); diff --git a/test.png b/test.png index 199c8e9..8d55da7 100644 Binary files a/test.png and b/test.png differ