From 2f5ee2007d258d947f2efab3bf3460479fe34813 Mon Sep 17 00:00:00 2001 From: John Tsiombikas Date: Wed, 14 Mar 2018 10:13:23 +0200 Subject: [PATCH] using aabb planes as mirror planes --- src/geom.cc | 25 ++++++++++++++++++++++++- src/geom.h | 10 ++++++++++ src/metascene.cc | 43 +++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/geom.cc b/src/geom.cc index 3a287a6..1813eea 100644 --- a/src/geom.cc +++ b/src/geom.cc @@ -1,5 +1,6 @@ -#include +#include #include +#include #include "geom.h" #include "app.h" @@ -144,6 +145,28 @@ Vec3 AABox::get_corner(int idx) const 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}; diff --git a/src/geom.h b/src/geom.h index 5de6a62..0d8dd00 100644 --- a/src/geom.h +++ b/src/geom.h @@ -16,7 +16,17 @@ enum GeomObjectType { GOBJ_DISC }; +enum { + AABOX_PLANE_PX, + AABOX_PLANE_NX, + AABOX_PLANE_PY, + AABOX_PLANE_NY, + AABOX_PLANE_PZ, + AABOX_PLANE_NZ +}; + class GeomObject; +class Plane; struct HitPoint { float dist; // parametric distance along the ray diff --git a/src/metascene.cc b/src/metascene.cc index 09527e9..82cf669 100644 --- a/src/metascene.cc +++ b/src/metascene.cc @@ -37,6 +37,7 @@ struct MaterialEditTexture { struct MaterialEditMirror { float reflectivity; + int plane; }; struct MaterialEdit { @@ -227,7 +228,7 @@ int MetaScene::calc_mirror_planes() 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(); @@ -235,6 +236,14 @@ int MetaScene::calc_mirror_planes() 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 @@ -499,6 +508,35 @@ static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *nod 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) { @@ -512,6 +550,7 @@ static bool proc_mtledit(MetaScene *mscn, MaterialEdit *med, struct ts_node *nod med->type = MTL_EDIT_MIRROR; med->mirror.reflectivity = refl; + med->mirror.plane = plane; break; } } @@ -578,7 +617,7 @@ static void apply_mtledit(Material *mtl, const MaterialEdit &med) 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; } -- 1.7.10.4