diff --git a/Collision.c b/Collision.c index cfd3e68..efee13a 100644 --- a/Collision.c +++ b/Collision.c @@ -51,14 +51,26 @@ vec3 collisionPlane(ray *lightray, plane *object) { vec3 collisionQuad(ray *lightray, quad *object) { vec3 collisionPoint; - vec3 corner = vec3Add(vec3Add(object->v1, object->v2),object->origin); plane Plane = (plane){object->origin, object->normal}; collisionPoint = collisionPlane(lightray, &Plane); - if (collisionPoint.x < corner.x && collisionPoint.y < corner.y && collisionPoint.z < corner.z && collisionPoint.x > object->origin.x && collisionPoint.y > object->origin.y && collisionPoint.z > object->origin.z) { - return collisionPoint; - } - return (vec3){-10000000, -10000000, -10000000}; + if (collisionPoint.x <= -1000000) return collisionPoint; + + vec3 localCollisionPoint = vec3Subtract(collisionPoint, object->origin); + + double scalar1 = vec3Product(object->v1,localCollisionPoint); + double scalar2 = vec3Product(object->v2,localCollisionPoint); + if (scalar1 < 0 || scalar2 < 0) return (vec3){-10000000, -10000000, -10000000}; + + double scalarSelf1 = vec3Product(object->v1,object->v1); + double scalarSelf2 = vec3Product(object->v2,object->v2); + + + if (scalar1 / scalarSelf1 > 1 || scalar2 / scalarSelf2 > 1) return (vec3){-10000000, -10000000, -10000000}; + + return collisionPoint; + + } \ No newline at end of file diff --git a/examples.c b/examples.c index be88367..6f5509c 100644 --- a/examples.c +++ b/examples.c @@ -8,6 +8,7 @@ #include "raytrace.h" #include #include "interface.h" +#include "helper.h" void example_color(int width, int height, int channels, unsigned char **image) { for (int i = 0; i < height * width; i++) { @@ -57,6 +58,39 @@ void exampleSphere( int width, int height, int channels, unsigned char **image) + displaySzene(objectList, objectCount, cam1, width, height, channels, image); + +} + +void exampleCornell( int width, int height, int channels, unsigned char **image) { + camera cam1; + cam1.origin.x = 0; + cam1.origin.y = 0; + cam1.origin.z = 0; + cam1.direction.x = 0; + cam1.direction.y = 0; + cam1.direction.z = 1; + cam1.resolutionX = width; + cam1.resolutionY = height; + cam1.screenDistance = 1; + cam1.width = 1.920; + cam1.height = 1.080; + int objectCount = 7; + object objectList[objectCount]; + + cube makroObj1 = createCuboid((vec3){0.5,-0.5,5}, (vec3){1,0,0}, (vec3){0,1,0}, (vec3){0,0,1},(RGBA){1,0,0.5,1}); + objectList[0] = (object){1, &(makroObj1.quad1), 0}; + objectList[1] = (object){1, &(makroObj1.quad2), 0}; + objectList[2] = (object){1, &(makroObj1.quad3), 0}; + objectList[3] = (object){1, &(makroObj1.quad4), 0}; + objectList[4] = (object){1, &(makroObj1.quad5), 0}; + objectList[5] = (object){1, &(makroObj1.quad6), 0}; + + quad light1 = (quad){(vec3){0,1,5}, (vec3){0,0,1}, (vec3){0,1,0}, (vec3){0,-1,0}, (RGBA){1,1,1,1}}; + objectList[6] = (object){1, &(light1), 1}; + + + displaySzene(objectList, objectCount, cam1, width, height, channels, image); } diff --git a/examples.h b/examples.h index ed08ff6..b6e65a5 100644 --- a/examples.h +++ b/examples.h @@ -1,2 +1,3 @@ void example_color(int, int, int, unsigned char **); -void exampleSphere(int, int, int, unsigned char **); \ No newline at end of file +void exampleSphere(int, int, int, unsigned char **); +void exampleCornell( int width, int height, int channels, unsigned char **image); \ No newline at end of file diff --git a/helper.c b/helper.c index 2f47373..90edc4f 100644 --- a/helper.c +++ b/helper.c @@ -32,3 +32,53 @@ int epsilonCheck(vec3 p1, vec3 p2) { //if distance is to small, the test fails ( } return 1; } + +cube createCuboid(vec3 origin, vec3 right, vec3 up, vec3 forward, RGBA color) { + cube object; + object.quad1.origin = origin; + object.quad1.v1 = right; + object.quad1.v2 = up; + object.quad1.normal = vec3Normalize(vec3Cross(object.quad1.v1, object.quad1.v2)); + object.quad1.color = color; + + object.quad2.origin = vec3Add(origin, forward); + object.quad2.v1 = vec3Scale(forward, -1); + object.quad2.v2 = up; + object.quad2.normal = vec3Normalize(vec3Cross(object.quad2.v1, object.quad2.v2)); + object.quad2.color = color; + + object.quad3.origin = vec3Add(object.quad2.origin, right); + object.quad3.v1 = vec3Scale(right, -1); + object.quad3.v2 = up; + object.quad3.normal = vec3Normalize(vec3Cross(object.quad3.v1, object.quad3.v2)); + object.quad3.color = color; + + object.quad4.origin = vec3Add(origin, right); + object.quad4.v1 = forward; + object.quad4.v2 = up; + object.quad4.normal = vec3Normalize(vec3Cross(object.quad4.v1, object.quad4.v2)); + object.quad4.color = color; + + object.quad5.origin = origin; + object.quad5.v1 = forward; + object.quad5.v2 = right; + object.quad5.normal = vec3Normalize(vec3Cross(object.quad5.v1, object.quad5.v2)); + object.quad5.color = color; + + object.quad6.origin = vec3Add(origin, up); + object.quad6.v1 = right; + object.quad6.v2 = forward; + object.quad6.normal = vec3Normalize(vec3Cross(object.quad6.v1, object.quad6.v2)); + object.quad6.color = color; + + return object; +} + +cube createCube(vec3 origin, double size, double pitch, double yaw, double roll) { + vec3 right, up, forward; + right = vec3Scale((vec3){1,0,0}, size); + up = vec3Scale((vec3){0,1,0}, size); + forward = vec3Scale((vec3){0,0,1}, size); + + +} \ No newline at end of file diff --git a/helper.h b/helper.h index ed1cb11..60725c0 100644 --- a/helper.h +++ b/helper.h @@ -4,3 +4,5 @@ vec3 randUnitVector(); int epsilonCheck(vec3, vec3); + +cube createCuboid(vec3 origin, vec3 right, vec3 up, vec3 forward, RGBA color); diff --git a/main.c b/main.c index c74759c..f7fbe4c 100644 --- a/main.c +++ b/main.c @@ -28,7 +28,7 @@ int main() { //example_color(width, height, channels, image); - exampleSphere(width, height, channels, &image); + exampleCornell(width, height, channels, &image); stbi_write_png("C:/Users/ary/CLionProjects/Raytracer/test.png", width, height, channels, image, width * channels); diff --git a/raytrace.c b/raytrace.c index 9e73850..c2bc91c 100644 --- a/raytrace.c +++ b/raytrace.c @@ -7,6 +7,7 @@ #include "helper.h" #include #define epsilon 1e-8 +#define RAYSPERPIXEL 3 #define lightBounces 3 enum objectType { SPHERETYPE, @@ -25,6 +26,7 @@ RGBA getRayColor(RGBA colorList[], bool isLightList[]) { if (firstLightHitIndex == -1) { rayColor = (RGBA){0,0,0,1}; + return rayColor; } else { @@ -41,9 +43,9 @@ RGBA getRayColor(RGBA colorList[], bool isLightList[]) { //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}; - return rayColor; } + } RGBA raytrace(object objectList[], int objectCount, ray *lightray) { @@ -119,10 +121,9 @@ RGBA raytrace(object objectList[], int objectCount, ray *lightray) { RGBA getPixelColor(object objectList[], int objectCount, ray *lightray) { RGBA pixel = (RGBA){0,0,0,0}; - int raysPerPixel = 2; ray originalRay = *lightray; - for (int k = 0; k < raysPerPixel; k++) { + for (int k = 0; k < RAYSPERPIXEL; k++) { *lightray = originalRay; RGBA tempPixel = raytrace(objectList, objectCount, lightray); @@ -134,9 +135,9 @@ RGBA getPixelColor(object objectList[], int objectCount, ray *lightray) { } //average pixelData - pixel.r = pixel.r / raysPerPixel; - pixel.g = pixel.g / raysPerPixel; - pixel.b = pixel.b / raysPerPixel; + pixel.r = pixel.r / RAYSPERPIXEL; + pixel.g = pixel.g / RAYSPERPIXEL; + pixel.b = pixel.b / RAYSPERPIXEL; //pixel.a = pixel.a / raysPerPixel; diff --git a/structs.h b/structs.h index 2369b0a..fb687c1 100644 --- a/structs.h +++ b/structs.h @@ -51,4 +51,8 @@ struct object { int objectType; // 0 = sphere, 1 = Quad void *objectPointer; bool isLight; -}; typedef struct object object; \ No newline at end of file +}; typedef struct object object; + +struct cube { + quad quad1,quad2,quad3,quad4,quad5,quad6; +}; typedef struct cube cube; \ No newline at end of file diff --git a/test.png b/test.png index 1f6548a..ec1cf53 100644 Binary files a/test.png and b/test.png differ