36 lines
1.1 KiB
C
36 lines
1.1 KiB
C
#include <stdlib.h>
|
|
#include "structs.h"
|
|
#include "collision.h"
|
|
#include "vectorOp.h"
|
|
#define MAXSPHERES 100
|
|
|
|
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*) );
|
|
|
|
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;
|
|
|
|
} |