~ubuntu-branches/ubuntu/utopic/blender/utopic-proposed

1.3.11 by Matteo F. Vescovi
Import upstream version 2.65a+svn53743
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
__device float2 direction_to_spherical(float3 dir)
41
{
42
	float theta = acosf(dir.z);
43
	float phi = atan2f(dir.x, dir.y);
44
45
	return make_float2(theta, phi);
46
}
47
48
__device float3 spherical_to_direction(float theta, float phi)
49
{
50
	return make_float3(
51
		sinf(theta)*cosf(phi),
52
		sinf(theta)*sinf(phi),
53
		cosf(theta));
54
}
55
56
/* Equirectangular coordinates <-> Cartesian direction */
57
58
__device float2 direction_to_equirectangular(float3 dir)
59
{
1.5.3 by Matteo F. Vescovi
Import upstream version 2.68a
60
	float u = -atan2f(dir.y, dir.x)/(M_2PI_F) + 0.5f;
1.3.11 by Matteo F. Vescovi
Import upstream version 2.65a+svn53743
61
	float v = atan2f(dir.z, hypotf(dir.x, dir.y))/M_PI_F + 0.5f;
62
63
	return make_float2(u, v);
64
}
65
66
__device float3 equirectangular_to_direction(float u, float v)
67
{
68
	float phi = M_PI_F*(1.0f - 2.0f*u);
69
	float theta = M_PI_F*(1.0f - v);
70
71
	return make_float3(
72
		sinf(theta)*cosf(phi),
73
		sinf(theta)*sinf(phi),
74
		cosf(theta));
75
}
76
77
/* Fisheye <-> Cartesian direction */
78
79
__device float2 direction_to_fisheye(float3 dir, float fov)
80
{
81
	float r = atan2f(sqrtf(dir.y*dir.y +  dir.z*dir.z), dir.x) / fov;
82
	float phi = atan2f(dir.z, dir.y);
83
84
	float u = r * cosf(phi) + 0.5f;
85
	float v = r * sinf(phi) + 0.5f;
86
87
	return make_float2(u, v);
88
}
89
90
__device float3 fisheye_to_direction(float u, float v, float fov)
91
{
92
	u = (u - 0.5f) * 2.0f;
93
	v = (v - 0.5f) * 2.0f;
94
95
	float r = sqrtf(u*u + v*v);
96
97
	if(r > 1.0f)
98
		return make_float3(0.0f, 0.0f, 0.0f);
99
100
	float phi = acosf((r != 0.0f)? u/r: 0.0f);
101
	float theta = r * fov * 0.5f;
102
103
	if(v < 0.0f) phi = -phi;
104
105
	return make_float3(
106
		 cosf(theta),
107
		 -cosf(phi)*sinf(theta),
108
		 sinf(phi)*sinf(theta)
109
	);
110
}
111
112
__device float2 direction_to_fisheye_equisolid(float3 dir, float lens, float width, float height)
113
{
114
	float theta = acosf(dir.x);
115
	float r = 2.0f * lens * sinf(theta * 0.5f);
116
	float phi = atan2f(dir.z, dir.y);
117
118
	float u = r * cosf(phi) / width + 0.5f;
119
	float v = r * sinf(phi) / height + 0.5f;
120
121
	return make_float2(u, v);
122
}
123
124
__device float3 fisheye_equisolid_to_direction(float u, float v, float lens, float fov, float width, float height)
125
{
126
	u = (u - 0.5f) * width;
127
	v = (v - 0.5f) * height;
128
129
	float rmax = 2.0f * lens * sinf(fov * 0.25f);
130
	float r = sqrtf(u*u + v*v);
131
132
	if(r > rmax)
133
		return make_float3(0.0f, 0.0f, 0.0f);
134
135
	float phi = acosf((r != 0.0f)? u/r: 0.0f);
136
	float theta = 2.0f * asinf(r/(2.0f * lens));
137
138
	if(v < 0.0f) phi = -phi;
139
140
	return make_float3(
141
		 cosf(theta),
142
		 -cosf(phi)*sinf(theta),
143
		 sinf(phi)*sinf(theta)
144
	);
145
}
146
147
/* Mirror Ball <-> Cartesion direction */
148
149
__device float3 mirrorball_to_direction(float u, float v)
150
{
151
	/* point on sphere */
152
	float3 dir;
153
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));
157
158
	/* reflection */
159
	float3 I = make_float3(0.0f, -1.0f, 0.0f);
160
161
	return 2.0f*dot(dir, I)*dir - I;
162
}
163
164
__device float2 direction_to_mirrorball(float3 dir)
165
{
166
	/* inverse of mirrorball_to_direction */
167
	dir.y -= 1.0f;
168
169
	float div = 2.0f*sqrtf(max(-0.5f*dir.y, 0.0f));
170
	if(div > 0.0f)
171
		dir /= div;
172
173
	float u = 0.5f*(dir.x + 1.0f);
174
	float v = 0.5f*(dir.z + 1.0f);
175
176
	return make_float2(u, v);
177
}
178
179
__device float3 panorama_to_direction(KernelGlobals *kg, float u, float v)
180
{
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:
187
		default:
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);
190
	}
191
}
192
193
__device float2 direction_to_panorama(KernelGlobals *kg, float3 dir)
194
{
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:
201
		default:
202
			return direction_to_fisheye_equisolid(dir, kernel_data.cam.fisheye_lens,
203
				kernel_data.cam.sensorwidth, kernel_data.cam.sensorheight);
204
	}
205
}
206
207
CCL_NAMESPACE_END
208
209
#endif /* __KERNEL_PROJECTION_CL__ */
210