简体   繁体   中英

Rotating 3D model in opengl

I am trying to allocate my model a position in scene according to the 9DoF parameters. Out of which, the rotations are giving me a hard time. If I am keeping x and z rotations zero then the roty seems to work fine and model revolve around y axis only. But as soon as I am giving x and y rotations any value, then model moves around other two axis while doing y rotation.

Below is the code I am using to give the rotx , roty , rotz values. I feel like there is some problem with matrix multiplication. Because 0 * x + roty * y + 0 * z = roty only but anything * x + roty * y + anything * z = anything .

model = glm::translate(model, glm::vec3(transX, transY, transZ));
model = glm::rotate(model, toRadians * rotX, glm::vec3(1, 0, 0));
model = glm::rotate(model, toRadians * rotY, glm::vec3(0, 1, 0));
model = glm::rotate(model, toRadians * rotZ, glm::vec3(0, 0, 1));

For orienting an object, apply the rotation first, only after that think about translation/rotating about another object:

model = glm::rotate(model, toRadians * rotX, glm::vec3(1, 0, 0));
model = glm::rotate(model, toRadians * rotY, glm::vec3(0, 1, 0));
model = glm::rotate(model, toRadians * rotZ, glm::vec3(0, 0, 1));
model = glm::translate(model, glm::vec3(transX, transY, transZ));

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