clray

diff src/scene.cc @ 29:353d80127627

doh ... it doesn't work
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 21 Aug 2010 20:51:57 +0100
parents 97cfd9675310
children 04803c702014
line diff
     1.1 --- a/src/scene.cc	Sat Aug 21 03:42:49 2010 +0100
     1.2 +++ b/src/scene.cc	Sat Aug 21 20:51:57 2010 +0100
     1.3 @@ -70,7 +70,6 @@
     1.4  	facebuf = 0;
     1.5  	num_faces = -1;
     1.6  	kdtree = 0;
     1.7 -	num_kdnodes = -1;
     1.8  	kdbuf = 0;
     1.9  }
    1.10  
    1.11 @@ -172,25 +171,30 @@
    1.12  		((Scene*)this)->build_kdtree();
    1.13  	}
    1.14  
    1.15 -	if(!get_num_kdnodes()) {
    1.16 -		return 0;
    1.17 -	}
    1.18 +	int max_nodes = (int)pow(2, kdtree_depth(kdtree)) - 1;
    1.19 +	printf("allocating storage for the complete tree (%d)\n", max_nodes);
    1.20  
    1.21 -	kdbuf = new KDNodeGPU[num_kdnodes + 1];
    1.22 +	kdbuf = new KDNodeGPU[max_nodes + 1];
    1.23  	kdtree_gpu_flatten(kdbuf, 1, kdtree, get_face_buffer());
    1.24  	return kdbuf;
    1.25  }
    1.26  
    1.27 -int Scene::get_num_kdnodes() const
    1.28 +static int ipow(int x, int n)
    1.29  {
    1.30 -	if(num_kdnodes >= 0) {
    1.31 -		return num_kdnodes;
    1.32 +	assert(n >= 0);
    1.33 +
    1.34 +	int res = 1;
    1.35 +	for(int i=0; i<n; i++) {
    1.36 +		res *= x;
    1.37  	}
    1.38 -
    1.39 -	num_kdnodes = kdtree_nodes(kdtree);
    1.40 -	return num_kdnodes;
    1.41 +	return res;
    1.42  }
    1.43  
    1.44 +int Scene::get_kdtree_buffer_size() const
    1.45 +{
    1.46 +	// 2**depth - 1 nodes for the complete tree + 1 for the unused heap item 0.
    1.47 +	return ipow(2, kdtree_depth(kdtree)) * sizeof(KDNodeGPU);
    1.48 +}
    1.49  
    1.50  void Scene::draw_kdtree() const
    1.51  {
    1.52 @@ -255,6 +259,8 @@
    1.53  
    1.54  bool Scene::build_kdtree()
    1.55  {
    1.56 +	assert(kdtree == 0);
    1.57 +
    1.58  	const Face *faces = get_face_buffer();
    1.59  	int num_faces = get_num_faces();
    1.60  
    1.61 @@ -351,7 +357,7 @@
    1.62  		}
    1.63  	}
    1.64  
    1.65 -	printf("current cost: %f,   best_cost: %f\n", kd->cost, best_sum_cost);
    1.66 +	//printf("current cost: %f,   best_cost: %f\n", kd->cost, best_sum_cost);
    1.67  	if(best_sum_cost > kd->cost && (opt_max_items == 0 || kd->num_faces <= opt_max_items)) {
    1.68  		return true;	// stop splitting if it doesn't reduce the cost
    1.69  	}
    1.70 @@ -461,7 +467,7 @@
    1.71  			fprintf(stderr, "kdtree_gpu_flatten WARNING: more than %d faces in node, skipping!\n", (int)MAX_FACES);
    1.72  			break;
    1.73  		}
    1.74 -		dest->face_idx[dest->num_faces++] = *it - facebuf;
    1.75 +		dest->face_idx[dest->num_faces++] = *it++ - facebuf;
    1.76  	}
    1.77  
    1.78  	if(node->left) {
    1.79 @@ -475,10 +481,10 @@
    1.80  {
    1.81  	if(!node) return;
    1.82  
    1.83 -	for(int i=0; i<level; i++) {
    1.84 +	/*for(int i=0; i<level; i++) {
    1.85  		fputs("   ", stdout);
    1.86  	}
    1.87 -	printf("- %d (cost: %f)\n", node->num_faces, node->cost);
    1.88 +	printf("- %d (cost: %f)\n", node->num_faces, node->cost);*/
    1.89  
    1.90  	print_item_counts(node->left, level + 1);
    1.91  	print_item_counts(node->right, level + 1);