use sensor size when calculating dof rather then hard coded values.
authorCampbell Barton <ideasman42@gmail.com>
Thu, 9 Aug 2012 15:59:32 +0000 (15:59 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Thu, 9 Aug 2012 15:59:32 +0000 (15:59 +0000)
source/blender/blenkernel/intern/camera.c
source/blender/compositor/operations/COM_ConvertDepthToRadiusOperation.cpp
source/blender/nodes/composite/nodes/node_composite_defocus.c

index e1cfcee13672c4b77f902b4b2542357b306e2950..ed7ac0e1a32fba8e307609052af139d90dbfbb62 100644 (file)
@@ -57,8 +57,8 @@ void *BKE_camera_add(const char *name)
        cam =  BKE_libblock_alloc(&G.main->camera, ID_CA, name);
 
        cam->lens = 35.0f;
-       cam->sensor_x = 32.0f;
-       cam->sensor_y = 18.0f;
+       cam->sensor_x = DEFAULT_SENSOR_WIDTH;
+       cam->sensor_y = DEFAULT_SENSOR_HEIGHT;
        cam->clipsta = 0.1f;
        cam->clipend = 100.0f;
        cam->drawsize = 0.5f;
index e005232ec008ca33d54e363ae74c10204716ca47..efe641b1fe50ec7e889d1a314ca049d2c7eaca11 100644 (file)
@@ -22,6 +22,7 @@
 
 #include "COM_ConvertDepthToRadiusOperation.h"
 #include "BLI_math.h"
+#include "BKE_camera.h"
 #include "DNA_camera_types.h"
 
 ConvertDepthToRadiusOperation::ConvertDepthToRadiusOperation() : NodeOperation()
@@ -37,41 +38,35 @@ ConvertDepthToRadiusOperation::ConvertDepthToRadiusOperation() : NodeOperation()
 
 float ConvertDepthToRadiusOperation::determineFocalDistance()
 {
-
-       if (this->m_cameraObject == NULL || this->m_cameraObject->type != OB_CAMERA) {
-               return 10.0f;
-       }
-       else {
+       if (this->m_cameraObject && this->m_cameraObject->type == OB_CAMERA) {
                Camera *camera = (Camera *)this->m_cameraObject->data;
                this->m_cam_lens = camera->lens;
-               if (camera->dof_ob) {
-                       /* too simple, better to return the distance on the view axis only
-                        * return len_v3v3(ob->obmat[3], cam->dof_ob->obmat[3]); */
-                       float mat[4][4], imat[4][4], obmat[4][4];
-
-                       copy_m4_m4(obmat, this->m_cameraObject->obmat);
-                       normalize_m4(obmat);
-                       invert_m4_m4(imat, obmat);
-                       mult_m4_m4m4(mat, imat, camera->dof_ob->obmat);
-                       return fabsf(mat[3][2]);
-               }
-               else {
-                       return camera->YF_dofdist;
-               }
+               return BKE_camera_object_dof_distance(this->m_cameraObject);
+       }
+       else {
+               return 10.0f;
        }
 }
 
 void ConvertDepthToRadiusOperation::initExecution()
 {
+       float cam_sensor = DEFAULT_SENSOR_WIDTH;
+       Camera *camera = NULL;
+
+       if (this->m_cameraObject && this->m_cameraObject->type == OB_CAMERA) {
+               camera = (Camera *)this->m_cameraObject->data;
+               cam_sensor = BKE_camera_sensor_size(camera->sensor_fit, camera->sensor_x, camera->sensor_y);
+       }
+
        this->m_inputOperation = this->getInputSocketReader(0);
        float focalDistance = determineFocalDistance();
        if (focalDistance == 0.0f) focalDistance = 1e10f;  /* if the dof is 0.0 then set it be be far away */
-       this->m_inverseFocalDistance = 1.f / focalDistance;
+       this->m_inverseFocalDistance = 1.0f / focalDistance;
        this->m_aspect = (this->getWidth() > this->getHeight()) ? (this->getHeight() / (float)this->getWidth()) : (this->getWidth() / (float)this->getHeight());
-       this->m_aperture = 0.5f * (this->m_cam_lens / (this->m_aspect * 32.0f)) / this->m_fStop;
-       float minsz = MIN2(getWidth(), getHeight());
-       this->m_dof_sp = (float)minsz / (16.f / this->m_cam_lens);    // <- == aspect * MIN2(img->x, img->y) / tan(0.5f * fov);
-       
+       this->m_aperture = 0.5f * (this->m_cam_lens / (this->m_aspect * cam_sensor)) / this->m_fStop;
+       float minsz = min(getWidth(), getHeight());
+       this->m_dof_sp = (float)minsz / ((cam_sensor / 2.0f) / this->m_cam_lens);    // <- == aspect * MIN2(img->x, img->y) / tan(0.5f * fov);
+
        if (this->m_blurPostOperation) {
                m_blurPostOperation->setSigma(min(m_aperture * 128.0f, this->m_maxRadius));
        }
index 45d070ba8d2604468bc2d9ce01ac1a63090f4af1..280ff56be12eda816dd7b0d9c7f3c1aa2d9e129b 100644 (file)
@@ -254,6 +254,7 @@ static void defocus_blur(bNode *node, CompBuf *new, CompBuf *img, CompBuf *zbuf,
        BokehCoeffs BKH[8];     // bokeh shape data, here never > 8 pts.
        float bkh_b[4] = {0};   // shape 2D bound
        float cam_fdist=1, cam_invfdist=1, cam_lens=35;
+       float cam_sensor = DEFAULT_SENSOR_WIDTH;
        float dof_sp, maxfgc, bk_hn_theta=0, inradsq=0;
        int y, len_bkh=0, ydone = FALSE;
        float aspect, aperture;
@@ -268,17 +269,17 @@ static void defocus_blur(bNode *node, CompBuf *new, CompBuf *img, CompBuf *zbuf,
                Camera* cam = (Camera*)camob->data;
                cam_lens = cam->lens;
                cam_fdist = BKE_camera_object_dof_distance(camob);
-               if (cam_fdist==0.0f) cam_fdist = 1e10f; /* if the dof is 0.0 then set it be be far away */
-               cam_invfdist = 1.f/cam_fdist;
+               cam_sensor = BKE_camera_sensor_size(cam->sensor_fit, cam->sensor_x, cam->sensor_y);
+               if (cam_fdist == 0.0f) cam_fdist = 1e10f; /* if the dof is 0.0 then set it be be far away */
+               cam_invfdist = 1.f / cam_fdist;
        }
-
        // guess work here.. best match with raytraced result
        minsz = MIN2(img->x, img->y);
-       dof_sp = (float)minsz / (16.f / cam_lens);      // <- == aspect * MIN2(img->x, img->y) / tan(0.5f * fov);
+       dof_sp = (float)minsz / ((cam_sensor / 2.0f) / cam_lens);       // <- == aspect * MIN2(img->x, img->y) / tan(0.5f * fov);
        
        // aperture
        aspect = (img->x > img->y) ? (img->y / (float)img->x) : (img->x / (float)img->y);
-       aperture = 0.5f*(cam_lens / (aspect*32.f)) / nqd->fstop;
+       aperture = 0.5f * (cam_lens / (aspect * cam_sensor)) / nqd->fstop;
        
        // if not disk, make bokeh coefficients and other needed data
        if (nqd->bktype!=0) {