简体   繁体   English

OpenGl绕点旋转

[英]OpenGl Rotation around point

My problem is that I need to rotate a white square around the center of the far left end, and no matter what I try, I cannot seem to do it. 我的问题是我需要围绕最左端的中心旋转一个白色正方形,无论我尝试什么,我似乎都做不到。

I need to rotate an object around radius , which is radius far from the position ( getPosition().x/y ), which i have already translated to. 我需要围绕radius旋转一个对象,该对象的radius距离我已经转换到的位置( getPosition().x/y )较远。 I need to rotate it r degrees. 我需要将其旋转r度。 If it matters, I am using an orthographic ( glOrtho ) projection. 如果有关系,我正在使用正交投影( glOrtho )投影。

So far this what I tried: 到目前为止,这是我尝试过的:

//Try 1
glRotatef(r, 0, 0, 1.0f);
glTranslatef(radius, radius, 0);
glBegin(GL_QUADS)
//draw here...

//Try 2
glTranslatef(-radius, -radius, 0);
glRotatef(r, 0, 0, 1.0f);
glTranslatef(radius, radius, 0);
glBegin(GL_QUADS)
//draw here...

//Try 3
glTranslatef(radius + getPosition().x, radius + getPosition().y, 0);
glRotatef(r, 0, 0, 1.0f);
glTranslatef(radius, radius, 0);
glBegin(GL_QUADS)
//draw here...

I have tried Googling and searching on StackOverflow numerous times, with no luck. 我已经尝试过Googling,并在StackOverflow上搜索了很多次,但是没有运气。 Two of these "solutions" came from answers found on StackOverflow itself. 其中两个“解决方案”来自StackOverflow本身的答案。

All of these attempts rotate around the origin. 所有这些尝试都围绕原点旋转。 I have also tried numerous other more nonsensical combinations, to no avail. 我也尝试了许多其他更荒谬的组合,但无济于事。 If it matters, a little bit before this code, I translate the matrix (and don't pop it back out). 如果很重要,那么在此代码之前,我将转换矩阵(并且不要将其弹出)。 I don't think this is the problem, as popping the matrix and pushing a new one back on right before any of these attempts does not fix the problem. 我不认为这是问题所在,因为在进行上述任何尝试之前弹出矩阵并向后推一个新的矩阵都无法解决问题。

Any help would be much appreciated. 任何帮助将非常感激。

So I found the solution: 所以我找到了解决方案:

glTranslatef(radius + getPosition().x, radius + getPosition().y, 0);
glRotatef(r, 0, 0, 1.0f);
glBegin(GL_POLYGON);

It turns out you dont have to translate after the rotation, which is was several sources suggested. 事实证明,你没有翻译后的旋转,这是几个来源建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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