versuch reflexionen einzubauen. man sieht sie zwar, aber der rest ist auch noch beleuchtet

This commit is contained in:
any
2026-07-26 20:49:08 +02:00
parent 76f02ac5c5
commit 1c4dce53c1
3 changed files with 31 additions and 15 deletions
+2 -2
View File
@@ -84,8 +84,8 @@ void exampleSphere( int width, int height, int channels, unsigned char *image) {
free(lightRay); free(lightRay);
image[step] = (int)(pixelColor.r * 255.0); image[step] = (int)(pixelColor.r * 255.0);
image[step+1] = 0; image[step+1] = (int)(pixelColor.g * 255.0);
image[step+2] = 0; image[step+2] = (int)(pixelColor.b * 255.0);
if (channels == 4) { if (channels == 4) {
image[step+3] = 255; image[step+3] = 255;
} }
+19 -3
View File
@@ -8,9 +8,14 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) {
RGBA pixel = (RGBA){0,0,0,1}; RGBA pixel = (RGBA){0,0,0,1};
const int lightBounces = 1; const int lightBounces = 1;
sphere **spheresHitList = (sphere **)calloc( (lightBounces + 1),sizeof(sphere*) ); sphere **spheresHitList = (sphere **)calloc( (lightBounces + 1),sizeof(sphere*) );
int hitLightIndex = -1;
vec3 sphereNormal;
sphere *nearestSphere = nullptr; sphere *nearestSphere = nullptr;
vec3 nearestCollisionPoint = {10000,10000,10000}; vec3 nearestCollisionPoint = {10000,10000,10000};
for (int j = 0; j <= lightBounces; j++) {
nearestSphere = nullptr;
nearestCollisionPoint = (vec3){10000,10000,10000};
for (int i = 0; i < 100 && objectList[i] != nullptr; i++) { for (int i = 0; i < 100 && objectList[i] != nullptr; i++) {
vec3 collisionPoint = collisionPointSphere(lightray, objectList[i]); vec3 collisionPoint = collisionPointSphere(lightray, objectList[i]);
if (collisionPoint.x == -10000000) { if (collisionPoint.x == -10000000) {
@@ -24,12 +29,23 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) {
} }
} }
if (nearestSphere != nullptr) { if (nearestSphere != nullptr) {
vec3 sphereNormal = vec3Normalize(vec3Subtract(nearestCollisionPoint, nearestSphere->origin)); sphereNormal = vec3Normalize(vec3Subtract(nearestCollisionPoint, nearestSphere->origin));////////////////////////////// optimize divide by sphere radius instead
pixel.r = -vec3Product(sphereNormal, lightray->direction); lightray->direction = vec3Add(lightray->direction, vec3Scale(sphereNormal, 2));
lightray->origin = nearestCollisionPoint;
}
else {
sphereNormal = (vec3){0,0,0};
}
} }
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); free(spheresHitList);
return pixel; return pixel;
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 261 KiB

After

Width:  |  Height:  |  Size: 520 KiB