0

我正在尝试创建仅包含(或仅对用户可见)位置、旋转和平移向量的用户友好转换类(以 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());
        }

    };

我该如何翻译?

4

1 回答 1

2

openGL 中的转换存储在堆栈中,然后反向应用于模型。在您的代码中,您首先应用比例,然后是平移,最后是旋转。由于旋转是从 (0, 0, 0) 进行的,因此如果您移动(平移)了对象,您将更改其位置。要旋转一个对象,它的中心应该在 (0, 0, 0)。

正确的转换顺序是:缩放/旋转,然后平移

因此,在您的代码中,翻译应该是您进行的第一个转换。

于 2013-08-18T11:11:21.227 回答