Subversion Repositories Games.Carmageddon

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 pmbaty 1
#include "transfrm.h"
2
#include "CORE/MATH/angles.h"
3
#include "CORE/MATH/matrix34.h"
4
#include "CORE/MATH/quat.h"
5
#include "CORE/MATH/vector.h"
6
#include "harness/trace.h"
7
 
8
#include <string.h>
9
 
10
#define M34 BR_TRANSFORM_MATRIX34
11
#define MLP BR_TRANSFORM_MATRIX34_LP
12
#define QUT BR_TRANSFORM_QUAT
13
#define EUL BR_TRANSFORM_EULER
14
#define LUP BR_TRANSFORM_LOOK_UP
15
#define TRA BR_TRANSFORM_TRANSLATION
16
#define IDT BR_TRANSFORM_IDENTITY
17
 
18
br_uint_8 _CombineTransforms[7][7] = {
19
 
20
    /* M34 */ {
21
        M34,
22
        M34,
23
        M34,
24
        M34,
25
        M34,
26
        M34,
27
        M34,
28
    },
29
    /* MLP */ {
30
        M34,
31
        MLP,
32
        MLP,
33
        MLP,
34
        MLP,
35
        MLP,
36
        MLP,
37
    },
38
    /* QUT */ {
39
        M34,
40
        MLP,
41
        MLP,
42
        MLP,
43
        MLP,
44
        QUT,
45
        QUT,
46
    },
47
    /* EUL */ {
48
        M34,
49
        MLP,
50
        MLP,
51
        MLP,
52
        MLP,
53
        EUL,
54
        EUL,
55
    },
56
    /* LUP */ {
57
        M34,
58
        MLP,
59
        MLP,
60
        MLP,
61
        MLP,
62
        LUP,
63
        LUP,
64
    },
65
    /* TRA */ {
66
        M34,
67
        MLP,
68
        QUT,
69
        EUL,
70
        LUP,
71
        TRA,
72
        TRA,
73
    },
74
    /* IDT */ {
75
        M34,
76
        MLP,
77
        QUT,
78
        EUL,
79
        LUP,
80
        TRA,
81
        IDT,
82
    },
83
};
84
 
85
// IDA: void __cdecl BrTransformToMatrix34(br_matrix34 *mat, br_transform *xform)
86
void BrTransformToMatrix34(br_matrix34* mat, br_transform* xform) {
87
    LOG_TRACE("(%p, %p)", mat, xform);
88
 
89
    switch (xform->type) {
90
    case BR_TRANSFORM_MATRIX34:
91
    case BR_TRANSFORM_MATRIX34_LP:
92
        *mat = xform->t.mat;
93
        break;
94
 
95
    case BR_TRANSFORM_QUAT:
96
 
97
        BrQuatToMatrix34(mat, &xform->t.quat.q);
98
 
99
        mat->m[V_W][V_X] = xform->t.quat.t.v[V_X];
100
        mat->m[V_W][V_Y] = xform->t.quat.t.v[V_Y];
101
        mat->m[V_W][V_Z] = xform->t.quat.t.v[V_Z];
102
        break;
103
 
104
    case BR_TRANSFORM_EULER:
105
 
106
        BrEulerToMatrix34(mat, &xform->t.euler.e);
107
 
108
        mat->m[V_W][V_X] = xform->t.euler.t.v[V_X];
109
        mat->m[V_W][V_Y] = xform->t.euler.t.v[V_Y];
110
        mat->m[V_W][V_Z] = xform->t.euler.t.v[V_Z];
111
 
112
        break;
113
 
114
    case BR_TRANSFORM_TRANSLATION:
115
        BrMatrix34Translate(mat,
116
            xform->t.look_up.t.v[V_X],
117
            xform->t.look_up.t.v[V_Y],
118
            xform->t.look_up.t.v[V_Z]);
119
 
120
        break;
121
 
122
    case BR_TRANSFORM_LOOK_UP: {
123
        /*
124
                         * 1) Normalise Lookat vector to get Z component of matrix
125
                         * 2) Cross with up vector and normalise to get X component
126
                         * 3) Cross X & Z to get Y
127
                         */
128
        br_vector3 vx, vy, vz;
129
 
130
        BrVector3Normalise(&vz, &xform->t.look_up.look);
131
        BrVector3Negate(&vz, &vz);
132
        BrVector3Cross(&vx, &xform->t.look_up.up, &vz);
133
        BrVector3Normalise(&vx, &vx);
134
        BrVector3Cross(&vy, &vz, &vx);
135
 
136
        mat->m[V_X][V_X] = vx.v[V_X];
137
        mat->m[V_X][V_Y] = vx.v[V_Y];
138
        mat->m[V_X][V_Z] = vx.v[V_Z];
139
        mat->m[V_Y][V_X] = vy.v[V_X];
140
        mat->m[V_Y][V_Y] = vy.v[V_Y];
141
        mat->m[V_Y][V_Z] = vy.v[V_Z];
142
        mat->m[V_Z][V_X] = vz.v[V_X];
143
        mat->m[V_Z][V_Y] = vz.v[V_Y];
144
        mat->m[V_Z][V_Z] = vz.v[V_Z];
145
 
146
        mat->m[V_W][V_X] = xform->t.look_up.t.v[V_X];
147
        mat->m[V_W][V_Y] = xform->t.look_up.t.v[V_Y];
148
        mat->m[V_W][V_Z] = xform->t.look_up.t.v[V_Z];
149
    } break;
150
 
151
    case BR_TRANSFORM_IDENTITY:
152
        BrMatrix34Identity(mat);
153
        break;
154
    }
155
}
156
 
157
// IDA: void __cdecl BrMatrix34PreTransform(br_matrix34 *mat, br_transform *xform)
158
void BrMatrix34PreTransform(br_matrix34* mat, br_transform* xform) {
159
    br_matrix34 tmp;
160
    LOG_TRACE("(%p, %p)", mat, xform);
161
 
162
    if (xform->type == BR_TRANSFORM_IDENTITY) {
163
        return;
164
    }
165
 
166
    BrTransformToMatrix34(&tmp, xform);
167
    BrMatrix34Pre(mat, &tmp);
168
}
169
 
170
// IDA: void __cdecl BrMatrix34PostTransform(br_matrix34 *mat, br_transform *xform)
171
void BrMatrix34PostTransform(br_matrix34* mat, br_transform* xform) {
172
    br_matrix34 tmp;
173
    LOG_TRACE("(%p, %p)", mat, xform);
174
 
175
    if (xform->type == BR_TRANSFORM_IDENTITY) {
176
        return;
177
    }
178
 
179
    BrTransformToMatrix34(&tmp, xform);
180
    BrMatrix34Post(mat, &tmp);
181
}
182
 
183
// IDA: void __cdecl BrMatrix4PreTransform(br_matrix4 *mat, br_transform *xform)
184
void BrMatrix4PreTransform(br_matrix4* mat, br_transform* xform) {
185
    //br_matrix34 tmp; // Pierre-Marie Baty -- unused variable
186
    LOG_TRACE("(%p, %p)", mat, xform);
187
    NOT_IMPLEMENTED();
188
}
189
 
190
// IDA: void __cdecl BrMatrix34ToTransform(br_transform *xform, br_matrix34 *mat)
191
void BrMatrix34ToTransform(br_transform* xform, br_matrix34* mat) {
192
    LOG_TRACE("(%p, %p)", xform, mat);
193
 
194
    switch (xform->type) {
195
    case BR_TRANSFORM_MATRIX34:
196
        memcpy(&xform->t.mat, mat, sizeof(br_matrix34));
197
        break;
198
    case BR_TRANSFORM_MATRIX34_LP:
199
        memcpy(&xform->t.mat, mat, sizeof(br_matrix34));
200
        BrMatrix34LPNormalise(&xform->t.mat, &xform->t.mat);
201
        break;
202
    case BR_TRANSFORM_QUAT:
203
        BrMatrix34ToQuat(&xform->t.quat.q, mat);
204
        BrVector3Copy(&xform->t.quat.t, (br_vector3*)mat->m[3]);
205
        break;
206
    case BR_TRANSFORM_EULER:
207
        BrMatrix34ToEuler(&xform->t.euler.e, mat);
208
        BrVector3Copy(&xform->t.quat.t, (br_vector3*)mat->m[3]);
209
        break;
210
    case BR_TRANSFORM_LOOK_UP:
211
        NOT_IMPLEMENTED();
212
        break;
213
    case BR_TRANSFORM_TRANSLATION:
214
        BrVector3Copy(&xform->t.quat.t, (br_vector3*)mat->m[3]);
215
        break;
216
    default:
217
        TELL_ME_IF_WE_PASS_THIS_WAY();
218
    }
219
}
220
 
221
// IDA: void __cdecl BrTransformToTransform(br_transform *dest, br_transform *src)
222
void BrTransformToTransform(br_transform* dest, br_transform* src) {
223
    br_matrix34 temp;
224
    LOG_TRACE("(%p, %p)", dest, src);
225
 
226
    if (src->type == dest->type) {
227
        memcpy(dest, src, sizeof(br_transform));
228
        return;
229
    }
230
    switch (dest->type) {
231
    case BR_TRANSFORM_MATRIX34:
232
        BrTransformToMatrix34(&dest->t.mat, src);
233
        break;
234
    case BR_TRANSFORM_MATRIX34_LP:
235
        BrTransformToMatrix34(&dest->t.mat, src);
236
        BrMatrix34LPNormalise(&dest->t.mat, &dest->t.mat);
237
        break;
238
    default:
239
        BrTransformToMatrix34(&temp, src);
240
        BrMatrix34ToTransform(dest, &temp);
241
    }
242
}