如何在 C++ 中将 OpenGL 基元存储在树形数据结构中?

发布于 2024-09-25 14:57:29 字数 579 浏览 4 评论 0原文

我是 OpenGL 和 C++ 新手。假设我从一个 2D 正方形(在最左边)开始,如下图所示。我想让它与 glutKeyboardFunc() 交互,这样当我按下一个数字时,一个新框就会在相应的边缘旁边绘制。

alt text

图上最好的方法是使用一个包含所有框的树结构。但我不确定如何将基本原语保存到数据结构(例如树)中。

我知道我只能调用 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.

alt text

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染年凉城似染瑾 2024-10-02 14:57:29

如果我理解你的问题,你可以创建一个代表盒子的类,例如:

class Box {
public:
  void Move(int newx, int newy) {
    x = newx;
    y = newy;
  }
  void Resize(int newx, int newy) {
    w = newx;
    h = newy;
  }
  int getX() { return x; }
  int getY() { return y; }
  int getW() { return w; }
  int getH() { return h; }
private:
  int x, y, w, h;
};

然后,你可以有一个全局向量来容纳所有盒子:

#include <vector>

vector<Box> box;

然后你可以添加一个像这样的盒子:

Box newbox;
newbox.Move(40,50);
newbox.Resize(10,10);
box.push_back(newbox);

并像这样移动现有的盒子:

box[3].Move(70,20);

很抱歉我无法完全回答您的问题,但我希望您知道如何完成您的项目。

编辑:要实现树结构,您可以将这些变量添加到 Box 类中:

private:
  Box* parent;
  vector<Box*> child;

这将允许您跟踪树结构。然后执行类似以下操作:

myBox->getChild(0)->getChild(1)

将检索 myBox 第一个子项的第二个子项。我希望这对你有意义。

If I understand your question, you can make a class that represents the box, such as:

class Box {
public:
  void Move(int newx, int newy) {
    x = newx;
    y = newy;
  }
  void Resize(int newx, int newy) {
    w = newx;
    h = newy;
  }
  int getX() { return x; }
  int getY() { return y; }
  int getW() { return w; }
  int getH() { return h; }
private:
  int x, y, w, h;
};

Then, you can have a global vector to hold all the boxes:

#include <vector>

vector<Box> box;

You can then add a box like this:

Box newbox;
newbox.Move(40,50);
newbox.Resize(10,10);
box.push_back(newbox);

And move an existing one like this:

box[3].Move(70,20);

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:

private:
  Box* parent;
  vector<Box*> child;

This would allow you to keep track of the tree structure. Then doing something like:

myBox->getChild(0)->getChild(1)

would retrieve the second child of the first child of myBox. I hope this makes sense to you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文