OpenGL Robot
Language: C++, Middleware: OpenGL
Overview of the project
Motivation
- How to animate 3D characters in OpenGL
Problem which I had
- Unlike single object animation, animating human-like object requires delicate control over each part of body. If we use absolute coordinate to animate them, the position of parts, which are far from the origin like fingers, will be computed by extremely complex transformation matrix. For such a reason, we need to use relative coordinate.
Which features are included in this project
- Building scene graph for the body
- Animating the body using rotation matrix
How to solve the problem
- Once we build scene graph for the body, we can animate it as rotating joints of the body. Lower part of body will follow its upper part. We do not need to translate each part of body using absolute coordinate.
Detail of the project
1.Introduction
OpenGL is mechanism to draw polygons or images in the frames. The images can be drawn using pre-defined interface in OpenGL. However, modern computer-generated images have complicated hierarchies.
Furthermore, if the scene contains animation, drawing each polygons and sphere with individual coordinates will be disaster. With this reason, the scenegraphs has to be used for the complex scene.
Scenegraph is the structure containing collection of nodes, normally, in tree structure in order to define objects and their relationship in the scene. Without the scenegraph, it is impossible to handle objects in complex scene. For example, if we want to draw two planets which one of them revolve around another, we have to define the equation to draw the revolving planet. However, with scenegraph, all we have to do is defining the rotation information for each planet (centre point and radius).
2.Robot drawing – Scenegraph
The robot is constructed using scenegraph (tree structure). Each body parts and joints are assigned to the single node. As mentions, there are two types of node. The node for body part is the static part to support whole robot and the node for the joint is in charge of movement of the robot. In other words, only joint node can have the angle information for rotation, but all nodes can have coordinate information
General tree structure is used for this project, but the constructor and update function is considerably modified. The construct has arguments for the name of parent and current node, and the joint declaration. The update function is main part for drawing OpenGL robot, which operates recursively to draw from root to leap node.
Each different body parts is connected by the joint like human body. The reason why the sphere is set as joint is that it is much easier to handle (rotate) compared to cube due to the fact that it has single centre and do not have to translate coordinate for rotation.
In this project, hip ("lowerBody") has been set as centre point for robot, which means that the other parts will be translated and rotated based on its coordinate. The key advantage for the scenegraph (tree structure) is that we do not have to consider the position and state of individual object (node). For example, if shoulder (parent node - joint) rotates 30 degree, upper arm, lower arm and palm, which are child nodes respectively, will be rotated or moved accordingly.
The entire structure of the robot is depicted below.
The robot is constructed using scenegraph (tree structure). Each body parts and joints are assigned to the single node. As mentions, there are two types of node. The node for body part is the static part to support whole robot and the node for the joint is in charge of movement of the robot. In other words, only joint node can have the angle information for rotation, but all nodes can have coordinate information
General tree structure is used for this project, but the constructor and update function is considerably modified. The construct has arguments for the name of parent and current node, and the joint declaration. The update function is main part for drawing OpenGL robot, which operates recursively to draw from root to leap node.
Each different body parts is connected by the joint like human body. The reason why the sphere is set as joint is that it is much easier to handle (rotate) compared to cube due to the fact that it has single centre and do not have to translate coordinate for rotation.
In this project, hip ("lowerBody") has been set as centre point for robot, which means that the other parts will be translated and rotated based on its coordinate. The key advantage for the scenegraph (tree structure) is that we do not have to consider the position and state of individual object (node). For example, if shoulder (parent node - joint) rotates 30 degree, upper arm, lower arm and palm, which are child nodes respectively, will be rotated or moved accordingly.
The entire structure of the robot is depicted below.
In the OpenGL, this hierarchy can be implemented using glPushMatrix(), glPopMatrix(). The difference between tree structure and OpenGL structure is little. Push, PopMatrix is the stack structure in OpenGL. Push is for going down the stack and Pop is for going up the stack. If we change the state of higher stack level (using glTranslatef, glRotatef), it will affect the entire state in lower stack level. So I have simply synchronized tree structure and the stack using update function. As mentioned before, the update function calls child nodes recursively after checking all siblings from root to leap node. For example, “left shoulder” and “right shoulder” have same parent node which is “body”. So, “left and right shoulders” are sibling. When it comes to OpenGL stack, “body” stays higher stack level than “left and right shoulder”, and “left shoulder” and “right shoulder” stays same level.
In the update function, glPushMatrix() will be called when child node is called in scenegraph. By doing this, these different structure can be simply synchronized.
Following image is the entire shape of robot and its structure.
Especially, the rotation of joint is based on Euler angles. So, it is available to animate human-like motion using these angles.
In order to show animation, key
frame has been used. Since rotating joint is main source of animation, desired
body part will be animated as increasing regarded angles in every key frame.
So, the angles are linearly increased or decreased.
As limiting the angle, it is available to restrict angle movement. Namely, once the maximum angle has been set, we do not have to calculate every angle or coordinate in every key frame. For loop animation, it is enough to set maximum angle and minimum angle like below.
As limiting the angle, it is available to restrict angle movement. Namely, once the maximum angle has been set, we do not have to calculate every angle or coordinate in every key frame. For loop animation, it is enough to set maximum angle and minimum angle like below.
Setting joint limitation
3.Conclusion
Through this project, I could understand the structure of scenegraph and relative coordinate in OpenGL. Scenegraph plays significant role for computer-generated image in OpenGL, which organizes scene making objects control easier. Once scenegraph is built, animation can be applied as changing the angle of joint. We do not have to apply every angle into the single frame. Once maximum and minimum angle has set, the motion will be applied through the frame.