示例#1
0
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)
{
	/* 3d view parameters */
	bcam->nearclip = b_v3d.clip_start();
	bcam->farclip = b_v3d.clip_end();
	bcam->lens = b_v3d.lens();
	bcam->shuttertime = b_scene.render().motion_blur_shutter();
	curvemapping_to_array(b_scene.render().motion_blur_shutter_curve(),
	                      bcam->shutter_curve,
	                      RAMP_TABLE_SIZE);

	if(b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_CAMERA) {
		/* camera view */
		BL::Object b_ob = (b_v3d.lock_camera_and_layers())? b_scene.camera(): b_v3d.camera();

		if(b_ob) {
			blender_camera_from_object(bcam, b_engine, b_ob, skip_panorama);

			if(!skip_panorama && bcam->type == CAMERA_PANORAMA) {
				/* in panorama camera view, we map viewplane to camera border */
				BoundBox2D view_box, cam_box;

				blender_camera_view_subset(b_engine, b_scene.render(), b_scene, b_ob, b_v3d, b_rv3d, width, height,
					&view_box, &cam_box);

				bcam->pano_viewplane = view_box.make_relative_to(cam_box);
			}
			else {
				/* magic zoom formula */
				bcam->zoom = (float)b_rv3d.view_camera_zoom();
				bcam->zoom = (1.41421f + bcam->zoom/50.0f);
				bcam->zoom *= bcam->zoom;
				bcam->zoom = 2.0f/bcam->zoom;

				/* offset */
				bcam->offset = get_float2(b_rv3d.view_camera_offset());
			}
		}
	}
	else if(b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_ORTHO) {
		/* orthographic view */
		bcam->farclip *= 0.5f;
		bcam->nearclip = -bcam->farclip;

		float sensor_size;
		if(bcam->sensor_fit == BlenderCamera::VERTICAL)
			sensor_size = bcam->sensor_height;
		else
			sensor_size = bcam->sensor_width;

		bcam->type = CAMERA_ORTHOGRAPHIC;
		bcam->ortho_scale = b_rv3d.view_distance() * sensor_size / b_v3d.lens();
	}

	bcam->zoom *= 2.0f;

	/* 3d view transform */
	bcam->matrix = transform_inverse(get_transform(b_rv3d.view_matrix()));
}
BufferParams BlenderSync::get_buffer_params(BL::Scene b_scene, BL::SpaceView3D b_v3d, BL::RegionView3D b_rv3d, Camera *cam, int width, int height)
{
	BufferParams params;
	bool use_border = false;

	params.full_width = width;
	params.full_height = height;

	if(b_v3d && b_rv3d && b_rv3d.view_perspective() != BL::RegionView3D::view_perspective_CAMERA)
		use_border = b_v3d.use_render_border();
	else
		use_border = b_scene.render().use_border();

	if(use_border) {
		/* border render */
		params.full_x = cam->border.left*width;
		params.full_y = cam->border.bottom*height;
		params.width = (int)(cam->border.right*width) - params.full_x;
		params.height = (int)(cam->border.top*height) - params.full_y;

		/* survive in case border goes out of view or becomes too small */
		params.width = max(params.width, 1);
		params.height = max(params.height, 1);
	}
	else {
		params.width = width;
		params.height = height;
	}

	return params;
}
BufferParams BlenderSync::get_buffer_params(BL::RenderSettings b_render, BL::SpaceView3D b_v3d, BL::RegionView3D b_rv3d, Camera *cam, int width, int height)
{
	BufferParams params;
	bool use_border = false;

	params.full_width = width;
	params.full_height = height;

	if(b_v3d && b_rv3d && b_rv3d.view_perspective() != BL::RegionView3D::view_perspective_CAMERA)
		use_border = b_v3d.use_render_border();
	else
		use_border = b_render.use_border();

	if(use_border) {
		/* border render */
		/* the viewport may offset the border outside the view */
		BoundBox2D border = cam->border.clamp();
		params.full_x = (int)(border.left * (float)width);
		params.full_y = (int)(border.bottom * (float)height);
		params.width = (int)(border.right * (float)width) - params.full_x;
		params.height = (int)(border.top * (float)height) - params.full_y;

		/* survive in case border goes out of view or becomes too small */
		params.width = max(params.width, 1);
		params.height = max(params.height, 1);
	}
	else {
		params.width = width;
		params.height = height;
	}

	return params;
}
static void blender_camera_border(BlenderCamera *bcam, BL::RenderEngine b_engine, BL::RenderSettings b_render, BL::Scene b_scene, BL::SpaceView3D b_v3d,
	BL::RegionView3D b_rv3d, int width, int height)
{
	bool is_camera_view;

	/* camera view? */
	is_camera_view = b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_CAMERA;

	if(!is_camera_view) {
		/* 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
		 */
		if(b_v3d.use_render_border()) {
			bcam->border.left = b_v3d.render_border_min_x();
			bcam->border.right = b_v3d.render_border_max_x();
			bcam->border.bottom = b_v3d.render_border_min_y();
			bcam->border.top = b_v3d.render_border_max_y();
		}
		return;
	}

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

	if(!b_ob)
		return;

	/* Determine camera border inside the viewport. */
	BoundBox2D full_border;
	blender_camera_border_subset(b_engine,
	                             b_render,
	                             b_scene,
	                             b_v3d,
	                             b_rv3d,
	                             b_ob,
	                             width, height,
	                             full_border,
	                             &bcam->viewport_camera_border);

	if(!b_render.use_border()) {
		return;
	}

	bcam->border.left = b_render.border_min_x();
	bcam->border.right = b_render.border_max_x();
	bcam->border.bottom = b_render.border_min_y();
	bcam->border.top = b_render.border_max_y();

	/* Determine viewport subset matching camera border. */
	blender_camera_border_subset(b_engine,
	                             b_render,
	                             b_scene,
	                             b_v3d,
	                             b_rv3d,
	                             b_ob,
	                             width, height,
	                             bcam->border,
	                             &bcam->border);
	bcam->border.clamp();
}
static void blender_camera_border(BlenderCamera *bcam, BL::Scene b_scene, BL::SpaceView3D b_v3d,
	BL::RegionView3D b_rv3d, int width, int height)
{
	BL::RenderSettings r = b_scene.render();
	bool is_camera_view;

	/* camera view? */
	is_camera_view = b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_CAMERA;

	if(!is_camera_view) {
		/* 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
		 */
		if(b_v3d.use_render_border()) {
			bcam->border.left = b_v3d.render_border_min_x();
			bcam->border.right = b_v3d.render_border_max_x();
			bcam->border.bottom = b_v3d.render_border_min_y();
			bcam->border.top = b_v3d.render_border_max_y();

			return;
		}
	}
	else if(!r.use_border())
		return;

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

	if(!b_ob)
		return;

	bcam->border.left = r.border_min_x();
	bcam->border.right = r.border_max_x();
	bcam->border.bottom = r.border_min_y();
	bcam->border.top = r.border_max_y();

	/* determine camera viewport subset */
	BoundBox2D view_box, cam_box;

	blender_camera_view_subset(b_scene, b_ob, b_v3d, b_rv3d, width, height,
		&view_box, &cam_box);

	/* determine viewport subset matching camera border */
	cam_box = cam_box.make_relative_to(view_box);
	bcam->border = cam_box.subset(bcam->border).clamp();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Fill the Octane Camera properties from Blender View data
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void BlenderSync::load_camera_from_view(Camera* cam, BL::Scene b_scene, BL::SpaceView3D b_v3d, BL::RegionView3D b_rv3d, int width, int height, float2& offset, bool skip_panorama) {
    float zoom;

    if(b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_CAMERA) {
        BL::Object b_ob = (b_v3d.lock_camera_and_layers()) ? b_scene.camera() : b_v3d.camera();

        if(b_ob) {
            cam->matrix = scene->matrix * get_transform(b_ob.matrix_world());

            // Magic zoom formula
            zoom = (float) b_rv3d.view_camera_zoom();
            zoom = (1.41421f + zoom/50.0f);
            zoom *= zoom;
            zoom = 2.0f/zoom;
            zoom *= 2.0f;

            cam->zoom = zoom;

            offset = get_float2(b_rv3d.view_camera_offset());

            load_camera_from_object(cam, b_ob, width, height, offset, skip_panorama);
        }
    } //if(b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_CAMERA)
    else if(b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_ORTHO || b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_PERSP) {
        cam->zoom = 2.0f;

        cam->near_clip_depth    = b_v3d.clip_start();
        cam->far_clip_depth     = b_v3d.clip_end();

        cam->matrix = scene->matrix * transform_inverse(get_transform(b_rv3d.view_matrix()));

        cam->type = CAMERA_PERSPECTIVE;
        if(b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_ORTHO)
            cam->ortho = true;
        else
            cam->ortho = false;

        PointerRNA oct_camera = RNA_pointer_get(&b_scene.ptr, "oct_view_cam");
        get_cam_settings(cam, oct_camera, true);

        cam->lens_shift_x   = 0;
        cam->lens_shift_y   = 0;

        cam->sensorwidth    = 32.0f;
        cam->sensorheight   = 18.0f;
        cam->sensor_fit     = Camera::AUTO;

        if(cam->ortho) {
            float ortho_scale;
            get_viewport_ortho_scale(cam, b_rv3d.view_distance(), b_v3d.lens(), width, height, &ortho_scale);
            cam->fov = ortho_scale * cam->zoom;
        }
        else {
            float sensor_size;
            get_camera_sensor_size(cam, width, height, &sensor_size);
            cam->fov = 2.0f * atanf((0.5f * sensor_size * cam->zoom) / b_v3d.lens()) *180.0f / M_PI_F;
        }

        // Position
        cam->look_at.x = cam->eye_point.x = cam->matrix.x.w;
        cam->look_at.y = cam->eye_point.y = cam->matrix.y.w;
        cam->look_at.z = cam->eye_point.z = cam->matrix.z.w;

        if(cam->ortho) {
            float3 dir = transform_direction(&cam->matrix, make_float3(0.0f, 0.0f, b_rv3d.view_distance()));
            cam->eye_point.x = cam->eye_point.x + dir.x;
            cam->eye_point.y = cam->eye_point.y + dir.y;
            cam->eye_point.z = cam->eye_point.z + dir.z;
        }
        else {
            float3 dir = transform_direction(&cam->matrix, make_float3(0.0f, 0.0f, -1.0f));
            cam->look_at.x = cam->look_at.x + dir.x;
            cam->look_at.y = cam->look_at.y + dir.y;
            cam->look_at.z = cam->look_at.z + dir.z;
        }
        cam->up = normalize(transform_direction(&cam->matrix, make_float3(0.0f, 1.0f, 0.0f)));

    } //else if(b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_ORTHO || b_rv3d.view_perspective() == BL::RegionView3D::view_perspective_PERSP)

    get_camera_border(cam, b_v3d, b_rv3d, width, height);
} //load_camera_from_view()
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// 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()