clray

changeset 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
files src/rt.cc src/scene.cc src/scene.h
diffstat 3 files changed, 91 insertions(+), 26 deletions(-) [+]
line diff
     1.1 --- a/src/rt.cc	Fri Aug 13 18:20:45 2010 +0100
     1.2 +++ b/src/rt.cc	Sat Aug 14 03:02:52 2010 +0100
     1.3 @@ -36,7 +36,6 @@
     1.4  };
     1.5  
     1.6  static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg);
     1.7 -static Face *create_face_buffer(Mesh **meshes, int num_meshes);
     1.8  
     1.9  static Face *faces;
    1.10  static Ray *prim_rays;
    1.11 @@ -78,8 +77,7 @@
    1.12  		return false;
    1.13  	}
    1.14  
    1.15 -	/*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size());
    1.16 -	if(!faces) {
    1.17 +	if(!(faces = (Face*)scn->get_face_buffer())) {
    1.18  		fprintf(stderr, "failed to create face buffer\n");
    1.19  		return false;
    1.20  	}
    1.21 @@ -234,23 +232,3 @@
    1.22  	Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}};
    1.23  	return ray;
    1.24  }
    1.25 -
    1.26 -static Face *create_face_buffer(Mesh **meshes, int num_meshes)
    1.27 -{
    1.28 -	int num_faces = 0;
    1.29 -	for(int i=0; i<num_meshes; i++) {
    1.30 -		num_faces += meshes[i]->faces.size();
    1.31 -	}
    1.32 -	printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes);
    1.33 -
    1.34 -	Face *faces = new Face[num_faces];
    1.35 -	memset(faces, 0, num_faces * sizeof *faces);
    1.36 -	Face *fptr = faces;
    1.37 -
    1.38 -	for(int i=0; i<num_meshes; i++) {
    1.39 -		for(size_t j=0; j<meshes[i]->faces.size(); j++) {
    1.40 -			*fptr++ = meshes[i]->faces[j];
    1.41 -		}
    1.42 -	}
    1.43 -	return faces;
    1.44 -}
     2.1 --- a/src/scene.cc	Fri Aug 13 18:20:45 2010 +0100
     2.2 +++ b/src/scene.cc	Sat Aug 14 03:02:52 2010 +0100
     2.3 @@ -20,13 +20,37 @@
     2.4  	return true;
     2.5  }
     2.6  
     2.7 +Scene::Scene()
     2.8 +{
     2.9 +	facebuf = 0;
    2.10 +	num_faces = -1;
    2.11 +	kdtree = 0;
    2.12 +}
    2.13 +
    2.14 +Scene::~Scene()
    2.15 +{
    2.16 +	delete [] facebuf;
    2.17 +}
    2.18 +
    2.19  bool Scene::add_mesh(Mesh *m)
    2.20  {
    2.21  	// make sure triangles have material ids
    2.22  	for(size_t i=0; i<m->faces.size(); i++) {
    2.23  		m->faces[i].matid = m->matid;
    2.24  	}
    2.25 -	meshes.push_back(m);
    2.26 +
    2.27 +	try {
    2.28 +		meshes.push_back(m);
    2.29 +	}
    2.30 +	catch(...) {
    2.31 +		return false;
    2.32 +	}
    2.33 +
    2.34 +	// invalidate facebuffer and count
    2.35 +	delete [] facebuf;
    2.36 +	facebuf = 0;
    2.37 +	num_faces = -1;
    2.38 +
    2.39  	return true;
    2.40  }
    2.41  
    2.42 @@ -37,7 +61,11 @@
    2.43  
    2.44  int Scene::get_num_faces() const
    2.45  {
    2.46 -	int num_faces = 0;
    2.47 +	if(num_faces >= 0) {
    2.48 +		return num_faces;
    2.49 +	}
    2.50 +
    2.51 +	num_faces = 0;
    2.52  	for(size_t i=0; i<meshes.size(); i++) {
    2.53  		num_faces += meshes[i]->faces.size();
    2.54  	}
    2.55 @@ -64,3 +92,42 @@
    2.56  	}
    2.57  	return &matlib[0];
    2.58  }
    2.59 +
    2.60 +const Face *Scene::get_face_buffer() const
    2.61 +{
    2.62 +	if(facebuf) {
    2.63 +		return facebuf;
    2.64 +	}
    2.65 +
    2.66 +	int num_meshes = get_num_meshes();
    2.67 +
    2.68 +	printf("constructing face buffer with %d faces (out of %d meshes)\n", get_num_faces(), num_meshes);
    2.69 +	facebuf = new Face[num_faces];
    2.70 +	Face *fptr = facebuf;
    2.71 +
    2.72 +	for(int i=0; i<num_meshes; i++) {
    2.73 +		for(size_t j=0; j<meshes[i]->faces.size(); j++) {
    2.74 +			*fptr++ = meshes[i]->faces[j];
    2.75 +		}
    2.76 +	}
    2.77 +	return facebuf;
    2.78 +}
    2.79 +
    2.80 +static void build_kdtree(KDNode **kd, std::list<const Face*> *faces);
    2.81 +
    2.82 +void Scene::build_kdtree()
    2.83 +{
    2.84 +	const Face *faces = get_face_buffer();
    2.85 +	int num_faces = get_num_faces();
    2.86 +
    2.87 +	std::list<const Face*> facelist;
    2.88 +	for(int i=0; i<num_faces; i++) {
    2.89 +		facelist.push_back(faces + i);
    2.90 +	}
    2.91 +
    2.92 +	::build_kdtree(&kdtree, &facelist);
    2.93 +}
    2.94 +
    2.95 +static void build_kdtree(KDNode **kd, std::list<const Face*> *faces)
    2.96 +{
    2.97 +}
     3.1 --- a/src/scene.h	Fri Aug 13 18:20:45 2010 +0100
     3.2 +++ b/src/scene.h	Sat Aug 14 03:02:52 2010 +0100
     3.3 @@ -3,6 +3,7 @@
     3.4  
     3.5  #include <stdio.h>
     3.6  #include <vector>
     3.7 +#include <list>
     3.8  
     3.9  struct Vertex {
    3.10  	float pos[4];
    3.11 @@ -47,13 +48,31 @@
    3.12  struct KDNode {
    3.13  	int axis;
    3.14  	float pt;
    3.15 +
    3.16 +	KDNode *left, *right;
    3.17 +	std::list<Face*> faces;
    3.18  };
    3.19  
    3.20 +struct KDNodeGPU {
    3.21 +	int axis;
    3.22 +	float pt;
    3.23 +};
    3.24 +
    3.25 +
    3.26  class Scene {
    3.27 +private:
    3.28 +	mutable Face *facebuf;
    3.29 +	mutable int num_faces;
    3.30 +
    3.31  public:
    3.32  	std::vector<Mesh*> meshes;
    3.33  	std::vector<Material> matlib;
    3.34 -	std::vector<KDNode> kdtree;
    3.35 +
    3.36 +	KDNode *kdtree;
    3.37 +	std::vector<KDNode> kdtree_gpu;
    3.38 +
    3.39 +	Scene();
    3.40 +	~Scene();
    3.41  
    3.42  	bool add_mesh(Mesh *m);
    3.43  	int get_num_meshes() const;
    3.44 @@ -66,6 +85,7 @@
    3.45  	bool load(const char *fname);
    3.46  	bool load(FILE *fp);
    3.47  
    3.48 +	const Face *get_face_buffer() const;
    3.49  	void build_kdtree();
    3.50  };
    3.51