File: TextureMatrix.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (189 lines) | stat: -rw-r--r-- 7,582 bytes parent folder | download | duplicates (3)
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
#include "TextureMatrix.h"

#include "ShaderExpression.h"
#include "MaterialManager.h"

namespace shaders
{

namespace
{
    inline void unlinkAndDeleteExpression(IShaderExpression::Ptr& expression)
    {
        if (expression)
        {
            expression->unlinkFromRegisters();
            expression.reset();
        }
    }
}

struct TextureMatrix::TemporaryMatrix
{
    IShaderExpression::Ptr xx;
    IShaderExpression::Ptr yx;
    IShaderExpression::Ptr tx;
    IShaderExpression::Ptr xy;
    IShaderExpression::Ptr yy;
    IShaderExpression::Ptr ty;
};

TextureMatrix::TextureMatrix(ExpressionSlots& expressions, Registers& registers) :
    _expressions(expressions),
    _registers(registers)
{}

void TextureMatrix::setIdentity()
{
    // Initialise the texture matrix to identity (set the diagonals to 1)
    xx().registerIndex = REG_ONE;
    yx().registerIndex = REG_ZERO;
    tx().registerIndex = REG_ZERO;
    xy().registerIndex = REG_ZERO;
    yy().registerIndex = REG_ONE;
    ty().registerIndex = REG_ZERO;

    unlinkAndDeleteExpression(xx().expression);
    unlinkAndDeleteExpression(yx().expression);
    unlinkAndDeleteExpression(tx().expression);
    unlinkAndDeleteExpression(xy().expression);
    unlinkAndDeleteExpression(yy().expression);
    unlinkAndDeleteExpression(ty().expression);
}

Matrix4 TextureMatrix::getMatrix4()
{
    auto matrix = Matrix4::getIdentity();

    matrix.xx() = _registers[xx().registerIndex];
    matrix.yx() = _registers[yx().registerIndex];
    matrix.tx() = _registers[tx().registerIndex];
    matrix.xy() = _registers[xy().registerIndex];
    matrix.yy() = _registers[yy().registerIndex];
    matrix.ty() = _registers[ty().registerIndex];

    return matrix;
}

void TextureMatrix::applyTransformation(const IShaderLayer::Transformation& transformation)
{
    TemporaryMatrix matrix;

    switch (transformation.type)
    {
    case IShaderLayer::TransformType::Translate:
        matrix.xx = ShaderExpression::createConstant(1);
        matrix.yx = ShaderExpression::createConstant(0);
        matrix.tx = transformation.expression1;
        matrix.xy = ShaderExpression::createConstant(0);
        matrix.yy = ShaderExpression::createConstant(1);
        matrix.ty = transformation.expression2;
        break;
    case IShaderLayer::TransformType::Scale:
        matrix.xx = transformation.expression1;
        matrix.yx = ShaderExpression::createConstant(0);
        matrix.tx = ShaderExpression::createConstant(0);
        matrix.xy = ShaderExpression::createConstant(0);
        matrix.yy = transformation.expression2;
        matrix.ty = ShaderExpression::createConstant(0);
        break;
    case IShaderLayer::TransformType::CenterScale:
        matrix.xx = transformation.expression1;
        matrix.yx = ShaderExpression::createConstant(0);
        matrix.tx = ShaderExpression::createAddition(
            ShaderExpression::createConstant(0.5),
            ShaderExpression::createMultiplication(ShaderExpression::createConstant(-0.5), transformation.expression1)
        );
        matrix.xy = ShaderExpression::createConstant(0);
        matrix.yy = transformation.expression2;
        matrix.ty = ShaderExpression::createAddition(
            ShaderExpression::createConstant(0.5),
            ShaderExpression::createMultiplication(ShaderExpression::createConstant(-0.5), transformation.expression2)
        );
        break;
    case IShaderLayer::TransformType::Shear:
        matrix.xx = ShaderExpression::createConstant(1);
        matrix.yx = transformation.expression1;
        matrix.tx = ShaderExpression::createMultiplication(ShaderExpression::createConstant(-0.5), transformation.expression1);
        matrix.xy = transformation.expression2;
        matrix.yy = ShaderExpression::createConstant(1);
        matrix.ty = ShaderExpression::createMultiplication(ShaderExpression::createConstant(-0.5), transformation.expression2);
        break;
    case IShaderLayer::TransformType::Rotate:
    {
        auto sinTable = GetShaderSystem()->getTable("sinTable");
        auto cosTable = GetShaderSystem()->getTable("cosTable");

        if (!sinTable || !cosTable)
        {
            rError() << "Cannot create rotate stage transform without sinTable and cosTable declarations" << std::endl;
            return;
        }

        // sin(expr) and cos(expr) shortcuts
        auto sinExpr = ShaderExpression::createTableLookup(sinTable, transformation.expression1);
        auto cosExpr = ShaderExpression::createTableLookup(cosTable, transformation.expression1);

        matrix.xx = ShaderExpression::createTableLookup(cosTable, transformation.expression1);
        matrix.yx = ShaderExpression::createMultiplication(
            ShaderExpression::createConstant(-1),
            ShaderExpression::createTableLookup(sinTable, transformation.expression1)
        );
        matrix.tx = ShaderExpression::createAddition(
            ShaderExpression::createConstant(0.5),
            ShaderExpression::createAddition(
                ShaderExpression::createMultiplication(ShaderExpression::createConstant(-0.5), cosExpr),
                ShaderExpression::createMultiplication(ShaderExpression::createConstant(+0.5), sinExpr)
            )
        );
        matrix.xy = ShaderExpression::createTableLookup(sinTable, transformation.expression1);
        matrix.yy = ShaderExpression::createTableLookup(cosTable, transformation.expression1);
        matrix.ty = ShaderExpression::createAddition(
            ShaderExpression::createConstant(0.5),
            ShaderExpression::createAddition(
                ShaderExpression::createMultiplication(ShaderExpression::createConstant(-0.5), sinExpr),
                ShaderExpression::createMultiplication(ShaderExpression::createConstant(-0.5), cosExpr)
            )
        );
        break;
    }
    default:
        return;
    };

    multiplyMatrix(matrix);
}

void TextureMatrix::multiplyMatrix(const TemporaryMatrix& matrix)
{
    auto xx = add(multiply(matrix.xx, this->xx()), multiply(matrix.yx, this->xy()));
    auto xy = add(multiply(matrix.xy, this->xx()), multiply(matrix.yy, this->xy()));
    auto yx = add(multiply(matrix.xx, this->yx()), multiply(matrix.yx, this->yy()));
    auto yy = add(multiply(matrix.xy, this->yx()), multiply(matrix.yy, this->yy()));
    auto tx = add(add(multiply(matrix.xx, this->tx()), multiply(matrix.yx, this->ty())), matrix.tx);
    auto ty = add(add(multiply(matrix.xy, this->tx()), multiply(matrix.yy, this->ty())), matrix.ty);

    _expressions.assign(IShaderLayer::Expression::TextureMatrixRow0Col0, xx, REG_ONE);
    _expressions.assign(IShaderLayer::Expression::TextureMatrixRow0Col1, yx, REG_ZERO);
    _expressions.assign(IShaderLayer::Expression::TextureMatrixRow1Col1, yy, REG_ONE);
    _expressions.assign(IShaderLayer::Expression::TextureMatrixRow1Col0, xy, REG_ZERO);
    _expressions.assign(IShaderLayer::Expression::TextureMatrixRow0Col2, tx, REG_ZERO);
    _expressions.assign(IShaderLayer::Expression::TextureMatrixRow1Col2, ty, REG_ZERO);
}

IShaderExpression::Ptr TextureMatrix::add(const IShaderExpression::Ptr& a, const IShaderExpression::Ptr& b)
{
    assert(a);
    assert(b);
    return ShaderExpression::createAddition(a, b);
}

IShaderExpression::Ptr TextureMatrix::multiply(const IShaderExpression::Ptr& a, ExpressionSlot& b)
{
    // Create a constant if there's no expression in the slot yet
    auto bExpr = b.expression ? b.expression : ShaderExpression::createConstant(_registers[b.registerIndex]);

    return ShaderExpression::createMultiplication(a, bExpr);
}

}