开发者

How to properly apply transformations to OpenGL models

I've been trying to figure this out for six months now, and have a workable solution, but I just can't figure out how to get my model to behav开发者_Python百科e exactly how I want. I'm rendering a 3D model with an arcball rotation implementation and zoom/pan functionality for the iPad, although the transformation code will be identical to that of desktop OpenGL. Currently, the user has the ability to change the center of rotation of the model by clicking on the model. The center of rotation changes, but the origin stays stationary which causes the model to move away from where the user touched the screen. What I would like it to do is allow the user to touch the model and have the model stay stationary, but have it move the origin to where the user touched. I'm not sure what transformations or the order of transformations needed to do this. The current order of transformations is:

glPushMatrix();
glTranslatef(pan.x, pan.y, 0);
glMultMatrixf(mat); //this is my rotation/scale matrix I get from my arcball class
glTranslatef(-origin.x, -origin.y, -origin.z);
//draw model
glPopMatrix();

Any idea what changes need to be made to make it so the origin moves instead of the model?


At first blush, I'd say you should shift the scene's origin to the desired rotation point before applying the rotation matrix, so that the shift happens along the global axes. then apply the rotation matrix, draw the item, and shift the origin back to the original origin for the rest of the display function (which should be taken care of in the glPopMatrix).

glPushMatrix();
glTranslatef(pan.x, pan.y, 0);
glTranslatef(-origin.x, -origin.y, -origin.z);
glMultMatrixf(mat); //this is my rotation/scale matrix I get from my arcball class
//draw model
glPopMatrix();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜