Cogs.Core
RayBoxIntersection.h
1#pragma once
2
3#include "Math/LinePlaneIntersection.h"
4#include "Math/RayTriangleIntersection.h"
5
6#include "Foundation/Geometry/Frustum.hpp"
7
8#include <glm/vec3.hpp>
9
10namespace Cogs
11{
12 namespace Geometry
13 {
14 inline bool intersect(const Cogs::Geometry::BoundingBox & box, const glm::vec3 & origin, const glm::vec3 & direction, glm::vec3 & intersection,
15 const bool /*usefullviewvolume*/)
16 {
17 glm::vec3 bounds[2] = {
18 box.min,
19 box.max
20 };
21
22 glm::vec3 pointOnRay, ptonbox;
23 double sqrmindist = DBL_MAX;
24
25 for (int j = 0; j < 2; j++) {
26 for (int i = 0; i < 3; i++) {
27 glm::vec3 norm(0, 0, 0);
28
29 norm[i] = 1.0f;
30
31 glm::vec3 hitPoint;
32
33 Plane plane;
34 plane.normal = norm;
35 plane.distance = bounds[j][i];
36
37 if (intersect(plane.normal, plane.distance, origin, direction, hitPoint)) {
38 int i1 = (i + 1) % 3;
39 int i2 = (i + 2) % 3;
40
41 double x, y;
42
43 double d = distanceToQuad(
44 bounds[0][i1], bounds[0][i2],
45 bounds[1][i1], bounds[1][i2],
46 hitPoint[i1], hitPoint[i2],
47 x, y);
48
49 if (d <= 0.0f) {
50 // Center of ray hit box directly
51 intersection = hitPoint;
52
53 return true;
54 } else if (d < sqrmindist) {
55 sqrmindist = d;
56 pointOnRay = ptonbox = hitPoint;
57
58 ptonbox[i1] = static_cast<float>(x);
59 ptonbox[i2] = static_cast<float>(y);
60 }
61 }
62 }
63 }
64
65 return false;
66 }
67 }
68}
@ Geometry
Store entity vector fields (vector<vec3>, vector<vec2>, vector<int>, vector<float>).
Contains all Cogs related functionality.
Definition: FieldSetter.h:23