-#include <algorithm>
+#include <stdlib.h>
#include <float.h>
+#include <algorithm>
#include "geom.h"
#include "app.h"
return Vec3(v[xidx[idx]].x, v[yidx[idx]].y, v[zidx[idx]].z);
}
+Plane AABox::get_plane(int pidx) const
+{
+ switch(pidx) {
+ case AABOX_PLANE_PX:
+ return Plane(Vec3(max.x, 0, 0), Vec3(1, 0, 0));
+ case AABOX_PLANE_NX:
+ return Plane(Vec3(min.x, 0, 0), Vec3(-1, 0, 0));
+ case AABOX_PLANE_PY:
+ return Plane(Vec3(0, max.x, 0), Vec3(0, 1, 0));
+ case AABOX_PLANE_NY:
+ return Plane(Vec3(0, min.x, 0), Vec3(0, -1, 0));
+ case AABOX_PLANE_PZ:
+ return Plane(Vec3(0, 0, max.z), Vec3(0, 0, 1));
+ case AABOX_PLANE_NZ:
+ return Plane(Vec3(0, 0, min.z), Vec3(0, 0, -1));
+ default:
+ break;
+ }
+ abort();
+ return Plane();
+}
+
bool AABox::intersect(const Ray &ray, HitPoint *hit) const
{
Vec3 param[2] = {min, max};
struct MaterialEditMirror {
float reflectivity;
+ int plane;
};
struct MaterialEdit {
mir->reflect = obj->mtl.reflect;
if(obj->mtl.flat_mirror == MTL_MIRROR_AUTO) {
- // assume the object is actually flat, so grab the first triangle and make a plane
+ // 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();
mir->plane.pt = face.v[0];
mir->plane.normal = face.normal;
} else {
+ int plane_idx = obj->mtl.flat_mirror - MTL_MIRROR_AABB_PX;
+ if(obj->node) {
+ mir->plane = obj->node->get_bounds().get_plane(plane_idx);
+ } else {
+ mir->plane = obj->get_aabox().get_plane(plane_idx);
+ }
+ debug_log("mirror plane: p(%f %f %f) n(%f %f %f)\n", mir->plane.pt.x, mir->plane.pt.y,
+ mir->plane.pt.z, mir->plane.normal.x, mir->plane.normal.y, mir->plane.normal.z);
}
// check to see if we have found this mirror plane already
if(strcmp(cn->name, "mirror") == 0) {
// make this object a flat mirror (hopefully the object is flat otherwise this won't work)
float refl = 1.0f;
+ int plane = MTL_MIRROR_AUTO;
+
+ struct ts_attr *aplane = ts_get_attr(cn, "plane");
+ if(aplane) {
+ if(aplane->val.type == TS_NUMBER) {
+ plane = MTL_MIRROR_AABB_PX + aplane->val.inum;
+ } else {
+ char csign, caxis;
+ if(sscanf(aplane->val.str, "aabb%c%c", &csign, &caxis) != 2 || (csign != '+' && csign != '-')) {
+ error_log("invalid reflect plane specifier: %s\n", aplane->val.str);
+ continue;
+ }
+
+ switch(tolower(caxis)) {
+ case 'x':
+ plane = caxis == '+' ? MTL_MIRROR_AABB_PX : MTL_MIRROR_AABB_NX;
+ break;
+ case 'y':
+ plane = caxis == '+' ? MTL_MIRROR_AABB_PY : MTL_MIRROR_AABB_NY;
+ break;
+ case 'z':
+ plane = caxis == '+' ? MTL_MIRROR_AABB_PZ : MTL_MIRROR_AABB_NZ;
+ break;
+ default:
+ error_log("invalid reflect plane specifier: %s\n", aplane->val.str);
+ continue;
+ }
+ }
+ }
struct ts_attr *arefl = ts_get_attr(cn, "reflect");
if(arefl) {
med->type = MTL_EDIT_MIRROR;
med->mirror.reflectivity = refl;
+ med->mirror.plane = plane;
break;
}
}
break;
case MTL_EDIT_MIRROR:
- mtl->flat_mirror = med.mirror.reflectivity > 1e-6;
+ mtl->flat_mirror = med.mirror.plane;
mtl->reflect = med.mirror.reflectivity;
break;
}