summaryrefslogtreecommitdiffstats
path: root/matrix4x4.cpp
blob: d0c6f18b63131e16b70f5d99fb0ebf09fbd141ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the Graphics Dojo project on Trolltech Labs.
**
** This file may be used under the terms of the GNU General Public
** License version 2.0 or 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of
** this file.  Please review the following information to ensure GNU
** General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#include "matrix4x4.h"

#include "qmath.h"

Matrix4x4::Matrix4x4()
{
    float identity[] =
    {
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1
    };

    setData(identity);
}

Matrix4x4::Matrix4x4(float *data)
{
    setData(data);
}

Matrix4x4 Matrix4x4::orderSwapped() const
{
    float data[16];
    for (int i = 0; i < 4; ++i)
        for (int j = 0; j < 4; ++j)
            data[4*i + j] = m[4*j + i];
    return Matrix4x4(data);
}

void Matrix4x4::setData(float *data)
{
    for (int i = 0; i < 16; ++i)
        m[i] = data[i];
}

Matrix4x4 &Matrix4x4::operator*=(const Matrix4x4 &rhs)
{
    const Matrix4x4 lhs = *this;
    for (int i = 0; i < 4; ++i) {
        for (int j = 0; j < 4; ++j) {
            m[4*i + j] = 0;
            for (int k = 0; k < 4; ++k)
                m[4*i + j] += lhs.m[4*i + k] * rhs.m[4*k + j];
        }
    }
    return *this;
}

Matrix4x4 Matrix4x4::operator*(const Matrix4x4 &other) const
{
    return Matrix4x4(*this) *= other;
}

Point3d Matrix4x4::operator*(const Point3d &p) const
{
    qreal x = m[0] * p.x + m[4] * p.y + m[8] * p.z + m[12];
    qreal y = m[1] * p.x + m[5] * p.y + m[9] * p.z + m[13];
    qreal z = m[2] * p.x + m[6] * p.y + m[10] * p.z + m[14];
    qreal w = m[3] * p.x + m[7] * p.y + m[11] * p.z + m[15];

    return Point3d(x / w, y / w, z / w);
}

Matrix4x4 Matrix4x4::fromRotation(float angle, Qt::Axis axis)
{
    QTransform rot;
    rot.rotate(angle);
    switch (axis) {
    case Qt::XAxis:
        {
        float data[] =
        {
            1, 0, 0, 0,
            0, rot.m11(), rot.m12(), 0,
            0, rot.m21(), rot.m22(), 0,
            0, 0, 0, 1
        };
        return Matrix4x4(data);
        }
    case Qt::YAxis:
        {
        float data[] =
        {
            rot.m11(), 0, rot.m12(), 0,
            0, 1, 0, 0,
            rot.m21(), 0, rot.m22(), 0,
            0, 0, 0, 1
        };
        return Matrix4x4(data);
        }
    case Qt::ZAxis:
        {
        float data[] =
        {
            rot.m11(), rot.m12(), 0, 0,
            rot.m21(), rot.m22(), 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
        };
        return Matrix4x4(data);
        }
    default:
        return Matrix4x4();
    }
}

Matrix4x4 Matrix4x4::fromTranslation(float dx, float dy, float dz)
{
    float data[] =
    {
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, 0,
        dx, dy, dz, 1
    };
    return Matrix4x4(data);
}

Matrix4x4 Matrix4x4::fromScale(float sx, float sy, float sz)
{
    float data[] =
    {
        sx, 0, 0, 0,
        0, sy, 0, 0,
        0, 0, sz, 0,
        0, 0, 0, 1
    };
    return Matrix4x4(data);
}

Matrix4x4 Matrix4x4::fromProjection(float fovAngle)
{
    float fov = qCos(fovAngle / 2) / qSin(fovAngle / 2);

    float zNear = 0.001;
    float zFar = 1000;

    float m33 = (zNear + zFar) / (zNear - zFar);
    float m34 = (2 * zNear * zFar) / (zNear - zFar);

    float data[] =
    {
        fov, 0, 0, 0,
        0, fov, 0, 0,
        0, 0, m33, -1,
        0, 0, m34, 0
    };
    return Matrix4x4(data);
}

QTransform Matrix4x4::toQTransform() const
{
    return QTransform(m[0], m[1], m[3],
                      m[4], m[5], m[7],
                      m[12], m[13], m[15]);
}

Matrix4x4 Matrix4x4::fromQTransform(const QTransform &transform)
{
    float data[] =
    {
        transform.m11(), transform.m12(), 0, transform.m13(),
        transform.m21(), transform.m22(), 0, transform.m23(),
        0, 0, 1, 0,
        transform.m31(), transform.m32(), 0, transform.m33()
    };

    return Matrix4x4(data);
}