Rotating a multipart object

前端 未结 2 995
醉酒成梦
醉酒成梦 2021-01-16 10:53

I\'ve created an object that has about 7+ parts to it including its body and smaller parts that \'attach\' to it in different places. My goal is to rotate the entire object.

2条回答
  •  旧巷少年郎
    2021-01-16 11:26

    1. you are missing glMatrixMode calls

      If you are using only GL_MODELVIEW than it works (if lastly set active) but when your code grows longer and you add other calls you might broke this and suddenly your code will not work as expected (and very hard to debug). Therefore it is better to add glMatrixMode(GL_MODELVIEW); before any transformation chunk of code.

    2. You are push/pop ing wrongly

      Your objects are nested so the matrices must be nested too. That means any part attached to owner part must start with the owners part matrix. So you need to have some hierarchy of the parts (assembly order) so you know what parts are attached to which ones and where.

      so You should have a list of parts connected to any part ... Something like:

      List part[noOfObj];
      

      so any part[i], i=<0,noOfObj-1> has children parts part[i][0,1,2...,part[i].num-1] connected to it (where num is size of the list). And part[0] is the main part. That changes things a bit but simple recursion helps:

      void part_draw(int ix) // this is just recursion call used by the main function do not use it directly
       {
       glMatrixMode(GL_MODELVIEW);
       glPushMatrix();
       glTranslatef(subPosX[ix], subPosY[ix], subPosZ[ix]);
      
       glPushMatrix();            // this should not be here 
       glScalef(9.0, 1.75, 1.75); // this should not be here
       ..... // draw the object ix here
       glMatrixMode(GL_MODELVIEW);// this should not be here
       glPopMatrix();             // this should not be here
      
       for (int iy=0;iy

      Now beware that subPosX/Y/Z positions must be in parent part coordinate system. Also This will not work for cyclicly nested objects (loops) as that would lead to infinite loop causing stack overflow.

提交回复
热议问题