Blender  V3.3
motion_triangle_intersect.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0
2  * Copyright 2011-2022 Blender Foundation */
3 
4 /* Motion Triangle Primitive
5  *
6  * These are stored as regular triangles, plus extra positions and normals at
7  * times other than the frame center. Computing the triangle vertex positions
8  * or normals at a given ray time is a matter of interpolation of the two steps
9  * between which the ray time lies.
10  *
11  * The extra positions and normals are stored as ATTR_STD_MOTION_VERTEX_POSITION
12  * and ATTR_STD_MOTION_VERTEX_NORMAL mesh attributes.
13  */
14 
15 #pragma once
16 
18 
24  const int isect_object,
25  const int isect_prim,
26  const float u,
27  const float v,
28  float3 verts[3])
29 {
30  /* This appears to give slightly better precision than interpolating with w = (1 - u - v). */
31  float3 P = verts[0] + u * (verts[1] - verts[0]) + v * (verts[2] - verts[0]);
32 
33  if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
34  const Transform tfm = object_get_transform(kg, sd);
35  P = transform_point(&tfm, P);
36  }
37 
38  return P;
39 }
40 
41 /* Ray intersection. We simply compute the vertex positions at the given ray
42  * time and do a ray intersection with the resulting triangle.
43  */
44 
47  float3 P,
48  float3 dir,
49  float tmin,
50  float tmax,
51  float time,
52  uint visibility,
53  int object,
54  int prim,
55  int prim_addr)
56 {
57  /* Get vertex locations for intersection. */
58  float3 verts[3];
59  motion_triangle_vertices(kg, object, prim, time, verts);
60  /* Ray-triangle intersection, unoptimized. */
61  float t, u, v;
62  if (ray_triangle_intersect(P, dir, tmin, tmax, verts[0], verts[1], verts[2], &u, &v, &t)) {
63 #ifdef __VISIBILITY_FLAG__
64  /* Visibility flag test. we do it here under the assumption
65  * that most triangles are culled by node flags.
66  */
67  if (kernel_data_fetch(prim_visibility, prim_addr) & visibility)
68 #endif
69  {
70  isect->t = t;
71  isect->u = u;
72  isect->v = v;
73  isect->prim = prim;
74  isect->object = object;
75  isect->type = PRIMITIVE_MOTION_TRIANGLE;
76  return true;
77  }
78  }
79  return false;
80 }
81 
82 /* Special ray intersection routines for local intersections. In that case we
83  * only want to intersect with primitives in the same object, and if case of
84  * multiple hits we pick a single random primitive as the intersection point.
85  * Returns whether traversal should be stopped.
86  */
87 #ifdef __BVH_LOCAL__
88 ccl_device_inline bool motion_triangle_intersect_local(KernelGlobals kg,
89  ccl_private LocalIntersection *local_isect,
90  float3 P,
91  float3 dir,
92  float time,
93  int object,
94  int prim,
95  int prim_addr,
96  float tmin,
97  float tmax,
98  ccl_private uint *lcg_state,
99  int max_hits)
100 {
101  /* Get vertex locations for intersection. */
102  float3 verts[3];
103  motion_triangle_vertices(kg, object, prim, time, verts);
104  /* Ray-triangle intersection, unoptimized. */
105  float t, u, v;
106  if (!ray_triangle_intersect(P, dir, tmin, tmax, verts[0], verts[1], verts[2], &u, &v, &t)) {
107  return false;
108  }
109 
110  /* If no actual hit information is requested, just return here. */
111  if (max_hits == 0) {
112  return true;
113  }
114 
115  int hit;
116  if (lcg_state) {
117  /* Record up to max_hits intersections. */
118  for (int i = min(max_hits, local_isect->num_hits) - 1; i >= 0; --i) {
119  if (local_isect->hits[i].t == t) {
120  return false;
121  }
122  }
123 
124  local_isect->num_hits++;
125 
126  if (local_isect->num_hits <= max_hits) {
127  hit = local_isect->num_hits - 1;
128  }
129  else {
130  /* Reservoir sampling: if we are at the maximum number of
131  * hits, randomly replace element or skip it.
132  */
133  hit = lcg_step_uint(lcg_state) % local_isect->num_hits;
134 
135  if (hit >= max_hits)
136  return false;
137  }
138  }
139  else {
140  /* Record closest intersection only. */
141  if (local_isect->num_hits && t > local_isect->hits[0].t) {
142  return false;
143  }
144 
145  hit = 0;
146  local_isect->num_hits = 1;
147  }
148 
149  /* Record intersection. */
150  ccl_private Intersection *isect = &local_isect->hits[hit];
151  isect->t = t;
152  isect->u = u;
153  isect->v = v;
154  isect->prim = prim;
155  isect->object = object;
156  isect->type = PRIMITIVE_MOTION_TRIANGLE;
157 
158  /* Record geometric normal. */
159  local_isect->Ng[hit] = normalize(cross(verts[1] - verts[0], verts[2] - verts[0]));
160 
161  return false;
162 }
163 #endif /* __BVH_LOCAL__ */
164 
unsigned int uint
Definition: BLI_sys_types.h:67
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum const void *lists _GL_VOID_RET _GL_VOID const GLdouble *equation _GL_VOID_RET _GL_VOID GLdouble GLdouble blue _GL_VOID_RET _GL_VOID GLfloat GLfloat blue _GL_VOID_RET _GL_VOID GLint GLint blue _GL_VOID_RET _GL_VOID GLshort GLshort blue _GL_VOID_RET _GL_VOID GLubyte GLubyte blue _GL_VOID_RET _GL_VOID GLuint GLuint blue _GL_VOID_RET _GL_VOID GLushort GLushort blue _GL_VOID_RET _GL_VOID GLbyte GLbyte GLbyte alpha _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble alpha _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat alpha _GL_VOID_RET _GL_VOID GLint GLint GLint alpha _GL_VOID_RET _GL_VOID GLshort GLshort GLshort alpha _GL_VOID_RET _GL_VOID GLubyte GLubyte GLubyte alpha _GL_VOID_RET _GL_VOID GLuint GLuint GLuint alpha _GL_VOID_RET _GL_VOID GLushort GLushort GLushort alpha _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLint GLsizei GLsizei GLenum type _GL_VOID_RET _GL_VOID GLsizei GLenum GLenum const void *pixels _GL_VOID_RET _GL_VOID const void *pointer _GL_VOID_RET _GL_VOID GLdouble v _GL_VOID_RET _GL_VOID GLfloat v _GL_VOID_RET _GL_VOID GLint GLint i2 _GL_VOID_RET _GL_VOID GLint j _GL_VOID_RET _GL_VOID GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble GLdouble GLdouble zFar _GL_VOID_RET _GL_UINT GLdouble *equation _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLenum GLfloat *v _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLfloat *values _GL_VOID_RET _GL_VOID GLushort *values _GL_VOID_RET _GL_VOID GLenum GLfloat *params _GL_VOID_RET _GL_VOID GLenum GLdouble *params _GL_VOID_RET _GL_VOID GLenum GLint *params _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_BOOL GLfloat param _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLushort pattern _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLint GLdouble GLdouble GLint GLint const GLdouble *points _GL_VOID_RET _GL_VOID GLdouble GLdouble u2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLint GLdouble GLdouble v2 _GL_VOID_RET _GL_VOID GLenum GLfloat param _GL_VOID_RET _GL_VOID GLenum GLint param _GL_VOID_RET _GL_VOID GLenum mode _GL_VOID_RET _GL_VOID GLdouble GLdouble nz _GL_VOID_RET _GL_VOID GLfloat GLfloat nz _GL_VOID_RET _GL_VOID GLint GLint nz _GL_VOID_RET _GL_VOID GLshort GLshort nz _GL_VOID_RET _GL_VOID GLsizei const void *pointer _GL_VOID_RET _GL_VOID GLsizei const GLfloat *values _GL_VOID_RET _GL_VOID GLsizei const GLushort *values _GL_VOID_RET _GL_VOID GLint param _GL_VOID_RET _GL_VOID const GLuint const GLclampf *priorities _GL_VOID_RET _GL_VOID GLdouble y _GL_VOID_RET _GL_VOID GLfloat y _GL_VOID_RET _GL_VOID GLint y _GL_VOID_RET _GL_VOID GLshort y _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLfloat GLfloat z _GL_VOID_RET _GL_VOID GLint GLint z _GL_VOID_RET _GL_VOID GLshort GLshort z _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble w _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat w _GL_VOID_RET _GL_VOID GLint GLint GLint w _GL_VOID_RET _GL_VOID GLshort GLshort GLshort w _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble y2 _GL_VOID_RET _GL_VOID GLfloat GLfloat GLfloat y2 _GL_VOID_RET _GL_VOID GLint GLint GLint y2 _GL_VOID_RET _GL_VOID GLshort GLshort GLshort y2 _GL_VOID_RET _GL_VOID GLdouble GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLdouble GLdouble z _GL_VOID_RET _GL_VOID GLuint *buffer _GL_VOID_RET _GL_VOID GLdouble t _GL_VOID_RET _GL_VOID GLfloat t _GL_VOID_RET _GL_VOID GLint t _GL_VOID_RET _GL_VOID GLshort t _GL_VOID_RET _GL_VOID GLdouble t
ATTR_WARN_UNUSED_RESULT const BMVert * v
#define ccl_private
Definition: cuda/compat.h:48
#define ccl_device_inline
Definition: cuda/compat.h:34
#define CCL_NAMESPACE_END
Definition: cuda/compat.h:9
double time
const KernelGlobalsCPU *ccl_restrict KernelGlobals
#define kernel_data_fetch(name, index)
static float verts[][3]
CCL_NAMESPACE_END CCL_NAMESPACE_BEGIN ccl_device_inline float3 transform_point(ccl_private const Transform *t, const float3 a)
ccl_device_inline Transform object_get_transform(KernelGlobals kg, ccl_private const ShaderData *sd)
@ PRIMITIVE_MOTION_TRIANGLE
Definition: kernel/types.h:559
ShaderData
Definition: kernel/types.h:925
@ SD_OBJECT_TRANSFORM_APPLIED
Definition: kernel/types.h:808
CCL_NAMESPACE_BEGIN ccl_device uint lcg_step_uint(T rng)
Definition: lcg.h:11
static float P(float k)
Definition: math_interp.c:25
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, int prim, float time, 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 int isect_object, const int isect_prim, const float u, const float v, float3 verts[3])
vec_base< T, 3 > cross(const vec_base< T, 3 > &a, const vec_base< T, 3 > &b)
vec_base< T, Size > normalize(const vec_base< T, Size > &v)
#define min(a, b)
Definition: sort.c:35