Code refactor: add ProjectionTransform separate from regular Transform.
[blender-staging.git] / intern / cycles / render / camera.cpp
1 /*
2  * Copyright 2011-2013 Blender Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "render/camera.h"
18 #include "render/mesh.h"
19 #include "render/object.h"
20 #include "render/scene.h"
21 #include "render/tables.h"
22
23 #include "device/device.h"
24
25 #include "util/util_foreach.h"
26 #include "util/util_function.h"
27 #include "util/util_math_cdf.h"
28 #include "util/util_vector.h"
29
30 /* needed for calculating differentials */
31 #include "kernel/kernel_compat_cpu.h"
32 #include "kernel/split/kernel_split_data.h"
33 #include "kernel/kernel_globals.h"
34 #include "kernel/kernel_projection.h"
35 #include "kernel/kernel_differential.h"
36 #include "kernel/kernel_montecarlo.h"
37 #include "kernel/kernel_camera.h"
38
39 CCL_NAMESPACE_BEGIN
40
41 static float shutter_curve_eval(float x,
42                                 array<float>& shutter_curve)
43 {
44         if(shutter_curve.size() == 0) {
45                 return 1.0f;
46         }
47
48         x *= shutter_curve.size();
49         int index = (int)x;
50         float frac = x - index;
51         if(index < shutter_curve.size() - 1) {
52                 return lerp(shutter_curve[index], shutter_curve[index + 1], frac);
53         }
54         else {
55                 return shutter_curve[shutter_curve.size() - 1];
56         }
57 }
58
59 NODE_DEFINE(Camera)
60 {
61         NodeType* type = NodeType::add("camera", create);
62
63         SOCKET_FLOAT(shuttertime, "Shutter Time", 1.0f);
64
65         static NodeEnum motion_position_enum;
66         motion_position_enum.insert("start", MOTION_POSITION_START);
67         motion_position_enum.insert("center", MOTION_POSITION_CENTER);
68         motion_position_enum.insert("end", MOTION_POSITION_END);
69         SOCKET_ENUM(motion_position, "Motion Position", motion_position_enum, MOTION_POSITION_CENTER);
70
71         static NodeEnum rolling_shutter_type_enum;
72         rolling_shutter_type_enum.insert("none", ROLLING_SHUTTER_NONE);
73         rolling_shutter_type_enum.insert("top", ROLLING_SHUTTER_TOP);
74         SOCKET_ENUM(rolling_shutter_type, "Rolling Shutter Type", rolling_shutter_type_enum,  ROLLING_SHUTTER_NONE);
75         SOCKET_FLOAT(rolling_shutter_duration, "Rolling Shutter Duration", 0.1f);
76
77         SOCKET_FLOAT_ARRAY(shutter_curve, "Shutter Curve", array<float>());
78
79         SOCKET_FLOAT(aperturesize, "Aperture Size", 0.0f);
80         SOCKET_FLOAT(focaldistance, "Focal Distance", 10.0f);
81         SOCKET_UINT(blades, "Blades", 0);
82         SOCKET_FLOAT(bladesrotation, "Blades Rotation", 0.0f);
83
84         SOCKET_TRANSFORM(matrix, "Matrix", transform_identity());
85
86         SOCKET_FLOAT(aperture_ratio, "Aperture Ratio", 1.0f);
87
88         static NodeEnum type_enum;
89         type_enum.insert("perspective", CAMERA_PERSPECTIVE);
90         type_enum.insert("orthograph", CAMERA_ORTHOGRAPHIC);
91         type_enum.insert("panorama", CAMERA_PANORAMA);
92         SOCKET_ENUM(type, "Type", type_enum, CAMERA_PERSPECTIVE);
93
94         static NodeEnum panorama_type_enum;
95         panorama_type_enum.insert("equirectangular", PANORAMA_EQUIRECTANGULAR);
96         panorama_type_enum.insert("mirrorball", PANORAMA_MIRRORBALL);
97         panorama_type_enum.insert("fisheye_equidistant", PANORAMA_FISHEYE_EQUIDISTANT);
98         panorama_type_enum.insert("fisheye_equisolid", PANORAMA_FISHEYE_EQUISOLID);
99         SOCKET_ENUM(panorama_type, "Panorama Type", panorama_type_enum, PANORAMA_EQUIRECTANGULAR);
100
101         SOCKET_FLOAT(fisheye_fov, "Fisheye FOV", M_PI_F);
102         SOCKET_FLOAT(fisheye_lens, "Fisheye Lens", 10.5f);
103         SOCKET_FLOAT(latitude_min, "Latitude Min", -M_PI_2_F);
104         SOCKET_FLOAT(latitude_max, "Latitude Max", M_PI_2_F);
105         SOCKET_FLOAT(longitude_min, "Longitude Min", -M_PI_F);
106         SOCKET_FLOAT(longitude_max, "Longitude Max", M_PI_F);
107         SOCKET_FLOAT(fov, "FOV", M_PI_4_F);
108         SOCKET_FLOAT(fov_pre, "FOV Pre", M_PI_4_F);
109         SOCKET_FLOAT(fov_post, "FOV Post", M_PI_4_F);
110
111         static NodeEnum stereo_eye_enum;
112         stereo_eye_enum.insert("none", STEREO_NONE);
113         stereo_eye_enum.insert("left", STEREO_LEFT);
114         stereo_eye_enum.insert("right", STEREO_RIGHT);
115         SOCKET_ENUM(stereo_eye, "Stereo Eye", stereo_eye_enum, STEREO_NONE);
116
117         SOCKET_FLOAT(interocular_distance, "Interocular Distance", 0.065f);
118         SOCKET_FLOAT(convergence_distance, "Convergence Distance", 30.0f * 0.065f);
119
120         SOCKET_BOOLEAN(use_pole_merge, "Use Pole Merge", false);
121         SOCKET_FLOAT(pole_merge_angle_from, "Pole Merge Angle From",  60.0f * M_PI_F / 180.0f);
122         SOCKET_FLOAT(pole_merge_angle_to, "Pole Merge Angle To", 75.0f * M_PI_F / 180.0f);
123
124         SOCKET_FLOAT(sensorwidth, "Sensor Width", 0.036f);
125         SOCKET_FLOAT(sensorheight, "Sensor Height", 0.024f);
126
127         SOCKET_FLOAT(nearclip, "Near Clip", 1e-5f);
128         SOCKET_FLOAT(farclip, "Far Clip", 1e5f);
129
130         SOCKET_FLOAT(viewplane.left, "Viewplane Left", 0);
131         SOCKET_FLOAT(viewplane.right, "Viewplane Right", 0);
132         SOCKET_FLOAT(viewplane.bottom, "Viewplane Bottom", 0);
133         SOCKET_FLOAT(viewplane.top, "Viewplane Top", 0);
134
135         SOCKET_FLOAT(border.left, "Border Left", 0);
136         SOCKET_FLOAT(border.right, "Border Right", 0);
137         SOCKET_FLOAT(border.bottom, "Border Bottom", 0);
138         SOCKET_FLOAT(border.top, "Border Top", 0);
139
140         SOCKET_FLOAT(offscreen_dicing_scale, "Offscreen Dicing Scale", 1.0f);
141
142         return type;
143 }
144
145 Camera::Camera()
146 : Node(node_type)
147 {
148         shutter_table_offset = TABLE_OFFSET_INVALID;
149
150         width = 1024;
151         height = 512;
152         resolution = 1;
153
154         motion.pre = transform_identity();
155         motion.post = transform_identity();
156         use_motion = false;
157         use_perspective_motion = false;
158
159         shutter_curve.resize(RAMP_TABLE_SIZE);
160         for(int i = 0; i < shutter_curve.size(); ++i) {
161                 shutter_curve[i] = 1.0f;
162         }
163
164         compute_auto_viewplane();
165
166         screentoworld = projection_identity();
167         rastertoworld = projection_identity();
168         ndctoworld = projection_identity();
169         rastertocamera = projection_identity();
170         cameratoworld = transform_identity();
171         worldtoraster = projection_identity();
172
173         dx = make_float3(0.0f, 0.0f, 0.0f);
174         dy = make_float3(0.0f, 0.0f, 0.0f);
175
176         need_update = true;
177         need_device_update = true;
178         need_flags_update = true;
179         previous_need_motion = -1;
180
181         memset(&kernel_camera, 0, sizeof(kernel_camera));
182 }
183
184 Camera::~Camera()
185 {
186 }
187
188 void Camera::compute_auto_viewplane()
189 {
190         if(type == CAMERA_PANORAMA) {
191                 viewplane.left = 0.0f;
192                 viewplane.right = 1.0f;
193                 viewplane.bottom = 0.0f;
194                 viewplane.top = 1.0f;
195         }
196         else {
197                 float aspect = (float)width/(float)height;
198                 if(width >= height) {
199                         viewplane.left = -aspect;
200                         viewplane.right = aspect;
201                         viewplane.bottom = -1.0f;
202                         viewplane.top = 1.0f;
203                 }
204                 else {
205                         viewplane.left = -1.0f;
206                         viewplane.right = 1.0f;
207                         viewplane.bottom = -1.0f/aspect;
208                         viewplane.top = 1.0f/aspect;
209                 }
210         }
211 }
212
213 void Camera::update(Scene *scene)
214 {
215         Scene::MotionType need_motion = scene->need_motion();
216
217         if(previous_need_motion != need_motion) {
218                 /* scene's motion model could have been changed since previous device
219                  * camera update this could happen for example in case when one render
220                  * layer has got motion pass and another not */
221                 need_device_update = true;
222         }
223
224         if(!need_update)
225                 return;
226
227         /* Full viewport to camera border in the viewport. */
228         Transform fulltoborder = transform_from_viewplane(viewport_camera_border);
229         Transform bordertofull = transform_inverse(fulltoborder);
230
231         /* ndc to raster */
232         Transform ndctoraster = transform_scale(width, height, 1.0f) * bordertofull;
233         Transform full_ndctoraster = transform_scale(full_width, full_height, 1.0f) * bordertofull;
234
235         /* raster to screen */
236         Transform screentondc = fulltoborder * transform_from_viewplane(viewplane);
237
238         Transform screentoraster = ndctoraster * screentondc;
239         Transform rastertoscreen = transform_inverse(screentoraster);
240         Transform full_screentoraster = full_ndctoraster * screentondc;
241         Transform full_rastertoscreen = transform_inverse(full_screentoraster);
242
243         /* screen to camera */
244         ProjectionTransform cameratoscreen;
245         if(type == CAMERA_PERSPECTIVE)
246                 cameratoscreen = projection_perspective(fov, nearclip, farclip);
247         else if(type == CAMERA_ORTHOGRAPHIC)
248                 cameratoscreen = projection_orthographic(nearclip, farclip);
249         else
250                 cameratoscreen = projection_identity();
251         
252         ProjectionTransform screentocamera = projection_inverse(cameratoscreen);
253
254         rastertocamera = screentocamera * rastertoscreen;
255         ProjectionTransform full_rastertocamera = screentocamera * full_rastertoscreen;
256         cameratoraster = screentoraster * cameratoscreen;
257
258         cameratoworld = matrix;
259         screentoworld = cameratoworld * screentocamera;
260         rastertoworld = cameratoworld * rastertocamera;
261         ndctoworld = rastertoworld * ndctoraster;
262
263         /* note we recompose matrices instead of taking inverses of the above, this
264          * is needed to avoid inverting near degenerate matrices that happen due to
265          * precision issues with large scenes */
266         worldtocamera = transform_inverse(matrix);
267         worldtoscreen = cameratoscreen * worldtocamera;
268         worldtondc = screentondc * worldtoscreen;
269         worldtoraster = ndctoraster * worldtondc;
270
271         /* differentials */
272         if(type == CAMERA_ORTHOGRAPHIC) {
273                 dx = transform_perspective_direction(&rastertocamera, make_float3(1, 0, 0));
274                 dy = transform_perspective_direction(&rastertocamera, make_float3(0, 1, 0));
275                 full_dx = transform_perspective_direction(&full_rastertocamera, make_float3(1, 0, 0));
276                 full_dy = transform_perspective_direction(&full_rastertocamera, make_float3(0, 1, 0));
277         }
278         else if(type == CAMERA_PERSPECTIVE) {
279                 dx = transform_perspective(&rastertocamera, make_float3(1, 0, 0)) -
280                      transform_perspective(&rastertocamera, make_float3(0, 0, 0));
281                 dy = transform_perspective(&rastertocamera, make_float3(0, 1, 0)) -
282                      transform_perspective(&rastertocamera, make_float3(0, 0, 0));
283                 full_dx = transform_perspective(&full_rastertocamera, make_float3(1, 0, 0)) -
284                      transform_perspective(&full_rastertocamera, make_float3(0, 0, 0));
285                 full_dy = transform_perspective(&full_rastertocamera, make_float3(0, 1, 0)) -
286                      transform_perspective(&full_rastertocamera, make_float3(0, 0, 0));
287         }
288         else {
289                 dx = make_float3(0.0f, 0.0f, 0.0f);
290                 dy = make_float3(0.0f, 0.0f, 0.0f);
291         }
292
293         dx = transform_direction(&cameratoworld, dx);
294         dy = transform_direction(&cameratoworld, dy);
295         full_dx = transform_direction(&cameratoworld, full_dx);
296         full_dy = transform_direction(&cameratoworld, full_dy);
297
298         if(type == CAMERA_PERSPECTIVE) {
299                 float3 v = transform_perspective(&full_rastertocamera, make_float3(full_width, full_height, 1.0f));
300
301                 frustum_right_normal = normalize(make_float3(v.z, 0.0f, -v.x));
302                 frustum_top_normal = normalize(make_float3(0.0f, v.z, -v.y));
303         }
304
305         /* TODO(sergey): Support other types of camera. */
306         if(type == CAMERA_PERSPECTIVE) {
307                 /* TODO(sergey): Move to an utility function and de-duplicate with
308                  * calculation above.
309                  */
310                 ProjectionTransform screentocamera_pre =
311                         projection_inverse(projection_perspective(fov_pre,
312                                                                   nearclip,
313                                                                   farclip));
314                 ProjectionTransform screentocamera_post =
315                         projection_inverse(projection_perspective(fov_post,
316                                                                   nearclip,
317                                                                   farclip));
318                 perspective_motion.pre = screentocamera_pre * rastertoscreen;
319                 perspective_motion.post = screentocamera_post * rastertoscreen;
320         }
321
322         /* Compute kernel camera data. */
323         KernelCamera *kcam = &kernel_camera;
324
325         /* store matrices */
326         kcam->screentoworld = screentoworld;
327         kcam->rastertoworld = rastertoworld;
328         kcam->rastertocamera = rastertocamera;
329         kcam->cameratoworld = cameratoworld;
330         kcam->worldtocamera = worldtocamera;
331         kcam->worldtoscreen = worldtoscreen;
332         kcam->worldtoraster = worldtoraster;
333         kcam->worldtondc = worldtondc;
334         kcam->ndctoworld = ndctoworld;
335
336         /* camera motion */
337         kcam->have_motion = 0;
338         kcam->have_perspective_motion = 0;
339
340         if(need_motion == Scene::MOTION_PASS) {
341                 /* TODO(sergey): Support perspective (zoom, fov) motion. */
342                 if(type == CAMERA_PANORAMA) {
343                         if(use_motion) {
344                                 kcam->motion.pre = transform_inverse(motion.pre);
345                                 kcam->motion.post = transform_inverse(motion.post);
346                         }
347                         else {
348                                 kcam->motion.pre = kcam->worldtocamera;
349                                 kcam->motion.post = kcam->worldtocamera;
350                         }
351                 }
352                 else {
353                         if(use_motion) {
354                                 kcam->perspective_motion.pre = cameratoraster * transform_inverse(motion.pre);
355                                 kcam->perspective_motion.post = cameratoraster * transform_inverse(motion.post);
356                         }
357                         else {
358                                 kcam->perspective_motion.pre = worldtoraster;
359                                 kcam->perspective_motion.post = worldtoraster;
360                         }
361                 }
362         }
363         else if(need_motion == Scene::MOTION_BLUR) {
364                 if(use_motion) {
365                         transform_motion_decompose(&kcam->motion, &motion, &matrix);
366                         kcam->have_motion = 1;
367                 }
368                 if(use_perspective_motion) {
369                         kcam->perspective_motion = perspective_motion;
370                         kcam->have_perspective_motion = 1;
371                 }
372         }
373
374         /* depth of field */
375         kcam->aperturesize = aperturesize;
376         kcam->focaldistance = focaldistance;
377         kcam->blades = (blades < 3)? 0.0f: blades;
378         kcam->bladesrotation = bladesrotation;
379
380         /* motion blur */
381         kcam->shuttertime = (need_motion == Scene::MOTION_BLUR) ? shuttertime: -1.0f;
382
383         /* type */
384         kcam->type = type;
385
386         /* anamorphic lens bokeh */
387         kcam->inv_aperture_ratio = 1.0f / aperture_ratio;
388
389         /* panorama */
390         kcam->panorama_type = panorama_type;
391         kcam->fisheye_fov = fisheye_fov;
392         kcam->fisheye_lens = fisheye_lens;
393         kcam->equirectangular_range = make_float4(longitude_min - longitude_max, -longitude_min,
394                                                   latitude_min -  latitude_max, -latitude_min + M_PI_2_F);
395
396         switch(stereo_eye) {
397                 case STEREO_LEFT:
398                         kcam->interocular_offset = -interocular_distance * 0.5f;
399                         break;
400                 case STEREO_RIGHT:
401                         kcam->interocular_offset = interocular_distance * 0.5f;
402                         break;
403                 case STEREO_NONE:
404                 default:
405                         kcam->interocular_offset = 0.0f;
406                         break;
407         }
408
409         kcam->convergence_distance = convergence_distance;
410         if(use_pole_merge) {
411                 kcam->pole_merge_angle_from = pole_merge_angle_from;
412                 kcam->pole_merge_angle_to = pole_merge_angle_to;
413         }
414         else {
415                 kcam->pole_merge_angle_from = -1.0f;
416                 kcam->pole_merge_angle_to = -1.0f;
417         }
418
419         /* sensor size */
420         kcam->sensorwidth = sensorwidth;
421         kcam->sensorheight = sensorheight;
422
423         /* render size */
424         kcam->width = width;
425         kcam->height = height;
426         kcam->resolution = resolution;
427
428         /* store differentials */
429         kcam->dx = float3_to_float4(dx);
430         kcam->dy = float3_to_float4(dy);
431
432         /* clipping */
433         kcam->nearclip = nearclip;
434         kcam->cliplength = (farclip == FLT_MAX)? FLT_MAX: farclip - nearclip;
435
436         /* Camera in volume. */
437         kcam->is_inside_volume = 0;
438
439         /* Rolling shutter effect */
440         kcam->rolling_shutter_type = rolling_shutter_type;
441         kcam->rolling_shutter_duration = rolling_shutter_duration;
442
443         /* Set further update flags */
444         need_update = false;
445         need_device_update = true;
446         need_flags_update = true;
447         previous_need_motion = need_motion;
448 }
449
450 void Camera::device_update(Device * /* device */,
451                            DeviceScene *dscene,
452                            Scene *scene)
453 {
454         update(scene);
455
456         if(!need_device_update)
457                 return;
458
459         scene->lookup_tables->remove_table(&shutter_table_offset);
460         if(kernel_camera.shuttertime != -1.0f) {
461                 vector<float> shutter_table;
462                 util_cdf_inverted(SHUTTER_TABLE_SIZE,
463                                   0.0f,
464                                   1.0f,
465                                   function_bind(shutter_curve_eval, _1, shutter_curve),
466                                   false,
467                                   shutter_table);
468                 shutter_table_offset = scene->lookup_tables->add_table(dscene,
469                                                                        shutter_table);
470                 kernel_camera.shutter_table_offset = (int)shutter_table_offset;
471         }
472
473         dscene->data.cam = kernel_camera;
474 }
475
476 void Camera::device_update_volume(Device * /*device*/,
477                                   DeviceScene *dscene,
478                                   Scene *scene)
479 {
480         if(!need_device_update && !need_flags_update) {
481                 return;
482         }
483         KernelCamera *kcam = &dscene->data.cam;
484         BoundBox viewplane_boundbox = viewplane_bounds_get();
485         for(size_t i = 0; i < scene->objects.size(); ++i) {
486                 Object *object = scene->objects[i];
487                 if(object->mesh->has_volume &&
488                    viewplane_boundbox.intersects(object->bounds))
489                 {
490                         /* TODO(sergey): Consider adding more grained check. */
491                         kcam->is_inside_volume = 1;
492                         break;
493                 }
494         }
495         need_device_update = false;
496         need_flags_update = false;
497 }
498
499 void Camera::device_free(Device * /*device*/,
500                          DeviceScene * /*dscene*/,
501                          Scene *scene)
502 {
503         scene->lookup_tables->remove_table(&shutter_table_offset);
504 }
505
506 bool Camera::modified(const Camera& cam)
507 {
508         return !Node::equals(cam);
509 }
510
511 bool Camera::motion_modified(const Camera& cam)
512 {
513         return !((motion == cam.motion) &&
514                  (use_motion == cam.use_motion) &&
515                  (use_perspective_motion == cam.use_perspective_motion));
516 }
517
518 void Camera::tag_update()
519 {
520         need_update = true;
521 }
522
523 float3 Camera::transform_raster_to_world(float raster_x, float raster_y)
524 {
525         float3 D, P;
526         if(type == CAMERA_PERSPECTIVE) {
527                 D = transform_perspective(&rastertocamera,
528                                           make_float3(raster_x, raster_y, 0.0f));
529                 float3 Pclip = normalize(D);
530                 P = make_float3(0.0f, 0.0f, 0.0f);
531                 /* TODO(sergey): Aperture support? */
532                 P = transform_point(&cameratoworld, P);
533                 D = normalize(transform_direction(&cameratoworld, D));
534                 /* TODO(sergey): Clipping is conditional in kernel, and hence it could
535                  * be mistakes in here, currently leading to wrong camera-in-volume
536                  * detection.
537                  */
538                 P += nearclip * D / Pclip.z;
539         }
540         else if(type == CAMERA_ORTHOGRAPHIC) {
541                 D = make_float3(0.0f, 0.0f, 1.0f);
542                 /* TODO(sergey): Aperture support? */
543                 P = transform_perspective(&rastertocamera,
544                                           make_float3(raster_x, raster_y, 0.0f));
545                 P = transform_point(&cameratoworld, P);
546                 D = normalize(transform_direction(&cameratoworld, D));
547         }
548         else {
549                 assert(!"unsupported camera type");
550         }
551         return P;
552 }
553
554 BoundBox Camera::viewplane_bounds_get()
555 {
556         /* TODO(sergey): This is all rather stupid, but is there a way to perform
557          * checks we need in a more clear and smart fasion?
558          */
559         BoundBox bounds = BoundBox::empty;
560
561         if(type == CAMERA_PANORAMA) {
562                 if(use_spherical_stereo == false) {
563                         bounds.grow(make_float3(cameratoworld.x.w,
564                                                 cameratoworld.y.w,
565                                                 cameratoworld.z.w));
566                 }
567                 else {
568                         float half_eye_distance = interocular_distance * 0.5f;
569
570                         bounds.grow(make_float3(cameratoworld.x.w + half_eye_distance,
571                                                 cameratoworld.y.w,
572                                                 cameratoworld.z.w));
573
574                         bounds.grow(make_float3(cameratoworld.z.w,
575                                                 cameratoworld.y.w + half_eye_distance,
576                                                 cameratoworld.z.w));
577
578                         bounds.grow(make_float3(cameratoworld.x.w - half_eye_distance,
579                                                 cameratoworld.y.w,
580                                                 cameratoworld.z.w));
581
582                         bounds.grow(make_float3(cameratoworld.x.w,
583                                                 cameratoworld.y.w - half_eye_distance,
584                                                 cameratoworld.z.w));
585                 }
586         }
587         else {
588                 bounds.grow(transform_raster_to_world(0.0f, 0.0f));
589                 bounds.grow(transform_raster_to_world(0.0f, (float)height));
590                 bounds.grow(transform_raster_to_world((float)width, (float)height));
591                 bounds.grow(transform_raster_to_world((float)width, 0.0f));
592                 if(type == CAMERA_PERSPECTIVE) {
593                         /* Center point has the most distance in local Z axis,
594                          * use it to construct bounding box/
595                          */
596                         bounds.grow(transform_raster_to_world(0.5f*width, 0.5f*height));
597                 }
598         }
599         return bounds;
600 }
601
602 float Camera::world_to_raster_size(float3 P)
603 {
604         float res = 1.0f;
605
606         if(type == CAMERA_ORTHOGRAPHIC) {
607                 res = min(len(full_dx), len(full_dy));
608
609                 if(offscreen_dicing_scale > 1.0f) {
610                         float3 p = transform_point(&worldtocamera, P);
611                         float3 v = transform_perspective(&rastertocamera, make_float3(width, height, 0.0f));
612
613                         /* Create point clamped to frustum */
614                         float3 c;
615                         c.x = max(-v.x, min(v.x, p.x));
616                         c.y = max(-v.y, min(v.y, p.y));
617                         c.z = max(0.0f, p.z);
618
619                         float f_dist = len(p - c) / sqrtf((v.x*v.x+v.y*v.y)*0.5f);
620
621                         if(f_dist > 0.0f) {
622                                 res += res * f_dist * (offscreen_dicing_scale - 1.0f);
623                         }
624                 }
625         }
626         else if(type == CAMERA_PERSPECTIVE) {
627                 /* Calculate as if point is directly ahead of the camera. */
628                 float3 raster = make_float3(0.5f*width, 0.5f*height, 0.0f);
629                 float3 Pcamera = transform_perspective(&rastertocamera, raster);
630
631                 /* dDdx */
632                 float3 Ddiff = transform_direction(&cameratoworld, Pcamera);
633                 float3 dx = len_squared(full_dx) < len_squared(full_dy) ? full_dx : full_dy;
634                 float3 dDdx = normalize(Ddiff + dx) - normalize(Ddiff);
635
636                 /* dPdx */
637                 float dist = len(transform_point(&worldtocamera, P));
638                 float3 D = normalize(Ddiff);
639                 res = len(dist*dDdx - dot(dist*dDdx, D)*D);
640
641                 /* Decent approx distance to frustum (doesn't handle corners correctly, but not that big of a deal) */
642                 float f_dist = 0.0f;
643
644                 if(offscreen_dicing_scale > 1.0f) {
645                         float3 p = transform_point(&worldtocamera, P);
646
647                         /* Distance from the four planes */
648                         float r = dot(p, frustum_right_normal);
649                         float t = dot(p, frustum_top_normal);
650                         p = make_float3(-p.x, -p.y, p.z);
651                         float l = dot(p, frustum_right_normal);
652                         float b = dot(p, frustum_top_normal);
653                         p = make_float3(-p.x, -p.y, p.z);
654
655                         if(r <= 0.0f && l <= 0.0f && t <= 0.0f && b <= 0.0f) {
656                                 /* Point is inside frustum */
657                                 f_dist = 0.0f;
658                         }
659                         else if(r > 0.0f && l > 0.0f && t > 0.0f && b > 0.0f) {
660                                 /* Point is behind frustum */
661                                 f_dist = len(p);
662                         }
663                         else {
664                                 /* Point may be behind or off to the side, need to check */
665                                 float3 along_right = make_float3(-frustum_right_normal.z, 0.0f, frustum_right_normal.x);
666                                 float3 along_left = make_float3(frustum_right_normal.z, 0.0f, frustum_right_normal.x);
667                                 float3 along_top = make_float3(0.0f, -frustum_top_normal.z, frustum_top_normal.y);
668                                 float3 along_bottom = make_float3(0.0f, frustum_top_normal.z, frustum_top_normal.y);
669
670                                 float dist[] = {r, l, t, b};
671                                 float3 along[] = {along_right, along_left, along_top, along_bottom};
672
673                                 bool test_o = false;
674
675                                 float *d = dist;
676                                 float3 *a = along;
677                                 for(int i = 0; i < 4; i++, d++, a++) {
678                                         /* Test if we should check this side at all */
679                                         if(*d > 0.0f) {
680                                                 if(dot(p, *a) >= 0.0f) {
681                                                         /* We are in front of the back edge of this side of the frustum */
682                                                         f_dist = max(f_dist, *d);
683                                                 }
684                                                 else {
685                                                         /* Possibly far enough behind the frustum to use distance to origin instead of edge */
686                                                         test_o = true;
687                                                 }
688                                         }
689                                 }
690
691                                 if(test_o) {
692                                         f_dist = (f_dist > 0) ? min(f_dist, len(p)) : len(p);
693                                 }
694                         }
695
696                         if(f_dist > 0.0f) {
697                                 res += len(dDdx - dot(dDdx, D)*D) * f_dist * (offscreen_dicing_scale - 1.0f);
698                         }
699                 }
700         }
701         else if(type == CAMERA_PANORAMA) {
702                 float3 D = transform_point(&worldtocamera, P);
703                 float dist = len(D);
704
705                 Ray ray;
706
707                 /* Distortion can become so great that the results become meaningless, there
708                  * may be a better way to do this, but calculating differentials from the
709                  * point directly ahead seems to produce good enough results. */
710 #if 0
711                 float2 dir = direction_to_panorama(&kernel_camera, normalize(D));
712                 float3 raster = transform_perspective(&cameratoraster, make_float3(dir.x, dir.y, 0.0f));
713
714                 ray.t = 1.0f;
715                 camera_sample_panorama(&kernel_camera, raster.x, raster.y, 0.0f, 0.0f, &ray);
716                 if(ray.t == 0.0f) {
717                         /* No differentials, just use from directly ahead. */
718                         camera_sample_panorama(&kernel_camera, 0.5f*width, 0.5f*height, 0.0f, 0.0f, &ray);
719                 }
720 #else
721                 camera_sample_panorama(&kernel_camera, 0.5f*width, 0.5f*height, 0.0f, 0.0f, &ray);
722 #endif
723
724                 differential_transfer(&ray.dP, ray.dP, ray.D, ray.dD, ray.D, dist);
725
726                 return max(len(ray.dP.dx) * (float(width)/float(full_width)),
727                            len(ray.dP.dy) * (float(height)/float(full_height)));
728         }
729
730         return res;
731 }
732
733 CCL_NAMESPACE_END