Browse Source

Start working on dumping the world (to a JSON file) for debug purposes.

Godzil 4 years ago
parent
commit
c369d2fe2d

+ 3 - 0
source/include/material.h

@@ -13,6 +13,7 @@
 #include <colour.h>
 #include <pattern.h>
 #include <light.h>
+#include <stdio.h>
 
 class Shape;
 
@@ -47,6 +48,8 @@ public:
                                                       double_equal(this->refractiveIndex, b.refractiveIndex) &&
                                                       (this->colour == b.colour); };
     bool operator!=(const Material &b) const { return !(*this == b); };
+
+    void dumpMe(FILE *fp);
 };
 
 

+ 2 - 0
source/include/pattern.h

@@ -12,6 +12,7 @@
 #include <colour.h>
 #include <tuple.h>
 #include <matrix.h>
+#include <stdio.h>
 
 class Shape;
 
@@ -28,6 +29,7 @@ public:
     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);

+ 3 - 0
source/include/shape.h

@@ -11,6 +11,7 @@
 
 class Shape;
 
+#include <stdio.h>
 #include <ray.h>
 #include <tuple.h>
 #include <matrix.h>
@@ -63,6 +64,8 @@ public:
 
     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);

+ 2 - 0
source/include/world.h

@@ -14,6 +14,7 @@
 #include <shape.h>
 #include <intersect.h>
 #include <ray.h>
+#include <stdio.h>
 
 class World
 {
@@ -51,6 +52,7 @@ public:
 
     Intersect intersect(Ray r);
 
+    void dumpMe(FILE *fp);
 };
 
 #endif /* DORAYME_WORLD_H */

+ 3 - 0
source/matrix.cpp

@@ -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++)
@@ -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)

+ 7 - 0
source/pattern.cpp

@@ -9,6 +9,7 @@
 
 #include <pattern.h>
 #include <shape.h>
+#include <stdio.h>
 
 Pattern::Pattern(Colour a, Colour b): a(a), b(b)
 {
@@ -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 - 0
source/pattern/checkerspattern.h

@@ -9,6 +9,8 @@
 #ifndef DORAYME_CHECKERSPATTERN_H
 #define DORAYME_CHECKERSPATTERN_H
 
+#include <stdio.h>
+
 class CheckersPattern : public Pattern
 {
 public:
@@ -20,6 +22,12 @@ public:
 
         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 - 0
source/pattern/gradientpattern.h

@@ -10,6 +10,7 @@
 #define DORAYME_GRADIENTPATTERN_H
 
 #include <pattern.h>
+#include <stdio.h>
 
 class GradientPattern : public Pattern
 {
@@ -25,6 +26,12 @@ public:
 
         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 - 0
source/pattern/ringpattern.h

@@ -24,6 +24,11 @@ public:
 
         return (fmod(value, 2) == 0)?this->a:this->b;
     }
+
+    void dumpMe(FILE *fp) {
+        fprintf(fp, "\"Type\": \"Ring\"\n");
+        Pattern::dumpMe(fp);
+    }
 };
 
 

+ 5 - 0
source/pattern/strippattern.h

@@ -27,6 +27,11 @@ public:
         }
         return this->b;
     }
+
+    void dumpMe(FILE *fp) {
+        fprintf(fp, "\"Type\": \"Strip\",\n");
+        Pattern::dumpMe(fp);
+    }
 };
 
 #endif /* DORAYME_STRIPPATTERN_H */

+ 5 - 0
source/pattern/testpattern.h

@@ -22,6 +22,11 @@ public:
     {
         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 - 0
source/shapes/material.cpp

@@ -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 - 0
source/shapes/shape.cpp

@@ -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 - 0
source/world.cpp

@@ -9,6 +9,8 @@
 #include <world.h>
 #include <light.h>
 #include <shape.h>
+#include <stdio.h>
+#include <string.h>
 
 #define MIN_ALLOC (2)
 
@@ -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 - 0
tests/christmasball_render.cpp

@@ -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),