【发布时间】:2013-08-18 10:57:35
【问题描述】:
我正在尝试创建仅包含(或仅对用户可见)位置、旋转和平移向量的用户友好转换类(以 Unity 为例)。
使用 glTranslate、glRotate 和 glScale 函数对 OpenGL 应用转换很容易。我在每个对象即将被绘制之前为它调用 Transform 方法。但是我在改变与旋转相关的位置时遇到了麻烦。这是我的代码。
// 对象的示例渲染方法
void Render()
{
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
transform->Transform();
glEnable(GL_COLOR_MATERIAL);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(3, GL_FLOAT, 0, m_colors->constData());
glVertexPointer(3, GL_FLOAT, 0, m_positions->constData());
glDrawArrays(GL_LINES, 0, 6);
glDisable(GL_COLOR_MATERIAL);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glPopMatrix();
}
// 转换类
class Transformation
{
public:
QVector3D Position;
QVector3D Rotation;
QVector3D Scale;
Transformation()
{
Position = QVector3D(0.0f, 0.0f, 0.0f);
Rotation = QVector3D(0.0f, 0.0f, 0.0f);
Scale = QVector3D(1.0f, 1.0f, 1.0f);
}
~Transformation()
{
}
void Translate(const QVector3D& amount)
{
}
void Rotate(const QVector3D& amount)
{
Rotation += amount;
Rotation.setX(AdjustDegree(Rotation.x()));
Rotation.setY(AdjustDegree(Rotation.y()));
Rotation.setZ(AdjustDegree(Rotation.z()));
}
void Transform()
{
// Rotation
glRotatef(Rotation.x(), 1.0f, 0.0f, 0.0f);
glRotatef(Rotation.y(), 0.0f, 1.0f, 0.0f);
glRotatef(Rotation.z(), 0.0f, 0.0f, 1.0f);
// Translation
glTranslatef(Position.x(), Position.y(), Position.z());
// Scale
glScalef(Scale.x(), Scale.y(), Scale.z());
}
};
我该如何翻译?
【问题讨论】:
-
我的建议:不要使用 OpenGL 内置的变换矩阵操纵器。一方面,他们使用起来很麻烦。更重要的是,它们已完全从更高版本中删除。使用真正的矩阵数学库,如 GLM、Eigen 或 linmath.h
-
感谢您的提示,但没有人回答我的问题。我在问如何按旋转度数进行翻译。
-
这就是我写这篇文章作为评论的原因。
标签: opengl rotation translation transformation