# HG changeset patch # User John Tsiombikas # Date 1281751372 -3600 # Node ID 13091c00d7ca10bc42e8ede15f3722d50ac41b8a # Parent 51f115e337c2b1436a5a6023d5ac6a8a58dbe97c - moved create_face_buffer to Scene::get_face_buffer and did a few reorganizations. - starting work on the kdtree creation diff -r 51f115e337c2 -r 13091c00d7ca src/rt.cc --- a/src/rt.cc Fri Aug 13 18:20:45 2010 +0100 +++ b/src/rt.cc Sat Aug 14 03:02:52 2010 +0100 @@ -36,7 +36,6 @@ }; static Ray get_primary_ray(int x, int y, int w, int h, float vfov_deg); -static Face *create_face_buffer(Mesh **meshes, int num_meshes); static Face *faces; static Ray *prim_rays; @@ -78,8 +77,7 @@ return false; } - /*Face **/faces = create_face_buffer(&scn->meshes[0], scn->meshes.size()); - if(!faces) { + if(!(faces = (Face*)scn->get_face_buffer())) { fprintf(stderr, "failed to create face buffer\n"); return false; } @@ -234,23 +232,3 @@ Ray ray = {{0, 0, 0, 1}, {px, py, -pz, 1}}; return ray; } - -static Face *create_face_buffer(Mesh **meshes, int num_meshes) -{ - int num_faces = 0; - for(int i=0; ifaces.size(); - } - printf("constructing face buffer with %d faces (out of %d meshes)\n", num_faces, num_meshes); - - Face *faces = new Face[num_faces]; - memset(faces, 0, num_faces * sizeof *faces); - Face *fptr = faces; - - for(int i=0; ifaces.size(); j++) { - *fptr++ = meshes[i]->faces[j]; - } - } - return faces; -} diff -r 51f115e337c2 -r 13091c00d7ca src/scene.cc --- a/src/scene.cc Fri Aug 13 18:20:45 2010 +0100 +++ b/src/scene.cc Sat Aug 14 03:02:52 2010 +0100 @@ -20,13 +20,37 @@ return true; } +Scene::Scene() +{ + facebuf = 0; + num_faces = -1; + kdtree = 0; +} + +Scene::~Scene() +{ + delete [] facebuf; +} + bool Scene::add_mesh(Mesh *m) { // make sure triangles have material ids for(size_t i=0; ifaces.size(); i++) { m->faces[i].matid = m->matid; } - meshes.push_back(m); + + try { + meshes.push_back(m); + } + catch(...) { + return false; + } + + // invalidate facebuffer and count + delete [] facebuf; + facebuf = 0; + num_faces = -1; + return true; } @@ -37,7 +61,11 @@ int Scene::get_num_faces() const { - int num_faces = 0; + if(num_faces >= 0) { + return num_faces; + } + + num_faces = 0; for(size_t i=0; ifaces.size(); } @@ -64,3 +92,42 @@ } return &matlib[0]; } + +const Face *Scene::get_face_buffer() const +{ + if(facebuf) { + return facebuf; + } + + int num_meshes = get_num_meshes(); + + printf("constructing face buffer with %d faces (out of %d meshes)\n", get_num_faces(), num_meshes); + facebuf = new Face[num_faces]; + Face *fptr = facebuf; + + for(int i=0; ifaces.size(); j++) { + *fptr++ = meshes[i]->faces[j]; + } + } + return facebuf; +} + +static void build_kdtree(KDNode **kd, std::list *faces); + +void Scene::build_kdtree() +{ + const Face *faces = get_face_buffer(); + int num_faces = get_num_faces(); + + std::list facelist; + for(int i=0; i *faces) +{ +} diff -r 51f115e337c2 -r 13091c00d7ca src/scene.h --- a/src/scene.h Fri Aug 13 18:20:45 2010 +0100 +++ b/src/scene.h Sat Aug 14 03:02:52 2010 +0100 @@ -3,6 +3,7 @@ #include #include +#include struct Vertex { float pos[4]; @@ -47,13 +48,31 @@ struct KDNode { int axis; float pt; + + KDNode *left, *right; + std::list faces; }; +struct KDNodeGPU { + int axis; + float pt; +}; + + class Scene { +private: + mutable Face *facebuf; + mutable int num_faces; + public: std::vector meshes; std::vector matlib; - std::vector kdtree; + + KDNode *kdtree; + std::vector kdtree_gpu; + + Scene(); + ~Scene(); bool add_mesh(Mesh *m); int get_num_meshes() const; @@ -66,6 +85,7 @@ bool load(const char *fname); bool load(FILE *fp); + const Face *get_face_buffer() const; void build_kdtree(); };