CCL_NAMESPACE_BEGIN

BlenderSession::BlenderSession(BL::RenderEngine b_engine_, BL::UserPreferences b_userpref_,
	BL::BlendData b_data_, BL::Scene b_scene_)
: b_engine(b_engine_), b_userpref(b_userpref_), b_data(b_data_), b_scene(b_scene_),
  b_v3d(PointerRNA_NULL), b_rv3d(PointerRNA_NULL),
  b_rr(PointerRNA_NULL), b_rlay(PointerRNA_NULL)
{
	/* offline render */
	BL::RenderSettings r = b_scene.render();

	width = (int)(r.resolution_x()*r.resolution_percentage()/100);
	height = (int)(r.resolution_y()*r.resolution_percentage()/100);
	background = true;
	last_redraw_time = 0.0f;

	create_session();
}
static void blender_camera_init(BlenderCamera *bcam, BL::Scene b_scene)
{
	memset(bcam, 0, sizeof(BlenderCamera));

	bcam->type = CAMERA_PERSPECTIVE;
	bcam->zoom = 1.0f;
	bcam->pixelaspect = make_float2(1.0f, 1.0f);
	bcam->sensor_width = 32.0f;
	bcam->sensor_height = 18.0f;
	bcam->sensor_fit = BlenderCamera::AUTO;
	bcam->shuttertime = 1.0f;
	bcam->border.right = 1.0f;
	bcam->border.top = 1.0f;
	bcam->pano_viewplane.right = 1.0f;
	bcam->pano_viewplane.top = 1.0f;

	/* render resolution */
	BL::RenderSettings r = b_scene.render();

	bcam->full_width = (int)(r.resolution_x()*r.resolution_percentage()/100);
	bcam->full_height = (int)(r.resolution_y()*r.resolution_percentage()/100);
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Sync rendered View from blender scene to Octane camera data
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BlenderSync::get_camera_border(Camera *cam, BL::SpaceView3D b_v3d, BL::RegionView3D b_rv3d, int width, int height) {
    BL::RenderSettings r = b_scene.render();

    /* camera view? */
    if(b_rv3d.view_perspective() != BL::RegionView3D::view_perspective_CAMERA) {
        /* for non-camera view check whether render border is enabled for viewport
        * and if so use border from 3d viewport assume viewport has got correctly clamped border already */
        cam->use_border = b_v3d.use_render_border();
        if(cam->use_border) {
            cam->border.x = (uint32_t)(b_v3d.render_border_min_x() * (float)width);
            cam->border.y = (uint32_t)((1.0f - b_v3d.render_border_max_y()) * (float)height);
            cam->border.z = (uint32_t)(b_v3d.render_border_max_x() * (float)width);
            cam->border.w = (uint32_t)((1.0f - b_v3d.render_border_min_y()) * (float)height);
            return;
        }
    }
    else {
        cam->use_border = r.use_border();
        if(!cam->use_border) return;

        BL::Object b_ob = (b_v3d.lock_camera_and_layers()) ? b_scene.camera() : b_v3d.camera();
        if(!b_ob) return;

        float aspectratio, xaspect, yaspect;
        bool horizontal_fit;

        // Get View plane
        float xratio = (float)width * cam->pixelaspect.x;
        float yratio = (float)height * cam->pixelaspect.y;

        if(cam->sensor_fit == Camera::AUTO)             horizontal_fit = (xratio > yratio);
        else if(cam->sensor_fit == Camera::HORIZONTAL)  horizontal_fit = true;
        else                                            horizontal_fit = false;

        if(horizontal_fit) {
            aspectratio = xratio / yratio;
            xaspect     = aspectratio;
            yaspect     = 1.0f;
        }
        else {
            aspectratio = yratio / xratio;
            xaspect     = 1.0f;
            yaspect     = aspectratio;
        }

        BoundBox2D view_box(-xaspect, xaspect, -yaspect, yaspect);
        view_box = view_box * cam->zoom;

        //float view_dx = 2.0f * (aspectratio * cam->lens_shift_x + cam->offset_x * xaspect * 2.0f);
        //float view_dy = 2.0f * (aspectratio * cam->lens_shift_y + cam->offset_y * yaspect * 2.0f);

        //view_box.left   += view_dx;
        //view_box.right  += view_dx;
        //view_box.bottom += view_dy;
        //view_box.top    += view_dy;
        view_box = view_box  / aspectratio;

        // Get camera plane
        BL::ID b_ob_data = b_ob.data();
        BL::Camera b_camera(b_ob_data);

        xratio = (float)r.resolution_x() * r.resolution_percentage() / 100;
        yratio = (float)r.resolution_y() * r.resolution_percentage() / 100;

        if(b_camera.sensor_fit() == BL::Camera::sensor_fit_AUTO)            horizontal_fit = (xratio > yratio);
        else if(b_camera.sensor_fit() == BL::Camera::sensor_fit_HORIZONTAL) horizontal_fit = true;
        else                                                                horizontal_fit = false;

        if(horizontal_fit) {
            aspectratio = xratio / yratio;
            xaspect     = aspectratio;
            yaspect     = 1.0f;
        }
        else {
            aspectratio = yratio / xratio;
            xaspect     = 1.0f;
            yaspect     = aspectratio;
        }

        BoundBox2D cam_box(-xaspect, xaspect, -yaspect, yaspect);

        //float cam_dx = 2.0f * aspectratio * b_camera.shift_x();
        //float cam_dy = 2.0f * aspectratio * b_camera.shift_y();

        //cam_box.left    += cam_dx;
        //cam_box.right   += cam_dx;
        //cam_box.bottom  += cam_dy;
        //cam_box.top     += cam_dy;
        cam_box = cam_box / aspectratio;

        // Get render region
        cam_box = cam_box.make_relative_to(view_box);
        BoundBox2D orig_border(r.border_min_x(), r.border_max_x(), r.border_min_y(), r.border_max_y());
        BoundBox2D border = cam_box.subset(orig_border).clamp();

        cam->border.x = (uint32_t)(border.left * (float)width);
        cam->border.y = (uint32_t)((1.0f - border.top) * (float)height);
        cam->border.z = (uint32_t)(border.right * (float)width);
        cam->border.w = (uint32_t)((1.0f - border.bottom) * (float)height);
    }
} //get_camera_border()