OpenGL 3D Articulated robot
Published on 03/29/2010
6 min read
I had to use openGL and C++ and to use rotations, translations and scales to move each part of the robot. I’ll explain how it works.I love spherical forms; this is the reason why I said to myself: “Why not create a robot in the shape of a sphere?” I kept this idea and tried to figure out: “How do I make a robot out of a sphere? Obviously, the answer was: “Divide this sphere into several parts”.
openGL + SDL + C with codeBlock
Step 1: Where to start?
I began to open 3Dsmax and I made a spherical low polygon (i.e. 12 edges) to obtain the position of each sphere’s face: its body. Then, I created a second sphere inside the first one to simulate a density for the body. With a Boolean operation, I subtracted the inside sphere from the outside sphere.
Once I had converted the sphere to an editable polygon, I removed each part of the body that was redundant. Thus, I kept unique fragment form the sphere’s body because all of the parts of the sphere are identical. Finally, I obtained three fragments and manually recorded the position of each point.
Step 2: the robot’s armor
Once I had my points, I created a project C++, added my libraries openGL and SDL. I created the 3 fragments of the sphere. Then all I had to do was display it with a rotation of 30°, 60°, ect… which closed the bottom part of my sphere. I did the same operation with a rotation of 180 degrees for the top of my sphere. Voila! I had recreated the whole sphere as I had using 3DsMax.
To judge the quality of my robot, which is a sphere, I used a trackball as if my sphere were a planet. I learned to create a trackball thanks to Kayl in his tutorial (french): Camera Display. I modified his algorithm to coordinate it with my landmark and to be able to do vertical rotations.
To be able to move certain parts of the robot, I added constraints to the display of the robot’s armor which left blank spots. Then, I replaced the holes in my sphere with the missing parts that would become its appendages (head, feet, hands…).
Step 3 : The Inside of the Sphere
Inside my sphere, I had to affix the arms, the legs, the canon and the neck. This however, was not the most difficult part. The most challenging part was connecting all of these elements to each other.To explain how I put it together, I will use the construction of a leg as an example. I began by placing the four parts of the sphere which represent a foot in the center of my word openGL. Then, I placed one of the four toes in the centre and created a small ball as a joint, translated it and added a cylinder (like a bone). To create the rest of this foot, I did the same manipulation for the tree other toes and Iperformed other translations and rotations and added spheres and cylinders to the leg. Next, I created a sphere to simulate a central joint at the intersection of the 4 cylinders. Translations and rotations along with the creation of cylinders and balls all contributed to the creation of a complete leg. After all of these transformations, I had to position the four parts (the toes) of the sphere backinto their places. In order to accomplish this, I did the above transformations in reverse order.
Step 4 : Move this robot
Time and distance are two elements used to move parts of the robot. Indeed, in the case of my example with a leg: I adjusted the speed and distance of each part when it moves out the sphere.
When I want to move a part in relation to another part, I perform rotation around a joint. We can notice here that it was useful to position every joints in the center of my space openGL because a rotation is a transformation that is done around the center of my space (where is a joint).
I created a table X containing the maximum value of rotation angles for each limb to have a robot completely opened. Another table Y contains the real value of rotation angles applied on the robot at time t. A function, using the time I chose for the unfolding of each part, apply the good value to Y thanks to X and t.
Every angle into Y is added to the rotation of the corresponding angle during the creation of robot’s limbs.
So that the robot returns to a form of sphere, another function decrement angles into Y to 0.
Step 5 : the general appearance
As one goes along I did my robot, I answered to myself: “how do I improve the aesthetic of it?”. Indeed, my sphere was a spherical low polygon: I wanted to obtain a high quality sphere.
After some researches, I got it doesn’t exist a function to improve the spherical form of my robot (I had suspected this result but it is important to verify if something exists before coding it: it a saving of time)
Finally, after internet researches, I realized my own function which creates a fragment of sphere in which I can choose the size, the resolution and the depth of it.
I was able to:
- create fingers and hands: I chose a smaller size for it
- create retractable parts to have way out for the canons
- create rectangles in the robot’s back to obtain a reactor.
- remove old points I had registered at the beginning thanks to 3Dsmax.
Step 6 : The end
I created my textures thanks to Photoshop CS4. In my program, I did an algorithm to apply it in the good way on each part of the robot’s spherical fragments. Besides, I applied these textures on quadric cylinders and quadric spheres but I didn’t need to create an algorithm (because quadric had been created so that texture is easy to apply.). To load these textures ( .jpeg) I used this French tutorial : OpenGL Textures written by Kayl.
I had a hard work to succeed to apply light (I nearly abandoned). Indeed, I spent a lot of time to understand what the link between light and openGL quad (sphere’s parts) is. I found out that I had to attribute to each quad the good light when it was created. To accomplish it, I made an algorithm to calculate and to affect to each quad the good normal value according to the position of light.
For the background, I chose to use a skybox : a easy way to have a background which don’t slow computer. To do it I used this French tutorial:Raptor - OpenGL Skybox realized by Cyril. I modified his algorithm to coordinate the camera position (trackball) and the skybox. Moreover, I modified it to replace the bitmap loading by a jpeg loading (lighter ^^).