Skip to content

Commit

Permalink
Start working on dumping the world (to a JSON file) for debug purposes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Godzil committed Feb 27, 2020
1 parent 2926166 commit c369d2f
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 0 deletions.
3 changes: 3 additions & 0 deletions source/include/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <colour.h>
#include <pattern.h>
#include <light.h>
#include <stdio.h>

class Shape;

Expand Down Expand Up @@ -47,6 +48,8 @@ class Material
double_equal(this->refractiveIndex, b.refractiveIndex) &&
(this->colour == b.colour); };
bool operator!=(const Material &b) const { return !(*this == b); };

void dumpMe(FILE *fp);
};


Expand Down
2 changes: 2 additions & 0 deletions source/include/pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <colour.h>
#include <tuple.h>
#include <matrix.h>
#include <stdio.h>

class Shape;

Expand All @@ -28,6 +29,7 @@ class Pattern
Pattern(Colour a, Colour b);

virtual Colour patternAt(Tuple point) = 0;
virtual void dumpMe(FILE *fp);

void setTransform(Matrix transform);
Colour patternAtObject(Shape *object, Tuple point);
Expand Down
3 changes: 3 additions & 0 deletions source/include/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

class Shape;

#include <stdio.h>
#include <ray.h>
#include <tuple.h>
#include <matrix.h>
Expand Down Expand Up @@ -63,6 +64,8 @@ class Shape

virtual void updateTransform();

virtual void dumpMe(FILE *fp);

Tuple worldToObject(Tuple point) { return this->inverseTransform * point; };
Tuple objectToWorld(Tuple point) { return this->transformMatrix * point; };
Tuple normalToWorld(Tuple normalVector);
Expand Down
2 changes: 2 additions & 0 deletions source/include/world.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <shape.h>
#include <intersect.h>
#include <ray.h>
#include <stdio.h>

class World
{
Expand Down Expand Up @@ -51,6 +52,7 @@ class World

Intersect intersect(Ray r);

void dumpMe(FILE *fp);
};

#endif /* DORAYME_WORLD_H */
3 changes: 3 additions & 0 deletions source/matrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ Matrix Matrix::transpose()
{
int x, y;
Matrix ret = Matrix(this->size);

#pragma omp parallel for simd private(y, x)
for (y = 0 ; y < this->size ; y++)
{
for (x = 0 ; x < this->size ; x++)
Expand All @@ -139,6 +141,7 @@ Matrix Matrix::submatrix(int row, int column)
int i, j;
int x = 0, y = 0;
Matrix ret = Matrix(this->size - 1);

for (i = 0 ; i < this->size ; i++)
{
if (i == row)
Expand Down
7 changes: 7 additions & 0 deletions source/pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <pattern.h>
#include <shape.h>
#include <stdio.h>

Pattern::Pattern(Colour a, Colour b): a(a), b(b)
{
Expand All @@ -28,4 +29,10 @@ void Pattern::setTransform(Matrix transform)
{
this->transformMatrix = transform;
this->inverseTransform = transform.inverse();
}

void Pattern::dumpMe(FILE *fp)
{
fprintf(fp, "\"Colour A\": {\"red\": %f, \"green\": %f, \"blue\": %f},\n", this->a.x, this->a.y, this->a.z);
fprintf(fp, "\"Colour B\": {\"red\": %f, \"green\": %f, \"blue\": %f},\n", this->b.x, this->b.y, this->b.z);
}
8 changes: 8 additions & 0 deletions source/pattern/checkerspattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#ifndef DORAYME_CHECKERSPATTERN_H
#define DORAYME_CHECKERSPATTERN_H

#include <stdio.h>

class CheckersPattern : public Pattern
{
public:
Expand All @@ -20,6 +22,12 @@ class CheckersPattern : public Pattern

return (fmod(value, 2) == 0)?this->a:this->b;
}

void dumpMe(FILE *fp) {
fprintf(fp, "\"Type\": \"Checkers\",\n");
Pattern::dumpMe(fp);
}

};

#endif /* DORAYME_CHECKERSPATTERN_H */
7 changes: 7 additions & 0 deletions source/pattern/gradientpattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define DORAYME_GRADIENTPATTERN_H

#include <pattern.h>
#include <stdio.h>

class GradientPattern : public Pattern
{
Expand All @@ -25,6 +26,12 @@ class GradientPattern : public Pattern

return Colour(ret.x, ret.y, ret.z);
}

void dumpMe(FILE *fp) {
fprintf(fp, "\"Type\": \"Gradient\",\n");
Pattern::dumpMe(fp);
}

};

#endif /* DORAYME_GRADIENTPATTERN_H */
5 changes: 5 additions & 0 deletions source/pattern/ringpattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class RingPattern : public Pattern

return (fmod(value, 2) == 0)?this->a:this->b;
}

void dumpMe(FILE *fp) {
fprintf(fp, "\"Type\": \"Ring\"\n");
Pattern::dumpMe(fp);
}
};


Expand Down
5 changes: 5 additions & 0 deletions source/pattern/strippattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class StripPattern : public Pattern
}
return this->b;
}

void dumpMe(FILE *fp) {
fprintf(fp, "\"Type\": \"Strip\",\n");
Pattern::dumpMe(fp);
}
};

#endif /* DORAYME_STRIPPATTERN_H */
5 changes: 5 additions & 0 deletions source/pattern/testpattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ class TestPattern : public Pattern
{
return Colour(point.x, point.y, point.z);
}

void dumpMe(FILE *fp) {
fprintf(fp, "\"Type\": \"Test\",\n");
Pattern::dumpMe(fp);
}
};

#endif /* DORAYME_TESTPATTERN_H */
19 changes: 19 additions & 0 deletions source/shapes/material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,23 @@ Colour Material::lighting(Light light, Tuple point, Tuple eyeVector, Tuple norma
finalColour = emissiveColour + ambientColour + diffuseColour + specularColour;

return Colour(finalColour.x, finalColour.y, finalColour.z);
}

void Material::dumpMe(FILE *fp)
{
fprintf(fp, "\"Colour\": {\"red\": %f, \"green\": %f, \"blue\": %f},\n", this->colour.x, this->colour.y, this->colour.z);
fprintf(fp, "\"Ambient\": %f,\n", this->ambient);
fprintf(fp, "\"Diffuse\": %f,\n", this->diffuse);
fprintf(fp, "\"Specular\": %f,\n", this->specular);
fprintf(fp, "\"Shininess\": %f,\n", this->shininess);
fprintf(fp, "\"Reflective\": %f,\n", this->reflective);
fprintf(fp, "\"Transparency\": %f,\n", this->transparency);
fprintf(fp, "\"Emissive\": %f,\n", this->emissive);
fprintf(fp, "\"RefractiveIndex\": %f,\n", this->refractiveIndex);
if (this->pattern)
{
fprintf(fp, "\"Pattern\": {\n", this->emissive);
this->pattern->dumpMe(fp);
fprintf(fp, "},\n");
}
}
8 changes: 8 additions & 0 deletions source/shapes/shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,12 @@ BoundingBox Shape::getBounds()
ret.min = this->objectToWorld(Point(-1, -1, -1));
ret.max = this->objectToWorld(Point(1, 1, 1));
return ret;
}

void Shape::dumpMe(FILE *fp)
{
fprintf(fp, "\"Material\": {\n");
this->material.dumpMe(fp);
fprintf(fp, "},\n");
fprintf(fp, "\"DropShadow\": %d,\n", this->dropShadow);
}
30 changes: 30 additions & 0 deletions source/world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
#include <world.h>
#include <light.h>
#include <shape.h>
#include <stdio.h>
#include <string.h>

#define MIN_ALLOC (2)

Expand Down Expand Up @@ -197,4 +199,32 @@ Colour World::refractedColour(Computation comps, uint32_t depthCount)
Tuple hitColour = this->colourAt(refractedRay, depthCount - 1) * comps.material->transparency;

return Colour(hitColour.x, hitColour.y, hitColour.z);
}

void World::dumpMe(FILE *fp)
{
int i;
/* JSON Opening */
fprintf(fp, "{\n");

fprintf(fp, "\"Lights\": {\n");
for(i = 0; i < this->lightCount; i++)
{
fprintf(fp, "\"%d\": {\n", i);
//this->lightList[i]->dumpMe(fp);
fprintf(fp, "},\n");
}
fprintf(fp, "},\n");

fprintf(fp, "\"Objects\": {\n");
for(i = 0; i < this->objectCount; i++)
{
fprintf(fp, "\"%d\": {\n", i);
this->objectList[i]->dumpMe(fp);
fprintf(fp, "},\n");
}
fprintf(fp, "},\n");

/* JSON Closing */
fprintf(fp, "}\n");
}
9 changes: 9 additions & 0 deletions tests/christmasball_render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,15 @@ int main()

/* ----------------------------- */

FILE *fpOut = fopen("christmas_worlddump.json", "wt");
if (fpOut)
{
w.dumpMe(fpOut);
fclose(fpOut);
}
/* ----------------------------- */


/* Set the camera */
Camera camera = Camera(40, 30, 1.047);
camera.setTransform(viewTransform(Point(0, 0, -4),
Expand Down

0 comments on commit c369d2f

Please sign in to comment.