static float blender_camera_focal_distance(BL::Object b_ob, BL::Camera b_camera)
{
	BL::Object b_dof_object = b_camera.dof_object();

	if(!b_dof_object)
		return b_camera.dof_distance();
	
	/* for dof object, return distance along camera Z direction */
	Transform obmat = transform_clear_scale(get_transform(b_ob.matrix_world()));
	Transform dofmat = get_transform(b_dof_object.matrix_world());
	Transform mat = transform_inverse(obmat) * dofmat;

	return fabsf(transform_get_column(&mat, 3).z);
}
static float blender_camera_focal_distance(BL::RenderEngine& b_engine,
                                           BL::Object& b_ob,
                                           BL::Camera& b_camera,
                                           BlenderCamera *bcam)
{
	BL::Object b_dof_object = b_camera.dof_object();

	if(!b_dof_object)
		return b_camera.dof_distance();
	
	/* for dof object, return distance along camera Z direction */
	BL::Array<float, 16> b_ob_matrix;
	b_engine.camera_model_matrix(b_ob, bcam->use_spherical_stereo, b_ob_matrix);
	Transform obmat = transform_clear_scale(get_transform(b_ob_matrix));
	Transform dofmat = get_transform(b_dof_object.matrix_world());
	float3 view_dir = normalize(transform_get_column(&obmat, 2));
	float3 dof_dir = transform_get_column(&obmat, 3) - transform_get_column(&dofmat, 3);
	return fabsf(dot(view_dir, dof_dir));
}
static Transform blender_camera_matrix(const Transform& tfm, CameraType type)
{
	Transform result;

	if(type == CAMERA_PANORAMA) {
		/* make it so environment camera needs to be pointed in the direction
		 * of the positive x-axis to match an environment texture, this way
		 * it is looking at the center of the texture */
		result = tfm *
			make_transform( 0.0f, -1.0f, 0.0f, 0.0f,
			                0.0f,  0.0f, 1.0f, 0.0f,
			               -1.0f,  0.0f, 0.0f, 0.0f,
			                0.0f,  0.0f, 0.0f, 1.0f);
	}
	else {
		/* note the blender camera points along the negative z-axis */
		result = tfm * transform_scale(1.0f, 1.0f, -1.0f);
	}

	return transform_clear_scale(result);
}
static Transform blender_camera_matrix(const Transform& tfm,
                                       const CameraType type,
                                       const PanoramaType panorama_type)
{
	Transform result;

	if(type == CAMERA_PANORAMA) {
		if(panorama_type == PANORAMA_MIRRORBALL) {
			/* Mirror ball camera is looking into the negative Y direction
			 * which matches texture mirror ball mapping.
			 */
			result = tfm *
				make_transform(1.0f, 0.0f, 0.0f, 0.0f,
				               0.0f, 0.0f, 1.0f, 0.0f,
				               0.0f, 1.0f, 0.0f, 0.0f,
				               0.0f, 0.0f, 0.0f, 1.0f);
		}
		else {
			/* Make it so environment camera needs to be pointed in the direction
			 * of the positive x-axis to match an environment texture, this way
			 * it is looking at the center of the texture
			 */
			result = tfm *
				make_transform( 0.0f, -1.0f, 0.0f, 0.0f,
				                0.0f,  0.0f, 1.0f, 0.0f,
				               -1.0f,  0.0f, 0.0f, 0.0f,
				                0.0f,  0.0f, 0.0f, 1.0f);
		}
	}
	else {
		/* note the blender camera points along the negative z-axis */
		result = tfm * transform_scale(1.0f, 1.0f, -1.0f);
	}

	return transform_clear_scale(result);
}