add Raytracer::setThreads() pyrit
authorRadek Brich <radek.brich@devl.cz>
Sun, 25 Nov 2007 21:47:10 +0100
branchpyrit
changeset 16 20bceb605f48
parent 15 a0a3e334744f
child 17 5176ba000a67
add Raytracer::setThreads() resolved all memory leaks during rendering
.bzrignore
Makefile
ccdemos/Makefile
ccdemos/spheres_shadow.cc
src/kdtree.cc
src/kdtree.h
src/raytracer.cc
src/raytracer.h
--- 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
--- 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
--- 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
 
--- 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);
 
--- 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<StackElem*> st;
-
 	/* pointers to the far child node and current node */
 	KdNode *farchild, *node;
 	node = root; /* start from the kd-tree root node */
 
+	stack<StackElem*> 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 */
 
--- 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);
--- 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++)
--- 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