summaryrefslogtreecommitdiffstats
path: root/rwsdk/include/d3d8/rtbezpat.h
blob: 8a5960cb6a8d52b3919377a9c7a8dfac9f11f218 (plain) (blame)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * Data structures for rtbezpat toolkit
 * Copyright (c) Criterion Software Limited
 */

#if (!defined(RTBEZPAT_H))
#define RTBEZPAT_H

/**
 * \defgroup rtbezpatch RtBezPat
 * \ingroup rttool
 *
 * The Bezier Patch Toolkit is a group of functions that support the way
 * RenderWare processes patches.
 */

/**
 * \ingroup rtbezpatch
 * \typedef RtBezierV4d
 * typedef for struct RtBezierV4d.
 */
typedef struct RtBezierV4d RtBezierV4d;

/**
 * \ingroup rtbezpatch
 * \struct RtBezierV4d
 *  This type represents 4d points and vectors specified by
 * the (x, y, z, w) coordinates of a 4d point or
 * the (x, y, z, w) components of a 4d vector.
 */
struct RtBezierV4d
{
    RwReal              x;
                /**< X value */
    RwReal              y;
                /**< Y value */
    RwReal              z;
                /**< Z value */
    RwReal              w;
                /**< W value */
};

/**
 * \ingroup rtbezpatch
 * \typedef RtBezierRow
 *  typedef for a row of vectors.
 * RtBezierRow is an  array of 4 vectors
 */
typedef RtBezierV4d RtBezierRow[4];

/**
 * \ingroup rtbezpatch
 * \typedef RtBezierMatrix
 *  typedef for a matrix of 4*4 vectors.
 *  RtBezierMatrix is an array of 4 rows.
 */
typedef RtBezierRow RtBezierMatrix[4];

/*
 * Bernstein polynomials;
 */

#define RtBern03(_u, _cu) ( (_cu) * (_cu) * (_cu) )
#define RtBern13(_u, _cu) ( 3 * (_u) * (_cu) * (_cu) )
#define RtBern23(_u, _cu) ( 3 * (_u) * (_u) * (_cu) )
#define RtBern33(_u, _cu) ( (_u) * (_u) * (_u) )

#define RtBezierQuadSample3dMacro(_out, _P, _u, _v)                     \
MACRO_START                                                             \
{                                                                       \
    const RtBezierV4d       * const P0 = &(_P)[0][0];                   \
    const RtBezierV4d       * const P1 = &(_P)[1][0];                   \
    const RtBezierV4d       * const P2 = &(_P)[2][0];                   \
    const RtBezierV4d       * const P3 = &(_P)[3][0];                   \
    const RwReal              _pu = (_u);                               \
    const RwReal              _cu = ((RwReal)1) - _pu;                  \
    const RwReal              B03u = RtBern03(_pu,_cu);                 \
    const RwReal              B13u = RtBern13(_pu,_cu);                 \
    const RwReal              B23u = RtBern23(_pu,_cu);                 \
    const RwReal              B33u = RtBern33(_pu,_cu);                 \
    const RwReal              _pv = (_v);                               \
    const RwReal              _cv = ((RwReal)1) - _pv;                  \
    const RwReal              B03v = RtBern03(_pv,_cv);                 \
    const RwReal              B13v = RtBern13(_pv,_cv);                 \
    const RwReal              B23v = RtBern23(_pv,_cv);                 \
    const RwReal              B33v = RtBern33(_pv,_cv);                 \
    RtBezierRow               A;                                        \
                                                                        \
    A[0].x = B03u*P0[0].x + B13u*P1[0].x + B23u*P2[0].x + B33u*P3[0].x; \
    A[0].y = B03u*P0[0].y + B13u*P1[0].y + B23u*P2[0].y + B33u*P3[0].y; \
    A[0].z = B03u*P0[0].z + B13u*P1[0].z + B23u*P2[0].z + B33u*P3[0].z; \
    A[1].x = B03u*P0[1].x + B13u*P1[1].x + B23u*P2[1].x + B33u*P3[1].x; \
    A[1].y = B03u*P0[1].y + B13u*P1[1].y + B23u*P2[1].y + B33u*P3[1].y; \
    A[1].z = B03u*P0[1].z + B13u*P1[1].z + B23u*P2[1].z + B33u*P3[1].z; \
    A[2].x = B03u*P0[2].x + B13u*P1[2].x + B23u*P2[2].x + B33u*P3[2].x; \
    A[2].y = B03u*P0[2].y + B13u*P1[2].y + B23u*P2[2].y + B33u*P3[2].y; \
    A[2].z = B03u*P0[2].z + B13u*P1[2].z + B23u*P2[2].z + B33u*P3[2].z; \
    A[3].x = B03u*P0[3].x + B13u*P1[3].x + B23u*P2[3].x + B33u*P3[3].x; \
    A[3].y = B03u*P0[3].y + B13u*P1[3].y + B23u*P2[3].y + B33u*P3[3].y; \
    A[3].z = B03u*P0[3].z + B13u*P1[3].z + B23u*P2[3].z + B33u*P3[3].z; \
                                                                        \
    (_out)->x = A[0].x*B03v + A[1].x*B13v + A[2].x*B23v + A[3].x*B33v;  \
    (_out)->y = A[0].y*B03v + A[1].y*B13v + A[2].y*B23v + A[3].y*B33v;  \
    (_out)->z = A[0].z*B03v + A[1].z*B13v + A[2].z*B23v + A[3].z*B33v;  \
}                                                                       \
MACRO_STOP

#define RtBezierQuadDifferenceStepU3dMacro(_row)        \
MACRO_START                                             \
{                                                       \
    (_row)[0].x += (_row)[1].x;                         \
    (_row)[0].y += (_row)[1].y;                         \
    (_row)[0].z += (_row)[1].z;                         \
                                                        \
    (_row)[1].x += (_row)[2].x;                         \
    (_row)[1].y += (_row)[2].y;                         \
    (_row)[1].z += (_row)[2].z;                         \
                                                        \
    (_row)[2].x += (_row)[3].x;                         \
    (_row)[2].y += (_row)[3].y;                         \
    (_row)[2].z += (_row)[3].z;                         \
                                                        \
}                                                       \
MACRO_STOP

#define RtBezierQuadDifferenceStepU4dMacro(_row)        \
MACRO_START                                             \
{                                                       \
    (_row)[0].x += (_row)[1].x;                         \
    (_row)[0].y += (_row)[1].y;                         \
    (_row)[0].z += (_row)[1].z;                         \
    (_row)[0].w += (_row)[1].w;                         \
                                                        \
    (_row)[1].x += (_row)[2].x;                         \
    (_row)[1].y += (_row)[2].y;                         \
    (_row)[1].z += (_row)[2].z;                         \
    (_row)[1].w += (_row)[2].w;                         \
                                                        \
    (_row)[2].x += (_row)[3].x;                         \
    (_row)[2].y += (_row)[3].y;                         \
    (_row)[2].z += (_row)[3].z;                         \
    (_row)[2].w += (_row)[3].w;                         \
                                                        \
}                                                       \
MACRO_STOP

#define RtBezierQuadDifferenceStepV3dMacro(_mat)        \
MACRO_START                                             \
{                                                       \
    RtBezierV4d      * const m0 = &(_mat)[0][0];        \
    RtBezierV4d      * const m1 = &(_mat)[1][0];        \
    RtBezierV4d      * const m2 = &(_mat)[2][0];        \
    RtBezierV4d      * const m3 = &(_mat)[3][0];        \
                                                        \
    /* (_row) 0 */                                      \
    m0[0].x += m1[0].x;                                 \
    m0[0].y += m1[0].y;                                 \
    m0[0].z += m1[0].z;                                 \
                                                        \
    m0[1].x += m1[1].x;                                 \
    m0[1].y += m1[1].y;                                 \
    m0[1].z += m1[1].z;                                 \
                                                        \
    m0[2].x += m1[2].x;                                 \
    m0[2].y += m1[2].y;                                 \
    m0[2].z += m1[2].z;                                 \
                                                        \
    m0[3].x += m1[3].x;                                 \
    m0[3].y += m1[3].y;                                 \
    m0[3].z += m1[3].z;                                 \
                                                        \
    /* (_row) 1 */                                      \
    m1[0].x += m2[0].x;                                 \
    m1[0].y += m2[0].y;                                 \
    m1[0].z += m2[0].z;                                 \
                                                        \
    m1[1].x += m2[1].x;                                 \
    m1[1].y += m2[1].y;                                 \
    m1[1].z += m2[1].z;                                 \
                                                        \
    m1[2].x += m2[2].x;                                 \
    m1[2].y += m2[2].y;                                 \
    m1[2].z += m2[2].z;                                 \
                                                        \
    m1[3].x += m2[3].x;                                 \
    m1[3].y += m2[3].y;                                 \
    m1[3].z += m2[3].z;                                 \
                                                        \
    /* (_row) 2 */                                      \
    m2[0].x += m3[0].x;                                 \
    m2[0].y += m3[0].y;                                 \
    m2[0].z += m3[0].z;                                 \
                                                        \
    m2[1].x += m3[1].x;                                 \
    m2[1].y += m3[1].y;                                 \
    m2[1].z += m3[1].z;                                 \
                                                        \
    m2[2].x += m3[2].x;                                 \
    m2[2].y += m3[2].y;                                 \
    m2[2].z += m3[2].z;                                 \
                                                        \
    m2[3].x += m3[3].x;                                 \
    m2[3].y += m3[3].y;                                 \
    m2[3].z += m3[3].z;                                 \
}                                                       \
MACRO_STOP

#define RtBezierQuadDifferenceStepV4dMacro(_mat)        \
MACRO_START                                             \
{                                                       \
    RtBezierV4d      * const m0 = &(_mat)[0][0];        \
    RtBezierV4d      * const m1 = &(_mat)[1][0];        \
    RtBezierV4d      * const m2 = &(_mat)[2][0];        \
    RtBezierV4d      * const m3 = &(_mat)[3][0];        \
                                                        \
    /* (_row) 0 */                                      \
    m0[0].x += m1[0].x;                                 \
    m0[0].y += m1[0].y;                                 \
    m0[0].z += m1[0].z;                                 \
    m0[0].w += m1[0].w;                                 \
                                                        \
    m0[1].x += m1[1].x;                                 \
    m0[1].y += m1[1].y;                                 \
    m0[1].z += m1[1].z;                                 \
    m0[1].w += m1[1].w;                                 \
                                                        \
    m0[2].x += m1[2].x;                                 \
    m0[2].y += m1[2].y;                                 \
    m0[2].z += m1[2].z;                                 \
    m0[2].w += m1[2].w;                                 \
                                                        \
    m0[3].x += m1[3].x;                                 \
    m0[3].y += m1[3].y;                                 \
    m0[3].z += m1[3].z;                                 \
    m0[3].w += m1[3].w;                                 \
                                                        \
    /* (_row) 1 */                                      \
    m1[0].x += m2[0].x;                                 \
    m1[0].y += m2[0].y;                                 \
    m1[0].z += m2[0].z;                                 \
    m1[0].w += m2[0].w;                                 \
                                                        \
    m1[1].x += m2[1].x;                                 \
    m1[1].y += m2[1].y;                                 \
    m1[1].z += m2[1].z;                                 \
    m1[1].w += m2[1].w;                                 \
                                                        \
    m1[2].x += m2[2].x;                                 \
    m1[2].y += m2[2].y;                                 \
    m1[2].z += m2[2].z;                                 \
    m1[2].w += m2[2].w;                                 \
                                                        \
    m1[3].x += m2[3].x;                                 \
    m1[3].y += m2[3].y;                                 \
    m1[3].z += m2[3].z;                                 \
    m1[3].w += m2[3].w;                                 \
                                                        \
    /* (_row) 2 */                                      \
    m2[0].x += m3[0].x;                                 \
    m2[0].y += m3[0].y;                                 \
    m2[0].z += m3[0].z;                                 \
    m2[0].w += m3[0].w;                                 \
                                                        \
    m2[1].x += m3[1].x;                                 \
    m2[1].y += m3[1].y;                                 \
    m2[1].z += m3[1].z;                                 \
    m2[1].w += m3[1].w;                                 \
                                                        \
    m2[2].x += m3[2].x;                                 \
    m2[2].y += m3[2].y;                                 \
    m2[2].z += m3[2].z;                                 \
    m2[2].w += m3[2].w;                                 \
                                                        \
    m2[3].x += m3[3].x;                                 \
    m2[3].y += m3[3].y;                                 \
    m2[3].z += m3[3].z;                                 \
    m2[3].w += m3[3].w;                                 \
}                                                       \
MACRO_STOP

#ifdef    __cplusplus
extern              "C"
{
#endif                          /* __cplusplus */
extern void
RtBezierQuadControlFit3d(RtBezierMatrix B,
                             RtBezierMatrix P);

extern void
RtBezierQuadBernsteinWeight3d(RtBezierMatrix W,
                              RtBezierMatrix B);

extern void
RtBezierQuadBernsteinWeight4d(RtBezierMatrix W,
                              RtBezierMatrix B);

extern void
RtBezierQuadPointDifference3d(RtBezierMatrix D,
                                         RtBezierMatrix W,
                                         RwReal PointU,
                                         RwReal PointV,
                                         RwReal StepU,
                                         RwReal stepV);

extern void
RtBezierQuadPointDifference4d(RtBezierMatrix D,
                                         RtBezierMatrix W,
                                         RwReal PointU,
                                         RwReal PointV,
                                         RwReal StepU,
                                         RwReal stepV);

extern void
RtBezierQuadOriginDifference3d(RtBezierMatrix D,
                                        RtBezierMatrix W,
                                        RwReal stepU,
                                        RwReal setpV);

extern void
RtBezierQuadOriginDifference4d(RtBezierMatrix D,
                                        RtBezierMatrix W,
                                        RwReal stepU,
                                        RwReal setpV);

#if ( defined(RWDEBUG) || defined(RWSUPPRESSINLINE) )

extern void
RtBezierQuadSample3d(RwV3d * out,
                         RtBezierMatrix B,
                         RwReal u,
                         RwReal v);

extern void         RtBezierQuadDifferenceStepU3d(RtBezierRow row);
extern void         RtBezierQuadDifferenceStepU4d(RtBezierRow row);

extern void         RtBezierQuadDifferenceStepV3d(RtBezierMatrix mat);
extern void         RtBezierQuadDifferenceStepV4d(RtBezierMatrix mat);


#else  /* ( defined(RWDEBUG) || defined(RWSUPPRESSINLINE) ) */

#define RtBezierQuadSample3d(_out, _P, _u, _v)          \
        RtBezierQuadSample3dMacro(_out, _P, _u, _v)

#define RtBezierQuadDifferenceStepU3d(_row)             \
        RtBezierQuadDifferenceStepU3dMacro(_row)

#define RtBezierQuadDifferenceStepU4d(_row)             \
        RtBezierQuadDifferenceStepU4dMacro(_row)

#define RtBezierQuadDifferenceStepV3d(_mat)             \
        RtBezierQuadDifferenceStepV3dMacro(_mat)

#define RtBezierQuadDifferenceStepV4d(_mat)             \
        RtBezierQuadDifferenceStepV4dMacro(_mat)

#endif  /* ( defined(RWDEBUG) || defined(RWSUPPRESSINLINE) ) */

/*
 *
 */

extern void
RtBezierTriangleControlFit3d(RtBezierMatrix T, RtBezierMatrix P);

extern void
RtBezierQuadFromTriangle(RtBezierMatrix Q, RtBezierMatrix T);

extern void
RtBezierQuadTangent(RtBezierMatrix D,
                    RwReal theta,
                    RtBezierMatrix P);

extern void
RtBezierQuadTangentPair(RtBezierMatrix Dt,
                        RtBezierMatrix Dp,
                        RwReal theta, 
                        RtBezierMatrix P);

extern void
RtBezierQuadGetNormals(RtBezierMatrix N,
                             RtBezierMatrix B);

#if (defined(RWDEBUG) && defined(RWVERBOSE))

extern void
_rtBezierGnuPlot(RtBezierMatrix B, 
                 RwChar * name,
                 RwChar * title);

#else                           /* (defined(RWDEBUG) && defined(RWVERBOSE)) */

#define _rtBezierGnuPlot(B, name, title) /* No-op */

#endif                          /* (defined(RWDEBUG) && defined(RWVERBOSE)) */

#ifdef    __cplusplus
}
#endif                          /* __cplusplus */

#endif                          /* (!defined(RTBEZPAT_H)) */