Refactor
code ist jetzt schner aber ich habe durch einen pointer fehler in raytrace.c (nearestObject = ¤tObject //zeigt immer auf die selbne adresse) ein art negativ bild erzeugt
This commit is contained in:
+119
-130
@@ -2,156 +2,145 @@
|
||||
#include "structs.h"
|
||||
#include "collision.h"
|
||||
#include "vectorOp.h"
|
||||
#define MAXSPHERES 100
|
||||
#define MAXOBJECTS 100
|
||||
#define BADPOINT (vec3){-10000000,-10000000,-10000000}
|
||||
#include "helper.h"
|
||||
#include <math.h>
|
||||
#define epsilon 1e-8
|
||||
#define lightBounces 3
|
||||
enum objectType {
|
||||
SPHERETYPE,
|
||||
QUADTYPE
|
||||
};
|
||||
|
||||
RGBA raytraceSphere(sphere **sphereList, int sphereCount, quad **quadList, int quadCount, ray *lightray) {
|
||||
RGBA getRayColor(RGBA colorList[], bool isLightList[]) {
|
||||
int firstLightHitIndex = -1;
|
||||
RGBA rayColor = {1,1,1,1};
|
||||
for (int i = 0; i <= lightBounces; i++) {
|
||||
if (isLightList[i] == 1) {
|
||||
firstLightHitIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (firstLightHitIndex == -1) {
|
||||
rayColor = (RGBA){0,0,0,1};
|
||||
}
|
||||
else
|
||||
{
|
||||
//pixel.r += spheresHitList[firstLightHitIndex]->color.r;
|
||||
//pixel.g += spheresHitList[firstLightHitIndex]->color.g;
|
||||
//pixel.b += spheresHitList[firstLightHitIndex]->color.b;
|
||||
//pixel.a += spheresHitList[firstLightHitIndex]->color.a;
|
||||
for (int i = firstLightHitIndex; i >= 0; i--) {
|
||||
rayColor.r = rayColor.r * colorList[i].r;
|
||||
rayColor.g = rayColor.g * colorList[i].g;
|
||||
rayColor.b = rayColor.b * colorList[i].b;
|
||||
//rayColor.a = rayColor.a * colorList[i].a;
|
||||
}
|
||||
|
||||
//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};
|
||||
|
||||
return rayColor;
|
||||
}
|
||||
}
|
||||
|
||||
RGBA raytrace(object objectList[], int objectCount, ray *lightray) {
|
||||
RGBA objectColorList[lightBounces + 1] = {0};
|
||||
object objectHitList[lightBounces + 1] = {0};
|
||||
bool isLightList[lightBounces + 1] = {false};
|
||||
|
||||
for (int j = 0; j <= lightBounces; j++) {
|
||||
object currentObject;
|
||||
object *nearestObject = nullptr;
|
||||
double nearestObjectDistance = 10000000;
|
||||
vec3 nearestCollisionPoint = (vec3){10000,10000,10000};
|
||||
double nearestCollisionPointDistance = 10000000;
|
||||
|
||||
|
||||
|
||||
|
||||
for (int i = 0; i < objectCount; i++) {
|
||||
currentObject = objectList[i];
|
||||
vec3 collisionPoint;
|
||||
|
||||
switch (currentObject.objectType) {
|
||||
case SPHERETYPE:
|
||||
collisionPoint = collisionPointSphere(lightray, (sphere*)(objectList[i].objectPointer));
|
||||
break;
|
||||
case QUADTYPE:
|
||||
collisionPoint = collisionQuad(lightray, (quad*)(objectList[i].objectPointer));
|
||||
break;
|
||||
}
|
||||
|
||||
double collisionPointDistance = 1000000;
|
||||
if (collisionPoint.x != BADPOINT.x) {
|
||||
collisionPointDistance = vec3Length(vec3Subtract(collisionPoint, lightray->origin));
|
||||
if (collisionPointDistance < nearestCollisionPointDistance) {
|
||||
nearestCollisionPoint = collisionPoint;
|
||||
nearestCollisionPointDistance = collisionPointDistance;
|
||||
nearestObject = ¤tObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
vec3 objectNormal = BADPOINT;
|
||||
if (nearestObject != nullptr) {
|
||||
switch (nearestObject->objectType) {
|
||||
case SPHERETYPE:
|
||||
sphere *nearestSphere = (sphere*)(nearestObject->objectPointer);
|
||||
objectColorList[j] = nearestSphere->color;
|
||||
objectNormal = vec3Scale(vec3Subtract(nearestCollisionPoint, nearestSphere->origin), 1/(nearestSphere->radius));////////////////////////////// optimize, schreibe divide vector funktion
|
||||
break;
|
||||
case QUADTYPE:
|
||||
quad *nearestQuad = (quad*)(nearestObject->objectPointer);
|
||||
objectColorList[j] = nearestQuad->color;
|
||||
objectNormal = nearestQuad->normal;
|
||||
break;
|
||||
}
|
||||
objectHitList[j] = *nearestObject;
|
||||
isLightList[j] = nearestObject->isLight;
|
||||
|
||||
//diffus:
|
||||
lightray->direction = vec3Normalize(vec3Add(objectNormal, randUnitVector())); //maybe kann man sich durch umstrukturierung das normalisieren einsparen
|
||||
if (fabs(lightray->direction.x ) <= epsilon || fabs(lightray->direction.y ) <= epsilon || fabs(lightray->direction.z) <= epsilon) {
|
||||
lightray->direction = objectNormal;
|
||||
}
|
||||
//reflektion:
|
||||
//lightray->direction = vec3Add(lightray->direction, vec3Scale(sphereNormal, 2));
|
||||
lightray->origin = nearestCollisionPoint;
|
||||
}
|
||||
}
|
||||
return getRayColor(objectColorList, isLightList);
|
||||
|
||||
}
|
||||
|
||||
|
||||
RGBA getPixelColor(object objectList[], int objectCount, ray *lightray) {
|
||||
RGBA pixel = (RGBA){0,0,0,0};
|
||||
#define lightBounces 3
|
||||
//sphere **spheresHitList = (sphere **)calloc( (lightBounces + 1),sizeof(sphere*) );
|
||||
//vec3 *spheresNormalList = (vec3 *)calloc( lightBounces + 1,sizeof(vec3) );
|
||||
int hitLightIndex = -1;
|
||||
vec3 sphereNormal;
|
||||
int raysPerPixel = 20;
|
||||
int raysPerPixel = 2;
|
||||
ray originalRay = *lightray;
|
||||
|
||||
for (int k = 0; k < raysPerPixel; k++) {
|
||||
sphere *spheresHitList[lightBounces + 1] = {0};
|
||||
quad *quadHitList[lightBounces + 1] = {0};
|
||||
vec3 spheresNormalList[lightBounces + 1] = {0};
|
||||
*lightray = originalRay;
|
||||
RGBA tempPixel = (RGBA){1,1,1,1};
|
||||
sphere *nearestSphere = nullptr;
|
||||
quad *nearestQuad = nullptr;
|
||||
vec3 nearestCollisionPoint = {10000,10000,10000};
|
||||
RGBA objectColorList[lightBounces + 1] = {0};
|
||||
|
||||
RGBA tempPixel = raytrace(objectList, objectCount, lightray);
|
||||
|
||||
for (int j = 0; j <= lightBounces; j++) {
|
||||
nearestSphere = nullptr;
|
||||
double nearestSphereDistance = 10000000;
|
||||
nearestQuad = nullptr;
|
||||
double nearestQuadDistance = 10000000;
|
||||
nearestCollisionPoint = (vec3){10000,10000,10000};
|
||||
|
||||
for (int i = 0; i < sphereCount; i++) {
|
||||
vec3 collisionPoint = collisionPointSphere(lightray, sphereList[i]);
|
||||
double collisionPointDistance = 1000000;
|
||||
if (collisionPoint.x == -10000000) {
|
||||
|
||||
}
|
||||
else{
|
||||
collisionPointDistance = vec3Length(vec3Subtract(collisionPoint, lightray->origin));
|
||||
if (collisionPointDistance < vec3Length(vec3Subtract(nearestCollisionPoint, lightray->origin))) {
|
||||
nearestCollisionPoint = collisionPoint;
|
||||
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) {
|
||||
objectColorList[j] = nearestSphere->color;
|
||||
spheresHitList[j] = nearestSphere;
|
||||
sphereNormal = vec3Scale(vec3Subtract(nearestCollisionPoint, nearestSphere->origin), 1/(nearestSphere->radius));////////////////////////////// optimize, schreibe divide vector funktion
|
||||
spheresNormalList[j] = sphereNormal;
|
||||
//diffus:
|
||||
lightray->direction = vec3Add(sphereNormal, randUnitVector());
|
||||
if (fabs(lightray->direction.x ) <= epsilon || fabs(lightray->direction.y ) <= epsilon || fabs(lightray->direction.z) <= epsilon) {
|
||||
lightray->direction = sphereNormal;
|
||||
}
|
||||
//reflektion:
|
||||
//lightray->direction = vec3Add(lightray->direction, vec3Scale(sphereNormal, 2));
|
||||
lightray->origin = nearestCollisionPoint;
|
||||
}
|
||||
else {
|
||||
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;
|
||||
for (int i = 0; i <= lightBounces && spheresHitList[i] != nullptr; i++) {
|
||||
if (spheresHitList[i]->isLight == 1) {
|
||||
firstLightHitIndex = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (firstLightHitIndex == -1) {
|
||||
tempPixel = (RGBA){0,0,0,1};
|
||||
}
|
||||
else
|
||||
{
|
||||
//pixel.r += spheresHitList[firstLightHitIndex]->color.r;
|
||||
//pixel.g += spheresHitList[firstLightHitIndex]->color.g;
|
||||
//pixel.b += spheresHitList[firstLightHitIndex]->color.b;
|
||||
//pixel.a += spheresHitList[firstLightHitIndex]->color.a;
|
||||
for (int i = firstLightHitIndex; i >= 0; i--) {
|
||||
tempPixel.r = tempPixel.r * spheresHitList[i]->color.r;
|
||||
tempPixel.g = tempPixel.g * spheresHitList[i]->color.g;
|
||||
tempPixel.b = tempPixel.b * spheresHitList[i]->color.b;
|
||||
tempPixel.a = tempPixel.a * spheresHitList[i]->color.a;
|
||||
}
|
||||
pixel.r += tempPixel.r;
|
||||
pixel.g += tempPixel.g;
|
||||
pixel.b += tempPixel.b;
|
||||
pixel.a += tempPixel.a;
|
||||
|
||||
//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};
|
||||
}
|
||||
pixel.r += tempPixel.r;
|
||||
pixel.g += tempPixel.g;
|
||||
pixel.b += tempPixel.b;
|
||||
//pixel.a += tempPixel.a;
|
||||
}
|
||||
|
||||
//average pixelData
|
||||
pixel.r = pixel.r / raysPerPixel;
|
||||
pixel.g = pixel.g / raysPerPixel;
|
||||
pixel.b = pixel.b / raysPerPixel;
|
||||
pixel.a = pixel.a / raysPerPixel;
|
||||
//pixel.a = pixel.a / raysPerPixel;
|
||||
|
||||
|
||||
//free(spheresHitList);
|
||||
//free(spheresNormalList);
|
||||
return pixel;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user