简体   繁体   English

如何在C ++中实现一个场景图,其中每个场景节点都可以引用多个子场景节点

[英]How to implement a Scene Graph in C++ where each Scene Node can refer to multiple child Scene Nodes

I'm currently trying to port my game engine from Java to C++ so that I use more up-to-date versions of OpenGL. 我目前正在尝试将我的游戏引擎从Java移植到C ++,以便使用更多最新版本的OpenGL。 While it has generally been a very smooth process (despite having very minimal experience with C++), I ran into problems when trying to port my SceneNode class. 尽管通常这是一个非常平稳的过程(尽管对C ++的经验很少),但是在尝试移植SceneNode类时遇到了问题。

The SceneNode class is used to create a Scene Graph by storing a parent node and a collection of children nodes as members. SceneNode类用于通过将父节点和子节点的集合存储为成员来创建场景图。 In Java, it is fine for a SceneNode to have another SceneNode (such as a child node) as a member. 在Java中,SceneNode可以将另一个SceneNode(例如子节点)作为成员很好。 However, this is not possible in C++ without using pointers for example. 但是,如果不使用指针 ,则在C ++中是不可能的。

So, my question basically boils down to: What would be the recommended way to store a (or a collection of) SceneNode as a member of another SceneNode? 因此,我的问题基本上可以归结为: 将一个(或一个集合)SceneNode存储为另一个SceneNode成员的推荐方法是什么?

Below is an example of a draft implement showing the general idea of what I want to achieve: 以下是一个实施草稿的示例,显示了我想要实现的总体思路:

class SceneNode
{
public:

    //...
    // constructor, destructor etc
    //...

    void render(); // will call render() for each child node
    void addChild(SceneNode* child);

private:

    SceneNode* parent;
    std::vector<SceneNode*> children;

    //...
    // other members such as model data, transformation matrix etc
}

The above is certainly not set in stone, I'm very welcome to suggestions, it was purely for demonstration. 上面的内容当然不是一成不变的,我非常欢迎提出建议,它纯粹是为了演示。 It would also be useful to me to know if any suggested implementation requires additional care like a specialized copy constructor or destructor for example. 对我来说,了解是否有建议的实现是否需要额外的照顾(例如专门的复制构造函数或析构函数)也将很有用。

I'm assuming I will need to use the new keyword to generate the pointers, and potentially smart pointers if possible, but I am a beginner in C++ style memory management, so any help would be greatly appreciated. 我假设我将需要使用new关键字来生成指针,如果可能的话,还可能使用智能指针,但是我是C ++风格的内存管理的初学者,因此任何帮助将不胜感激。

Pointers are a good way to achieve what you're trying to do. 指针是实现您要执行的操作的好方法。 I think what you have is exactly what you need to start. 我认为您所拥有的正是您需要开始的。

Just because you are using pointers does not mean you need to dynamically create SceneNode's with "new" (though that is the easiest way to get started). 仅仅因为您使用指针并不意味着您需要用“ new”动态创建SceneNode(尽管这是最简单的入门方法)。 You could use a pool of nodes, or create pointers off of statically allocated nodes. 您可以使用节点池,也可以从静态分配的节点创建指针。

What I like to do is create two functions - something along the lines of: 我想做的是创建两个函数-类似以下内容:

SceneNode * Alloc_SceneNode();
void Dealloc_SceneNode(SceneNode *node);

Or they can be called CreateNode() and DestroyNode(), whatever terminology you want. 或将它们称为CreateNode()和DestroyNode(),无论您使用什么术语。 Use those functions to get new nodes and return nodes when you're done. 使用这些函数获取新节点并在完成后返回节点。 Behind the scenes, they can call "new" and "delete", but in the future if you'd like to switch to a pool of nodes (which I recommend for game development) it's easy to just edit those two functions and boom. 在后台,他们可以调用“ new”和“ delete”,但是在将来,如果您想切换到节点池(我建议用于游戏开发),则只需编辑这两个功能即可。 Your entire SceneGraph is no longer dynamically allocating, it's using whatever memory management pattern you want. 整个SceneGraph不再动态分配,而是使用所需的任何内存管理模式。

Oh yeah, and if you use new, make sure to delete the nodes when you're done. 哦,是的,如果您使用new,请确保在完成操作后删除节点。 The simplest way to do that for a hierarchital structure like a tree is to (in the destructor of SceneNode) call delete on all of your children. 对于像树这样的层次结构,执行此操作的最简单方法是(在SceneNode的析构函数中)在所有子级上调用delete。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM