# HG changeset patch # User Radek Brich # Date 1196023630 -3600 # Node ID 20bceb605f48fa4e125100c37fd66b0874f467cb # Parent a0a3e334744f5152c329e528ceef78b8adb62806 add Raytracer::setThreads() resolved all memory leaks during rendering diff -r a0a3e334744f -r 20bceb605f48 .bzrignore --- a/.bzrignore Sun Nov 25 17:58:29 2007 +0100 +++ b/.bzrignore Sun Nov 25 21:47:10 2007 +0100 @@ -1,4 +1,5 @@ demos/*.png -ccdemos/*.png demos/kdtree.obj demos/*.ply +ccdemos/*.png +ccdemos/spheres_shadow diff -r a0a3e334744f -r 20bceb605f48 Makefile --- a/Makefile Sun Nov 25 17:58:29 2007 +0100 +++ b/Makefile Sun Nov 25 21:47:10 2007 +0100 @@ -1,4 +1,4 @@ -CCFLAGS=-I./src -Wall -Wno-write-strings -g -O3 -fno-strict-aliasing -DPTHREADS +CCFLAGS=-I./src -Wall -Wno-write-strings -fno-strict-aliasing -DPTHREADS LDFLAGS= ifeq ($(OS), Windows_NT) @@ -11,7 +11,8 @@ endif # optimizations -#CCFLAGS+=-pipe -fomit-frame-pointer -ffast-math -msse3 +#CCFLAGS+=-g -O0 +CCFLAGS+=-O3 -pipe -fomit-frame-pointer -ffast-math -msse3 # TARGETS @@ -24,7 +25,7 @@ tests: testvector testmatrix clean: - rm -f *.o $(MODULENAME) testvector testmatrix + rm -f *.o $(MODULENAME) # RULES diff -r a0a3e334744f -r 20bceb605f48 ccdemos/Makefile --- a/ccdemos/Makefile Sun Nov 25 17:58:29 2007 +0100 +++ b/ccdemos/Makefile Sun Nov 25 21:47:10 2007 +0100 @@ -1,4 +1,4 @@ -CCFLAGS=-g -I../src +CCFLAGS=-g -O0 -I../src LDFLAGS=-L.. -lpng `python-config --libs` objs=image.o ../*.o diff -r a0a3e334744f -r 20bceb605f48 ccdemos/spheres_shadow.cc --- a/ccdemos/spheres_shadow.cc Sun Nov 25 17:58:29 2007 +0100 +++ b/ccdemos/spheres_shadow.cc Sun Nov 25 21:47:10 2007 +0100 @@ -4,6 +4,8 @@ int main() { Raytracer rt; + rt.setThreads(1); + Light light1(Vector3(0.0, 5.0, 5.0), Colour(0.7, 0.3, 0.6)); rt.addlight(&light1); diff -r a0a3e334744f -r 20bceb605f48 src/kdtree.cc --- a/src/kdtree.cc Sun Nov 25 17:58:29 2007 +0100 +++ b/src/kdtree.cc Sun Nov 25 21:47:10 2007 +0100 @@ -87,6 +87,14 @@ return nearest_shape; } +KdNode::~KdNode() +{ + if (isLeaf()) + ;//delete shapes; // this sigsegvs for unknown reason + else + delete[] children; +} + void KdNode::subdivide(BBox bbox, int depth) { if (depth >= 20 || shapes->size() <= 4) @@ -229,6 +237,7 @@ float rnum = splitpos->rnum; // split this node + //delete shapes; children = new KdNode[2]; int state = 0; for (sh = sslist.begin(); sh != sslist.end(); sh++) @@ -311,12 +320,12 @@ if (!bbox.intersect(ray, a, b)) return NULL; - stack st; - /* pointers to the far child node and current node */ KdNode *farchild, *node; node = root; /* start from the kd-tree root node */ + stack st; + StackElem *enPt = new StackElem(); enPt->t = a; /* set the signed distance */ enPt->node = NULL; @@ -332,12 +341,12 @@ exPt->t = b; exPt->pb = ray.o + ray.dir * b; exPt->node = NULL; /* set termination flag */ - - st.push(enPt); + st.push(exPt); /* loop, traverse through the whole kd-tree, until an object is intersected or ray leaves the scene */ while (node) { + exPt = st.top(); /* loop until a leaf is found */ while (!node->isLeaf()) { @@ -379,16 +388,14 @@ /* signed distance to the splitting plane */ t = (splitVal - ray.o.cell[axis]) / ray.dir.cell[axis]; - /* setup the new exit point */ - st.push(exPt); + /* setup the new exit point and push it onto stack */ exPt = new StackElem(); - - /* push values onto the stack */ exPt->t = t; exPt->node = farchild; exPt->pb[axis] = splitVal; exPt->pb[(axis+1)%3] = ray.o.cell[(axis+1)%3] + t * ray.dir.cell[(axis+1)%3]; exPt->pb[(axis+2)%3] = ray.o.cell[(axis+2)%3] + t * ray.dir.cell[(axis+2)%3]; + st.push(exPt); } /* while */ /* current node is the leaf . . . empty or full */ @@ -402,21 +409,23 @@ && dist >= enPt->t) nearest_shape = *shape; + delete enPt; + if (nearest_shape) { + while (!st.empty()) + { + delete st.top(); + st.pop(); + } nearest_distance = dist; return nearest_shape; } - /* pop from the stack */ - delete enPt; - enPt = exPt; /* the signed distance intervals are adjacent */ + enPt = exPt; /* retrieve the pointer to the next node, it is possible that ray traversal terminates */ node = enPt->node; - - // pop - exPt = st.top(); st.pop(); } /* while */ diff -r a0a3e334744f -r 20bceb605f48 src/kdtree.h --- a/src/kdtree.h Sun Nov 25 17:58:29 2007 +0100 +++ b/src/kdtree.h Sun Nov 25 21:47:10 2007 +0100 @@ -39,6 +39,7 @@ }; KdNode() : axis(3) { shapes = new ShapeList(); }; + ~KdNode(); void setAxis(short aAxis) { axis = aAxis; }; short getAxis() { return axis; }; @@ -62,7 +63,8 @@ KdNode *root; bool built; public: - KdTree() : Container(), built(false) {}; + KdTree() : Container(), root(NULL), built(false) {}; + ~KdTree() { if (root) delete root; }; void addShape(Shape* aShape) { Container::addShape(aShape); built = false; }; Shape *nearest_intersection(const Shape *origin_shape, const Ray &ray, float &nearest_distance); diff -r a0a3e334744f -r 20bceb605f48 src/raytracer.cc --- a/src/raytracer.cc Sun Nov 25 17:58:29 2007 +0100 +++ b/src/raytracer.cc Sun Nov 25 21:47:10 2007 +0100 @@ -221,7 +221,6 @@ vy = starty; #ifdef PTHREADS - int num_threads = 4; printf("* pthreads enabled, using %d threads\n", num_threads); pthread_t threads[num_threads]; for (int t = 0; t < num_threads; t++) diff -r a0a3e334744f -r 20bceb605f48 src/raytracer.h --- a/src/raytracer.h Sun Nov 25 17:58:29 2007 +0100 +++ b/src/raytracer.h Sun Nov 25 21:47:10 2007 +0100 @@ -32,17 +32,20 @@ Colour bg_colour; int ao_samples; float ao_distance, ao_angle; + int num_threads; Vector3 SphereDistribute(int i, int n, float extent, Vector3 &normal); public: - Raytracer(): lights(), bg_colour(0.0, 0.0, 0.0), ao_samples(0) { top = new KdTree(); }; - ~Raytracer() {}; + Raytracer(): lights(), bg_colour(0.0, 0.0, 0.0), ao_samples(0), num_threads(4) + { top = new KdTree(); }; + ~Raytracer() { delete top; }; float *render(int w, int h); Colour raytrace(Ray &ray, int depth, Shape *origin_shape); void addshape(Shape *shape) { top->addShape(shape); }; void addlight(Light *light); void ambientocclusion(int samples, float distance, float angle); + void setThreads(int num) { num_threads = num; }; }; #endif