Browse Source

Clearing up some memory to prevent stupid issues

Preparing for some optimisations. (absolutely need to reduce the ammount of allocations done.)
Godzil 4 years ago
parent
commit
ace7d53571
5 changed files with 24 additions and 4 deletions
  1. 1 0
      source/include/intersect.h
  2. 8 0
      source/include/list.h
  3. 11 0
      source/intersect.cpp
  4. 4 3
      source/intersection.cpp
  5. 0 1
      source/matrix.cpp

+ 1 - 0
source/include/intersect.h

@@ -21,6 +21,7 @@ private:
 public:
     Intersect();
     ~Intersect();
+    void reset();
     void add(Intersection i);
     int count() { return this->num; };
     Intersection operator[](const int p) { return *this->list[p]; }

+ 8 - 0
source/include/list.h

@@ -31,6 +31,14 @@ public:
         if (p == nullptr) { return; }
 
         /* clear up the list */
+        do
+        {
+            ChainList *next = p->next;
+            free(p);
+            p = next;
+        }
+        while(p != nullptr);
+
     }
 
     Shape *last()

+ 11 - 0
source/intersect.cpp

@@ -31,10 +31,20 @@ Intersect::Intersect()
 
 Intersect::~Intersect()
 {
+    int i;
+    for(i = 0; i < this->num; i++)
+    {
+        free(this->list[i]);
+    }
     /* Free stuff */
     free(this->list);
 }
 
+void Intersect::reset()
+{
+    this->num = 0;
+}
+
 void Intersect::add(Intersection i)
 {
     Intersection *x;
@@ -46,6 +56,7 @@ void Intersect::add(Intersection i)
         stats.addRealloc();
         this->list = (Intersection **)realloc(this->list, sizeof(Intersection *) * this->allocated);
     }
+
     this->list[this->num++] = new Intersection(i.t, i.object);
 
     stats.setMaxIntersect(this->num);

+ 4 - 3
source/intersection.cpp

@@ -30,14 +30,15 @@ Computation Intersection::prepareComputation(Ray r, Intersect *xs)
     Tuple underHitP = hitP - normalV * getEpsilon();
     Tuple reflectV = r.direction.reflect(normalV);
 
-    if (xs != nullptr)
+    /* 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))
     {
         List containers;
         int j, k;
 
-        for(j = 0; j < xs->count(); j++)
+        for (j = 0 ; j < xs->count() ; j++)
         {
-            Intersection i = (*xs)[j];
+            Intersection i = ( *xs )[j];
             if (*this == i)
             {
                 if (!containers.isEmpty())

+ 0 - 1
source/matrix.cpp

@@ -125,7 +125,6 @@ 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++)