Blender V4.3
motion_triangle_intersect.h
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2011-2022 Blender Foundation
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5/* Motion Triangle Primitive
6 *
7 * These are stored as regular triangles, plus extra positions and normals at
8 * times other than the frame center. Computing the triangle vertex positions
9 * or normals at a given ray time is a matter of interpolation of the two steps
10 * between which the ray time lies.
11 *
12 * The extra positions and normals are stored as ATTR_STD_MOTION_VERTEX_POSITION
13 * and ATTR_STD_MOTION_VERTEX_NORMAL mesh attributes.
14 */
15
16#pragma once
17
19
24 KernelGlobals kg, ccl_private ShaderData *sd, const float u, const float v, float3 verts[3])
25{
26 /* This appears to give slightly better precision than interpolating with w = (1 - u - v). */
27 float3 P = verts[0] + u * (verts[1] - verts[0]) + v * (verts[2] - verts[0]);
28
29 if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
30 const Transform tfm = object_get_transform(kg, sd);
31 P = transform_point(&tfm, P);
32 }
33
34 return P;
35}
36
37/* Ray intersection. We simply compute the vertex positions at the given ray
38 * time and do a ray intersection with the resulting triangle.
39 */
40
43 float3 P,
44 float3 dir,
45 float tmin,
46 float tmax,
47 float time,
48 uint visibility,
49 int object,
50 int prim,
51 int prim_addr)
52{
53 /* Get vertex locations for intersection. */
54 float3 verts[3];
55 motion_triangle_vertices(kg, object, prim, time, verts);
56 /* Ray-triangle intersection, unoptimized. */
57 float t, u, v;
58 if (ray_triangle_intersect(P, dir, tmin, tmax, verts[0], verts[1], verts[2], &u, &v, &t)) {
59#ifdef __VISIBILITY_FLAG__
60 /* Visibility flag test. we do it here under the assumption
61 * that most triangles are culled by node flags.
62 */
63 if (kernel_data_fetch(prim_visibility, prim_addr) & visibility)
64#endif
65 {
66 isect->t = t;
67 isect->u = u;
68 isect->v = v;
69 isect->prim = prim;
70 isect->object = object;
71 isect->type = PRIMITIVE_MOTION_TRIANGLE;
72 return true;
73 }
74 }
75 return false;
76}
77
78/* Special ray intersection routines for local intersections. In that case we
79 * only want to intersect with primitives in the same object, and if case of
80 * multiple hits we pick a single random primitive as the intersection point.
81 * Returns whether traversal should be stopped.
82 */
83#ifdef __BVH_LOCAL__
84ccl_device_inline bool motion_triangle_intersect_local(KernelGlobals kg,
85 ccl_private LocalIntersection *local_isect,
86 float3 P,
87 float3 dir,
88 float time,
89 int object,
90 int prim,
91 int prim_addr,
92 float tmin,
93 float tmax,
94 ccl_private uint *lcg_state,
95 int max_hits)
96{
97 /* Get vertex locations for intersection. */
98 float3 verts[3];
99 motion_triangle_vertices(kg, object, prim, time, verts);
100 /* Ray-triangle intersection, unoptimized. */
101 float t, u, v;
102 if (!ray_triangle_intersect(P, dir, tmin, tmax, verts[0], verts[1], verts[2], &u, &v, &t)) {
103 return false;
104 }
105
106 /* If no actual hit information is requested, just return here. */
107 if (max_hits == 0) {
108 return true;
109 }
110
111 int hit;
112 if (lcg_state) {
113 /* Record up to max_hits intersections. */
114 for (int i = min(max_hits, local_isect->num_hits) - 1; i >= 0; --i) {
115 if (local_isect->hits[i].t == t) {
116 return false;
117 }
118 }
119
120 local_isect->num_hits++;
121
122 if (local_isect->num_hits <= max_hits) {
123 hit = local_isect->num_hits - 1;
124 }
125 else {
126 /* Reservoir sampling: if we are at the maximum number of
127 * hits, randomly replace element or skip it.
128 */
129 hit = lcg_step_uint(lcg_state) % local_isect->num_hits;
130
131 if (hit >= max_hits)
132 return false;
133 }
134 }
135 else {
136 /* Record closest intersection only. */
137 if (local_isect->num_hits && t > local_isect->hits[0].t) {
138 return false;
139 }
140
141 hit = 0;
142 local_isect->num_hits = 1;
143 }
144
145 /* Record intersection. */
146 ccl_private Intersection *isect = &local_isect->hits[hit];
147 isect->t = t;
148 isect->u = u;
149 isect->v = v;
150 isect->prim = prim;
151 isect->object = object;
152 isect->type = PRIMITIVE_MOTION_TRIANGLE;
153
154 /* Record geometric normal. */
155 local_isect->Ng[hit] = normalize(cross(verts[1] - verts[0], verts[2] - verts[0]));
156
157 return false;
158}
159#endif /* __BVH_LOCAL__ */
160
unsigned int uint
ATTR_WARN_UNUSED_RESULT const BMVert * v
SIMD_FORCE_INLINE btVector3 & normalize()
Normalize this vector x^2 + y^2 + z^2 = 1.
Definition btVector3.h:303
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
static float verts[][3]
ccl_device_inline Transform object_get_transform(KernelGlobals kg, ccl_private const ShaderData *sd)
@ PRIMITIVE_MOTION_TRIANGLE
ShaderData
@ SD_OBJECT_TRANSFORM_APPLIED
CCL_NAMESPACE_BEGIN ccl_device uint lcg_step_uint(T rng)
Definition lcg.h:14
ccl_device_inline float cross(const float2 a, const float2 b)
ccl_device_forceinline bool ray_triangle_intersect(const float3 ray_P, const float3 ray_D, const float ray_tmin, const float ray_tmax, const float3 tri_a, const float3 tri_b, const float3 tri_c, ccl_private float *isect_u, ccl_private float *isect_v, ccl_private float *isect_t)
ccl_device_inline void motion_triangle_vertices(KernelGlobals kg, int object, uint3 tri_vindex, int numsteps, int numverts, int step, float t, float3 verts[3])
ccl_device_inline bool motion_triangle_intersect(KernelGlobals kg, ccl_private Intersection *isect, float3 P, float3 dir, float tmin, float tmax, float time, uint visibility, int object, int prim, int prim_addr)
CCL_NAMESPACE_BEGIN ccl_device_inline float3 motion_triangle_point_from_uv(KernelGlobals kg, ccl_private ShaderData *sd, const float u, const float v, float3 verts[3])
#define min(a, b)
Definition sort.c:32
CCL_NAMESPACE_END CCL_NAMESPACE_BEGIN ccl_device_inline float3 transform_point(ccl_private const Transform *t, const float3 a)
Definition transform.h:63