鼠标交互

1、鼠标交互

案例位置examples/Cuda/QtGUI/Qt_MouseInteractionInGraphicsPipeline

功能介绍:主要用于测试在Qt框架下鼠标交互功能。该案例展示了 CustomMouseInteraction 类的的基本使用。该功能主要用于响应鼠标事件,包括鼠标移动、点击和释放。当鼠标发出不同指令时,程序会调用不同的接口,便于用户和仿真程序的交互。案例中展示了当鼠标在仿真界面移动、点击时,程序会打印出相应的状态、位置坐标等信息。

案例说明:如下图所示,当鼠标在仿真界面移动,点击模型,选择多个模型时,后台程序会打印出相应的信息。

2、程序实现

接下来介绍案例的实现过程:

  • 创建Instances实例:
class Instances : public Node
{
public:
	Instances() {
		Transform3f tm;
		CArray<Transform3f> hTransform;
		for (uint i = 0; i < 5; i++)
		{
			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();
			hTransform.pushBack(tm);
		}

		this->stateTransforms()->allocate()->assign(hTransform);

		std::shared_ptr<TriangleSet<DataType3f>> triSet = std::make_shared<TriangleSet<DataType3f>>();
		triSet->loadObjFile(getAssetPath() + "armadillo/armadillo.obj");

		this->stateTopology()->setDataPtr(triSet);

		hTransform.clear();
	};

	DEF_ARRAY_STATE(Transform3f, Transforms, DeviceType::GPU, "Instance transform");

	DEF_INSTANCE_STATE(TopologyModule, Topology, "Topology");
};

  • 创建场景图:
	std::shared_ptr<SceneGraph> scn = std::make_shared<SceneGraph>();
  • 创建Instances实例:
	auto instanceNode = scn->addNode(std::make_shared<Instances>());
  • 创建鼠标交互事件:
	//Create a CustomMouseIteraction object to handle the mouse event,
	//Press/release the mouse button to show the information
	auto mouseInterator = std::make_shared<CustomMouseInteraction>();
	mouseInterator->setUpdateAlways(true);
	instanceNode->stateTopology()->connect(mouseInterator->inTopology());
	instanceNode->graphicsPipeline()->pushModule(mouseInterator);
  • 创建渲染节点:
	auto instanceRender = std::make_shared<GLInstanceVisualModule>();
	instanceRender->setColor(Vec3f(0, 1, 0));
	instanceNode->stateTopology()->connect(instanceRender->inTriangleSet());
	instanceNode->stateTransforms()->connect(instanceRender->inInstanceTransform());
	instanceNode->graphicsPipeline()->pushModule(instanceRender);