实例渲染

1、实例渲染

peridyno允许用户自定义实例节点,该节点只需继承Node节点。用户可以在节点中加载obj模型,并可以使用代码对模型进行位置,尺寸及旋转进行定义。

2、创建过程

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

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

	hTransform.clear();

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

	DEF_INSTANCE_STATE(TopologyModule, Topology, "Topology");

3、仿真效果

代码参考example/GL_InstanceVisualizer/main.h: