/
TexDef.cpp
167 lines (137 loc) · 3.88 KB
/
TexDef.cpp
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
#include "TexDef.h"
#include <math.h>
#include "texturelib.h"
#include "math/Vector2.h"
// Constructor
TexDef::TexDef()
{
_shift[0] = 0;
_shift[1] = 0;
_rotate = 0;
_scale[0] = 1;
_scale[1] = 1;
}
// Constructs a TexDef out of the given transformation matrix plus width/height
TexDef::TexDef(double width, double height, const Matrix4& transform)
{
_scale[0] = static_cast<double>((1.0 / Vector2(transform[0], transform[4]).getLength()) / width);
_scale[1] = static_cast<double>((1.0 / Vector2(transform[1], transform[5]).getLength()) / height);
_rotate = static_cast<double>(-radians_to_degrees(arctangent_yx(-transform[4], transform[0])));
if (_rotate == -180.0f)
{
_rotate = 180.0f;
}
_shift[0] = transform[12] * width;
_shift[1] = transform[13] * height;
// If the 2d cross-product of the x and y axes is positive, one of the axes has a negative scale.
if (Vector2(transform[0], transform[4]).crossProduct(Vector2(transform[1], transform[5])) > 0)
{
if (_rotate >= 180.0f)
{
_rotate -= 180.0f;
_scale[0] = -_scale[0];
}
else
{
_scale[1] = -_scale[1];
}
}
}
double TexDef::getRotation() const
{
return _rotate;
}
void TexDef::setRotation(double rotation)
{
_rotate = rotation;
}
Vector2 TexDef::getShift() const
{
return Vector2(_shift);
}
void TexDef::setShift(const Vector2& shift)
{
_shift[0] = shift.x();
_shift[1] = shift.y();
}
Vector2 TexDef::getScale() const
{
return Vector2(_scale);
}
void TexDef::setScale(const Vector2& scale)
{
_scale[0] = scale[0];
_scale[1] = scale[1];
}
void TexDef::shift(double s, double t)
{
_shift[0] += s;
_shift[1] += t;
}
void TexDef::scale(double s, double t)
{
_scale[0] += s;
_scale[1] += t;
}
void TexDef::rotate(double angle)
{
_rotate += angle;
_rotate = static_cast<double>(float_to_integer(_rotate) % 360);
}
// Checks the TexDef for insanely large values
bool TexDef::isSane() const
{
return fabs(_shift[0]) < (1 << 16)
&& fabs(_shift[1]) < (1 << 16);
}
// All texture-projection translation (shift) values are congruent modulo the dimensions of the texture.
// This function normalises shift values to the smallest positive congruent values.
void TexDef::normalise(double width, double height)
{
// it may be useful to also normalise the rotation here, if this function is used elsewhere.
_shift[0] = float_mod(_shift[0], width);
_shift[1] = float_mod(_shift[1], height);
}
/* Construct a transform in ST space from the texdef.
* Transforms constructed from quake's texdef format
* are (-shift)*(1/scale)*(-rotate) with x translation sign flipped.
* This would really make more sense if it was inverseof(shift*rotate*scale).. oh well.*/
Matrix4 TexDef::getTransform(double width, double height) const
{
Matrix4 transform;
double inverse_scale[2];
// transform to texdef shift/scale/rotate
inverse_scale[0] = 1 / (_scale[0] * width);
inverse_scale[1] = 1 / (_scale[1] * -height);
transform[12] = _shift[0] / width;
transform[13] = -_shift[1] / -height;
double c = cos(degrees_to_radians(-_rotate));
double s = sin(degrees_to_radians(-_rotate));
transform[0] = c * inverse_scale[0];
transform[1] = s * inverse_scale[1];
transform[4] = -s * inverse_scale[0];
transform[5] = c * inverse_scale[1];
transform[2] = transform[3] = transform[6] = transform[7] = transform[8] = transform[9] = transform[11] = transform[14] = 0;
transform[10] = transform[15] = 1;
return transform;
}
ShiftScaleRotation TexDef::toShiftScaleRotation() const
{
ShiftScaleRotation result;
result.shift[0] = _shift[0];
result.shift[1] = _shift[1];
result.rotate = _rotate;
result.scale[0] = _scale[0];
result.scale[1] = _scale[1];
return result;
}
TexDef TexDef::CreateFromShiftScaleRotation(const ShiftScaleRotation& scr)
{
TexDef texDef;
texDef._shift[0] = scr.shift[0];
texDef._shift[1] = scr.shift[1];
texDef._rotate = scr.rotate;
texDef._scale[0] = scr.scale[0];
texDef._scale[1] = scr.scale[1];
return texDef;
}