OpenCasCade(OCCT) 7.7.0 初探(六) 动画(C#、C++/CLI)

动画分两种,一种是图形的动画,也就是是直接移动或旋转图形产生的动画,另一种是相机的动画,图形不动而是相机移动绕图形旋转而产生的动画效果。懒得找录屏软件,用win11本身的录屏还得转换成gif,所以,只弄了一个图。

  • 图形动画

蓝色到红色为移动,红色到绿色为旋转,不加动画时没用问题,加入动画后,移动没问题,但旋转时会脱离旋转轴,但最终位置正确,如果是在原点旋转就不会脱离旋转轴,在其它任何坐标位置都会脱离旋转轴。不知道是bug还是我的代码有问题,请不吝指教,谢谢!新版本7.8中加入了绕轴旋转动画,不知道是否因为脱离旋转轴的修补?找到了脱离旋转轴的具体答案(由于广义gp_Trsf定义为 3x3 矩阵和平移向量,因此由轴和旋转角度定义的变换最终会成为围绕对象中心的平移和旋转。在这种情况下,原始意图(围绕特定轴旋转)丢失了,尽管最终转换是相同的,这在按设计使用gp_Trsf时不会有任何问题。但是,当使用旋转变换通过AIS_AnimationObject定义动画时,这种差异会导致意外的过渡。)

	void testAnimationObject()
	{
		//在0,0,0显示一个坐标
		gp_Pnt gps(0, 0, 0);
		gp_Ax2 ax2 = gp_Ax2(gps, gp_Dir(0, 0, 1));
		DisplayTrihedron(ax2);
		//从原点0,0,0和X,Y,Z轴方向绘制一个矩形
		TopoDS_Shape tshapea = BRepPrimAPI_MakeBox(100, 50, 80);
		//显示蓝色的矩形
		Handle(AIS_Shape) ashapea = new AIS_Shape(tshapea);
		ashapea->SetColor(Quantity_NOC_BLUE);
		myAISContext()->Display(ashapea, true);
		//将矩形移动
		gp_Pnt gpe(110, 60, 90);
		gp_Trsf trsfa;
		trsfa.SetTranslation(gps, gpe);
		AnimationObject(ashapea, trsfa);
		//旋转时,注意参考轴的变化
		myAISContext()->Remove(ashapea, true);
		tshapea = tshapea.Moved(trsfa, true);
		Handle(AIS_Shape) ashapeb = new AIS_Shape(tshapea);
		ashapeb->SetColor(Quantity_NOC_RED);
		myAISContext()->Display(ashapeb, true);
		ax2 = gp_Ax2(gpe, gp_Dir(0, 0, 1));
		DisplayTrihedron(ax2);
		gp_Trsf trsfb;
		trsfb.SetRotation(ax2.Axis(), AngleChange(90, true));
		AnimationObject(ashapeb, trsfb);

	}

	void AnimationObject(Handle(AIS_Shape) shape, gp_Trsf eTrsf)
	{
		Handle(AIS_Animation) ais_animation = new AIS_Animation("obj1");
		gp_Trsf sTrsf = shape->LocalTransformation();
		Handle(AIS_AnimationObject) ani_object = new AIS_AnimationObject("obj1", myAISContext(), shape, sTrsf, eTrsf);
		ani_object->SetOwnDuration(0.5);
		ais_animation->Add(ani_object);
		double duration = ais_animation->Duration();
		ais_animation->StartTimer(0, 1.0, true);
		while (!ais_animation->IsStopped())
		{
			ais_animation->UpdateTimer();
			myAISContext()->UpdateCurrentViewer();
		}
	}
  • 相机动画。相机动画主要是设置动画结束时相机的各种信息。这是一个简单设置视图中心点的动画。
	void testAnimationCamera()
	{
		//在0,0,0显示一个坐标
		gp_Pnt gps(0, 0, 0);
		gp_Ax2 ax2 = gp_Ax2(gps, gp_Dir(0, 0, 1));
		DisplayTrihedron(ax2);
		//从原点0,0,0和X,Y,Z轴方向绘制一个矩形
		TopoDS_Shape tshapea = BRepPrimAPI_MakeBox(100, 50, 80);
		//显示蓝色的矩形
		Handle(AIS_Shape) ashapea = new AIS_Shape(tshapea);
		ashapea->SetColor(Quantity_NOC_BLUE);
		myAISContext()->Display(ashapea, true);
		//简单动画相机测试,仅仅是移动了Eye,也就是把(0,0,0)移动到了视图中心点
		AnimationCamera(0, 0, 0);
	}

void AnimationCamera(double x, double y, double z)
{
	Handle(Graphic3d_Camera) camera_start = myView()->Camera();
	Handle(Graphic3d_Camera) camera_end = new Graphic3d_Camera();
	camera_end->Copy(camera_start);
	camera_end->MoveEyeTo(gp_Pnt(x, y, z));
	Handle(AIS_AnimationCamera) ani_camera = new AIS_AnimationCamera("Camera", myView());
	ani_camera->SetCameraStart(camera_start);
	ani_camera->SetCameraEnd(camera_end);
	Handle(AIS_Animation) animation = new AIS_Animation("obj1");
	ani_camera->SetOwnDuration(1);
	animation->Add(ani_camera);
	double duration2 = animation->Duration();
	animation->StartTimer(0, 1, true);
	while (!animation->IsStopped())
	{
		animation->UpdateTimer();
		myAISContext()->UpdateCurrentViewer();
	}
}

例程中的轴视图、前视图、后视图等没有动画效果,但是右上角的立方体点击后有动画效果,下面的例程是相机动画从任何角度变为前视图的动画效果,其它视图效果,请自行完善,提示一下,其它视图只是修改theOrientation的方向即可。

void testAnimationCamera()
{
	//在0,0,0显示一个坐标
	gp_Pnt gps(0, 0, 0);
	gp_Ax2 ax2 = gp_Ax2(gps, gp_Dir(0, 0, 1));
	DisplayTrihedron(ax2);
	//从原点0,0,0和X,Y,Z轴方向绘制一个矩形
	TopoDS_Shape tshapea = BRepPrimAPI_MakeBox(100, 50, 80);
	//显示蓝色的矩形
	Handle(AIS_Shape) ashapea = new AIS_Shape(tshapea);
	ashapea->SetColor(Quantity_NOC_BLUE);
	myAISContext()->Display(ashapea, true);
	//下面的相机动画就复杂了一些,改变视图方向为前视图,其他方向请自行
	V3d_TypeOfOrientation theOrientation = V3d_Yneg;
	Graphic3d_Vec3d anUp = Graphic3d_Vec3d(0.0, 0.0, 1.0);
	Handle(Graphic3d_Camera) camera_start = myView()->Camera();
	gp_Pnt theCenter = camera_start->Center();
	gp_XYZ centerXYZ(theCenter.X(), theCenter.Y(), theCenter.Z());
	Handle(Graphic3d_Camera) camera_end = new Graphic3d_Camera();
	camera_end->Copy(camera_start);
	const gp_Dir aBck = V3d::GetProjAxis(theOrientation);
	const gp_Pnt anOriginVCS = camera_end->ConvertWorld2View(gp::Origin());
	const Standard_Real aNewDist = camera_end->Eye().Distance(theCenter);
	camera_end->SetEyeAndCenter(centerXYZ, theCenter);
	camera_end->SetDirectionFromEye(-aBck);
	camera_end->SetUp(gp_Dir(anUp.x(), anUp.y(), anUp.z()));
	camera_end->OrthogonalizeUp();
	Handle(AIS_AnimationCamera) ani_camera = new AIS_AnimationCamera("Camera", myView());
	ani_camera->SetCameraStart(camera_start);
	ani_camera->SetCameraEnd(camera_end);
	Handle(AIS_Animation) animation = new AIS_Animation("obj1");
	ani_camera->SetOwnDuration(1);
	animation->Add(ani_camera);
	double duration2 = animation->Duration();
	animation->StartTimer(0, 1, true);
	while (!animation->IsStopped())
	{
		animation->UpdateTimer();
		myAISContext()->UpdateCurrentViewer();
	}
}

  • 8
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霜吹花落

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值