如何在 C++ 中将 OpenGL 基元存储在树形数据结构中?
我是 OpenGL 和 C++ 新手。假设我从一个 2D 正方形(在最左边)开始,如下图所示。我想让它与 glutKeyboardFunc() 交互,这样当我按下一个数字时,一个新框就会在相应的边缘旁边绘制。
图上最好的方法是使用一个包含所有框的树结构。但我不确定如何将基本原语保存到数据结构(例如树)中。
我知道我只能调用 glutDisplayFunc(myDisplay)
一次,其余的应该由 glutKeyboardFunc()
处理。
如有任何帮助,我们将不胜感激:)
更新:感谢您指出 glutPostRedisplay()
但如果我想让该框可选择并使用 glutMouseFunc()
继续跟踪当前选定的框,以及从那里开始,当我添加更多盒子时,我需要知道它创建了多少个子盒子,这样我就可以在绘制新盒子时提供正确的位置。现在似乎使用树数据结构更有意义?只是不确定如何将我需要的信息存储到树中。
I'm new to OpenGL and C++. Say I start with a 2D square (on the very left) like shown in the picture below. I want to make it interactive with the glutKeyboardFunc()
so when I press a number a new box will draw next to the corresponding edge.
Figure the best way to do this is to have a tree structure that hold all the boxes. But I'm not sure how I can hold basic primitives into a data structure, a tree, for instance.
I'm aware that I can only call the glutDisplayFunc(myDisplay)
once, and the rest should handle by the glutKeyboardFunc()
Any help would be appreciated :)
Update: thanks for pointing out glutPostRedisplay()
but what if I want to make the box selectable and keep tracking the current selected box with glutMouseFunc()
, and from there when I add more boxes, I need to know how many child box it has created, so I can provide the right position when new box is drawn. Seems that makes more sense now to use a tree data structure ? Just not sure how I can store the information I need into a tree.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解你的问题,你可以创建一个代表盒子的类,例如:
然后,你可以有一个全局向量来容纳所有盒子:
然后你可以添加一个像这样的盒子:
并像这样移动现有的盒子:
很抱歉我无法完全回答您的问题,但我希望您知道如何完成您的项目。
编辑:要实现树结构,您可以将这些变量添加到 Box 类中:
这将允许您跟踪树结构。然后执行类似以下操作:
将检索 myBox 第一个子项的第二个子项。我希望这对你有意义。
If I understand your question, you can make a class that represents the box, such as:
Then, you can have a global vector to hold all the boxes:
You can then add a box like this:
And move an existing one like this:
I'm sorry I cannot answer your question completely, but I hope you know have an idea on how to accomplish your project.
EDIT: To achieve a tree structure, you could add these variables to the Box class:
This would allow you to keep track of the tree structure. Then doing something like:
would retrieve the second child of the first child of myBox. I hope this makes sense to you.