Cycles:
[blender.git] / intern / cycles / kernel / kernel_camera.h
1 /*
2  * Copyright 2011, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  */
18
19 CCL_NAMESPACE_BEGIN
20
21 /* Perspective Camera */
22
23 __device float2 camera_sample_aperture(KernelGlobals *kg, float u, float v)
24 {
25         float blades = kernel_data.cam.blades;
26
27         if(blades == 0.0f) {
28                 /* sample disk */
29                 return concentric_sample_disk(u, v);
30         }
31         else {
32                 /* sample polygon */
33                 float rotation = kernel_data.cam.bladesrotation;
34                 return regular_polygon_sample(blades, rotation, u, v);
35         }
36 }
37
38 __device void camera_sample_perspective(KernelGlobals *kg, float raster_x, float raster_y, float lens_u, float lens_v, Ray *ray)
39 {
40         /* create ray form raster position */
41         Transform rastertocamera = kernel_data.cam.rastertocamera;
42         float3 Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x, raster_y, 0.0f));
43
44         ray->P = make_float3(0.0f, 0.0f, 0.0f);
45         ray->D = Pcamera;
46
47         /* modify ray for depth of field */
48         float aperturesize = kernel_data.cam.aperturesize;
49
50         if(aperturesize > 0.0f) {
51                 /* sample point on aperture */
52                 float2 lensuv = camera_sample_aperture(kg, lens_u, lens_v)*aperturesize;
53
54                 /* compute point on plane of focus */
55                 float ft = kernel_data.cam.focaldistance/ray->D.z;
56                 float3 Pfocus = ray->D*ft;
57
58                 /* update ray for effect of lens */
59                 ray->P = make_float3(lensuv.x, lensuv.y, 0.0f);
60                 ray->D = normalize(Pfocus - ray->P);
61         }
62
63         /* transform ray from camera to world */
64         Transform cameratoworld = kernel_data.cam.cameratoworld;
65
66 #ifdef __CAMERA_MOTION__
67         if(kernel_data.cam.have_motion)
68                 transform_motion_interpolate(&cameratoworld, (const DecompMotionTransform*)&kernel_data.cam.motion, ray->time);
69 #endif
70
71         ray->P = transform_point(&cameratoworld, ray->P);
72         ray->D = transform_direction(&cameratoworld, ray->D);
73         ray->D = normalize(ray->D);
74
75 #ifdef __RAY_DIFFERENTIALS__
76         /* ray differential */
77         float3 Ddiff = transform_direction(&cameratoworld, Pcamera);
78
79         ray->dP = differential3_zero();
80
81         ray->dD.dx = normalize(Ddiff + float4_to_float3(kernel_data.cam.dx)) - normalize(Ddiff);
82         ray->dD.dy = normalize(Ddiff + float4_to_float3(kernel_data.cam.dy)) - normalize(Ddiff);
83 #endif
84
85 #ifdef __CAMERA_CLIPPING__
86         /* clipping */
87         ray->P += kernel_data.cam.nearclip*ray->D;
88         ray->t = kernel_data.cam.cliplength;
89 #else
90         ray->t = FLT_MAX;
91 #endif
92 }
93
94 /* Orthographic Camera */
95
96 __device void camera_sample_orthographic(KernelGlobals *kg, float raster_x, float raster_y, float lens_u, float lens_v, Ray *ray)
97 {
98         /* create ray form raster position */
99         Transform rastertocamera = kernel_data.cam.rastertocamera;
100         float3 Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x, raster_y, 0.0f));
101
102         ray->D = make_float3(0.0f, 0.0f, 1.0f);
103
104         /* modify ray for depth of field */
105         float aperturesize = kernel_data.cam.aperturesize;
106
107         if(aperturesize > 0.0f) {
108                 /* sample point on aperture */
109                 float2 lensuv = camera_sample_aperture(kg, lens_u, lens_v)*aperturesize;
110
111                 /* compute point on plane of focus */
112                 float3 Pfocus = ray->D * kernel_data.cam.focaldistance;
113
114                 /* update ray for effect of lens */
115                 float3 lensuvw = make_float3(lensuv.x, lensuv.y, 0.0f);
116                 ray->P = Pcamera + lensuvw;
117                 ray->D = normalize(Pfocus - lensuvw);
118         }
119         else {
120                 ray->P = Pcamera;
121         }
122         /* transform ray from camera to world */
123         Transform cameratoworld = kernel_data.cam.cameratoworld;
124
125 #ifdef __CAMERA_MOTION__
126         if(kernel_data.cam.have_motion)
127                 transform_motion_interpolate(&cameratoworld, (const DecompMotionTransform*)&kernel_data.cam.motion, ray->time);
128 #endif
129
130         ray->P = transform_point(&cameratoworld, ray->P);
131         ray->D = transform_direction(&cameratoworld, ray->D);
132         ray->D = normalize(ray->D);
133
134 #ifdef __RAY_DIFFERENTIALS__
135         /* ray differential */
136         ray->dP.dx = float4_to_float3(kernel_data.cam.dx);
137         ray->dP.dy = float4_to_float3(kernel_data.cam.dy);
138
139         ray->dD = differential3_zero();
140 #endif
141
142 #ifdef __CAMERA_CLIPPING__
143         /* clipping */
144         ray->t = kernel_data.cam.cliplength;
145 #else
146         ray->t = FLT_MAX;
147 #endif
148 }
149
150 /* Panorama Camera */
151
152 __device void camera_sample_panorama(KernelGlobals *kg, float raster_x, float raster_y, float lens_u, float lens_v, Ray *ray)
153 {
154         Transform rastertocamera = kernel_data.cam.rastertocamera;
155         float3 Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x, raster_y, 0.0f));
156
157         /* create ray form raster position */
158         ray->P = make_float3(0.0f, 0.0f, 0.0f);
159
160 #ifdef __CAMERA_CLIPPING__
161         /* clipping */
162         ray->t = kernel_data.cam.cliplength;
163 #else
164         ray->t = FLT_MAX;
165 #endif
166
167         ray->D = panorama_to_direction(kg, Pcamera.x, Pcamera.y);
168
169         /* modify ray for depth of field */
170         float aperturesize = kernel_data.cam.aperturesize;
171
172         if(aperturesize > 0.0f) {
173                 /* sample point on aperture */
174                 float2 lensuv = camera_sample_aperture(kg, lens_u, lens_v)*aperturesize;
175
176                 /* compute point on plane of focus */
177                 float3 D = normalize(ray->D);
178                 float3 Pfocus = D * kernel_data.cam.focaldistance;
179
180                 /* calculate orthonormal coordinates perpendicular to D */
181                 float3 U, V;
182                 make_orthonormals(D, &U, &V);
183
184                 /* update ray for effect of lens */
185                 ray->P = U * lensuv.x + V * lensuv.y;
186                 ray->D = normalize(Pfocus - ray->P);
187         }
188
189         /* indicates ray should not receive any light, outside of the lens */
190         if(is_zero(ray->D)) {   
191                 ray->t = 0.0f;
192                 return;
193         }
194
195         /* transform ray from camera to world */
196         Transform cameratoworld = kernel_data.cam.cameratoworld;
197
198 #ifdef __CAMERA_MOTION__
199         if(kernel_data.cam.have_motion)
200                 transform_motion_interpolate(&cameratoworld, (const DecompMotionTransform*)&kernel_data.cam.motion, ray->time);
201 #endif
202
203         ray->P = transform_point(&cameratoworld, ray->P);
204         ray->D = transform_direction(&cameratoworld, ray->D);
205         ray->D = normalize(ray->D);
206
207 #ifdef __RAY_DIFFERENTIALS__
208         /* ray differential */
209         ray->dP = differential3_zero();
210
211         Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x + 1.0f, raster_y, 0.0f));
212         ray->dD.dx = normalize(transform_direction(&cameratoworld, panorama_to_direction(kg, Pcamera.x, Pcamera.y))) - ray->D;
213
214         Pcamera = transform_perspective(&rastertocamera, make_float3(raster_x, raster_y + 1.0f, 0.0f));
215         ray->dD.dy = normalize(transform_direction(&cameratoworld, panorama_to_direction(kg, Pcamera.x, Pcamera.y))) - ray->D;
216 #endif
217 }
218
219 /* Common */
220
221 __device void camera_sample(KernelGlobals *kg, int x, int y, float filter_u, float filter_v,
222         float lens_u, float lens_v, float time, Ray *ray)
223 {
224         /* pixel filter */
225         int filter_table_offset = kernel_data.film.filter_table_offset;
226         float raster_x = x + lookup_table_read(kg, filter_u, filter_table_offset, FILTER_TABLE_SIZE);
227         float raster_y = y + lookup_table_read(kg, filter_v, filter_table_offset, FILTER_TABLE_SIZE);
228
229 #ifdef __CAMERA_MOTION__
230         /* motion blur */
231         if(kernel_data.cam.shuttertime == -1.0f)
232                 ray->time = TIME_INVALID;
233         else
234                 ray->time = 0.5f + 0.5f*(time - 0.5f)*kernel_data.cam.shuttertime;
235 #endif
236
237         /* sample */
238         if(kernel_data.cam.type == CAMERA_PERSPECTIVE)
239                 camera_sample_perspective(kg, raster_x, raster_y, lens_u, lens_v, ray);
240         else if(kernel_data.cam.type == CAMERA_ORTHOGRAPHIC)
241                 camera_sample_orthographic(kg, raster_x, raster_y, lens_u, lens_v, ray);
242         else
243                 camera_sample_panorama(kg, raster_x, raster_y, lens_u, lens_v, ray);
244 }
245
246 /* Utilities */
247
248 __device_inline float camera_distance(KernelGlobals *kg, float3 P)
249 {
250         Transform cameratoworld = kernel_data.cam.cameratoworld;
251         float3 camP = make_float3(cameratoworld.x.w, cameratoworld.y.w, cameratoworld.z.w);
252
253         if(kernel_data.cam.type == CAMERA_ORTHOGRAPHIC) {
254                 float3 camD = make_float3(cameratoworld.x.z, cameratoworld.y.z, cameratoworld.z.z);
255                 return fabsf(dot((P - camP), camD));
256         }
257         else
258                 return len(P - camP);
259 }
260
261 CCL_NAMESPACE_END
262