根据距离平移 [英] Translating according to the distance

查看:80
本文介绍了根据距离平移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在原点有一个正方形.首先,我要旋转它,然后将其平移到距原点5个单位的位置.

I have a square at the origin. First, I want to rotate it.Then, translate it 5 units away from the origin.

旋转后,我想将其平移5的距离.但是当我实现下面的代码时.它不会将其翻译到我想要的位置.什么是正确的翻译方式. glTranslatef( 5,5, 5); glRotatef(45, x,y,z);

After rotation I want to translate it for a distance of 5. But when I implement the code below. It does not translate it to where I want. What is the correct way to Translate. glTranslatef( 5,5, 5); glRotatef(45, x,y,z);

推荐答案

如果方形中心位于原点,则glTranslatef然后glRotatef.

If your square center is at the origin, glTranslatef then glRotatef.

要绕自定义中心旋转,这是通用公式:

To rotate about a custom center, here is the generic formula :

TranslationFromRotCenterToOrigin * RotationMatrix * TranslationFromOriginToRotCenter

这在OpenGL中给出了相反的顺序(导致glTranslatef/glRotatef将当前OpenGL矩阵与指定的平移/旋转矩阵相乘)

which gives something in reverse order in OpenGL ( cause glTranslatef/glRotatef multiplies the current OpenGL matrix by the specified translation/rotation matrix )

glLoadIdentity();
glTranslatef( 5, 5, 5);
glTranslatef( toRotCenterX, toRotCenterY, toRotCenterZ );
glRotatef( 45, x, y, z );
glTranslatef( -toRotCenterX, -toRotCenterY, -toRotCenterZ );

希望这会有所帮助

这篇关于根据距离平移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆