svn merge ^/trunk/blender -r44562:45364
[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(&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->P + 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         ray->P = transform(&cameratoworld, ray->P);
67         ray->D = transform_direction(&cameratoworld, ray->D);
68         ray->D = normalize(ray->D);
69
70 #ifdef __RAY_DIFFERENTIALS__
71         /* ray differential */
72         float3 Ddiff = transform_direction(&cameratoworld, Pcamera);
73
74         ray->dP.dx = make_float3(0.0f, 0.0f, 0.0f);
75         ray->dP.dy = make_float3(0.0f, 0.0f, 0.0f);
76
77         ray->dD.dx = normalize(Ddiff + float4_to_float3(kernel_data.cam.dx)) - normalize(Ddiff);
78         ray->dD.dy = normalize(Ddiff + float4_to_float3(kernel_data.cam.dy)) - normalize(Ddiff);
79 #endif
80
81 #ifdef __CAMERA_CLIPPING__
82         /* clipping */
83         ray->P += kernel_data.cam.nearclip*ray->D;
84         ray->t = kernel_data.cam.cliplength;
85 #else
86         ray->t = FLT_MAX;
87 #endif
88 }
89
90 /* Orthographic Camera */
91
92 __device void camera_sample_orthographic(KernelGlobals *kg, float raster_x, float raster_y, Ray *ray)
93 {
94         /* create ray form raster position */
95         Transform rastertocamera = kernel_data.cam.rastertocamera;
96         float3 Pcamera = transform(&rastertocamera, make_float3(raster_x, raster_y, 0.0f));
97
98         ray->P = Pcamera;
99         ray->D = make_float3(0.0f, 0.0f, 1.0f);
100
101         /* transform ray from camera to world */
102         Transform cameratoworld = kernel_data.cam.cameratoworld;
103
104         ray->P = transform(&cameratoworld, ray->P);
105         ray->D = transform_direction(&cameratoworld, ray->D);
106         ray->D = normalize(ray->D);
107
108 #ifdef __RAY_DIFFERENTIALS__
109         /* ray differential */
110         ray->dP.dx = float4_to_float3(kernel_data.cam.dx);
111         ray->dP.dy = float4_to_float3(kernel_data.cam.dy);
112
113         ray->dD.dx = make_float3(0.0f, 0.0f, 0.0f);
114         ray->dD.dy = make_float3(0.0f, 0.0f, 0.0f);
115 #endif
116
117 #ifdef __CAMERA_CLIPPING__
118         /* clipping */
119         ray->t = kernel_data.cam.cliplength;
120 #else
121         ray->t = FLT_MAX;
122 #endif
123 }
124
125 /* Environment Camera */
126
127 __device void camera_sample_environment(KernelGlobals *kg, float raster_x, float raster_y, Ray *ray)
128 {
129         Transform rastertocamera = kernel_data.cam.rastertocamera;
130         float3 Pcamera = transform(&rastertocamera, make_float3(raster_x, raster_y, 0.0f));
131
132         /* create ray form raster position */
133         ray->P = make_float3(0.0, 0.0f, 0.0f);
134         ray->D = equirectangular_to_direction(Pcamera.x, Pcamera.y);
135
136         /* transform ray from camera to world */
137         Transform cameratoworld = kernel_data.cam.cameratoworld;
138
139         ray->P = transform(&cameratoworld, ray->P);
140         ray->D = transform_direction(&cameratoworld, ray->D);
141         ray->D = normalize(ray->D);
142
143 #ifdef __RAY_DIFFERENTIALS__
144         /* ray differential */
145         ray->dP.dx = make_float3(0.0f, 0.0f, 0.0f);
146         ray->dP.dy = make_float3(0.0f, 0.0f, 0.0f);
147
148         Pcamera = transform(&rastertocamera, make_float3(raster_x + 1.0f, raster_y, 0.0f));
149         ray->dD.dx = normalize(transform_direction(&cameratoworld, equirectangular_to_direction(Pcamera.x, Pcamera.y))) - ray->D;
150
151         Pcamera = transform(&rastertocamera, make_float3(raster_x, raster_y + 1.0f, 0.0f));
152         ray->dD.dy = normalize(transform_direction(&cameratoworld, equirectangular_to_direction(Pcamera.x, Pcamera.y))) - ray->D;
153 #endif
154
155 #ifdef __CAMERA_CLIPPING__
156         /* clipping */
157         ray->t = kernel_data.cam.cliplength;
158 #else
159         ray->t = FLT_MAX;
160 #endif
161 }
162
163 /* Common */
164
165 __device void camera_sample(KernelGlobals *kg, int x, int y, float filter_u, float filter_v, float lens_u, float lens_v, Ray *ray)
166 {
167         /* pixel filter */
168         float raster_x = x + kernel_tex_interp(__filter_table, filter_u, FILTER_TABLE_SIZE);
169         float raster_y = y + kernel_tex_interp(__filter_table, filter_v, FILTER_TABLE_SIZE);
170
171         /* motion blur */
172         //ray->time = lerp(time_t, kernel_data.cam.shutter_open, kernel_data.cam.shutter_close);
173
174         /* sample */
175         if(kernel_data.cam.type == CAMERA_PERSPECTIVE)
176                 camera_sample_perspective(kg, raster_x, raster_y, lens_u, lens_v, ray);
177         else if(kernel_data.cam.type == CAMERA_ORTHOGRAPHIC)
178                 camera_sample_orthographic(kg, raster_x, raster_y, ray);
179         else
180                 camera_sample_environment(kg, raster_x, raster_y, ray);
181 }
182
183 CCL_NAMESPACE_END
184