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