在 2D 中仅旋转一个四边形

发布于 2025-01-04 10:51:32 字数 557 浏览 1 评论 0原文

在 OpenGL 中,对于 2D 中的以下情况,如何旋转这一个四边形 - 并且仅旋转这一个四边形(场景中的其他所有内容都应保持在原来的位置)?

// Draw in immediate mode
glBegin(GL_QUADS);                      // begin drawing quads
glVertex2f(box.x,box.y);                // top-left corner
glVertex2f(box.x+box.w,box.y);          // top-right corner
glVertex2f(box.x+box.w,box.y+box.h);    // bottom-right corner
glVertex2f(box.x,box.y+box.h);          // bottom-left corner
glEnd();                                // end drawing quads

glRotatef(angle, x,y,z) 似乎旋转了我的整个场景。

In OpenGL, for the following situation in 2D, how can I rotate this one quad - and only this one quad (everything else in the scene should stay where it is)?

// Draw in immediate mode
glBegin(GL_QUADS);                      // begin drawing quads
glVertex2f(box.x,box.y);                // top-left corner
glVertex2f(box.x+box.w,box.y);          // top-right corner
glVertex2f(box.x+box.w,box.y+box.h);    // bottom-right corner
glVertex2f(box.x,box.y+box.h);          // bottom-left corner
glEnd();                                // end drawing quads

glRotatef(angle, x,y,z) seems to rotate my whole scene.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久伴你 2025-01-11 10:51:32

将其包含在 glPushMatrix 和 popMatrix 中

glPushMatrix(GL_MODELVIEW);

glRotatef(angle, x,y,z);

// Draw in immediate mode
glBegin(GL_QUADS);                      // begin drawing quads
glVertex2f(box.x,box.y);                // top-left corner
glVertex2f(box.x+box.w,box.y);          // top-right corner
glVertex2f(box.x+box.w,box.y+box.h);    // bottom-right corner
glVertex2f(box.x,box.y+box.h);          // bottom-left corner
glEnd();                                // end drawing quads

glPopMatrix(GL_MODELVIEW);

基本上,在上面的示例中,您将 modevliew 矩阵推入堆栈中的一个位置,本质上将其保存。然后旋转模型视图并绘制四边形。
然后,您在模型视图中弹回一个位置,恢复到旋转和绘图之前的状态。

Enclose it in glPushMatrix and popMatrix

glPushMatrix(GL_MODELVIEW);

glRotatef(angle, x,y,z);

// Draw in immediate mode
glBegin(GL_QUADS);                      // begin drawing quads
glVertex2f(box.x,box.y);                // top-left corner
glVertex2f(box.x+box.w,box.y);          // top-right corner
glVertex2f(box.x+box.w,box.y+box.h);    // bottom-right corner
glVertex2f(box.x,box.y+box.h);          // bottom-left corner
glEnd();                                // end drawing quads

glPopMatrix(GL_MODELVIEW);

Basically in the above example you are pushing the modevliew matrix one position up into the stack, saving it in an essence. Then you rotate the modelview and draw your quad.
Afterwards you pop back one position in the modelview taking back to how it was before the rotation and the drawing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文