+int MetaScene::calc_mirror_planes()
+{
+ int num_mirrors = 0;
+ while(mirrors) {
+ FlatMirror *m = mirrors;
+ mirrors = mirrors->next;
+ delete m;
+ }
+ mirrors = 0;
+ objmirror.clear();
+
+ int numscn = scenes.size();
+ for(int i=0; i<numscn; i++) {
+ Scene *scn = scenes[i];
+
+ int numobj = scn->objects.size();
+ for(int j=0; j<numobj; j++) {
+ Object *obj = scn->objects[j];
+
+ if(obj->mtl.flat_mirror && obj->get_type() == OBJ_MESH) {
+ const Mesh *mesh = ((ObjMesh*)obj)->mesh;
+ if(!mesh) continue;
+
+ // assume the object is actually flat, so grab the first triangle and make a plane
+ Triangle face = Triangle(0, (const Vec3*)mesh->get_attrib_data(MESH_ATTR_VERTEX),
+ mesh->get_index_data());
+ face.calc_normal();
+
+ FlatMirror *mir = new FlatMirror;
+ mir->plane.pt = face.v[0];
+ mir->plane.normal = face.normal;
+ mir->reflect = obj->mtl.reflect;
+
+ // check to see if we have found this mirror plane already
+ bool found = false;
+ FlatMirror *node = mirrors;
+ while(node) {
+ if(fabs(dot(mir->plane.normal, node->plane.normal)) < 1e-4 &&
+ fabs(dot(mir->plane.normal, normalize(mir->plane.pt - node->plane.pt))) < 1e-4) {
+ found = true;
+ break;
+ }
+ }
+
+ if(!found) {
+ mir->next = mirrors;
+ mirrors = mir;
+
+ objmirror[obj] = mir; // associate with object
+ ++num_mirrors;
+ } else {
+ delete mir;
+ }
+ }
+ }
+ }
+
+ return num_mirrors;
+}
+