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.
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.