Blender V5.3
cycles/util/projection.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 "util/transform.h"
8
10
12{
13 const float theta = safe_acosf(dir.z);
14 const float phi = atan2f(dir.y, dir.x);
15
16 return make_float2(theta, phi);
17}
18
19ccl_device float3 spherical_to_direction(const float theta, const float phi)
20{
21 return make_float3(sinf(theta) * cosf(phi), sinf(theta) * sinf(phi), cosf(theta));
22}
23
25{
26 const float sin_theta = sin_from_cos(cos_theta);
27 return make_float3(sin_theta * cosf(phi), sin_theta * sinf(phi), cos_theta);
28}
29
30ccl_device_inline float2 polar_to_cartesian(const float r, const float phi)
31{
32 return make_float2(r * cosf(phi), r * sinf(phi));
33}
34
39
40/* 4x4 projection matrix, perspective or orthographic. */
41
43 float4 x, y, z, w; /* rows */
44
45#ifndef __KERNEL_GPU__
47
48 explicit ProjectionTransform(const Transform &tfm)
49 : x(tfm.x), y(tfm.y), z(tfm.z), w(make_float4(0.0f, 0.0f, 0.0f, 1.0f))
50 {
51 }
52#endif
53};
54
59
61
62#if !defined(__KERNEL_METAL__)
64#endif
65
67
68/* Functions */
69
71 const float3 a)
72{
73 const float4 b = make_float4(a.x, a.y, a.z, 1.0f);
74 const float3 c = make_float3(dot(t->x, b), dot(t->y, b), dot(t->z, b));
75 const float w = dot(t->w, b);
76
77 return (w != 0.0f) ? c / w : zero_float3();
78}
79
81 const float3 a)
82{
83 const float3 c = make_float3(a.x * t->x.x + a.y * t->x.y + a.z * t->x.z,
84 a.x * t->y.x + a.y * t->y.y + a.z * t->y.z,
85 a.x * t->z.x + a.y * t->z.y + a.z * t->z.z);
86
87 return c;
88}
89
90/* Applies transform t to point a with given derivatives `da/dx` and `da/dy`.
91 * Returns t(a) and sets `out_dx/dy` to the values of `dt(a)/dx` and `dt(a)/dy`, respectively. */
93 const float3 a,
94 const float3 dx,
95 const float3 dy,
96 ccl_private float3 &out_dx,
97 ccl_private float3 &out_dy)
98{
99 const float4 b = make_float4(a.x, a.y, a.z, 1.0f);
100 const float3 c = make_float3(dot(t->x, b), dot(t->y, b), dot(t->z, b));
101 const float w = dot(t->w, b);
102
103 if (w != 0.0f) {
104 out_dx = (transform_perspective_direction(t, dx) - dot(make_float3(t->w), dx) * c) / w;
105 out_dy = (transform_perspective_direction(t, dy) - dot(make_float3(t->w), dy) * c) / w;
106 return c / w;
107 }
108 else {
109 out_dx = zero_float3();
110 out_dy = zero_float3();
111 return zero_float3();
112 }
113}
114
116 const float b,
117 const float c,
118 const float d,
119 const float e,
120 const float f,
121 const float g,
122 const float h,
123 const float i,
124 const float j,
125 const float k,
126 const float l,
127 const float m,
128 const float n,
129 const float o,
130 const float p)
131{
133
134 t.x.x = a;
135 t.x.y = b;
136 t.x.z = c;
137 t.x.w = d;
138 t.y.x = e;
139 t.y.y = f;
140 t.y.z = g;
141 t.y.w = h;
142 t.z.x = i;
143 t.z.y = j;
144 t.z.z = k;
145 t.z.w = l;
146 t.w.x = m;
147 t.w.y = n;
148 t.w.z = o;
149 t.w.w = p;
150
151 return t;
152}
153
155{
156 return make_projection(1.0f,
157 0.0f,
158 0.0f,
159 0.0f,
160 0.0f,
161 1.0f,
162 0.0f,
163 0.0f,
164 0.0f,
165 0.0f,
166 1.0f,
167 0.0f,
168 0.0f,
169 0.0f,
170 0.0f,
171 1.0f);
172}
173
174#ifndef __KERNEL_GPU__
176{
177 Transform tfm = {a.x, a.y, a.z};
178 return tfm;
179}
180#endif
181
183{
185
186 t.x.x = a.x.x;
187 t.x.y = a.y.x;
188 t.x.z = a.z.x;
189 t.x.w = a.w.x;
190 t.y.x = a.x.y;
191 t.y.y = a.y.y;
192 t.y.z = a.z.y;
193 t.y.w = a.w.y;
194 t.z.x = a.x.z;
195 t.z.y = a.y.z;
196 t.z.z = a.z.z;
197 t.z.w = a.w.z;
198 t.w.x = a.x.w;
199 t.w.y = a.y.w;
200 t.w.z = a.z.w;
201 t.w.w = a.w.w;
202
203 return t;
204}
205
206#if !defined(__KERNEL_METAL__)
208{
210 float M[4][4];
211 float R[4][4];
212
213 memcpy(R, (const float *)&tfmR, sizeof(R));
214 memcpy(M, (const float *)&tfm, sizeof(M));
215
217 return projection_identity();
218 }
219
220 memcpy((void *)&tfmR, R, sizeof(R));
221
222 return tfmR;
223}
224
227{
230
231 t.x = make_float4(dot(a.x, c.x), dot(a.x, c.y), dot(a.x, c.z), dot(a.x, c.w));
232 t.y = make_float4(dot(a.y, c.x), dot(a.y, c.y), dot(a.y, c.z), dot(a.y, c.w));
233 t.z = make_float4(dot(a.z, c.x), dot(a.z, c.y), dot(a.z, c.z), dot(a.z, c.w));
234 t.w = make_float4(dot(a.w, c.x), dot(a.w, c.y), dot(a.w, c.z), dot(a.w, c.w));
235
236 return t;
237}
238#endif
239
240#ifndef __KERNEL_GPU__
241
246
251
253{
254 print_float4(label, t.x);
255 print_float4(label, t.y);
256 print_float4(label, t.z);
257 print_float4(label, t.w);
258 printf("\n");
259}
260
262 const float n,
263 float f)
264{
266 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, f / (f - n), -f * n / (f - n), 0, 0, 1, 0);
267
268 const float inv_angle = 1.0f / tanf(0.5f * fov);
269
270 const Transform scale = transform_scale(inv_angle, inv_angle, 1);
271
272 return scale * persp;
273}
274
276{
277 const Transform t = transform_scale(1.0f, 1.0f, 1.0f / (zfar - znear));
278
279 return ProjectionTransform(t);
280}
281
282#endif /* __KERNEL_GPU__ */
283
#define UNLIKELY(x)
ccl_device_inline float cos_theta(const float3 w)
ccl_device_inline float sin_theta(const float3 w)
SIMD_FORCE_INLINE const btScalar & w() const
Return the w value.
Definition btQuadWord.h:119
#define printf
ccl_device_inline float3 transform_perspective_direction(const ccl_private ProjectionTransform *t, const float3 a)
CCL_NAMESPACE_END CCL_NAMESPACE_BEGIN ccl_device_inline float3 transform_perspective(const ccl_private ProjectionTransform *t, const float3 a)
ccl_device float3 spherical_cos_to_direction(const float cos_theta, const float phi)
CCL_NAMESPACE_BEGIN ccl_device float2 direction_to_spherical(const float3 dir)
ccl_device_inline float3 disk_to_hemisphere(const float2 p)
ccl_device_inline float3 transform_perspective_deriv(const ccl_private ProjectionTransform *t, const float3 a, const float3 dx, const float3 dy, ccl_private float3 &out_dx, ccl_private float3 &out_dy)
ccl_device_inline Transform projection_to_transform(const ProjectionTransform &a)
ccl_device_inline ProjectionTransform projection_identity()
ccl_device float3 spherical_to_direction(const float theta, const float phi)
ccl_device_inline ProjectionTransform projection_transpose(const ProjectionTransform a)
ccl_device_inline ProjectionTransform make_projection(const float a, const float b, const float c, const float d, const float e, const float f, const float g, const float h, const float i, const float j, const float k, const float l, const float m, const float n, const float o, const float p)
ccl_device_inline ProjectionTransform projection_inverse(const ProjectionTransform tfm)
ccl_device_inline void print_projection(const char *label, const ProjectionTransform &t)
ccl_device_inline float2 polar_to_cartesian(const float r, const float phi)
#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)
float dot(VecOp< float, D >, VecOp< float, D >) RET
float sin_from_cos(float c)
float3x3 scale(float3x3 mat, float2 scale)
float4x4 projection_orthographic(float left, float right, float bottom, float top, float near_clip, float far_clip)
Create an orthographic projection matrix using OpenGL coordinate convention: Maps each axis range to ...
float4x4 projection_perspective(float left, float right, float bottom, float top, float near_clip, float far_clip)
Create a perspective projection matrix using OpenGL coordinate convention: Maps each axis range to [-...
ccl_device_inline Extrema< T > operator*(const Extrema< T > a, const T b)
Definition math_base.h:967
ccl_device_inline float safe_sqrtf(const float f)
Definition math_base.h:571
ccl_device float safe_acosf(const float a)
Definition math_base.h:590
ccl_device_inline dual1 len_squared(const ccl_private dual< T > &a)
Definition math_dual.h:273
CCL_NAMESPACE_BEGIN ccl_device_inline float3 zero_float3()
Definition math_float3.h:17
#define M
#define R
CCL_NAMESPACE_BEGIN ccl_device_forceinline bool projection_inverse_impl(ccl_private float R[4][4], ccl_private float M[4][4])
#define ccl_device
#define sinf
#define make_float2
#define tanf
#define make_float4
#define cosf
#define atan2f
ProjectionTransform(const Transform &tfm)
ProjectionTransform()=default
float x
float y
float z
Definition sky_math.h:136
float y
Definition sky_math.h:136
float x
Definition sky_math.h:136
float y
Definition sky_math.h:225
float z
Definition sky_math.h:225
float x
Definition sky_math.h:225
float w
Definition sky_math.h:225
ccl_device_inline Transform transform_scale(const float3 s)
Definition transform.h:313
ccl_device_inline void print_float4(const ccl_private char *label, const float4 a)