szene mit vier roten kugeln aufgebaut. der renderer unterstuetzt jetzt bis zu 100 objekte

This commit is contained in:
any
2026-07-26 19:38:52 +02:00
parent 30b7d557d1
commit 76f02ac5c5
5 changed files with 45 additions and 18 deletions
+28 -10
View File
@@ -1,18 +1,36 @@
#include <stdlib.h>
#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;
}