clray

diff src/scene.cc @ 24:13091c00d7ca

- moved create_face_buffer to Scene::get_face_buffer and did a few reorganizations. - starting work on the kdtree creation
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 14 Aug 2010 03:02:52 +0100
parents 51f115e337c2
children 58642a8316b7
line diff
     1.1 --- a/src/scene.cc	Fri Aug 13 18:20:45 2010 +0100
     1.2 +++ b/src/scene.cc	Sat Aug 14 03:02:52 2010 +0100
     1.3 @@ -20,13 +20,37 @@
     1.4  	return true;
     1.5  }
     1.6  
     1.7 +Scene::Scene()
     1.8 +{
     1.9 +	facebuf = 0;
    1.10 +	num_faces = -1;
    1.11 +	kdtree = 0;
    1.12 +}
    1.13 +
    1.14 +Scene::~Scene()
    1.15 +{
    1.16 +	delete [] facebuf;
    1.17 +}
    1.18 +
    1.19  bool Scene::add_mesh(Mesh *m)
    1.20  {
    1.21  	// make sure triangles have material ids
    1.22  	for(size_t i=0; i<m->faces.size(); i++) {
    1.23  		m->faces[i].matid = m->matid;
    1.24  	}
    1.25 -	meshes.push_back(m);
    1.26 +
    1.27 +	try {
    1.28 +		meshes.push_back(m);
    1.29 +	}
    1.30 +	catch(...) {
    1.31 +		return false;
    1.32 +	}
    1.33 +
    1.34 +	// invalidate facebuffer and count
    1.35 +	delete [] facebuf;
    1.36 +	facebuf = 0;
    1.37 +	num_faces = -1;
    1.38 +
    1.39  	return true;
    1.40  }
    1.41  
    1.42 @@ -37,7 +61,11 @@
    1.43  
    1.44  int Scene::get_num_faces() const
    1.45  {
    1.46 -	int num_faces = 0;
    1.47 +	if(num_faces >= 0) {
    1.48 +		return num_faces;
    1.49 +	}
    1.50 +
    1.51 +	num_faces = 0;
    1.52  	for(size_t i=0; i<meshes.size(); i++) {
    1.53  		num_faces += meshes[i]->faces.size();
    1.54  	}
    1.55 @@ -64,3 +92,42 @@
    1.56  	}
    1.57  	return &matlib[0];
    1.58  }
    1.59 +
    1.60 +const Face *Scene::get_face_buffer() const
    1.61 +{
    1.62 +	if(facebuf) {
    1.63 +		return facebuf;
    1.64 +	}
    1.65 +
    1.66 +	int num_meshes = get_num_meshes();
    1.67 +
    1.68 +	printf("constructing face buffer with %d faces (out of %d meshes)\n", get_num_faces(), num_meshes);
    1.69 +	facebuf = new Face[num_faces];
    1.70 +	Face *fptr = facebuf;
    1.71 +
    1.72 +	for(int i=0; i<num_meshes; i++) {
    1.73 +		for(size_t j=0; j<meshes[i]->faces.size(); j++) {
    1.74 +			*fptr++ = meshes[i]->faces[j];
    1.75 +		}
    1.76 +	}
    1.77 +	return facebuf;
    1.78 +}
    1.79 +
    1.80 +static void build_kdtree(KDNode **kd, std::list<const Face*> *faces);
    1.81 +
    1.82 +void Scene::build_kdtree()
    1.83 +{
    1.84 +	const Face *faces = get_face_buffer();
    1.85 +	int num_faces = get_num_faces();
    1.86 +
    1.87 +	std::list<const Face*> facelist;
    1.88 +	for(int i=0; i<num_faces; i++) {
    1.89 +		facelist.push_back(faces + i);
    1.90 +	}
    1.91 +
    1.92 +	::build_kdtree(&kdtree, &facelist);
    1.93 +}
    1.94 +
    1.95 +static void build_kdtree(KDNode **kd, std::list<const Face*> *faces)
    1.96 +{
    1.97 +}