Cycles: don't count volume boundaries as transparent bounces.
[blender-staging.git] / intern / cycles / kernel / kernel_projection.h
1 /*
2  * Parts adapted from Open Shading Language with this license:
3  *
4  * Copyright (c) 2009-2010 Sony Pictures Imageworks Inc., et al.
5  * All Rights Reserved.
6  *
7  * Modifications Copyright 2011, Blender Foundation.
8  * 
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are
11  * met:
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.
31  */
32
33 #ifndef __KERNEL_PROJECTION_CL__
34 #define __KERNEL_PROJECTION_CL__
35
36 CCL_NAMESPACE_BEGIN
37
38 /* Spherical coordinates <-> Cartesian direction  */
39
40 ccl_device float2 direction_to_spherical(float3 dir)
41 {
42         float theta = safe_acosf(dir.z);
43         float phi = atan2f(dir.x, dir.y);
44
45         return make_float2(theta, phi);
46 }
47
48 ccl_device float3 spherical_to_direction(float theta, float phi)
49 {
50         float sin_theta = sinf(theta);
51         return make_float3(sin_theta*cosf(phi),
52                            sin_theta*sinf(phi),
53                            cosf(theta));
54 }
55
56 /* Equirectangular coordinates <-> Cartesian direction */
57
58 ccl_device float2 direction_to_equirectangular_range(float3 dir, float4 range)
59 {
60         if(is_zero(dir))
61                 return make_float2(0.0f, 0.0f);
62
63         float u = (atan2f(dir.y, dir.x) - range.y) / range.x;
64         float v = (acosf(dir.z / len(dir)) - range.w) / range.z;
65
66         return make_float2(u, v);
67 }
68
69 ccl_device float3 equirectangular_range_to_direction(float u, float v, float4 range)
70 {
71         float phi = range.x*u + range.y;
72         float theta = range.z*v + range.w;
73         float sin_theta = sinf(theta);
74         return make_float3(sin_theta*cosf(phi),
75                            sin_theta*sinf(phi),
76                            cosf(theta));
77 }
78
79 ccl_device float2 direction_to_equirectangular(float3 dir)
80 {
81         return direction_to_equirectangular_range(dir, make_float4(-M_2PI_F, M_PI_F, -M_PI_F, M_PI_F));
82 }
83
84 ccl_device float3 equirectangular_to_direction(float u, float v)
85 {
86         return equirectangular_range_to_direction(u, v, make_float4(-M_2PI_F, M_PI_F, -M_PI_F, M_PI_F));
87 }
88
89 /* Fisheye <-> Cartesian direction */
90
91 ccl_device float2 direction_to_fisheye(float3 dir, float fov)
92 {
93         float r = atan2f(sqrtf(dir.y*dir.y +  dir.z*dir.z), dir.x) / fov;
94         float phi = atan2f(dir.z, dir.y);
95
96         float u = r * cosf(phi) + 0.5f;
97         float v = r * sinf(phi) + 0.5f;
98
99         return make_float2(u, v);
100 }
101
102 ccl_device float3 fisheye_to_direction(float u, float v, float fov)
103 {
104         u = (u - 0.5f) * 2.0f;
105         v = (v - 0.5f) * 2.0f;
106
107         float r = sqrtf(u*u + v*v);
108
109         if(r > 1.0f)
110                 return make_float3(0.0f, 0.0f, 0.0f);
111
112         float phi = safe_acosf((r != 0.0f)? u/r: 0.0f);
113         float theta = r * fov * 0.5f;
114
115         if(v < 0.0f) phi = -phi;
116
117         return make_float3(
118                  cosf(theta),
119                  -cosf(phi)*sinf(theta),
120                  sinf(phi)*sinf(theta)
121         );
122 }
123
124 ccl_device float2 direction_to_fisheye_equisolid(float3 dir, float lens, float width, float height)
125 {
126         float theta = safe_acosf(dir.x);
127         float r = 2.0f * lens * sinf(theta * 0.5f);
128         float phi = atan2f(dir.z, dir.y);
129
130         float u = r * cosf(phi) / width + 0.5f;
131         float v = r * sinf(phi) / height + 0.5f;
132
133         return make_float2(u, v);
134 }
135
136 ccl_device_inline float3 fisheye_equisolid_to_direction(float u, float v,
137                                                         float lens,
138                                                         float fov,
139                                                         float width, float height)
140 {
141         u = (u - 0.5f) * width;
142         v = (v - 0.5f) * height;
143
144         float rmax = 2.0f * lens * sinf(fov * 0.25f);
145         float r = sqrtf(u*u + v*v);
146
147         if(r > rmax)
148                 return make_float3(0.0f, 0.0f, 0.0f);
149
150         float phi = safe_acosf((r != 0.0f)? u/r: 0.0f);
151         float theta = 2.0f * asinf(r/(2.0f * lens));
152
153         if(v < 0.0f) phi = -phi;
154
155         return make_float3(
156                  cosf(theta),
157                  -cosf(phi)*sinf(theta),
158                  sinf(phi)*sinf(theta)
159         );
160 }
161
162 /* Mirror Ball <-> Cartesion direction */
163
164 ccl_device float3 mirrorball_to_direction(float u, float v)
165 {
166         /* point on sphere */
167         float3 dir;
168
169         dir.x = 2.0f*u - 1.0f;
170         dir.z = 2.0f*v - 1.0f;
171
172         if(dir.x*dir.x + dir.z*dir.z > 1.0f)
173                 return make_float3(0.0f, 0.0f, 0.0f);
174
175         dir.y = -sqrtf(max(1.0f - dir.x*dir.x - dir.z*dir.z, 0.0f));
176
177         /* reflection */
178         float3 I = make_float3(0.0f, -1.0f, 0.0f);
179
180         return 2.0f*dot(dir, I)*dir - I;
181 }
182
183 ccl_device float2 direction_to_mirrorball(float3 dir)
184 {
185         /* inverse of mirrorball_to_direction */
186         dir.y -= 1.0f;
187
188         float div = 2.0f*sqrtf(max(-0.5f*dir.y, 0.0f));
189         if(div > 0.0f)
190                 dir /= div;
191
192         float u = 0.5f*(dir.x + 1.0f);
193         float v = 0.5f*(dir.z + 1.0f);
194
195         return make_float2(u, v);
196 }
197
198 ccl_device_inline float3 panorama_to_direction(ccl_constant KernelCamera *cam, float u, float v)
199 {
200         switch(cam->panorama_type) {
201                 case PANORAMA_EQUIRECTANGULAR:
202                         return equirectangular_range_to_direction(u, v, cam->equirectangular_range);
203                 case PANORAMA_MIRRORBALL:
204                         return mirrorball_to_direction(u, v);
205                 case PANORAMA_FISHEYE_EQUIDISTANT:
206                         return fisheye_to_direction(u, v, cam->fisheye_fov);
207                 case PANORAMA_FISHEYE_EQUISOLID:
208                 default:
209                         return fisheye_equisolid_to_direction(u, v, cam->fisheye_lens,
210                                 cam->fisheye_fov, cam->sensorwidth, cam->sensorheight);
211         }
212 }
213
214 ccl_device_inline float2 direction_to_panorama(ccl_constant KernelCamera *cam, float3 dir)
215 {
216         switch(cam->panorama_type) {
217                 case PANORAMA_EQUIRECTANGULAR:
218                         return direction_to_equirectangular_range(dir, cam->equirectangular_range);
219                 case PANORAMA_MIRRORBALL:
220                         return direction_to_mirrorball(dir);
221                 case PANORAMA_FISHEYE_EQUIDISTANT:
222                         return direction_to_fisheye(dir, cam->fisheye_fov);
223                 case PANORAMA_FISHEYE_EQUISOLID:
224                 default:
225                         return direction_to_fisheye_equisolid(dir, cam->fisheye_lens,
226                                 cam->sensorwidth, cam->sensorheight);
227         }
228 }
229
230 ccl_device_inline void spherical_stereo_transform(ccl_constant KernelCamera *cam, float3 *P, float3 *D)
231 {
232         float interocular_offset = cam->interocular_offset;
233
234         /* Interocular offset of zero means either non stereo, or stereo without
235          * spherical stereo. */
236         kernel_assert(interocular_offset != 0.0f);
237
238         if(cam->pole_merge_angle_to > 0.0f) {
239                 const float pole_merge_angle_from = cam->pole_merge_angle_from,
240                             pole_merge_angle_to = cam->pole_merge_angle_to;
241                 float altitude = fabsf(safe_asinf((*D).z));
242                 if(altitude > pole_merge_angle_to) {
243                         interocular_offset = 0.0f;
244                 }
245                 else if(altitude > pole_merge_angle_from) {
246                         float fac = (altitude - pole_merge_angle_from) / (pole_merge_angle_to - pole_merge_angle_from);
247                         float fade = cosf(fac * M_PI_2_F);
248                         interocular_offset *= fade;
249                 }
250         }
251
252         float3 up = make_float3(0.0f, 0.0f, 1.0f);
253         float3 side = normalize(cross(*D, up));
254         float3 stereo_offset = side * interocular_offset;
255
256         *P += stereo_offset;
257
258         /* Convergence distance is FLT_MAX in the case of parallel convergence mode,
259          * no need to modify direction in this case either. */
260         const float convergence_distance = cam->convergence_distance;
261
262         if(convergence_distance != FLT_MAX)
263         {
264                 float3 screen_offset = convergence_distance * (*D);
265                 *D = normalize(screen_offset - stereo_offset);
266         }
267 }
268
269 CCL_NAMESPACE_END
270
271 #endif  /* __KERNEL_PROJECTION_CL__ */