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