简体   繁体   中英

OpenGL glm rotate model around a point issue

I have a model and some helper cubes which are located on its axes, three on each axis for transformations, I used them for rotate the model around its local axes.

在此处输入图片说明

I want to make those cubes rotate around the model center with its rotation so I translate them to the model center, rotate them by the same angle on the same axis the translate them back. This is the code:

//Rotation around X axis
GLfloat theta=glm::radians(xoffset);
glm::quat Qx(glm::angleAxis(theta, glm::vec3(1.0f, 0.0f, 0.0f)));
glm::mat4 rotX = glm::mat4_cast(Qx);
pickedObject->Transform(rotX);//Multiply the model matrix by the transformation matrix
glm::vec3 op(pickedObject->getMatrix()[3]);//model position
for(TransformationHelper* h:pickedObject->GetTransformationHelpers()){//the small cubes
     glm::mat4 m,it,t;
     glm::vec3 hp(h->getMatrix()[3]);//the cube position
     t=glm::translate(m,op);//m is a unit matrix
     it=glm::translate(m,-op);
     m=t*rotX*it;
     h->Transform(m);
}

The result is unexpected

在此处输入图片说明

Update: after updating the translation matrix I got this result:

在此处输入图片说明

The translation is in the wrong direction; the correct offset should be hp-op , ie the matrix t should restore the cube's position after rotating.

 t=glm::translate(glm::mat(1.f),hp-op);

Also there is no need to use inverse since it is costly (and numerically less stable):

 it=glm::translate(glm::mat(1.f),op-hp);

(Note: here translate was called with an explicitly constructed identity matrix. See this post for a similar problem. Seehere for why this is necessary.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM