diff options
-rw-r--r-- | model.cpp | 52 | ||||
-rw-r--r-- | model.hpp | 1 |
2 files changed, 31 insertions, 22 deletions
@@ -417,24 +417,6 @@ void Model_Instance::render( } } -int Model_Instance::pick(const v3f& o, const v3f& d) { - int i, c = m->mesh_count; - for (i = 0; i < c; i++) { - AABB& b = bounds[i]; - v3f id = 1.0f / d; - v3f t1 = (b.min - o) * id; - v3f t2 = (b.max - o) * id; - float tmin = std::max(std::max(std::min(t1.x, t2.x), std::min(t1.y, t2.y)), std::min(t1.z, t2.z)); - float tmax = std::min(std::min(std::max(t1.x, t2.x), std::max(t1.y, t2.y)), std::max(t1.z, t2.z)); - if (tmax < 0.0f || tmin > tmax) - { - continue; - } - return i; - } - return -1; -} - void Model_Scene::init( Arena* arena, int max_instances, @@ -515,10 +497,38 @@ std::pair<Model_Instance*, int> Model_Scene::pick( e.z = -1.0f; e.w = 0.0f; v4f d4 = cam.get_view().inverse() * e; v3f d = v3f::normalised(v3f(d4.x, d4.y, d4.z)); + const v3f& o = cam.position; + float t = INFINITY; + Model_Instance* r1 = 0; + int r2 = -1; for (i = 0; i < c; i++) { - int m = instances[i].pick(cam.position, d); - if (m >= 0) return { &instances[i], m }; + Model_Instance& inst = instances[i]; + Model* m = inst.m; + int j; + for (j = 0; j < m->mesh_count; j++) { + AABB& b = inst.bounds[j]; + v3f id = 1.0f / d; + v3f t1 = (b.min - o) * id; + v3f t2 = (b.max - o) * id; + float tmin = std::max(std::max( + std::min(t1.x, t2.x), + std::min(t1.y, t2.y)), + std::min(t1.z, t2.z) + ); + float tmax = std::min(std::min( + std::max(t1.x, t2.x), + std::max(t1.y, t2.y)), + std::max(t1.z, t2.z) + ); + if (tmax < 0.0f || tmin > tmax) + continue; + if (tmin < t) { + t = tmin; + r1 = &inst; + r2 = j; + } + } } - return { 0, -1 }; + return { r1, r2 }; } @@ -108,7 +108,6 @@ struct Model_Instance { Texture_Id env_cubemap, Sampler_Id sampler ); - int pick(const v3f& o, const v3f& d); }; struct Model_Scene { |