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
+3 -3
View File
@@ -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)
+13 -4
View File
@@ -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);
}
+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;
}
+1 -1
View File
@@ -1,4 +1,4 @@
#pragma once
#include "structs.h"
RGBA raytraceSphere(sphere *, ray *);
RGBA raytraceSphere(sphere **, ray *);
BIN
View File
Binary file not shown.

Before

Width:  |  Height:  |  Size: 251 KiB

After

Width:  |  Height:  |  Size: 261 KiB