Blender V5.3
motion_triangle.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 or ATTR_STD_CORNER_NORMAL.
14 */
15
16#pragma once
17
18#include "kernel/bvh/util.h"
19
22
24
25/* Time interpolation of vertex positions and normals */
26
28 const uint3 tri_vindex,
29 int offset,
30 const int numverts,
31 const int numsteps,
32 int step,
33 float3 verts[3])
34{
35 const int center_step = (numsteps - 1) / 2;
36 if (step == center_step) {
37 /* Center step: first in the array. */
38 }
39 else {
40 /* Non-center step, stored after center with center index skipped. */
41 if (step < center_step) {
42 step++;
43 }
44 offset += step * numverts;
45 }
46
47 verts[0] = kernel_data_fetch(tri_verts, offset + tri_vindex.x);
48 verts[1] = kernel_data_fetch(tri_verts, offset + tri_vindex.y);
49 verts[2] = kernel_data_fetch(tri_verts, offset + tri_vindex.z);
50}
51
53 const int object,
54 const int object_flag,
55 const int prim,
56 const uint3 tri_vindex,
57 int offset,
58 const int numsteps,
59 int step,
60 float3 normals[3])
61{
62 const int center_step = (numsteps - 1) / 2;
63 if (step == center_step) {
64 /* Center step: first in the array. */
65 }
66 else {
67 /* Non-center step: stored after center with center index skipped. */
68 int stride;
69 if (object_flag & SD_OBJECT_HAS_CORNER_NORMALS) {
70 stride = kernel_data_fetch(objects, object).mesh_volume.num_prims * 3;
71 }
72 else {
73 stride = kernel_data_fetch(objects, object).numverts;
74 }
75 if (step < center_step) {
76 step++;
77 }
78 offset += step * stride;
79 }
80
81 int i0, i1, i2;
82 if (object_flag & SD_OBJECT_HAS_CORNER_NORMALS) {
83 i0 = prim * 3 + 0;
84 i1 = prim * 3 + 1;
85 i2 = prim * 3 + 2;
86 }
87 else {
88 i0 = tri_vindex.x;
89 i1 = tri_vindex.y;
90 i2 = tri_vindex.z;
91 }
92
93 attribute_data_fetch_normals(kg, offset, i0, i1, i2, normals);
94}
95
97 const int object,
98 const float time,
99 const int prim,
100 ccl_private uint3 *tri_vindex,
101 ccl_private int *numsteps,
102 ccl_private int *step,
103 ccl_private float *t)
104{
105 /* Get object motion info. */
106 *numsteps = kernel_data_fetch(objects, object).num_geom_steps;
107
108 /* Figure out which steps we need to fetch and their interpolation factor. */
109 const int maxstep = *numsteps - 1;
110 *step = min((int)(time * maxstep), maxstep - 1);
111 *t = time * maxstep - *step;
112
113 /* Get triangle indices. */
114 *tri_vindex = kernel_data_fetch(tri_vindex, prim);
115}
116
118 const int object,
119 const uint3 tri_vindex,
120 const int numsteps,
121 const int numverts,
122 const int step,
123 const float t,
124 float3 verts[3])
125{
126 /* Fetch vertex coordinates. */
127 const int offset = kernel_data_fetch(objects, object).position_offset;
128 float3 next_verts[3];
129 motion_triangle_verts_for_step(kg, tri_vindex, offset, numverts, numsteps, step, verts);
130 motion_triangle_verts_for_step(kg, tri_vindex, offset, numverts, numsteps, step + 1, next_verts);
131
132 /* Interpolate between steps. */
133 verts[0] = (1.0f - t) * verts[0] + t * next_verts[0];
134 verts[1] = (1.0f - t) * verts[1] + t * next_verts[1];
135 verts[2] = (1.0f - t) * verts[2] + t * next_verts[2];
136}
137
139 KernelGlobals kg, const int object, const int prim, const float time, float3 verts[3])
140{
141 int numsteps;
142 int step;
143 float t;
144 uint3 tri_vindex;
145 motion_triangle_compute_info(kg, object, time, prim, &tri_vindex, &numsteps, &step, &t);
146
147 const int numverts = kernel_data_fetch(objects, object).numverts;
148 motion_triangle_vertices(kg, object, tri_vindex, numsteps, numverts, step, t, verts);
149}
150
152 const int object,
153 const int prim,
154 const uint3 tri_vindex,
155 const int numsteps,
156 const int step,
157 const float t,
158 float3 normals[3])
159{
160 /* Fetch normals. */
161 const int object_flag = kernel_data_fetch(object_flag, object);
162 const int offset = kernel_data_fetch(objects, object).mesh_volume.normal_offset;
163 float3 next_normals[3];
165 kg, object, object_flag, prim, tri_vindex, offset, numsteps, step, normals);
167 kg, object, object_flag, prim, tri_vindex, offset, numsteps, step + 1, next_normals);
168
169 /* Interpolate between steps. */
170 normals[0] = normalize((1.0f - t) * normals[0] + t * next_normals[0]);
171 normals[1] = normalize((1.0f - t) * normals[1] + t * next_normals[1]);
172 normals[2] = normalize((1.0f - t) * normals[2] + t * next_normals[2]);
173}
174
176 const ccl_private ShaderData *sd,
177 float3 verts[3],
178 float3 normals[3])
179{
180 const int object = sd->object;
181 int numsteps, step;
182 float t;
183 uint3 tri_vindex;
184 motion_triangle_compute_info(kg, object, sd->time, sd->prim, &tri_vindex, &numsteps, &step, &t);
185
186 const int numverts = kernel_data_fetch(objects, object).numverts;
187 motion_triangle_vertices(kg, object, tri_vindex, numsteps, numverts, step, t, verts);
188 motion_triangle_normals(kg, object, sd->prim, tri_vindex, numsteps, step, t, normals);
189}
190
192 const float3 Ng,
193 const int object,
194 const int prim,
195 const uint3 tri_vindex,
196 const int numsteps,
197 const int step,
198 const float t,
199 const float u,
200 const float v)
201{
202 float3 normals[3];
203 motion_triangle_normals(kg, object, prim, tri_vindex, numsteps, step, t, normals);
204
205 /* Interpolate between normals. */
206 const float w = 1.0f - u - v;
207 const float3 N = safe_normalize(w * normals[0] + u * normals[1] + v * normals[2]);
208
209 return is_zero(N) ? Ng : N;
210}
211
213 const float3 Ng,
214 const int object,
215 const int prim,
216 const float u,
217 float v,
218 const float time)
219{
220 int numsteps;
221 int step;
222 float t;
223 uint3 tri_vindex;
224 motion_triangle_compute_info(kg, object, time, prim, &tri_vindex, &numsteps, &step, &t);
225
226 return motion_triangle_smooth_normal(kg, Ng, object, prim, tri_vindex, numsteps, step, t, u, v);
227}
228
229/* Compute motion triangle normals at the hit position, and offsetted positions in x and y
230 * direction for bump mapping. */
232 const float3 Ng,
233 const int object,
234 const int prim,
235 const float time,
236 const float u,
237 const float v,
238 const differential du,
239 const differential dv,
240 ccl_private float3 &N_x,
241 ccl_private float3 &N_y)
242{
243 int numsteps, step;
244 float t;
245 uint3 tri_vindex;
246 motion_triangle_compute_info(kg, object, time, prim, &tri_vindex, &numsteps, &step, &t);
247
248 float3 n[3];
249 motion_triangle_normals(kg, object, prim, tri_vindex, numsteps, step, t, n);
250
251 const float3 N = safe_normalize(triangle_interpolate(u, v, n[0], n[1], n[2]));
252 N_x = safe_normalize(triangle_interpolate(u + du.dx, v + dv.dx, n[0], n[1], n[2]));
253 N_y = safe_normalize(triangle_interpolate(u + du.dy, v + dv.dy, n[0], n[1], n[2]));
254
255 N_x = is_zero(N_x) ? Ng : N_x;
256 N_y = is_zero(N_y) ? Ng : N_y;
257 return is_zero(N) ? Ng : N;
258}
259
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
CCL_NAMESPACE_BEGIN ccl_device_inline T triangle_interpolate(const float u, const float v, const T f0, const T f1, const T f2)
VecBase< float, D > normalize(VecOp< float, D >) RET
VecBase< float, D > step(VecOp< float, D >, VecOp< float, D >) RET
bool is_zero(float3x3 a)
VecT safe_normalize(VecT vector)
ccl_device_inline void attribute_data_fetch_normals(KernelGlobals kg, const int offset, const int i0, const int i1, const int i2, ccl_private float3 N[3])
@ SD_OBJECT_HAS_CORNER_NORMALS
#define N
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_device_inline float3 motion_triangle_smooth_normal(KernelGlobals kg, const float3 Ng, const int object, const int prim, const uint3 tri_vindex, const int numsteps, const int step, const float t, const float u, const float v)
ccl_device_inline void motion_triangle_vertices_and_normals(KernelGlobals kg, const ccl_private ShaderData *sd, float3 verts[3], float3 normals[3])
ccl_device_inline void motion_triangle_compute_info(KernelGlobals kg, const int object, const float time, const int prim, ccl_private uint3 *tri_vindex, ccl_private int *numsteps, ccl_private int *step, ccl_private float *t)
ccl_device_inline void motion_triangle_normals(KernelGlobals kg, const int object, const int prim, const uint3 tri_vindex, const int numsteps, const int step, const float t, float3 normals[3])
CCL_NAMESPACE_BEGIN ccl_device_inline void motion_triangle_verts_for_step(KernelGlobals kg, const uint3 tri_vindex, int offset, const int numverts, const int numsteps, int step, float3 verts[3])
ccl_device_inline void motion_triangle_normals_for_step(KernelGlobals kg, const int object, const int object_flag, const int prim, const uint3 tri_vindex, int offset, const int numsteps, int step, float3 normals[3])
#define min(a, b)
Definition sort.cc:38
uint y
Definition types_uint3.h:13
uint z
Definition types_uint3.h:13
uint x
Definition types_uint3.h:13