Search code examples
openglglutfreeglut

How to draw a robotic arm which can pick objects in OpenGL?


I am having trouble in implementing a robotic arm which can pick objects, because of the glTranslate(), glRotate() calls I have in my implementation each and every part of the robot is dependent on the previous part.

Example:

     void drawRobo()
         {

              draw a Cylinder as Base;

              glTranslate(); 
              drawArmpart1();
              glTranslate();  
              drawJoint1();
              glRotate();  
              drawArmpart2();
              glTranslate();
              drawGrabbers(); // The claw or the endeffector   


         }
    void drawObjects()
          {
           glTranslate();
           drawCube() // Object
           glTranslate();
           drawSphere() // Object 2

          }

    void display()
      {
       drawRobo();
       drawObjects();
       glPostredisplay();
      }   

Now the problem is when i rotate the the endeffector or the grabber using glRotate(); my objects rotate as well, I don't want that. I want to be able to rotate the joints and the arm such that it comes closer to objects and then i want to pick them using the grabber.

How do i deal with this? I have the glPushMAtrix() and glPopMatrix() commands at all places where i need them.


Solution

  • Since you have this problem, it's obvious that you don't have appropriate push and pop matrix operations. Your pseudocode should be:

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    // setup view matrix
    
    glPushMatrix();
    // setup robot's world matrix
    drawRobo();
    glPopMatrix();
    
    // setup object's world matrix
    drawObjects();
    
    glutPostRedisplay();
    

    Notice that last draw call don't use push/pop - just because it is last so you don't need to recover matrix after it.