A new scene and some optimisations.

This commit is contained in:
Godzil
2020-03-12 17:45:29 +00:00
parent 7a43a98816
commit c858b4dcde
11 changed files with 259 additions and 50 deletions

View File

@@ -25,31 +25,7 @@ struct Computation
object(object), t(t), hitPoint(point), eyeVector(eyev), normalVector(normalv), inside(inside),
overHitPoint(overHitP), underHitPoint(underHitP), reflectVector(reflectV), n1(n1), n2(n2), material(objMat) { };
double schlick()
{
/* Find the cos of the angle betzeen the eye and normal vector */
double cos = this->eyeVector.dot(this->normalVector);
double r0;
/* Total internal reflection can only occur when n1 > n2 */
if (this->n1 > this->n2)
{
double n, sin2_t;
n = this->n1 / this->n2;
sin2_t = (n * n) * (1.0 - (cos * cos));
if (sin2_t > 1.0)
{
return 1.0;
}
/* Compute the cos of theta */
cos = sqrt(1.0 - sin2_t);
}
r0 = ((this->n1 - this->n2) / (this->n1 + this->n2));
r0 = r0 * r0;
return r0 + (1 - r0) * ((1 - cos)*(1 - cos)*(1 - cos)*(1 - cos)*(1 - cos));
};
double schlick();
Shape *object;
double t;
@@ -73,7 +49,6 @@ class Intersection
public:
double t;
Shape *object;
double u, v;
public:

View File

@@ -42,11 +42,14 @@ protected:
ShapeType type;
Matrix localTransformMatrix;
bool locked;
uint64_t objectId;
protected:
virtual void localIntersect(Ray r, Intersect &xs) = 0;
virtual Tuple localNormalAt(Tuple point, Intersection *hit) = 0;
static uint64_t newObjectId();
public:
Matrix transformMatrix;
Matrix inverseTransform;
@@ -65,6 +68,9 @@ public:
virtual void intersect(Ray &r, Intersect &xs);
Tuple normalAt(Tuple point, Intersection *hit = nullptr);
uint64_t getObjectId() { return this->objectId; };
void setObjectId(uint64_t oid) { this->objectId = oid; };
/* Bounding box points are always world value */
virtual BoundingBox getLocalBounds();
virtual BoundingBox getBounds();
@@ -92,6 +98,7 @@ public:
void setTransform(Matrix transform);
void setMaterial(Material material) { this->material = material; this->materialSet = true; };
Material *getMaterial();
Ray transform(Ray &r) { return Ray(this->transformMatrix * r.origin, this->transformMatrix * r.direction); };
Ray invTransform(Ray &r) { return Ray(this->inverseTransform * r.origin, this->inverseTransform * r.direction); };

View File

@@ -24,9 +24,17 @@ Intersect::Intersect()
{
this->allocated = MIN_ALLOC;
this->list = (Intersection **)calloc(sizeof(Intersection *), MIN_ALLOC);
stats.addMalloc();
stats.addIntersect();
this->num = 0;
if (this->list != nullptr)
{
stats.addMalloc();
stats.addIntersect();
this->num = 0;
}
else
{
printf("ABORT: Allocation error [%s]!\n", __FUNCTION__);
exit(-1);
}
}
Intersect::~Intersect()
@@ -34,12 +42,17 @@ Intersect::~Intersect()
int i;
for(i = 0; i < this->num; i++)
{
delete this->list[i];
if (this->list[i] != nullptr)
{
delete this->list[i];
this->list[i] = nullptr;
}
}
/* Free stuff */
if (this->list != nullptr)
{
free(this->list);
this->list = nullptr;
}
}

View File

@@ -10,6 +10,32 @@
#include <shape.h>
#include <list.h>
double Computation::schlick()
{
/* Find the cos of the angle betzeen the eye and normal vector */
double cos = this->eyeVector.dot(this->normalVector);
double r0;
/* Total internal reflection can only occur when n1 > n2 */
if (this->n1 > this->n2)
{
double n, sin2_t;
n = this->n1 / this->n2;
sin2_t = (n * n) * (1.0 - (cos * cos));
if (sin2_t > 1.0)
{
return 1.0;
}
/* Compute the cos of theta */
cos = sqrt(1.0 - sin2_t);
}
r0 = ((this->n1 - this->n2) / (this->n1 + this->n2));
r0 = r0 * r0;
return r0 + (1 - r0) * ((1 - cos)*(1 - cos)*(1 - cos)*(1 - cos)*(1 - cos));
};
Computation Intersection::prepareComputation(Ray r, Intersect *xs)
{
double n1 = 1.0;
@@ -42,7 +68,7 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs)
Tuple reflectV = r.direction.reflect(normalV);
/* If the hit object is not transparent, there is no need to do that. I think .*/
if ((xs != nullptr) && (xs->hit().object->material.transparency > 0))
if ((xs != nullptr) && (xs->hit().object->getMaterial()->transparency > 0))
{
List containers;
int j;
@@ -54,7 +80,7 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs)
{
if (!containers.isEmpty())
{
n1 = containers.last()->material.refractiveIndex;
n1 = containers.last()->getMaterial()->refractiveIndex;
}
}
@@ -71,7 +97,7 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs)
{
if (!containers.isEmpty())
{
n2 = containers.last()->material.refractiveIndex;
n2 = containers.last()->getMaterial()->refractiveIndex;
}
/* End the loop */
@@ -80,10 +106,7 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs)
}
}
Shape *s = this->object;
/* For now don't get root group material */
while((!s->materialSet) && (s->parent != nullptr)) { s = s->parent; }
Material *m = this->object->getMaterial();
return Computation(this->object,
this->t,
@@ -96,5 +119,5 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs)
n1,
n2,
underHitP,
&s->material);
}
m);
}

View File

@@ -101,13 +101,15 @@ Matrix Matrix::operator*(const Matrix &b) const
return ret;
}
#define FastGet4(_x, _y) (this->data[4 * (_x) + (_y)])
/* TODO: Check if we can optimise this function. It is called a lot */
Tuple Matrix::operator*(const Tuple &b) const
{
return Tuple(b.x * this->get(0, 0) + b.y * this->get(0, 1) + b.z * this->get(0, 2) + b.w * this->get(0, 3),
b.x * this->get(1, 0) + b.y * this->get(1, 1) + b.z * this->get(1, 2) + b.w * this->get(1, 3),
b.x * this->get(2, 0) + b.y * this->get(2, 1) + b.z * this->get(2, 2) + b.w * this->get(2, 3),
b.x * this->get(3, 0) + b.y * this->get(3, 1) + b.z * this->get(3, 2) + b.w * this->get(3, 3));
return Tuple(b.x * FastGet4(0, 0) + b.y * FastGet4(0, 1) + b.z * FastGet4(0, 2) + b.w * FastGet4(0, 3),
b.x * FastGet4(1, 0) + b.y * FastGet4(1, 1) + b.z * FastGet4(1, 2) + b.w * FastGet4(1, 3),
b.x * FastGet4(2, 0) + b.y * FastGet4(2, 1) + b.z * FastGet4(2, 2) + b.w * FastGet4(2, 3),
b.x * FastGet4(3, 0) + b.y * FastGet4(3, 1) + b.z * FastGet4(3, 2) + b.w * FastGet4(3, 3));
}
Matrix Matrix::identity()

View File

@@ -181,7 +181,7 @@ void RenderStats::printStats()
printf("Malloc called : %lld\n", this->mallocCallCount);
printf("Realloc called : %lld\n", this->reallocCallCount);
printf("Bounding box missed : %lld\n", this->discardedIntersectCount);
printf("Min depth atteined : %lld\n", this->maxDepthAttained);
printf("Min depth attained : %lld\n", this->maxDepthAttained);
printf("Max intersect count : %lld\n", this->maxIntersectOnARay);
printf("==================================================\n");
};

View File

@@ -484,6 +484,9 @@ int OBJFile::execLine(int argc, char *argv[], uint32_t currentLine)
this->verticesNormal(vn[2]),
this->verticesNormal(vn[3]));
}
/* Set the object id to the OBJ one */
t->setObjectId(this->getObjectId());
this->currentGroup->addObject(t);
ret = 0;
}
@@ -523,7 +526,7 @@ int OBJFile::execLine(int argc, char *argv[], uint32_t currentLine)
this->verticesNormal(vn[i]),
this->verticesNormal(vn[i + 1]));
}
t->setObjectId(this->getObjectId());
this->currentGroup->addObject(t);
}
ret = 0;

View File

@@ -15,6 +15,7 @@
Shape::Shape(ShapeType type)
{
this->objectId = Shape::newObjectId();
this->locked = false;
this->parent = nullptr;
this->dropShadow = true;
@@ -25,6 +26,17 @@ Shape::Shape(ShapeType type)
this->updateTransform();
}
uint64_t Shape::newObjectId()
{
static uint64_t id = 0;
uint64_t ret;
ret = id++;
return ret;
}
void Shape::intersect(Ray &r, Intersect &xs)
{
this->localIntersect(this->invTransform(r), xs);
@@ -95,6 +107,16 @@ BoundingBox Shape::getBounds()
return ret;
}
Material *Shape::getMaterial()
{
Shape *s = this;
while((!s->materialSet) && (s->parent != nullptr))
{
s = s->parent;
}
return &s->material;
}
void Shape::dumpMe(FILE *fp)
{
if (this->materialSet)
@@ -112,9 +134,9 @@ void Shape::dumpMe(FILE *fp)
this->getBounds().dumpMe(fp);
fprintf(fp, "},\n");
}
fprintf(fp, "\"id\": %p,\n", this);
fprintf(fp, "\"id\": %ld,\n", this->getObjectId());
if (this->parent)
{
fprintf(fp, "\"parentId\": %p,\n", this->parent);
fprintf(fp, "\"parentId\": %ld,\n", this->parent->getObjectId());
}
}