Welcome to mirror list, hosted at ThFree Co, Russian Federation.

motion_triangle.h « geom « kernel « cycles « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 06308071700ab1674359cf0d635707609addeade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/* SPDX-License-Identifier: Apache-2.0
 * Copyright 2011-2022 Blender Foundation */

/* Motion Triangle Primitive
 *
 * These are stored as regular triangles, plus extra positions and normals at
 * times other than the frame center. Computing the triangle vertex positions
 * or normals at a given ray time is a matter of interpolation of the two steps
 * between which the ray time lies.
 *
 * The extra positions and normals are stored as ATTR_STD_MOTION_VERTEX_POSITION
 * and ATTR_STD_MOTION_VERTEX_NORMAL mesh attributes.
 */

#pragma once

#include "kernel/bvh/util.h"

CCL_NAMESPACE_BEGIN

/* Time interpolation of vertex positions and normals */

ccl_device_inline void motion_triangle_verts_for_step(KernelGlobals kg,
                                                      uint4 tri_vindex,
                                                      int offset,
                                                      int numverts,
                                                      int numsteps,
                                                      int step,
                                                      float3 verts[3])
{
  if (step == numsteps) {
    /* center step: regular vertex location */
    verts[0] = kernel_data_fetch(tri_verts, tri_vindex.w + 0);
    verts[1] = kernel_data_fetch(tri_verts, tri_vindex.w + 1);
    verts[2] = kernel_data_fetch(tri_verts, tri_vindex.w + 2);
  }
  else {
    /* center step not store in this array */
    if (step > numsteps)
      step--;

    offset += step * numverts;

    verts[0] = kernel_data_fetch(attributes_float3, offset + tri_vindex.x);
    verts[1] = kernel_data_fetch(attributes_float3, offset + tri_vindex.y);
    verts[2] = kernel_data_fetch(attributes_float3, offset + tri_vindex.z);
  }
}

ccl_device_inline void motion_triangle_normals_for_step(KernelGlobals kg,
                                                        uint4 tri_vindex,
                                                        int offset,
                                                        int numverts,
                                                        int numsteps,
                                                        int step,
                                                        float3 normals[3])
{
  if (step == numsteps) {
    /* center step: regular vertex location */
    normals[0] = kernel_data_fetch(tri_vnormal, tri_vindex.x);
    normals[1] = kernel_data_fetch(tri_vnormal, tri_vindex.y);
    normals[2] = kernel_data_fetch(tri_vnormal, tri_vindex.z);
  }
  else {
    /* center step is not stored in this array */
    if (step > numsteps)
      step--;

    offset += step * numverts;

    normals[0] = kernel_data_fetch(attributes_float3, offset + tri_vindex.x);
    normals[1] = kernel_data_fetch(attributes_float3, offset + tri_vindex.y);
    normals[2] = kernel_data_fetch(attributes_float3, offset + tri_vindex.z);
  }
}

ccl_device_inline void motion_triangle_vertices(
    KernelGlobals kg, int object, int prim, float time, float3 verts[3])
{
  /* get motion info */
  int numsteps, numverts;
  object_motion_info(kg, object, &numsteps, &numverts, NULL);

  /* figure out which steps we need to fetch and their interpolation factor */
  int maxstep = numsteps * 2;
  int step = min((int)(time * maxstep), maxstep - 1);
  float t = time * maxstep - step;

  /* find attribute */
  int offset = intersection_find_attribute(kg, object, ATTR_STD_MOTION_VERTEX_POSITION);
  kernel_assert(offset != ATTR_STD_NOT_FOUND);

  /* fetch vertex coordinates */
  float3 next_verts[3];
  uint4 tri_vindex = kernel_data_fetch(tri_vindex, prim);

  motion_triangle_verts_for_step(kg, tri_vindex, offset, numverts, numsteps, step, verts);
  motion_triangle_verts_for_step(kg, tri_vindex, offset, numverts, numsteps, step + 1, next_verts);

  /* interpolate between steps */
  verts[0] = (1.0f - t) * verts[0] + t * next_verts[0];
  verts[1] = (1.0f - t) * verts[1] + t * next_verts[1];
  verts[2] = (1.0f - t) * verts[2] + t * next_verts[2];
}

ccl_device_inline void motion_triangle_vertices_and_normals(
    KernelGlobals kg, int object, int prim, float time, float3 verts[3], float3 normals[3])
{
  /* get motion info */
  int numsteps, numverts;
  object_motion_info(kg, object, &numsteps, &numverts, NULL);

  /* Figure out which steps we need to fetch and their interpolation factor. */
  int maxstep = numsteps * 2;
  int step = min((int)(time * maxstep), maxstep - 1);
  float t = time * maxstep - step;

  /* Find attribute. */
  int offset = intersection_find_attribute(kg, object, ATTR_STD_MOTION_VERTEX_POSITION);
  kernel_assert(offset != ATTR_STD_NOT_FOUND);

  /* Fetch vertex coordinates. */
  float3 next_verts[3];
  uint4 tri_vindex = kernel_data_fetch(tri_vindex, prim);

  motion_triangle_verts_for_step(kg, tri_vindex, offset, numverts, numsteps, step, verts);
  motion_triangle_verts_for_step(kg, tri_vindex, offset, numverts, numsteps, step + 1, next_verts);

  /* Interpolate between steps. */
  verts[0] = (1.0f - t) * verts[0] + t * next_verts[0];
  verts[1] = (1.0f - t) * verts[1] + t * next_verts[1];
  verts[2] = (1.0f - t) * verts[2] + t * next_verts[2];

  /* Compute smooth normal. */

  /* Find attribute. */
  offset = intersection_find_attribute(kg, object, ATTR_STD_MOTION_VERTEX_NORMAL);
  kernel_assert(offset != ATTR_STD_NOT_FOUND);

  /* Fetch vertex coordinates. */
  float3 next_normals[3];
  motion_triangle_normals_for_step(kg, tri_vindex, offset, numverts, numsteps, step, normals);
  motion_triangle_normals_for_step(
      kg, tri_vindex, offset, numverts, numsteps, step + 1, next_normals);

  /* Interpolate between steps. */
  normals[0] = (1.0f - t) * normals[0] + t * next_normals[0];
  normals[1] = (1.0f - t) * normals[1] + t * next_normals[1];
  normals[2] = (1.0f - t) * normals[2] + t * next_normals[2];
}

ccl_device_inline float3 motion_triangle_smooth_normal(
    KernelGlobals kg, float3 Ng, int object, int prim, float u, float v, float time)
{
  /* get motion info */
  int numsteps, numverts;
  object_motion_info(kg, object, &numsteps, &numverts, NULL);

  /* figure out which steps we need to fetch and their interpolation factor */
  int maxstep = numsteps * 2;
  int step = min((int)(time * maxstep), maxstep - 1);
  float t = time * maxstep - step;

  /* find attribute */
  int offset = intersection_find_attribute(kg, object, ATTR_STD_MOTION_VERTEX_NORMAL);
  kernel_assert(offset != ATTR_STD_NOT_FOUND);

  /* fetch normals */
  float3 normals[3], next_normals[3];
  uint4 tri_vindex = kernel_data_fetch(tri_vindex, prim);

  motion_triangle_normals_for_step(kg, tri_vindex, offset, numverts, numsteps, step, normals);
  motion_triangle_normals_for_step(
      kg, tri_vindex, offset, numverts, numsteps, step + 1, next_normals);

  /* interpolate between steps */
  normals[0] = (1.0f - t) * normals[0] + t * next_normals[0];
  normals[1] = (1.0f - t) * normals[1] + t * next_normals[1];
  normals[2] = (1.0f - t) * normals[2] + t * next_normals[2];

  /* interpolate between vertices */
  float w = 1.0f - u - v;
  float3 N = safe_normalize(u * normals[0] + v * normals[1] + w * normals[2]);

  return is_zero(N) ? Ng : N;
}

CCL_NAMESPACE_END