2
* Parts adapted from Open Shading Language with this license:
4
* Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
7
* Modifications Copyright 2011, Blender Foundation.
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions are
12
* * Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
14
* * Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in the
16
* documentation and/or other materials provided with the distribution.
17
* * Neither the name of Sony Pictures Imageworks nor the names of its
18
* contributors may be used to endorse or promote products derived from
19
* this software without specific prior written permission.
20
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
#ifndef __KERNEL_PROJECTION_CL__
34
#define __KERNEL_PROJECTION_CL__
38
/* Spherical coordinates <-> Cartesian direction */
40
__device float2 direction_to_spherical(float3 dir)
42
float theta = acosf(dir.z);
43
float phi = atan2f(dir.x, dir.y);
45
return make_float2(theta, phi);
48
__device float3 spherical_to_direction(float theta, float phi)
51
sinf(theta)*cosf(phi),
52
sinf(theta)*sinf(phi),
56
/* Equirectangular coordinates <-> Cartesian direction */
58
__device float2 direction_to_equirectangular(float3 dir)
60
float u = -atan2f(dir.y, dir.x)/(2.0f*M_PI_F) + 0.5f;
61
float v = atan2f(dir.z, hypotf(dir.x, dir.y))/M_PI_F + 0.5f;
63
return make_float2(u, v);
66
__device float3 equirectangular_to_direction(float u, float v)
68
float phi = M_PI_F*(1.0f - 2.0f*u);
69
float theta = M_PI_F*(1.0f - v);
72
sinf(theta)*cosf(phi),
73
sinf(theta)*sinf(phi),
77
/* Fisheye <-> Cartesian direction */
79
__device float2 direction_to_fisheye(float3 dir, float fov)
81
float r = atan2f(sqrtf(dir.y*dir.y + dir.z*dir.z), dir.x) / fov;
82
float phi = atan2f(dir.z, dir.y);
84
float u = r * cosf(phi) + 0.5f;
85
float v = r * sinf(phi) + 0.5f;
87
return make_float2(u, v);
90
__device float3 fisheye_to_direction(float u, float v, float fov)
92
u = (u - 0.5f) * 2.0f;
93
v = (v - 0.5f) * 2.0f;
95
float r = sqrtf(u*u + v*v);
98
return make_float3(0.0f, 0.0f, 0.0f);
100
float phi = acosf((r != 0.0f)? u/r: 0.0f);
101
float theta = r * fov * 0.5f;
103
if(v < 0.0f) phi = -phi;
107
-cosf(phi)*sinf(theta),
108
sinf(phi)*sinf(theta)
112
__device float2 direction_to_fisheye_equisolid(float3 dir, float lens, float width, float height)
114
float theta = acosf(dir.x);
115
float r = 2.0f * lens * sinf(theta * 0.5f);
116
float phi = atan2f(dir.z, dir.y);
118
float u = r * cosf(phi) / width + 0.5f;
119
float v = r * sinf(phi) / height + 0.5f;
121
return make_float2(u, v);
124
__device float3 fisheye_equisolid_to_direction(float u, float v, float lens, float fov, float width, float height)
126
u = (u - 0.5f) * width;
127
v = (v - 0.5f) * height;
129
float rmax = 2.0f * lens * sinf(fov * 0.25f);
130
float r = sqrtf(u*u + v*v);
133
return make_float3(0.0f, 0.0f, 0.0f);
135
float phi = acosf((r != 0.0f)? u/r: 0.0f);
136
float theta = 2.0f * asinf(r/(2.0f * lens));
138
if(v < 0.0f) phi = -phi;
142
-cosf(phi)*sinf(theta),
143
sinf(phi)*sinf(theta)
147
/* Mirror Ball <-> Cartesion direction */
149
__device float3 mirrorball_to_direction(float u, float v)
151
/* point on sphere */
154
dir.x = 2.0f*u - 1.0f;
155
dir.z = 2.0f*v - 1.0f;
156
dir.y = -sqrtf(max(1.0f - dir.x*dir.x - dir.z*dir.z, 0.0f));
159
float3 I = make_float3(0.0f, -1.0f, 0.0f);
161
return 2.0f*dot(dir, I)*dir - I;
164
__device float2 direction_to_mirrorball(float3 dir)
166
/* inverse of mirrorball_to_direction */
169
float div = 2.0f*sqrtf(max(-0.5f*dir.y, 0.0f));
173
float u = 0.5f*(dir.x + 1.0f);
174
float v = 0.5f*(dir.z + 1.0f);
176
return make_float2(u, v);
179
__device float3 panorama_to_direction(KernelGlobals *kg, float u, float v)
181
switch(kernel_data.cam.panorama_type) {
182
case PANORAMA_EQUIRECTANGULAR:
183
return equirectangular_to_direction(u, v);
184
case PANORAMA_FISHEYE_EQUIDISTANT:
185
return fisheye_to_direction(u, v, kernel_data.cam.fisheye_fov);
186
case PANORAMA_FISHEYE_EQUISOLID:
188
return fisheye_equisolid_to_direction(u, v, kernel_data.cam.fisheye_lens,
189
kernel_data.cam.fisheye_fov, kernel_data.cam.sensorwidth, kernel_data.cam.sensorheight);
193
__device float2 direction_to_panorama(KernelGlobals *kg, float3 dir)
195
switch(kernel_data.cam.panorama_type) {
196
case PANORAMA_EQUIRECTANGULAR:
197
return direction_to_equirectangular(dir);
198
case PANORAMA_FISHEYE_EQUIDISTANT:
199
return direction_to_fisheye(dir, kernel_data.cam.fisheye_fov);
200
case PANORAMA_FISHEYE_EQUISOLID:
202
return direction_to_fisheye_equisolid(dir, kernel_data.cam.fisheye_lens,
203
kernel_data.cam.sensorwidth, kernel_data.cam.sensorheight);
209
#endif /* __KERNEL_PROJECTION_CL__ */