Blender V5.3
point_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#pragma once
6
7#include "kernel/globals.h"
8#include "kernel/types.h"
9
10#include "kernel/geom/gsplat.h"
12#include "kernel/geom/object.h"
13
15
16/* Point primitive intersection functions. */
17
18#ifdef __POINTCLOUD__
19
20template<bool use_backface_culling = true>
21ccl_device_forceinline bool point_intersect_test(const float4 point,
22 const float3 ray_P,
23 const float3 ray_D,
24 const float ray_tmin,
25 const float ray_tmax,
26 ccl_private float *t)
27{
28 const float3 center = make_float3(point);
29 const float radius = point.w;
30
31 const float rd2 = 1.0f / dot(ray_D, ray_D);
32
33 const float3 c0 = center - ray_P;
34 const float projC0 = dot(c0, ray_D) * rd2;
35 const float3 perp = c0 - projC0 * ray_D;
36 const float l2 = dot(perp, perp);
37 const float r2 = radius * radius;
38 if (!(l2 <= r2)) {
39 return false;
40 }
41
42 const float td = sqrt((r2 - l2) * rd2);
43 const float t_front = projC0 - td;
44 const bool valid_front = (ray_tmin <= t_front) & (t_front <= ray_tmax);
45
46 if constexpr (use_backface_culling == false) {
47 const float t_back = projC0 + td;
48 const bool valid_back = (ray_tmin <= t_back) & (t_back <= ray_tmax);
49
50 /* check if there is a first hit */
51 const bool valid_first = valid_front | valid_back;
52 if (!valid_first) {
53 return false;
54 }
55
56 *t = (valid_front) ? t_front : t_back;
57 return true;
58 }
59
60 if (!valid_front) {
61 return false;
62 }
63 *t = t_front;
64 return true;
65}
66
67ccl_device_forceinline bool point_intersect(KernelGlobals kg,
69 const float3 ray_P,
70 const float3 ray_D,
71 const float ray_tmin,
72 const float ray_tmax,
73 const int object,
74 const int prim,
75 const float time,
76 const int type)
77{
78 const int position_offset = kernel_data_fetch(objects, object).position_offset;
79 const float4 point = (type & PRIMITIVE_MOTION) ?
80 motion_point(kg, object, prim, time) :
81 kernel_data_fetch(points, position_offset + prim);
82
83 if (!point_intersect_test(point, ray_P, ray_D, ray_tmin, ray_tmax, &isect->t)) {
84 return false;
85 }
86
87 isect->prim = prim;
88 isect->object = object;
89 isect->type = type;
90 isect->u = 0.0f;
91 isect->v = 0.0f;
92 return true;
93}
94
95ccl_device_inline void point_shader_setup(KernelGlobals kg,
96 ccl_private ShaderData *sd,
97 const ccl_private Intersection *isect,
98 const ccl_private Ray *ray)
99{
100 sd->shader = kernel_data_fetch(points_shader, isect->prim);
101 sd->P = ray->P + ray->D * isect->t;
102
103 /* Texture coordinates, zero for now. */
104# ifdef __UV__
105 sd->u = isect->u;
106 sd->v = isect->v;
107# endif
108
109 /* Compute point center for normal. */
110 const int position_offset = kernel_data_fetch(objects, sd->object).position_offset;
111 float3 center = make_float3((isect->type & PRIMITIVE_MOTION) ?
112 motion_point(kg, sd->object, sd->prim, sd->time) :
113 kernel_data_fetch(points, position_offset + sd->prim));
114 if (!(sd->object_flag & SD_OBJECT_TRANSFORM_APPLIED)) {
115 object_position_transform(kg, sd, &center);
116 }
117
118 /* Normal */
119# if defined(__GSPLATS__)
120 if (isect->type & PRIMITIVE_POINT)
121# endif
122 {
123 sd->Ng = normalize(sd->P - center);
124 }
125# if defined(__GSPLATS__)
126 else {
127 sd->Ng = gsplat_normal(kg, sd->object, sd->prim, sd->time, sd->type);
128 kernel_assert((kernel_data_fetch(object_flag, sd->object) & SD_OBJECT_TRANSFORM_APPLIED) == 0);
129 object_normal_transform(kg, sd, &sd->Ng);
130 }
131# endif
132
133 sd->N = sd->Ng;
134
135# ifdef __DPDU__
136 /* dPdu/dPdv: arbitrary tangent frame so bump mapping works. */
137 make_orthonormals(sd->Ng, &sd->dPdu, &sd->dPdv);
138# endif
139}
140
141#endif
142
#define kernel_assert(cond)
#define ccl_device_forceinline
#define kernel_data_fetch(name, index)
#define ccl_private
#define ccl_device_inline
#define CCL_NAMESPACE_END
ccl_device_forceinline float3 make_float3(const float x, const float y, const float z)
VecBase< float, D > normalize(VecOp< float, D >) RET
float dot(VecOp< float, D >, VecOp< float, D >) RET
#define sqrt
ccl_device_inline void object_normal_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *N)
ccl_device_inline void object_position_transform(KernelGlobals kg, const ccl_private ShaderData *sd, ccl_private T *P)
@ PRIMITIVE_MOTION
@ PRIMITIVE_POINT
@ SD_OBJECT_TRANSFORM_APPLIED
ccl_device_inline void make_orthonormals(const float3 N, ccl_private float3 *a, ccl_private float3 *b)
float w
Definition sky_math.h:225