b06ece049f1db0f208ca2c4da7c533dcb86ba49b
[blender.git] / intern / cycles / blender / blender_camera.cpp
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 #include "camera.h"
20 #include "scene.h"
21
22 #include "blender_sync.h"
23 #include "blender_util.h"
24
25 CCL_NAMESPACE_BEGIN
26
27 /* Blender Camera Intermediate: we first convert both the offline and 3d view
28  * render camera to this, and from there convert to our native camera format. */
29
30 struct BlenderCamera {
31         float nearclip;
32         float farclip;
33
34         CameraType type;
35         float ortho_scale;
36
37         float lens;
38         float shuttertime;
39
40         float aperturesize;
41         uint apertureblades;
42         float aperturerotation;
43         float focaldistance;
44
45         float2 shift;
46         float2 offset;
47         float zoom;
48
49         float2 pixelaspect;
50
51         PanoramaType panorama_type;
52         float fisheye_fov;
53         float fisheye_lens;
54
55         enum { AUTO, HORIZONTAL, VERTICAL } sensor_fit;
56         float sensor_width;
57         float sensor_height;
58
59         int full_width;
60         int full_height;
61
62         BoundBox2D border;
63         BoundBox2D pano_viewplane;
64
65         Transform matrix;
66 };
67
68 static void blender_camera_init(BlenderCamera *bcam, BL::RenderSettings b_render, BL::Scene b_scene)
69 {
70         memset(bcam, 0, sizeof(BlenderCamera));
71
72         bcam->type = CAMERA_PERSPECTIVE;
73         bcam->zoom = 1.0f;
74         bcam->pixelaspect = make_float2(1.0f, 1.0f);
75         bcam->sensor_width = 32.0f;
76         bcam->sensor_height = 18.0f;
77         bcam->sensor_fit = BlenderCamera::AUTO;
78         bcam->shuttertime = 1.0f;
79         bcam->border.right = 1.0f;
80         bcam->border.top = 1.0f;
81         bcam->pano_viewplane.right = 1.0f;
82         bcam->pano_viewplane.top = 1.0f;
83
84         /* render resolution */
85         bcam->full_width = (int)(b_render.resolution_x()*b_render.resolution_percentage()/100);
86         bcam->full_height = (int)(b_render.resolution_y()*b_render.resolution_percentage()/100);
87 }
88
89 static float blender_camera_focal_distance(BL::Object b_ob, BL::Camera b_camera)
90 {
91         BL::Object b_dof_object = b_camera.dof_object();
92
93         if(!b_dof_object)
94                 return b_camera.dof_distance();
95         
96         /* for dof object, return distance along camera Z direction */
97         Transform obmat = transform_clear_scale(get_transform(b_ob.matrix_world()));
98         Transform dofmat = get_transform(b_dof_object.matrix_world());
99         Transform mat = transform_inverse(obmat) * dofmat;
100
101         return fabsf(transform_get_column(&mat, 3).z);
102 }
103
104 static void blender_camera_from_object(BlenderCamera *bcam, BL::Object b_ob, bool skip_panorama = false)
105 {
106         BL::ID b_ob_data = b_ob.data();
107
108         if(b_ob_data.is_a(&RNA_Camera)) {
109                 BL::Camera b_camera(b_ob_data);
110                 PointerRNA ccamera = RNA_pointer_get(&b_camera.ptr, "cycles");
111
112                 bcam->nearclip = b_camera.clip_start();
113                 bcam->farclip = b_camera.clip_end();
114
115                 switch(b_camera.type())
116                 {
117                         case BL::Camera::type_ORTHO:
118                                 bcam->type = CAMERA_ORTHOGRAPHIC;
119                                 break;
120                         case BL::Camera::type_PANO:
121                                 if(!skip_panorama)
122                                         bcam->type = CAMERA_PANORAMA;
123                                 else
124                                         bcam->type = CAMERA_PERSPECTIVE;
125                                 break;
126                         case BL::Camera::type_PERSP:
127                         default:
128                                 bcam->type = CAMERA_PERSPECTIVE;
129                                 break;
130                 }       
131
132                 switch(RNA_enum_get(&ccamera, "panorama_type"))
133                 {
134                         case 1:
135                                 bcam->panorama_type = PANORAMA_FISHEYE_EQUIDISTANT;
136                                 break;
137                         case 2:
138                                 bcam->panorama_type = PANORAMA_FISHEYE_EQUISOLID;
139                                 break;
140                         case 0:
141                         default:
142                                 bcam->panorama_type = PANORAMA_EQUIRECTANGULAR;
143                                 break;
144                 }       
145
146                 bcam->fisheye_fov = RNA_float_get(&ccamera, "fisheye_fov");
147                 bcam->fisheye_lens = RNA_float_get(&ccamera, "fisheye_lens");
148
149                 bcam->ortho_scale = b_camera.ortho_scale();
150
151                 bcam->lens = b_camera.lens();
152
153                 /* allow f/stop number to change aperture_size but still
154                  * give manual control over aperture radius */
155                 int aperture_type = RNA_enum_get(&ccamera, "aperture_type");
156
157                 if(aperture_type == 1) {
158                         float fstop = RNA_float_get(&ccamera, "aperture_fstop");
159                         bcam->aperturesize = (bcam->lens*1e-3f)/(2.0f*max(fstop, 1e-5f));
160                 }
161                 else
162                         bcam->aperturesize = RNA_float_get(&ccamera, "aperture_size");
163
164                 bcam->apertureblades = RNA_int_get(&ccamera, "aperture_blades");
165                 bcam->aperturerotation = RNA_float_get(&ccamera, "aperture_rotation");
166                 bcam->focaldistance = blender_camera_focal_distance(b_ob, b_camera);
167
168                 bcam->shift.x = b_camera.shift_x();
169                 bcam->shift.y = b_camera.shift_y();
170
171                 bcam->sensor_width = b_camera.sensor_width();
172                 bcam->sensor_height = b_camera.sensor_height();
173
174                 if(b_camera.sensor_fit() == BL::Camera::sensor_fit_AUTO)
175                         bcam->sensor_fit = BlenderCamera::AUTO;
176                 else if(b_camera.sensor_fit() == BL::Camera::sensor_fit_HORIZONTAL)
177                         bcam->sensor_fit = BlenderCamera::HORIZONTAL;
178                 else
179                         bcam->sensor_fit = BlenderCamera::VERTICAL;
180         }
181         else {
182                 /* from lamp not implemented yet */
183         }
184 }
185
186 static Transform blender_camera_matrix(const Transform& tfm, CameraType type)
187 {
188         Transform result;
189
190         if(type == CAMERA_PANORAMA) {
191                 /* make it so environment camera needs to be pointed in the direction
192                  * of the positive x-axis to match an environment texture, this way
193                  * it is looking at the center of the texture */
194                 result = tfm *
195                         make_transform( 0.0f, -1.0f, 0.0f, 0.0f,
196                                         0.0f,  0.0f, 1.0f, 0.0f,
197                                        -1.0f,  0.0f, 0.0f, 0.0f,
198                                         0.0f,  0.0f, 0.0f, 1.0f);
199         }
200         else {
201                 /* note the blender camera points along the negative z-axis */
202                 result = tfm * transform_scale(1.0f, 1.0f, -1.0f);
203         }
204
205         return transform_clear_scale(result);
206 }
207
208 static void blender_camera_viewplane(BlenderCamera *bcam, int width, int height,
209         BoundBox2D *viewplane, float *aspectratio, float *sensor_size)
210 {
211         /* dimensions */
212         float xratio = width*bcam->pixelaspect.x;
213         float yratio = height*bcam->pixelaspect.y;
214
215         /* compute x/y aspect and ratio */
216         float xaspect, yaspect;
217         bool horizontal_fit;
218
219         /* sensor fitting */
220         if(bcam->sensor_fit == BlenderCamera::AUTO) {
221                 horizontal_fit = (xratio > yratio);
222                 *sensor_size = bcam->sensor_width;
223         }
224         else if(bcam->sensor_fit == BlenderCamera::HORIZONTAL) {
225                 horizontal_fit = true;
226                 *sensor_size = bcam->sensor_width;
227         }
228         else {
229                 horizontal_fit = false;
230                 *sensor_size = bcam->sensor_height;
231         }
232
233         if(horizontal_fit) {
234                 *aspectratio = xratio/yratio;
235                 xaspect = *aspectratio;
236                 yaspect = 1.0f;
237         }
238         else {
239                 *aspectratio = yratio/xratio;
240                 xaspect = 1.0f;
241                 yaspect = *aspectratio;
242         }
243
244         /* modify aspect for orthographic scale */
245         if(bcam->type == CAMERA_ORTHOGRAPHIC) {
246                 xaspect = xaspect*bcam->ortho_scale/(*aspectratio*2.0f);
247                 yaspect = yaspect*bcam->ortho_scale/(*aspectratio*2.0f);
248                 *aspectratio = bcam->ortho_scale/2.0f;
249         }
250
251         if(bcam->type == CAMERA_PANORAMA) {
252                 /* set viewplane */
253                 *viewplane = bcam->pano_viewplane;
254         }
255         else {
256                 /* set viewplane */
257                 viewplane->left = -xaspect;
258                 viewplane->right = xaspect;
259                 viewplane->bottom = -yaspect;
260                 viewplane->top = yaspect;
261
262                 /* zoom for 3d camera view */
263                 *viewplane = (*viewplane) * bcam->zoom;
264
265                 /* modify viewplane with camera shift and 3d camera view offset */
266                 float dx = 2.0f*(*aspectratio*bcam->shift.x + bcam->offset.x*xaspect*2.0f);
267                 float dy = 2.0f*(*aspectratio*bcam->shift.y + bcam->offset.y*yaspect*2.0f);
268
269                 viewplane->left += dx;
270                 viewplane->right += dx;
271                 viewplane->bottom += dy;
272                 viewplane->top += dy;
273         }
274 }
275
276 static void blender_camera_sync(Camera *cam, BlenderCamera *bcam, int width, int height)
277 {
278         /* copy camera to compare later */
279         Camera prevcam = *cam;
280         float aspectratio, sensor_size;
281
282         /* viewplane */
283         blender_camera_viewplane(bcam, width, height,
284                 &cam->viewplane, &aspectratio, &sensor_size);
285
286         /* panorama sensor */
287         if (bcam->type == CAMERA_PANORAMA && bcam->panorama_type == PANORAMA_FISHEYE_EQUISOLID) {
288                 float fit_xratio = bcam->full_width*bcam->pixelaspect.x;
289                 float fit_yratio = bcam->full_height*bcam->pixelaspect.y;
290                 bool horizontal_fit;
291                 float sensor_size;
292
293                 if(bcam->sensor_fit == BlenderCamera::AUTO) {
294                         horizontal_fit = (fit_xratio > fit_yratio);
295                         sensor_size = bcam->sensor_width;
296                 }
297                 else if(bcam->sensor_fit == BlenderCamera::HORIZONTAL) {
298                         horizontal_fit = true;
299                         sensor_size = bcam->sensor_width;
300                 }
301                 else { /* vertical */
302                         horizontal_fit = false;
303                         sensor_size = bcam->sensor_height;
304                 }
305
306                 if(horizontal_fit) {
307                         cam->sensorwidth = sensor_size;
308                         cam->sensorheight = sensor_size * fit_yratio / fit_xratio;
309                 }
310                 else {
311                         cam->sensorwidth = sensor_size * fit_xratio / fit_yratio;
312                         cam->sensorheight = sensor_size;
313                 }
314         }
315
316         /* clipping distances */
317         cam->nearclip = bcam->nearclip;
318         cam->farclip = bcam->farclip;
319
320         /* type */
321         cam->type = bcam->type;
322
323         /* panorama */
324         cam->panorama_type = bcam->panorama_type;
325         cam->fisheye_fov = bcam->fisheye_fov;
326         cam->fisheye_lens = bcam->fisheye_lens;
327
328         /* perspective */
329         cam->fov = 2.0f * atanf((0.5f * sensor_size) / bcam->lens / aspectratio);
330         cam->focaldistance = bcam->focaldistance;
331         cam->aperturesize = bcam->aperturesize;
332         cam->blades = bcam->apertureblades;
333         cam->bladesrotation = bcam->aperturerotation;
334
335         /* transform */
336         cam->matrix = blender_camera_matrix(bcam->matrix, bcam->type);
337         cam->motion.pre = cam->matrix;
338         cam->motion.post = cam->matrix;
339         cam->use_motion = false;
340         cam->shuttertime = bcam->shuttertime;
341
342         /* border */
343         cam->border = bcam->border;
344
345         /* set update flag */
346         if(cam->modified(prevcam))
347                 cam->tag_update();
348 }
349
350 /* Sync Render Camera */
351
352 void BlenderSync::sync_camera(BL::RenderSettings b_render, BL::Object b_override, int width, int height)
353 {
354         BlenderCamera bcam;
355         blender_camera_init(&bcam, b_render, b_scene);
356
357         /* pixel aspect */
358         bcam.pixelaspect.x = b_render.pixel_aspect_x();
359         bcam.pixelaspect.y = b_render.pixel_aspect_y();
360         bcam.shuttertime = b_render.motion_blur_shutter();
361
362         /* border */
363         if(b_render.use_border()) {
364                 bcam.border.left = b_render.border_min_x();
365                 bcam.border.right = b_render.border_max_x();
366                 bcam.border.bottom = b_render.border_min_y();
367                 bcam.border.top = b_render.border_max_y();
368         }
369
370         /* camera object */
371         BL::Object b_ob = b_scene.camera();
372
373         if(b_override)
374                 b_ob = b_override;
375
376         if(b_ob) {
377                 blender_camera_from_object(&bcam, b_ob);
378                 bcam.matrix = get_transform(b_ob.matrix_world());
379         }
380
381         /* sync */
382         Camera *cam = scene->camera;
383         blender_camera_sync(cam, &bcam, width, height);
384 }
385
386 void BlenderSync::sync_camera_motion(BL::Object b_ob, int motion)
387 {
388         Camera *cam = scene->camera;
389
390         Transform tfm = get_transform(b_ob.matrix_world());
391         tfm = blender_camera_matrix(tfm, cam->type);
392
393         if(tfm != cam->matrix) {
394                 if(motion == -1)
395                         cam->motion.pre = tfm;
396                 else
397                         cam->motion.post = tfm;
398
399                 cam->use_motion = true;
400         }
401 }
402
403 /* Sync 3D View Camera */
404
405 static void blender_camera_view_subset(BL::RenderSettings b_render, BL::Scene b_scene, BL::Object b_ob, BL::SpaceView3D b_v3d,
406         BL::RegionView3D b_rv3d, int width, int height, BoundBox2D *view_box, BoundBox2D *cam_box);
407
408 static void blender_camera_from_view(BlenderCamera *bcam, BL::Scene b_scene, BL::SpaceView3D b_v3d, BL::RegionView3D b_rv3d, int width, int height, bool skip_panorama = false)
409 {
410         /* 3d view parameters */
411         bcam->nearclip = b_v3d.clip_start();
412         bcam->farclip = b_v3d.clip_end();
413         bcam->lens = b_v3d.lens();
414         bcam->shuttertime = b_scene.render().motion_blur_shutter();
415
416         if(b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_CAMERA) {
417                 /* camera view */
418                 BL::Object b_ob = (b_v3d.lock_camera_and_layers())? b_scene.camera(): b_v3d.camera();
419
420                 if(b_ob) {
421                         blender_camera_from_object(bcam, b_ob, skip_panorama);
422
423                         if(!skip_panorama && bcam->type == CAMERA_PANORAMA) {
424                                 /* in panorama camera view, we map viewplane to camera border */
425                                 BoundBox2D view_box, cam_box;
426
427                                 blender_camera_view_subset(b_scene.render(), b_scene, b_ob, b_v3d, b_rv3d, width, height,
428                                         &view_box, &cam_box);
429
430                                 bcam->pano_viewplane = view_box.make_relative_to(cam_box);
431                         }
432                         else {
433                                 /* magic zoom formula */
434                                 bcam->zoom = (float)b_rv3d.view_camera_zoom();
435                                 bcam->zoom = (1.41421f + bcam->zoom/50.0f);
436                                 bcam->zoom *= bcam->zoom;
437                                 bcam->zoom = 2.0f/bcam->zoom;
438
439                                 /* offset */
440                                 bcam->offset = get_float2(b_rv3d.view_camera_offset());
441                         }
442                 }
443         }
444         else if(b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_ORTHO) {
445                 /* orthographic view */
446                 bcam->farclip *= 0.5f;
447                 bcam->nearclip = -bcam->farclip;
448
449                 float sensor_size;
450                 if(bcam->sensor_fit == BlenderCamera::VERTICAL)
451                         sensor_size = bcam->sensor_height;
452                 else
453                         sensor_size = bcam->sensor_width;
454
455                 bcam->type = CAMERA_ORTHOGRAPHIC;
456                 bcam->ortho_scale = b_rv3d.view_distance() * sensor_size / b_v3d.lens();
457         }
458
459         bcam->zoom *= 2.0f;
460
461         /* 3d view transform */
462         bcam->matrix = transform_inverse(get_transform(b_rv3d.view_matrix()));
463 }
464
465 static void blender_camera_view_subset(BL::RenderSettings b_render, BL::Scene b_scene, BL::Object b_ob, BL::SpaceView3D b_v3d,
466         BL::RegionView3D b_rv3d, int width, int height, BoundBox2D *view_box, BoundBox2D *cam_box)
467 {
468         BoundBox2D cam, view;
469         float view_aspect, cam_aspect, sensor_size;
470
471         /* get viewport viewplane */
472         BlenderCamera view_bcam;
473         blender_camera_init(&view_bcam, b_render, b_scene);
474         blender_camera_from_view(&view_bcam, b_scene, b_v3d, b_rv3d, width, height, true);
475
476         blender_camera_viewplane(&view_bcam, width, height,
477                 &view, &view_aspect, &sensor_size);
478
479         /* get camera viewplane */
480         BlenderCamera cam_bcam;
481         blender_camera_init(&cam_bcam, b_render, b_scene);
482         blender_camera_from_object(&cam_bcam, b_ob, true);
483
484         blender_camera_viewplane(&cam_bcam, cam_bcam.full_width, cam_bcam.full_height,
485                 &cam, &cam_aspect, &sensor_size);
486         
487         /* return */
488         *view_box = view * (1.0f/view_aspect);
489         *cam_box = cam * (1.0f/cam_aspect);
490 }
491
492 static void blender_camera_border(BlenderCamera *bcam, BL::RenderSettings b_render, BL::Scene b_scene, BL::SpaceView3D b_v3d,
493         BL::RegionView3D b_rv3d, int width, int height)
494 {
495         bool is_camera_view;
496
497         /* camera view? */
498         is_camera_view = b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_CAMERA;
499
500         if(!is_camera_view) {
501                 /* for non-camera view check whether render border is enabled for viewport
502                  * and if so use border from 3d viewport
503                  * assume viewport has got correctly clamped border already
504                  */
505                 if(b_v3d.use_render_border()) {
506                         bcam->border.left = b_v3d.render_border_min_x();
507                         bcam->border.right = b_v3d.render_border_max_x();
508                         bcam->border.bottom = b_v3d.render_border_min_y();
509                         bcam->border.top = b_v3d.render_border_max_y();
510
511                         return;
512                 }
513         }
514         else if(!b_render.use_border())
515                 return;
516
517         BL::Object b_ob = (b_v3d.lock_camera_and_layers())? b_scene.camera(): b_v3d.camera();
518
519         if(!b_ob)
520                 return;
521
522         bcam->border.left = b_render.border_min_x();
523         bcam->border.right = b_render.border_max_x();
524         bcam->border.bottom = b_render.border_min_y();
525         bcam->border.top = b_render.border_max_y();
526
527         /* determine camera viewport subset */
528         BoundBox2D view_box, cam_box;
529
530         blender_camera_view_subset(b_render, b_scene, b_ob, b_v3d, b_rv3d, width, height,
531                 &view_box, &cam_box);
532
533         /* determine viewport subset matching camera border */
534         cam_box = cam_box.make_relative_to(view_box);
535         bcam->border = cam_box.subset(bcam->border).clamp();
536 }
537
538 void BlenderSync::sync_view(BL::SpaceView3D b_v3d, BL::RegionView3D b_rv3d, int width, int height)
539 {
540         BlenderCamera bcam;
541         blender_camera_init(&bcam, b_scene.render(), b_scene);
542         blender_camera_from_view(&bcam, b_scene, b_v3d, b_rv3d, width, height);
543         blender_camera_border(&bcam, b_scene.render(), b_scene, b_v3d, b_rv3d, width, height);
544
545         blender_camera_sync(scene->camera, &bcam, width, height);
546 }
547
548 BufferParams BlenderSync::get_buffer_params(BL::RenderSettings b_render, BL::Scene b_scene, BL::SpaceView3D b_v3d, BL::RegionView3D b_rv3d, Camera *cam, int width, int height)
549 {
550         BufferParams params;
551         bool use_border = false;
552
553         params.full_width = width;
554         params.full_height = height;
555
556         if(b_v3d && b_rv3d && b_rv3d.view_perspective() != BL::RegionView3D::view_perspective_CAMERA)
557                 use_border = b_v3d.use_render_border();
558         else
559                 use_border = b_render.use_border();
560
561         if(use_border) {
562                 /* border render */
563                 params.full_x = cam->border.left*width;
564                 params.full_y = cam->border.bottom*height;
565                 params.width = (int)(cam->border.right*width) - params.full_x;
566                 params.height = (int)(cam->border.top*height) - params.full_y;
567
568                 /* survive in case border goes out of view or becomes too small */
569                 params.width = max(params.width, 1);
570                 params.height = max(params.height, 1);
571         }
572         else {
573                 params.width = width;
574                 params.height = height;
575         }
576
577         return params;
578 }
579
580 CCL_NAMESPACE_END
581