1/**************************************************************************/
2/* static_raycaster.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef STATIC_RAYCASTER_H
32#define STATIC_RAYCASTER_H
33
34#include "core/object/ref_counted.h"
35
36#if !defined(__aligned)
37
38#if defined(_WIN32) && defined(_MSC_VER)
39#define __aligned(...) __declspec(align(__VA_ARGS__))
40#else
41#define __aligned(...) __attribute__((aligned(__VA_ARGS__)))
42#endif
43
44#endif
45
46class StaticRaycaster : public RefCounted {
47 GDCLASS(StaticRaycaster, RefCounted)
48protected:
49 static StaticRaycaster *(*create_function)();
50
51public:
52 // compatible with embree3 rays
53 struct __aligned(16) Ray {
54 const static unsigned int INVALID_GEOMETRY_ID = ((unsigned int)-1); // from rtcore_common.h
55
56 /*! Default construction does nothing. */
57 _FORCE_INLINE_ Ray() :
58 geomID(INVALID_GEOMETRY_ID) {}
59
60 /*! Constructs a ray from origin, direction, and ray segment. Near
61 * has to be smaller than far. */
62 _FORCE_INLINE_ Ray(const Vector3 &p_org,
63 const Vector3 &p_dir,
64 float p_tnear = 0.0f,
65 float p_tfar = INFINITY) :
66 org(p_org),
67 tnear(p_tnear),
68 dir(p_dir),
69 time(0.0f),
70 tfar(p_tfar),
71 mask(-1),
72 u(0.0),
73 v(0.0),
74 primID(INVALID_GEOMETRY_ID),
75 geomID(INVALID_GEOMETRY_ID),
76 instID(INVALID_GEOMETRY_ID) {}
77
78 /*! Tests if we hit something. */
79 _FORCE_INLINE_ explicit operator bool() const { return geomID != INVALID_GEOMETRY_ID; }
80
81 public:
82 Vector3 org; //!< Ray origin + tnear
83 float tnear; //!< Start of ray segment
84 Vector3 dir; //!< Ray direction + tfar
85 float time; //!< Time of this ray for motion blur.
86 float tfar; //!< End of ray segment
87 unsigned int mask; //!< used to mask out objects during traversal
88 unsigned int id; //!< ray ID
89 unsigned int flags; //!< ray flags
90
91 Vector3 normal; //!< Not normalized geometry normal
92 float u; //!< Barycentric u coordinate of hit
93 float v; //!< Barycentric v coordinate of hit
94 unsigned int primID; //!< primitive ID
95 unsigned int geomID; //!< geometry ID
96 unsigned int instID; //!< instance ID
97 };
98
99 virtual bool intersect(Ray &p_ray) = 0;
100 virtual void intersect(Vector<Ray> &r_rays) = 0;
101
102 virtual void add_mesh(const PackedVector3Array &p_vertices, const PackedInt32Array &p_indices, unsigned int p_id) = 0;
103 virtual void commit() = 0;
104
105 virtual void set_mesh_filter(const HashSet<int> &p_mesh_ids) = 0;
106 virtual void clear_mesh_filter() = 0;
107
108 static Ref<StaticRaycaster> create();
109};
110
111#endif // STATIC_RAYCASTER_H
112