鼠标交互

1、鼠标交互

案例位置examples/Cuda/Tutorials/GL_MouseInteraction

功能介绍:该功能主要用于展示鼠标点击事件。案例展示了 CustomMouseInteraction 类的的基本使用。

案例说明:当鼠标在仿真界面中点击时,后台程序打印出鼠标点击的位置信息。同时当鼠标选中多个实例时,系统能将选中的实例的数量显示出来。

2、程序实现

  • 创建场景图:
	std::shared_ptr<SceneGraph> scn = std::make_shared<SceneGraph>();

	
  • 创建仿真实例模型:
	auto triSet = std::make_shared<TriangleSet<DataType3f>>();
	triSet->loadObjFile(getAssetPath() + "armadillo/armadillo.obj");

 	for (uint i = 0; i < 5; i++)
	{
		Transform3f tm;
		tm.translation() = Vec3f(0.4 * i, 0, 0);
		tm.scale() = Vec3f(1.0 + 0.1 * i, 1.0 - 0.1 * i, 1.0);
		tm.rotation() = Quat<float>(i * (-0.2), Vec3f(1, 0, 0)).toMatrix3x3();

		auto node = scn->addNode(std::make_shared<TransformNode>());
		node->setName("TN-" + std::to_string(i));
		//node->loadMesh("armadillo/armadillo.obj");
		node->setMesh(triSet);
		node->setTransform(tm);

		auto sm = std::make_shared<GLInstanceVisualModule>();
		sm->setColor(Vec3f(i * 0.2f, i * 0.2f, 1.f - i * 0.1f));
		sm->setAlpha(0.8f);

		node->setSurfaceVisualModule(sm);
	}

	scn->setUpperBound({ 4, 4, 4 });
  • 创建鼠标交互相应事件:
  void CustomMouseInteraction::onEvent(PMouseEvent event)
	{
		if (event.actionType == AT_PRESS)
		{
			printf("Mouse pressed: Origin: %f %f %f; Direction: %f %f %f \n", event.ray.origin.x, event.ray.origin.y, event.ray.origin.z, event.ray.direction.x, event.ray.direction.y, event.ray.direction.z);
		}
		else if (event.actionType == AT_RELEASE)
		{
			printf("Mouse released: Origin: %f %f %f; Direction: %f %f %f \n", event.ray.origin.x, event.ray.origin.y, event.ray.origin.z, event.ray.direction.x, event.ray.direction.y, event.ray.direction.z);

		}
		else
		{
			printf("%f %f \n", event.x, event.y);
			printf("Mouse repeated: Origin: %f %f %f; Direction: %f %f %f \n", event.ray.origin.x, event.ray.origin.y, event.ray.origin.z, event.ray.direction.x, event.ray.direction.y, event.ray.direction.z);
		}
	}