Rendu
A lightweight rendering engine for experimentations
Loading...
Searching...
No Matches
Raycaster.hpp
1#pragma once
2#include "resources/Mesh.hpp"
3#include "Common.hpp"
4#include "raycaster/Intersection.hpp"
5
10class Raycaster {
11
13
14public:
16 struct Hit {
17
19
20 bool hit;
21 float dist;
22 float u;
23 float v;
24 float w;
25 unsigned long localId;
26 unsigned long meshId;
27
29 Hit();
30
38 Hit(float distance, float uu, float vv, unsigned long lid, unsigned long mid);
39
40 private:
41 unsigned long internalId;
42 };
43
45 Raycaster() = default;
46
51 void addMesh(const Mesh & mesh, const glm::mat4 & model);
52
56 void updateHierarchy();
57
65 Hit intersects(const glm::vec3 & origin, const glm::vec3 & direction, float mini = 0.0001f, float maxi = 1e8f) const;
66
74 bool intersectsAny(const glm::vec3 & origin, const glm::vec3 & direction, float mini = 0.0001f, float maxi = 1e8f) const;
75
82 bool visible(const glm::vec3 & p0, const glm::vec3 & p1) const;
83
90 template<typename T>
91 static T interpolateAttribute(const Hit & hit, const Mesh & geometry, const std::vector<T> & attribute){
92 const unsigned long triId = hit.localId;
93 const unsigned long i0 = geometry.indices[triId];
94 const unsigned long i1 = geometry.indices[triId + 1];
95 const unsigned long i2 = geometry.indices[triId + 2];
96 return hit.w * attribute[i0] + hit.u * attribute[i1] + hit.v * attribute[i2];
97 }
98
100 Raycaster(const Raycaster &) = delete;
101
105 Raycaster & operator=(const Raycaster &) = delete;
106
108 Raycaster(Raycaster &&) = delete;
109
114
115private:
119 unsigned long v0 = 0;
120 unsigned long v1 = 0;
121 unsigned long v2 = 0;
122 unsigned long localId = 0;
123 unsigned int meshId = 0;
124 };
125
127 struct Node {
129 size_t left = 0;
130 size_t right = 0;
131 bool leaf = true;
132 };
133
141 Hit intersects(const Ray & ray, const TriangleInfos & tri, float mini, float maxi) const;
142
150 static bool intersects(const Ray & ray, const BoundingBox & box, float mini, float maxi);
151
152 std::vector<TriangleInfos> _triangles;
153 std::vector<glm::vec3> _vertices;
154 std::vector<Node> _hierarchy;
155
156 unsigned int _meshCount = 0;
157};
Represent the smallest axis-aligne box containing a given object or region of space.
Definition: Bounds.hpp:28
Represents a geometric mesh composed of vertices, other attributes and triangles.
Definition: Mesh.hpp:15
std::vector< unsigned int > indices
The triangular faces indices.
Definition: Mesh.hpp:132
Allows to cast rays against a polygonal mesh, on the CPU. Relies on an internal acceleration structur...
Definition: Raycaster.hpp:10
Raycaster()=default
Raycaster(Raycaster &&)=delete
Hit intersects(const glm::vec3 &origin, const glm::vec3 &direction, float mini=0.0001f, float maxi=1e8f) const
Definition: Raycaster.cpp:144
bool intersectsAny(const glm::vec3 &origin, const glm::vec3 &direction, float mini=0.0001f, float maxi=1e8f) const
Definition: Raycaster.cpp:187
static T interpolateAttribute(const Hit &hit, const Mesh &geometry, const std::vector< T > &attribute)
Definition: Raycaster.hpp:91
void addMesh(const Mesh &mesh, const glm::mat4 &model)
Definition: Raycaster.cpp:14
unsigned int _meshCount
Number of meshes stored in the raycaster.
Definition: Raycaster.hpp:156
std::vector< Node > _hierarchy
Acceleration structure.
Definition: Raycaster.hpp:154
Raycaster & operator=(const Raycaster &)=delete
void updateHierarchy()
Definition: Raycaster.cpp:54
bool visible(const glm::vec3 &p0, const glm::vec3 &p1) const
Definition: Raycaster.cpp:226
std::vector< glm::vec3 > _vertices
Merged vertices.
Definition: Raycaster.hpp:153
Raycaster(const Raycaster &)=delete
std::vector< TriangleInfos > _triangles
Merged triangles informations.
Definition: Raycaster.hpp:152
static bool intersects(const Ray &ray, const BoundingBox &box, float mini, float maxi)
Raycaster & operator=(Raycaster &&)=delete
Helper that can generate information on a raycaster internal data: BVH visualisation,...
Definition: RaycasterVisualisation.hpp:10
Represent a ray, shot from a given position in a specific direction.
Definition: Intersection.hpp:10
Definition: Raycaster.hpp:16
unsigned long localId
Position of the hit triangle first vertex in the mesh index buffer.
Definition: Raycaster.hpp:25
float dist
Distance from the ray origin to the hit location.
Definition: Raycaster.hpp:21
unsigned long internalId
Index of the triangle in the raycaster internal primitive list.
Definition: Raycaster.hpp:41
bool hit
Denote if there has been a hit.
Definition: Raycaster.hpp:20
float u
First barycentric coordinate.
Definition: Raycaster.hpp:22
float v
Second barycentric coordinate.
Definition: Raycaster.hpp:23
unsigned long meshId
Index of the mesh hit by the ray.
Definition: Raycaster.hpp:26
Hit()
Definition: Raycaster.cpp:6
float w
Third barycentric coordinate.
Definition: Raycaster.hpp:24
Definition: Raycaster.hpp:127
size_t left
Index of the left child element, or first triangle index if this is a leaf.
Definition: Raycaster.hpp:129
size_t right
Index of the right child element, or number of triangles if this is a leaf.
Definition: Raycaster.hpp:130
bool leaf
Is this a leaf in the hierarchy.
Definition: Raycaster.hpp:131
BoundingBox box
Bounding box of the contained geometry.
Definition: Raycaster.hpp:128
Definition: Raycaster.hpp:117
unsigned int meshId
Index of the mesh this triangle belongs to.
Definition: Raycaster.hpp:123
unsigned long localId
Position of the triangle first vertex in the mesh initial index buffer.
Definition: Raycaster.hpp:122
unsigned long v1
Second vertex index.
Definition: Raycaster.hpp:120
unsigned long v2
Third vertex index.
Definition: Raycaster.hpp:121
unsigned long v0
First vertex index.
Definition: Raycaster.hpp:119
BoundingBox box
The triangle axis-aligned bounding box.
Definition: Raycaster.hpp:118