#include "camera.hpp" #include "model.hpp" #include "scene.hpp" void update_scene( Model_Scene& ms, Device* dev, World& w ) { for (auto v : w.view()) { auto& t = v.get(); auto& m = v.get(); m.i->transform = t.mat; } ms.update(dev); } std::pair scene_pick( World& world, const Camera& cam, int w, int h, int mx, int my ) { v2f uv = (2.0f * v2f(mx, my) - v2f(w, h)) / (float)h; v4f e = cam.get_proj().inverse() * v4f(uv.x, uv.y, -1.0f, 1.0f); 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; Entity_Id r1 = 0; int r2 = -1; for (auto v : world.view()) { Model_Instance& inst = *v.get().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 = v.entity(); r2 = j; } } } return { r1, r2 }; }