clray
annotate 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 |
rev | line source |
---|---|
John@15 | 1 #include <math.h> |
nuclear@22 | 2 #include "scene.h" |
nuclear@6 | 3 |
John@15 | 4 #define FEQ(a, b) (fabs((a) - (b)) < 1e-8) |
John@15 | 5 bool Face::operator ==(const Face &f) const |
John@15 | 6 { |
John@15 | 7 for(int i=0; i<3; i++) { |
John@15 | 8 for(int j=0; j<3; j++) { |
John@15 | 9 if(!FEQ(v[i].pos[j], f.v[i].pos[j])) { |
John@15 | 10 return false; |
John@15 | 11 } |
John@15 | 12 if(!FEQ(v[i].normal[j], f.v[i].normal[j])) { |
John@15 | 13 return false; |
John@15 | 14 } |
John@15 | 15 } |
John@15 | 16 if(!FEQ(normal[i], f.normal[i])) { |
John@15 | 17 return false; |
John@15 | 18 } |
John@15 | 19 } |
John@15 | 20 return true; |
John@15 | 21 } |
John@15 | 22 |
nuclear@24 | 23 Scene::Scene() |
nuclear@24 | 24 { |
nuclear@24 | 25 facebuf = 0; |
nuclear@24 | 26 num_faces = -1; |
nuclear@24 | 27 kdtree = 0; |
nuclear@24 | 28 } |
nuclear@24 | 29 |
nuclear@24 | 30 Scene::~Scene() |
nuclear@24 | 31 { |
nuclear@24 | 32 delete [] facebuf; |
nuclear@24 | 33 } |
nuclear@24 | 34 |
nuclear@13 | 35 bool Scene::add_mesh(Mesh *m) |
nuclear@13 | 36 { |
nuclear@13 | 37 // make sure triangles have material ids |
nuclear@13 | 38 for(size_t i=0; i<m->faces.size(); i++) { |
nuclear@13 | 39 m->faces[i].matid = m->matid; |
nuclear@13 | 40 } |
nuclear@24 | 41 |
nuclear@24 | 42 try { |
nuclear@24 | 43 meshes.push_back(m); |
nuclear@24 | 44 } |
nuclear@24 | 45 catch(...) { |
nuclear@24 | 46 return false; |
nuclear@24 | 47 } |
nuclear@24 | 48 |
nuclear@24 | 49 // invalidate facebuffer and count |
nuclear@24 | 50 delete [] facebuf; |
nuclear@24 | 51 facebuf = 0; |
nuclear@24 | 52 num_faces = -1; |
nuclear@24 | 53 |
nuclear@13 | 54 return true; |
nuclear@13 | 55 } |
nuclear@13 | 56 |
John@14 | 57 int Scene::get_num_meshes() const |
John@14 | 58 { |
John@14 | 59 return (int)meshes.size(); |
John@14 | 60 } |
John@14 | 61 |
nuclear@13 | 62 int Scene::get_num_faces() const |
nuclear@13 | 63 { |
nuclear@24 | 64 if(num_faces >= 0) { |
nuclear@24 | 65 return num_faces; |
nuclear@24 | 66 } |
nuclear@24 | 67 |
nuclear@24 | 68 num_faces = 0; |
nuclear@13 | 69 for(size_t i=0; i<meshes.size(); i++) { |
nuclear@13 | 70 num_faces += meshes[i]->faces.size(); |
nuclear@13 | 71 } |
nuclear@13 | 72 return num_faces; |
nuclear@13 | 73 } |
nuclear@13 | 74 |
John@14 | 75 int Scene::get_num_materials() const |
John@14 | 76 { |
John@14 | 77 return (int)matlib.size(); |
John@14 | 78 } |
John@14 | 79 |
John@14 | 80 Material *Scene::get_materials() |
John@14 | 81 { |
John@14 | 82 if(matlib.empty()) { |
John@14 | 83 return 0; |
John@14 | 84 } |
John@14 | 85 return &matlib[0]; |
John@14 | 86 } |
John@14 | 87 |
John@14 | 88 const Material *Scene::get_materials() const |
John@14 | 89 { |
John@14 | 90 if(matlib.empty()) { |
John@14 | 91 return 0; |
John@14 | 92 } |
John@14 | 93 return &matlib[0]; |
John@14 | 94 } |
nuclear@24 | 95 |
nuclear@24 | 96 const Face *Scene::get_face_buffer() const |
nuclear@24 | 97 { |
nuclear@24 | 98 if(facebuf) { |
nuclear@24 | 99 return facebuf; |
nuclear@24 | 100 } |
nuclear@24 | 101 |
nuclear@24 | 102 int num_meshes = get_num_meshes(); |
nuclear@24 | 103 |
nuclear@24 | 104 printf("constructing face buffer with %d faces (out of %d meshes)\n", get_num_faces(), num_meshes); |
nuclear@24 | 105 facebuf = new Face[num_faces]; |
nuclear@24 | 106 Face *fptr = facebuf; |
nuclear@24 | 107 |
nuclear@24 | 108 for(int i=0; i<num_meshes; i++) { |
nuclear@24 | 109 for(size_t j=0; j<meshes[i]->faces.size(); j++) { |
nuclear@24 | 110 *fptr++ = meshes[i]->faces[j]; |
nuclear@24 | 111 } |
nuclear@24 | 112 } |
nuclear@24 | 113 return facebuf; |
nuclear@24 | 114 } |
nuclear@24 | 115 |
nuclear@24 | 116 static void build_kdtree(KDNode **kd, std::list<const Face*> *faces); |
nuclear@24 | 117 |
nuclear@24 | 118 void Scene::build_kdtree() |
nuclear@24 | 119 { |
nuclear@24 | 120 const Face *faces = get_face_buffer(); |
nuclear@24 | 121 int num_faces = get_num_faces(); |
nuclear@24 | 122 |
nuclear@24 | 123 std::list<const Face*> facelist; |
nuclear@24 | 124 for(int i=0; i<num_faces; i++) { |
nuclear@24 | 125 facelist.push_back(faces + i); |
nuclear@24 | 126 } |
nuclear@24 | 127 |
nuclear@24 | 128 ::build_kdtree(&kdtree, &facelist); |
nuclear@24 | 129 } |
nuclear@24 | 130 |
nuclear@24 | 131 static void build_kdtree(KDNode **kd, std::list<const Face*> *faces) |
nuclear@24 | 132 { |
nuclear@24 | 133 } |