-3

我创建了一个可以移除其侧面的自定义立方体。我最终想将立方体打印到纸质打印机上。我想通过使用 OpenGL 旋转方法的输出来进行自己的旋转。

我的问题:是否存在描述旋转后身份矩阵的文档?或者源代码可用于 OpenGL 旋转方法?

4

2 回答 2

0

glRotate 创建一个旋转矩阵并将其相乘在当前矩阵堆栈顶部的矩阵上。旋转矩阵是线性代数中众所周知的事情。

于 2013-10-31T15:43:27.447 回答
0

您可以在互联网上阅读有关矩阵计算和矩阵变换的信息,这里有一些链接。

尽管这是我为准确计算您要计算的内容而创建的。没有太多解释,因为它基本上只是一堆数学公式。

public class Matrix4
{
    public float m00, m01, m02, m03;
    public float m10, m11, m12, m13;
    public float m20, m21, m22, m23;
    public float m30, m31, m32, m33;

    public Matrix4()
    {
        this.set(
            1f, 0f, 0f, 0f,
            0f, 1f, 0f, 0f,
            0f, 0f, 1f, 0f,
            0f, 0f, 0f, 1f);
    }

    public void rotate(float angle, float x, float y, float z)
    {
        float sin = (float) Math.sin(angle);
        float cos = (float) Math.cos(angle);

        if ((x * x + y * y + z * z) != 1f)
        {
            float length = (float) Math.sqrt(x * x + y * y + z * z);

            if (length > 0f)
            {
                length = 1f / length;

                x *= length;
                y *= length;
                z *= length;
            }
        }

        this.mul(
            x * x * (1f - cos) + cos, x * y * (1f - cos) - z * sin, x * z * (1f - cos) + y * sin, 0f,
            y * x * (1f - cos) + z * sin, y * y * (1f - cos) + cos, y * z * (1f - cos) - x * sin, 0f,
            x * z * (1f - cos) - y * sin, y * z * (1f - cos) + x * sin, z * z * (1f - cos) + cos, 0f,
            0f, 0f, 0f, 1f);
    }

    public void mul(
            float m00, float m01, float m02, float m03,
            float m10, float m11, float m12, float m13,
            float m20, float m21, float m22, float m23,
            float m30, float m31, float m32, float m33)
    {
        float mm00 = this.m00 * m00 + this.m01 * m10 + this.m02 * m20 + this.m03 * m30;
        float mm01 = this.m00 * m01 + this.m01 * m11 + this.m02 * m21 + this.m03 * m31;
        float mm02 = this.m00 * m02 + this.m01 * m12 + this.m02 * m22 + this.m03 * m32;
        float mm03 = this.m00 * m03 + this.m01 * m13 + this.m02 * m23 + this.m03 * m33;

        float mm10 = this.m10 * m00 + this.m11 * m10 + this.m12 * m20 + this.m13 * m30;
        float mm11 = this.m10 * m01 + this.m11 * m11 + this.m12 * m21 + this.m13 * m31;
        float mm12 = this.m10 * m02 + this.m11 * m12 + this.m12 * m22 + this.m13 * m32;
        float mm13 = this.m10 * m03 + this.m11 * m13 + this.m12 * m23 + this.m13 * m33;

        float mm20 = this.m20 * m00 + this.m21 * m10 + this.m22 * m20 + this.m23 * m30;
        float mm21 = this.m20 * m01 + this.m21 * m11 + this.m22 * m21 + this.m23 * m31;
        float mm22 = this.m20 * m02 + this.m21 * m12 + this.m22 * m22 + this.m23 * m32;
        float mm23 = this.m20 * m03 + this.m21 * m13 + this.m22 * m23 + this.m23 * m33;

        float mm30 = this.m30 * m00 + this.m31 * m10 + this.m32 * m20 + this.m33 * m30;
        float mm31 = this.m30 * m01 + this.m31 * m11 + this.m32 * m21 + this.m33 * m31;
        float mm32 = this.m30 * m02 + this.m31 * m12 + this.m32 * m22 + this.m33 * m32;
        float mm33 = this.m30 * m03 + this.m31 * m13 + this.m32 * m23 + this.m33 * m33;

        this.m00 = mm00; this.m01 = mm01; this.m02 = mm02; this.m03 = mm03;
        this.m10 = mm10; this.m11 = mm11; this.m12 = mm12; this.m13 = mm13;
        this.m20 = mm20; this.m21 = mm21; this.m22 = mm22; this.m23 = mm23;
        this.m30 = mm30; this.m31 = mm31; this.m32 = mm32; this.m33 = mm33;
    }

    public void set(
            float m00, float m01, float m02, float m03,
            float m10, float m11, float m12, float m13,
            float m20, float m21, float m22, float m23,
            float m30, float m31, float m32, float m33)
    {
        this.m00 = m00;
        this.m01 = m01;
        this.m02 = m02;
        this.m03 = m03;

        this.m10 = m10;
        this.m11 = m11;
        this.m12 = m12;
        this.m13 = m13;

        this.m20 = m20;
        this.m21 = m21;
        this.m22 = m22;
        this.m23 = m23;

        this.m30 = m30;
        this.m31 = m31;
        this.m32 = m32;
        this.m33 = m33;
    }
}

以下将创建一个身份矩阵Matrix4 m = new Matrix4();

重要的是请注意,rotate()函数中给出的角度Matrix4弧度为单位,而在 OpenGL 中,glRotate()函数要求角度以为单位。

此外,如果矩阵是“相反的”,那么当您使用它时,只需在将矩阵用于其他任何事情之前计算矩阵的转置,然后您就可以使用它。

于 2013-10-31T15:49:19.600 回答