raytracer fr eine sphere ist fertig, aber das gerenderte bild sieht komisch aus, als wrden zeilen verschoben werden und keine kugel ist zu erkennen. ausserdem gibt es jetzt vektor operationen wie das skalarprodukt.

This commit is contained in:
any
2026-07-26 17:54:45 +02:00
parent 361eaefdb5
commit 72d683c1f2
11 changed files with 150 additions and 14 deletions
+66
View File
@@ -3,6 +3,10 @@
//
#include "examples.h"
#include "structs.h"
#include "vectorOp.h"
#include "raytrace.h"
#include <stdlib.h>
void example_color(int width, int height, int channels, unsigned char *image) {
for (int i = 0; i < height * width; i++) {
@@ -17,3 +21,65 @@ void example_color(int width, int height, int channels, unsigned char *image) {
}
}
}
void exampleSphere(int height, int width, 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;
vec3 sphereOrigin = (vec3){0,0,5};
double sphereRadius = 1.0;
sphere obj = (sphere){sphereOrigin, sphereRadius};
vec3 worldUp = (vec3){0, 1, 0};
vec3 screenX = vec3Cross(worldUp, cam1.direction);
vec3 screenY = vec3Cross(screenX, cam1.direction);
vec3 screenMiddlePoint = vec3Add(cam1.origin, vec3Scale(cam1.direction, cam1.screenDistance));
vec3 screenTopLeft = vec3Add(
vec3Add(screenMiddlePoint,
vec3Scale(screenY, cam1.height)),
vec3Scale(screenX, cam1.width)
);
double pixelStepX = cam1.width / cam1.resolutionX;
double pixelStepY = cam1.height / cam1.resolutionY;
vec3 vecPixelStepX = vec3Scale(screenX, pixelStepX);
vec3 vecPixelStepY = vec3Scale(screenY, pixelStepY);
for (int i = 0; i < height * width; i++) {
int step = i *channels;
int x = i % width;
int y = i / width;
RGBA pixelColor;
vec3 *pixelOrigin = (vec3 *)malloc(sizeof(vec3));
*pixelOrigin = vec3Add(screenTopLeft,
vec3Add(
vec3Scale(vecPixelStepX, x),
vec3Scale(vecPixelStepY, y)
)
);
ray *lightRay = (ray *)malloc(sizeof(ray));;
lightRay->origin = cam1.origin;
lightRay->direction = vec3Subtract(*pixelOrigin, cam1.origin);
pixelColor = raytraceSphere(&obj, lightRay);
free(pixelOrigin);
free(lightRay);
image[step] = (int)(pixelColor.r * 255.0);
image[step+1] = 0;
image[step+2] = 0;
if (channels == 4) {
image[step+3] = 255;
}
}
}