实例渲染

1、实例渲染

案例位置examples/Cuda/Tutorials/GL_InstanceVisualizer

功能介绍:该案例展示了如何创建了Instances实例。PeriDyno引擎允许用户自定义实例节点,并支持对模型的位置,尺寸大小、颜色及旋转进行定义。

案例说明:案例中展示了5个Armadillo模型实例的创建,并按照先后顺序进行旋转、缩放以及作色。

2、创建过程

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

  • 自定义Instance类,该类继承于Node节点
	class Instances : public Node
  • 通过this->stateTransforms()->allocate()->assign()函数设置模型的位置,尺寸,旋转等变换,并绑定拓扑结构。
	// geometry
	std::shared_ptr<TriangleSet<DataType3f>> triSet = std::make_shared<TriangleSet<DataType3f>>();
	triSet->loadObjFile(getAssetPath() + "armadillo/armadillo.obj");
	this->stateTopology()->setDataPtr(triSet);

	// instance transforms and colors
	CArray<Transform3f> instanceTransforms;
	CArray<Vec3f>		instanceColors;

	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();
		instanceTransforms.pushBack(tm);

		instanceColors.pushBack(Vec3f(i * 0.2f, i * 0.2f, 1.f - i * 0.1f));
	}

	this->stateTransforms()->allocate()->assign(instanceTransforms);
	this->stateColors()->allocate()->assign(instanceColors);

	instanceTransforms.clear();
	

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

	DEF_INSTANCE_STATE(TopologyModule, Topology, "Topology");
  • 创建instance节点,并添加渲染节点:
	std::shared_ptr<SceneGraph> scn = std::make_shared<SceneGraph>();

	// create a instance node
	auto instanceNode = scn->addNode(std::make_shared<Instances>());

	// config instance rendering
	auto instanceRender = std::make_shared<GLInstanceVisualModule>();
	instanceRender->setColor(Vec3f(0, 1, 0));
	//instanceRender->setAlpha(0.5f);
	//instanceRender->varUseVertexNormal()->setValue(true);

	instanceNode->stateTopology()->connect(instanceRender->inTriangleSet());
	instanceNode->stateTransforms()->connect(instanceRender->inInstanceTransform());
	instanceNode->stateColors()->connect(instanceRender->inInstanceColor());
	instanceNode->graphicsPipeline()->pushModule(instanceRender);

	scn->setUpperBound({ 4, 4, 4});