OpenGL transforming objects with multiple rotations of Different axis

前端 未结 2 1646
半阙折子戏
半阙折子戏 2020-12-18 11:45

I am building a modeling program and I\'d like to do transformations on objects in their own space and then assign that single object to a group to rotate around another axi

2条回答
  •  醉酒成梦
    2020-12-18 12:11

    Here is a partial answer to the behavior I want and the setup I used. This seems to be what I need to do to get the correct transforms in object space while apart of a group rotation. Here I have a model composed of 7 different individual meshes that is rotated around the origin of (0,5,0) on the y Axis, this is just an arbitrary rotation I choose for testing.

     for (int i = 0; i < models.at(currentSelectedPointer.x)->meshes.size()i++)
        {
        glm::mat4 rotMat;
        rotMat = glm::translate(rotMat, glm::vec3(5, 0, 0));
        rotMat = glm::rotate(rotMat, f, glm::vec3(0, 1.0, 0.0));
        rotMat = glm::translate(rotMat, glm::vec3(-5, 0, 0));
        models.at(currentSelectedPointer.x)->meshes.at(i).groupRotation = rotMat;
        }
    

    all the meshes are now rotating around (0,5,0) as a group, not at (0,5,0), on the Y axis.

    to do the correct rotation transform on a single object in it's own object space, I have to undo the location of the groupRotation's origin (Sorry for the messy code, but I did it in steps like this to keep everything seperated and easily disectable). Also the individual object has an identity matrix for both it's translation and it's scale.

         //These will be used to shift the groupRotation origin back to the 
         // origin in order to rotate around the object's origin.
    
         glm::mat4 gotoGroupAxis;
         gotoGroupAxis= glm::translate(gotoGroupAxis, glm::vec3(5, 0, 0));
         glm::mat4 goBack ;
         goBack = glm::translate(goBack , glm::vec3(-5, 0, 0));
    
         ////////Group rotation and it's inverse        
         glm::mat4 tempGroupRot = goBack *obj.groupRotation*gotoGroupAxis;
         glm::mat4 tempGroupRotInverse= glm::inverse(tempGroupRot);
    
         //thisRot and lastRot are matrix variables I use to accumulate and 
         //save rotations       
         obj.thisRot = tempGroupRotInverse* 
         glm::toMat4(currentRotation)*tempGroupRot * 
         obj.lastRot;
    
         //now I translate the object's rotation origin to it's center.
    
         glm::mat4 transform = glm::translate(transform, -obj.meshCenter);
         glm::mat4 transform1 = glm::translate(transform1, obj.meshCenter);
         //Finally I rotate the object in it's own space.
         obj.rotation =  transform1*obj.thisRot*transform;
    

    Update:

     //Translation works as well with
     obj.finalTranslation= tempGroupRotInverse* 
     obj.translation * tempGroupRot ;
    

    This is only a partial answer because I'm going to be doing transforms on an object level and group level and I'm almost certain that something will go wrong down the line that hasn't been taken into account by the answer I've posted.

提交回复
热议问题