szene mit vier roten kugeln aufgebaut. der renderer unterstuetzt jetzt bis zu 100 objekte
This commit is contained in:
+3
-3
@@ -4,9 +4,9 @@
|
|||||||
vec3 collisionPointSphere(ray *lightray, sphere *object)
|
vec3 collisionPointSphere(ray *lightray, sphere *object)
|
||||||
{
|
{
|
||||||
vec3 collisionPoint;
|
vec3 collisionPoint;
|
||||||
collisionPoint.x = -1;
|
collisionPoint.x = -10000000;
|
||||||
collisionPoint.y = -1;
|
collisionPoint.y = -10000000;
|
||||||
collisionPoint.z = -1;
|
collisionPoint.z = -10000000;
|
||||||
double a = lightray->direction.x * lightray->direction.x + lightray->direction.y * lightray->direction.y + lightray->direction.z * lightray->direction.z;
|
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 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)
|
double c = (lightray->origin.x * lightray->origin.x + object->origin.x * object->origin.x - 2 * lightray->origin.x * object->origin.x)
|
||||||
|
|||||||
+13
-4
@@ -36,8 +36,16 @@ void exampleSphere( int width, int height, int channels, unsigned char *image) {
|
|||||||
cam1.width = 1.920;
|
cam1.width = 1.920;
|
||||||
cam1.height = 1.080;
|
cam1.height = 1.080;
|
||||||
vec3 sphereOrigin = (vec3){0,0,5};
|
vec3 sphereOrigin = (vec3){0,0,5};
|
||||||
double sphereRadius = 2.0;
|
double sphereRadius = 1.0;
|
||||||
sphere obj = (sphere){sphereOrigin, sphereRadius};
|
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 worldUp = (vec3){0, 1, 0};
|
||||||
vec3 screenX = vec3Cross(worldUp, cam1.direction);
|
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 screenMiddlePoint = vec3Add(cam1.origin, vec3Scale(cam1.direction, cam1.screenDistance));
|
||||||
vec3 screenTopLeft = vec3Add(
|
vec3 screenTopLeft = vec3Add(
|
||||||
vec3Add(screenMiddlePoint,
|
vec3Add(screenMiddlePoint,
|
||||||
vec3Scale(screenY, -cam1.height/2.0)),
|
vec3Scale(screenY, -cam1.height/2.0)),///////////////////////////////////////////////////////////////////////////////////////////////////////////////aendern minus weg
|
||||||
vec3Scale(screenX, -cam1.width/2.0)
|
vec3Scale(screenX, -cam1.width/2.0)
|
||||||
);
|
);
|
||||||
double pixelStepX = cam1.width / cam1.resolutionX;
|
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));
|
lightRay->direction = vec3Normalize(vec3Subtract(*pixelOrigin, cam1.origin));
|
||||||
|
|
||||||
pixelColor = raytraceSphere(&obj, lightRay);
|
pixelColor = raytraceSphere(objectList, lightRay);
|
||||||
free(pixelOrigin);
|
free(pixelOrigin);
|
||||||
free(lightRay);
|
free(lightRay);
|
||||||
|
|
||||||
@@ -82,4 +90,5 @@ void exampleSphere( int width, int height, int channels, unsigned char *image) {
|
|||||||
image[step+3] = 255;
|
image[step+3] = 255;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
free(objectList);
|
||||||
}
|
}
|
||||||
|
|||||||
+27
-9
@@ -1,18 +1,36 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
#include "structs.h"
|
#include "structs.h"
|
||||||
#include "collision.h"
|
#include "collision.h"
|
||||||
#include "vectorOp.h"
|
#include "vectorOp.h"
|
||||||
|
#define MAXSPHERES 100
|
||||||
|
|
||||||
RGBA raytraceSphere(sphere *obj, ray *lightray) {
|
RGBA raytraceSphere(sphere **objectList, ray *lightray) {
|
||||||
RGBA pixel;
|
RGBA pixel = (RGBA){0,0,0,1};
|
||||||
pixel.r = 0;
|
const int lightBounces = 1;
|
||||||
pixel.g = 0;
|
sphere **spheresHitList = (sphere **)calloc( (lightBounces + 1),sizeof(sphere*) );
|
||||||
pixel.b = 0;
|
|
||||||
pixel.a = 1;
|
|
||||||
|
|
||||||
vec3 collisionPoint = collisionPointSphere(lightray, obj);
|
sphere *nearestSphere = nullptr;
|
||||||
if (collisionPoint.x == -1) {return pixel;}
|
vec3 nearestCollisionPoint = {10000,10000,10000};
|
||||||
vec3 sphereNormal = vec3Normalize(vec3Subtract(collisionPoint, obj->origin));
|
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);
|
pixel.r = -vec3Product(sphereNormal, lightray->direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
free(spheresHitList);
|
||||||
return pixel;
|
return pixel;
|
||||||
|
|
||||||
}
|
}
|
||||||
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "structs.h"
|
#include "structs.h"
|
||||||
|
|
||||||
RGBA raytraceSphere(sphere *, ray *);
|
RGBA raytraceSphere(sphere **, ray *);
|
||||||
|
|||||||
Reference in New Issue
Block a user