在 OpenGL 中旋转三角形 [英] Rotate Triangle In OpenGL

查看:42
本文介绍了在 OpenGL 中旋转三角形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试围绕它的中心点旋转一个三角形.我知道 OpenGL 围绕原点旋转,所以我需要将中间点平移到原点,然后旋转,再平移回来.我已经注释掉了最后一行,以确保它至少围绕原点的中心旋转.它不是.尽管进行了转换,它似乎仍在围绕其旧原点旋转...请注意, ccc4 和 ccp 生成浮点数.这是我的代码:

I'm trying to rotate a triangle around it's center point. I'm aware that OpenGL rotates about the origin so I need to translate the middle point to the origin, then rotate, and translate back. I've commented out this last line to ensure that it at least rotates about its center at the origin. It does not. It appears to be rotating about its old origin despite the translation... Note that ccc4 and ccp generate floats. Here's my code:

ccColor4B colors[] = {
    ccc4(255, 0, 0, 255),
    ccc4(0, 255, 0, 255),
    ccc4(0, 0, 255, 255)
};

CGPoint vertices[] = {
    ccp(0,0),
    ccp(50,100),
    ccp(100,0),
};

CGPoint middle[] = {ccp(50,50)};
CGPoint origin[] = {ccp(0,0)};

// Rotate the triangle
glPushMatrix();
glTranslatef(-50, -50, 0);
glRotatef(45, 0, 0, 1.0);
// glTranslatef(50, 50, 0);

// Draw the triangle
glLineWidth(2);
glVertexPointer(2, GL_FLOAT, 0, vertices);
glColorPointer(4, GL_UNSIGNED_BYTE, 0, colors);
glColor4ub(0, 0, 255, 255);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 3);

// Revert rotation, we only want triangle to rotate 
glPopMatrix();

// Draw the points
glDisableClientState(GL_COLOR_ARRAY);

glPointSize(5);
glColor4ub(255, 255, 255, 255);
glVertexPointer(2, GL_FLOAT, 0, middle);
glDrawArrays(GL_POINTS, 0, 1);

glPointSize(5);
glColor4ub(0, 255, 0, 255);
glVertexPointer(2, GL_FLOAT, 0, origin);
glDrawArrays(GL_POINTS, 0, 1);

glEnableClientState(GL_COLOR_ARRAY);
// End points

输出如下:

推荐答案

您需要将转换视为相对于您调用它们的顺序反向应用.

You need to think of transforms as applying in reverse relative to the order in which you call them.

实际上,从变换局部坐标系 (LCS) 而非对象的角度考虑更容易,这允许您按照调用顺序在心理上应用变换.要绕中心旋转,请将 LCS 平移到中心,旋转,然后再次将其平移回来:

Actually, it's easier to think in terms of transforming the local coordinate system (LCS), not the object, which allows you to mentally apply transforms in the order they're called. To rotate about the center, translate the LCS to the center, rotate, then translate it back out again:

glTranslatef(50, 50, 0);
glRotatef(45, 0, 0, 1);
glTranslatef(-50, -50, 0);

这篇关于在 OpenGL 中旋转三角形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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