我正在尝试在使用ECS的同时在opengl中实现实例化渲染。作为一种解决方案,我认为:与其为每个实体调用绘制函数,不如为每种类型的几何体调用绘制函数。要做到这一点,我想提供多态绘制函数,它们驻留在几何体特定的命名空间中,例如TorusGeom命名空间。
所以我有一个std::map< std::type_index, std::unique_ptr<DrawFunctor> >
为了添加一个特定的draw函子,我有
template <typename T>
void addFunctor() const // new entry iff map doesn't yet contain functor of type
{
static_assert(std::is_base_of<DrawFunctor, T>::value, "invalid typename, not a DrawFunctor");
using std::type_index;
type_index functorType = type_index(typeid(T));
if (functors.find(functorType) == functors.end())
functors[functorType] = std::make_unique<T>(new T());
}
但是当我尝试调用addFunctor 时
entity->manager.addFunctor< TorusGeom::TorusDraw >();
~~~~~~~~~~~~~~~~~~~~
这行给出了错误type name not allowed.
,我不明白为什么。如果它是定义函子的方式:
struct DrawFunctor {
virtual void operator()() = 0;
virtual ~DrawFunctor() = default;
protected:
DrawFunctor() {}
};
struct TorusDraw : public DrawFunctor {
TorusDraw() : DrawFunctor() {}
void operator()() override final {
glDrawElementsInstanced(...));
}
};
我在Entity
之前错过了class Manager
的前向声明,感谢您在评论中的建议。