clray

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