-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDIVFXFL.CPP
293 lines (242 loc) · 9.06 KB
/
DIVFXFL.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
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
/*----------------------------------------------------------------------------
Texture Test Program - a cheesy test harness for texture mapping
by Chris Hecker for my Game Developer Magazine articles. See my homepage
for more information.
NOTE: This is a hacked test program, not a nice example of Windows programming.
The texture mappers are the only part of this you should look at.
This material is Copyright 1997 Chris Hecker, All Rights Reserved.
It's for you to read and learn from, not to put in your own articles
or books or on your website, etc. Thank you.
Chris Hecker
http://www.d6.com/users/checker
*/
/******** Perspective texture mapper *********/
#include <math.h>
#include <assert.h>
#include "mappers.h"
/******** structures, inlines, and function declarations **********/
struct gradients_fx_fl {
gradients_fx_fl( POINT3D const *pVertices );
float aOneOverZ[3]; // 1/z for each vertex
float aUOverZ[3]; // u/z for each vertex
float aVOverZ[3]; // v/z for each vertex
float dOneOverZdX, dOneOverZdY; // d(1/z)/dX, d(1/z)/dY
float dUOverZdX, dUOverZdY; // d(u/z)/dX, d(u/z)/dY
float dVOverZdX, dVOverZdY; // d(v/z)/dX, d(v/z)/dY
};
struct edge_fx_fl {
edge_fx_fl(gradients_fx_fl const &Gradients, POINT3D const *pVertices, int Top,
int Bottom );
inline int Step( void );
long X, XStep, Numerator, Denominator; // DDA info for x
long ErrorTerm;
int Y, Height; // current y and vertical count
float OneOverZ, OneOverZStep, OneOverZStepExtra;// 1/z and step
float UOverZ, UOverZStep, UOverZStepExtra; // u/z and step
float VOverZ, VOverZStep, VOverZStepExtra; // v/z and step
};
inline int edge_fx_fl::Step( void ) {
X += XStep; Y++; Height--;
UOverZ += UOverZStep; VOverZ += VOverZStep; OneOverZ += OneOverZStep;
ErrorTerm += Numerator;
if(ErrorTerm >= Denominator) {
X++;
ErrorTerm -= Denominator;
OneOverZ += OneOverZStepExtra;
UOverZ += UOverZStepExtra; VOverZ += VOverZStepExtra;
}
return Height;
}
void DrawScanLine( dib_info const &Dest, gradients_fx_fl const &Gradients,
edge_fx_fl *pLeft, edge_fx_fl *pRight, dib_info const &Texture );
/******** TextureMapTriangle **********/
void TextureMapTriangle_div_fx_fl( dib_info const &Dest,
POINT3D const *pVertices, dib_info const &Texture )
{
int Top, Middle, Bottom, MiddleForCompare, BottomForCompare;
fixed28_4 Y0 = pVertices[0].fxfl.Y, Y1 = pVertices[1].fxfl.Y,
Y2 = pVertices[2].fxfl.Y;
// sort vertices in y
if(Y0 < Y1) {
if(Y2 < Y0) {
Top = 2; Middle = 0; Bottom = 1;
MiddleForCompare = 0; BottomForCompare = 1;
} else {
Top = 0;
if(Y1 < Y2) {
Middle = 1; Bottom = 2;
MiddleForCompare = 1; BottomForCompare = 2;
} else {
Middle = 2; Bottom = 1;
MiddleForCompare = 2; BottomForCompare = 1;
}
}
} else {
if(Y2 < Y1) {
Top = 2; Middle = 1; Bottom = 0;
MiddleForCompare = 1; BottomForCompare = 0;
} else {
Top = 1;
if(Y0 < Y2) {
Middle = 0; Bottom = 2;
MiddleForCompare = 3; BottomForCompare = 2;
} else {
Middle = 2; Bottom = 0;
MiddleForCompare = 2; BottomForCompare = 3;
}
}
}
gradients_fx_fl Gradients(pVertices);
edge_fx_fl TopToBottom(Gradients,pVertices,Top,Bottom);
edge_fx_fl TopToMiddle(Gradients,pVertices,Top,Middle);
edge_fx_fl MiddleToBottom(Gradients,pVertices,Middle,Bottom);
edge_fx_fl *pLeft, *pRight;
int MiddleIsLeft;
// the triangle is clockwise, so if bottom > middle then middle is right
if(BottomForCompare > MiddleForCompare) {
MiddleIsLeft = 0;
pLeft = &TopToBottom; pRight = &TopToMiddle;
} else {
MiddleIsLeft = 1;
pLeft = &TopToMiddle; pRight = &TopToBottom;
}
int Height = TopToMiddle.Height;
while(Height--) {
DrawScanLine(Dest,Gradients,pLeft,pRight,Texture);
TopToMiddle.Step(); TopToBottom.Step();
}
Height = MiddleToBottom.Height;
if(MiddleIsLeft) {
pLeft = &MiddleToBottom; pRight = &TopToBottom;
} else {
pLeft = &TopToBottom; pRight = &MiddleToBottom;
}
while(Height--) {
DrawScanLine(Dest,Gradients,pLeft,pRight,Texture);
MiddleToBottom.Step(); TopToBottom.Step();
}
}
/********** gradients_fx_fl constructor **********/
gradients_fx_fl::gradients_fx_fl( POINT3D const *pVertices )
{
int Counter;
fixed28_4 X1Y0 = Fixed28_4Mul(pVertices[1].fxfl.X - pVertices[2].fxfl.X,
pVertices[0].fxfl.Y - pVertices[2].fxfl.Y);
fixed28_4 X0Y1 = Fixed28_4Mul(pVertices[0].fxfl.X - pVertices[2].fxfl.X,
pVertices[1].fxfl.Y - pVertices[2].fxfl.Y);
float OneOverdX = 1.0 / Fixed28_4ToFloat(X1Y0 - X0Y1);
float OneOverdY = -OneOverdX;
for(Counter = 0;Counter < 3;Counter++)
{
float const OneOverZ = 1/pVertices[Counter].fxfl.Z;
aOneOverZ[Counter] = OneOverZ;
aUOverZ[Counter] = pVertices[Counter].fxfl.U * OneOverZ;
aVOverZ[Counter] = pVertices[Counter].fxfl.V * OneOverZ;
}
dOneOverZdX = OneOverdX * (((aOneOverZ[1] - aOneOverZ[2]) *
Fixed28_4ToFloat(pVertices[0].fxfl.Y - pVertices[2].fxfl.Y)) -
((aOneOverZ[0] - aOneOverZ[2]) *
Fixed28_4ToFloat(pVertices[1].fxfl.Y - pVertices[2].fxfl.Y)));
dOneOverZdY = OneOverdY * (((aOneOverZ[1] - aOneOverZ[2]) *
Fixed28_4ToFloat(pVertices[0].fxfl.X - pVertices[2].fxfl.X)) -
((aOneOverZ[0] - aOneOverZ[2]) *
Fixed28_4ToFloat(pVertices[1].fxfl.X - pVertices[2].fxfl.X)));
dUOverZdX = OneOverdX * (((aUOverZ[1] - aUOverZ[2]) *
Fixed28_4ToFloat(pVertices[0].fxfl.Y - pVertices[2].fxfl.Y)) -
((aUOverZ[0] - aUOverZ[2]) *
Fixed28_4ToFloat(pVertices[1].fxfl.Y - pVertices[2].fxfl.Y)));
dUOverZdY = OneOverdY * (((aUOverZ[1] - aUOverZ[2]) *
Fixed28_4ToFloat(pVertices[0].fxfl.X - pVertices[2].fxfl.X)) -
((aUOverZ[0] - aUOverZ[2]) *
Fixed28_4ToFloat(pVertices[1].fxfl.X - pVertices[2].fxfl.X)));
dVOverZdX = OneOverdX * (((aVOverZ[1] - aVOverZ[2]) *
Fixed28_4ToFloat(pVertices[0].fxfl.Y - pVertices[2].fxfl.Y)) -
((aVOverZ[0] - aVOverZ[2]) *
Fixed28_4ToFloat(pVertices[1].fxfl.Y - pVertices[2].fxfl.Y)));
dVOverZdY = OneOverdY * (((aVOverZ[1] - aVOverZ[2]) *
Fixed28_4ToFloat(pVertices[0].fxfl.X - pVertices[2].fxfl.X)) -
((aVOverZ[0] - aVOverZ[2]) *
Fixed28_4ToFloat(pVertices[1].fxfl.X - pVertices[2].fxfl.X)));
}
/********** handle floor divides and mods correctly ***********/
inline void FloorDivMod( long Numerator, long Denominator, long &Floor,
long &Mod )
{
assert(Denominator > 0); // we assume it's positive
if(Numerator >= 0) {
// positive case, C is okay
Floor = Numerator / Denominator;
Mod = Numerator % Denominator;
} else {
// Numerator is negative, do the right thing
Floor = -((-Numerator) / Denominator);
Mod = (-Numerator) % Denominator;
if(Mod) {
// there is a remainder
Floor--; Mod = Denominator - Mod;
}
}
}
/********** edge_fx_fl constructor ***********/
edge_fx_fl::edge_fx_fl( gradients_fx_fl const &Gradients, POINT3D const *pVertices, int Top,
int Bottom )
{
Y = Ceil28_4(pVertices[Top].fxfl.Y);
int YEnd = Ceil28_4(pVertices[Bottom].fxfl.Y);
Height = YEnd - Y;
if(Height)
{
long dN = pVertices[Bottom].fxfl.Y - pVertices[Top].fxfl.Y;
long dM = pVertices[Bottom].fxfl.X - pVertices[Top].fxfl.X;
long InitialNumerator = dM*16*Y - dM*pVertices[Top].fxfl.Y +
dN*pVertices[Top].fxfl.X - 1 + dN*16;
FloorDivMod(InitialNumerator,dN*16,X,ErrorTerm);
FloorDivMod(dM*16,dN*16,XStep,Numerator);
Denominator = dN*16;
float YPrestep = Fixed28_4ToFloat(Y*16 - pVertices[Top].fxfl.Y);
float XPrestep = Fixed28_4ToFloat(X*16 - pVertices[Top].fxfl.X);
OneOverZ = Gradients.aOneOverZ[Top]
+ YPrestep * Gradients.dOneOverZdY
+ XPrestep * Gradients.dOneOverZdX;
OneOverZStep = XStep * Gradients.dOneOverZdX
+ Gradients.dOneOverZdY;
OneOverZStepExtra = Gradients.dOneOverZdX;
UOverZ = Gradients.aUOverZ[Top]
+ YPrestep * Gradients.dUOverZdY
+ XPrestep * Gradients.dUOverZdX;
UOverZStep = XStep * Gradients.dUOverZdX
+ Gradients.dUOverZdY;
UOverZStepExtra = Gradients.dUOverZdX;
VOverZ = Gradients.aVOverZ[Top]
+ YPrestep * Gradients.dVOverZdY
+ XPrestep * Gradients.dVOverZdX;
VOverZStep = XStep * Gradients.dVOverZdX
+ Gradients.dVOverZdY;
VOverZStepExtra = Gradients.dVOverZdX;
}
}
/********** DrawScanLine ************/
void DrawScanLine( dib_info const &Dest, gradients_fx_fl const &Gradients,
edge_fx_fl *pLeft, edge_fx_fl *pRight, dib_info const &Texture )
{
int XStart = pLeft->X;
int Width = pRight->X - XStart;
char unsigned *pDestBits = Dest.pBits;
char unsigned * const pTextureBits = Texture.pBits;
pDestBits += pLeft->Y * Dest.DeltaScan + XStart;
long TextureDeltaScan = Texture.DeltaScan;
float OneOverZ = pLeft->OneOverZ;
float UOverZ = pLeft->UOverZ;
float VOverZ = pLeft->VOverZ;
while(Width-- > 0)
{
float Z = 1/OneOverZ;
int U = UOverZ * Z;
int V = VOverZ * Z;
*(pDestBits++) = *(pTextureBits + U + (V * TextureDeltaScan));
OneOverZ += Gradients.dOneOverZdX;
UOverZ += Gradients.dUOverZdX;
VOverZ += Gradients.dVOverZdX;
}
}