Blender V5.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 are stored as additional motion steps in ATTR_STD_POSITION.
13 * Normals in ATTR_STD_VERTEX_NORMAL and ATTR_STD_CORNER_NORMAL.
14 */
15
16#pragma once
17
18#include "kernel/globals.h"
19#include "kernel/types.h"
20
23#include "kernel/geom/object.h"
24
25#include "util/math_intersect.h"
26
28
29/* Ray intersection. We simply compute the vertex positions at the given ray
30 * time and do a ray intersection with the resulting triangle.
31 */
32
35 const float3 P,
36 const float3 dir,
37 const float tmin,
38 const float tmax,
39 const float time,
40 const uint visibility,
41 const int object,
42 const int prim,
43 const int prim_addr)
44{
45 /* Get vertex locations for intersection. */
46 float3 verts[3];
47 motion_triangle_vertices(kg, object, prim, time, verts);
48 /* Ray-triangle intersection, unoptimized. */
49 float t;
50 float u;
51 float v;
52 if (ray_triangle_intersect(P, dir, tmin, tmax, verts[0], verts[1], verts[2], &u, &v, &t)) {
53#if defined(__VISIBILITY_FLAG__) && !defined(__KERNEL_HIPRT__)
54 /* Visibility flag test. we do it here under the assumption that most triangles are culled by
55 * node flags.
56 * Note that HIP-RT performs visibility check on the instance level. */
57 if (kernel_data_fetch(prim_visibility, prim_addr) & visibility)
58#endif
59 {
60 isect->t = t;
61 isect->u = u;
62 isect->v = v;
63 isect->prim = prim;
64 isect->object = object;
65 isect->type = PRIMITIVE_MOTION_TRIANGLE;
66 return true;
67 }
68 }
69 return false;
70}
71
72/* Special ray intersection routines for local intersections. In that case we
73 * only want to intersect with primitives in the same object, and if case of
74 * multiple hits we pick a single random primitive as the intersection point.
75 * Returns whether traversal should be stopped.
76 */
77#ifdef __BVH_LOCAL__
78ccl_device_inline bool motion_triangle_intersect_local(KernelGlobals kg,
79 ccl_private LocalIntersection *local_isect,
80 const float3 P,
81 const float3 dir,
82 const float time,
83 const int object,
84 const int prim,
85 const float tmin,
86 const float tmax,
87 ccl_private uint *lcg_state,
88 const int max_hits)
89{
90 /* Get vertex locations for intersection. */
91 float3 verts[3];
92 motion_triangle_vertices(kg, object, prim, time, verts);
93 /* Ray-triangle intersection, unoptimized. */
94 float t;
95 float u;
96 float v;
97 if (!ray_triangle_intersect(P, dir, tmin, tmax, verts[0], verts[1], verts[2], &u, &v, &t)) {
98 return false;
99 }
100
101 /* If no actual hit information is requested, just return here. */
102 if (max_hits == 0) {
103 return true;
104 }
105
106 const int hit_index = local_intersect_get_record_index(local_isect, t, lcg_state, max_hits);
107 if (hit_index == -1) {
108 return false;
109 }
110
111 /* Record intersection. */
112 ccl_private Intersection *isect = &local_isect->hits[hit_index];
113 isect->t = t;
114 isect->u = u;
115 isect->v = v;
116 isect->prim = prim;
117 isect->object = object;
118 isect->type = PRIMITIVE_MOTION_TRIANGLE;
119
120 /* Record geometric normal. */
121 local_isect->Ng[hit_index] = normalize(cross(verts[1] - verts[0], verts[2] - verts[0]));
122
123 return false;
124}
125#endif /* __BVH_LOCAL__ */
126
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< float, 3 > cross(VecOp< float, 3 >, VecOp< float, 3 >) RET
@ PRIMITIVE_MOTION_TRIANGLE
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, const int object, const uint3 tri_vindex, const int numsteps, const int numverts, const int step, const float t, float3 verts[3])
CCL_NAMESPACE_BEGIN ccl_device_inline bool motion_triangle_intersect(KernelGlobals kg, ccl_private Intersection *isect, const float3 P, const float3 dir, const float tmin, const float tmax, const float time, const uint visibility, const int object, const int prim, const int prim_addr)
ATTR_WARN_UNUSED_RESULT const BMVert * v