NON WORKING
absoluter clutter beim versuch quads zu implementieren. refactor absolut noetig
This commit is contained in:
+27
@@ -1,6 +1,7 @@
|
|||||||
#include "structs.h"
|
#include "structs.h"
|
||||||
#include "math.h"
|
#include "math.h"
|
||||||
#include "helper.h"
|
#include "helper.h"
|
||||||
|
#include "vectorOp.h"
|
||||||
|
|
||||||
vec3 collisionPointSphere(ray *lightray, sphere *object)
|
vec3 collisionPointSphere(ray *lightray, sphere *object)
|
||||||
{
|
{
|
||||||
@@ -35,3 +36,29 @@ vec3 collisionPointSphere(ray *lightray, sphere *object)
|
|||||||
collisionPoint.z = S * lightray->direction.z + lightray->origin.z;
|
collisionPoint.z = S * lightray->direction.z + lightray->origin.z;
|
||||||
return collisionPoint;
|
return collisionPoint;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
vec3 collisionPlane(ray *lightray, plane *object) {
|
||||||
|
vec3 collisionPoint;
|
||||||
|
double r;
|
||||||
|
|
||||||
|
if (vec3Product(lightray->direction, object->normal) >= 0) return (vec3){-10000000, -10000000, -10000000};
|
||||||
|
|
||||||
|
r = vec3Product(object->normal, vec3Subtract(object->origin, lightray->origin)) * 1/(vec3Product(lightray->direction, object->normal));
|
||||||
|
|
||||||
|
collisionPoint = vec3Add(lightray->origin, vec3Scale(lightray->direction, r));
|
||||||
|
return collisionPoint;
|
||||||
|
}
|
||||||
|
|
||||||
|
vec3 collisionQuad(ray *lightray, quad *object) {
|
||||||
|
vec3 collisionPoint;
|
||||||
|
vec3 corner = vec3Add(vec3Add(object->v1, object->v2),object->origin);
|
||||||
|
plane Plane = (plane){object->origin, object->normal};
|
||||||
|
collisionPoint = collisionPlane(lightray, &Plane);
|
||||||
|
|
||||||
|
if (collisionPoint.x < corner.x && collisionPoint.y < corner.y && collisionPoint.z < corner.z && collisionPoint.x > object->origin.x && collisionPoint.y > object->origin.y && collisionPoint.z > object->origin.z) {
|
||||||
|
return collisionPoint;
|
||||||
|
}
|
||||||
|
return (vec3){-10000000, -10000000, -10000000};
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,3 +2,5 @@
|
|||||||
#include "structs.h"
|
#include "structs.h"
|
||||||
|
|
||||||
vec3 collisionPointSphere(ray *, sphere *);
|
vec3 collisionPointSphere(ray *, sphere *);
|
||||||
|
|
||||||
|
vec3 collisionQuad(ray *lightray, quad *object);
|
||||||
+56
-5
@@ -7,7 +7,7 @@
|
|||||||
#include <math.h>
|
#include <math.h>
|
||||||
#define epsilon 1e-8
|
#define epsilon 1e-8
|
||||||
|
|
||||||
RGBA raytraceSphere(sphere **objectList, ray *lightray) {
|
RGBA raytraceSphere(sphere **sphereList, int sphereCount, quad **quadList, int quadCount, ray *lightray) {
|
||||||
RGBA pixel = (RGBA){0,0,0,0};
|
RGBA pixel = (RGBA){0,0,0,0};
|
||||||
#define lightBounces 3
|
#define lightBounces 3
|
||||||
//sphere **spheresHitList = (sphere **)calloc( (lightBounces + 1),sizeof(sphere*) );
|
//sphere **spheresHitList = (sphere **)calloc( (lightBounces + 1),sizeof(sphere*) );
|
||||||
@@ -19,30 +19,59 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) {
|
|||||||
|
|
||||||
for (int k = 0; k < raysPerPixel; k++) {
|
for (int k = 0; k < raysPerPixel; k++) {
|
||||||
sphere *spheresHitList[lightBounces + 1] = {0};
|
sphere *spheresHitList[lightBounces + 1] = {0};
|
||||||
|
quad *quadHitList[lightBounces + 1] = {0};
|
||||||
vec3 spheresNormalList[lightBounces + 1] = {0};
|
vec3 spheresNormalList[lightBounces + 1] = {0};
|
||||||
*lightray = originalRay;
|
*lightray = originalRay;
|
||||||
RGBA tempPixel = (RGBA){1,1,1,1};
|
RGBA tempPixel = (RGBA){1,1,1,1};
|
||||||
sphere *nearestSphere = nullptr;
|
sphere *nearestSphere = nullptr;
|
||||||
|
quad *nearestQuad = nullptr;
|
||||||
vec3 nearestCollisionPoint = {10000,10000,10000};
|
vec3 nearestCollisionPoint = {10000,10000,10000};
|
||||||
|
RGBA objectColorList[lightBounces + 1] = {0};
|
||||||
|
|
||||||
|
|
||||||
for (int j = 0; j <= lightBounces; j++) {
|
for (int j = 0; j <= lightBounces; j++) {
|
||||||
nearestSphere = nullptr;
|
nearestSphere = nullptr;
|
||||||
|
double nearestSphereDistance = 10000000;
|
||||||
|
nearestQuad = nullptr;
|
||||||
|
double nearestQuadDistance = 10000000;
|
||||||
nearestCollisionPoint = (vec3){10000,10000,10000};
|
nearestCollisionPoint = (vec3){10000,10000,10000};
|
||||||
for (int i = 0; i < 100 && objectList[i] != nullptr; i++) {
|
|
||||||
vec3 collisionPoint = collisionPointSphere(lightray, objectList[i]);
|
for (int i = 0; i < sphereCount; i++) {
|
||||||
|
vec3 collisionPoint = collisionPointSphere(lightray, sphereList[i]);
|
||||||
|
double collisionPointDistance = 1000000;
|
||||||
if (collisionPoint.x == -10000000) {
|
if (collisionPoint.x == -10000000) {
|
||||||
|
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
if (vec3Length(vec3Subtract(collisionPoint, lightray->origin)) < vec3Length(vec3Subtract(nearestCollisionPoint, lightray->origin))) {
|
collisionPointDistance = vec3Length(vec3Subtract(collisionPoint, lightray->origin));
|
||||||
|
if (collisionPointDistance < vec3Length(vec3Subtract(nearestCollisionPoint, lightray->origin))) {
|
||||||
nearestCollisionPoint = collisionPoint;
|
nearestCollisionPoint = collisionPoint;
|
||||||
nearestSphere = objectList[i];
|
nearestSphere = sphereList[i];
|
||||||
|
nearestSphereDistance = collisionPointDistance;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < quadCount; i++) {
|
||||||
|
vec3 collisionPoint = collisionQuad(lightray, quadList[i]);
|
||||||
|
double collisionPointDistance = 1000000;
|
||||||
|
if (collisionPoint.x == -10000000) {
|
||||||
|
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
collisionPointDistance = vec3Length(vec3Subtract(collisionPoint, lightray->origin));
|
||||||
|
if (collisionPointDistance < vec3Length(vec3Subtract(nearestCollisionPoint, lightray->origin))) {
|
||||||
|
nearestCollisionPoint = collisionPoint;
|
||||||
|
nearestQuad = quadList[i];
|
||||||
|
nearestQuadDistance = collisionPointDistance;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (nearestQuadDistance > nearestSphereDistance) {
|
||||||
if (nearestSphere != nullptr) {
|
if (nearestSphere != nullptr) {
|
||||||
|
objectColorList[j] = nearestSphere->color;
|
||||||
spheresHitList[j] = nearestSphere;
|
spheresHitList[j] = nearestSphere;
|
||||||
sphereNormal = vec3Scale(vec3Subtract(nearestCollisionPoint, nearestSphere->origin), 1/(nearestSphere->radius));////////////////////////////// optimize, schreibe divide vector funktion
|
sphereNormal = vec3Scale(vec3Subtract(nearestCollisionPoint, nearestSphere->origin), 1/(nearestSphere->radius));////////////////////////////// optimize, schreibe divide vector funktion
|
||||||
spheresNormalList[j] = sphereNormal;
|
spheresNormalList[j] = sphereNormal;
|
||||||
@@ -58,6 +87,28 @@ RGBA raytraceSphere(sphere **objectList, ray *lightray) {
|
|||||||
else {
|
else {
|
||||||
sphereNormal = (vec3){1,1,1};
|
sphereNormal = (vec3){1,1,1};
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
|
||||||
|
if (nearestQuad != nullptr) {
|
||||||
|
objectColorList[j] = nearestQuad->color;
|
||||||
|
quadHitList[j] = nearestQuad;
|
||||||
|
//diffus:
|
||||||
|
lightray->direction = vec3Add(nearestQuad->normal, randUnitVector());
|
||||||
|
if (fabs(lightray->direction.x ) <= epsilon || fabs(lightray->direction.y ) <= epsilon || fabs(lightray->direction.z) <= epsilon) {
|
||||||
|
lightray->direction = nearestQuad->normal;
|
||||||
|
}
|
||||||
|
//reflektion:
|
||||||
|
//lightray->direction = vec3Add(lightray->direction, vec3Scale(sphereNormal, 2));
|
||||||
|
lightray->origin = nearestCollisionPoint;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
//sphereNormal = (vec3){1,1,1};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int firstLightHitIndex = -1;
|
int firstLightHitIndex = -1;
|
||||||
|
|||||||
@@ -34,3 +34,17 @@ struct sphere {
|
|||||||
RGBA color;
|
RGBA color;
|
||||||
int isLight;
|
int isLight;
|
||||||
}; typedef struct sphere sphere;
|
}; typedef struct sphere sphere;
|
||||||
|
|
||||||
|
struct plane {
|
||||||
|
vec3 origin;
|
||||||
|
vec3 normal;
|
||||||
|
}; typedef struct plane plane;
|
||||||
|
|
||||||
|
struct quad {
|
||||||
|
vec3 origin;
|
||||||
|
vec3 v1;
|
||||||
|
vec3 v2;
|
||||||
|
vec3 normal;
|
||||||
|
RGBA color;
|
||||||
|
int isLight;
|
||||||
|
}; typedef struct quad quad;
|
||||||
Reference in New Issue
Block a user