-
Notifications
You must be signed in to change notification settings - Fork 0
/
fillrate.h
327 lines (266 loc) · 7.57 KB
/
fillrate.h
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
#pragma once
#include "framebuffer.h"
#include "renderfunc.h"
#include "sbuffer.h"
#include "trunc.h"
const float minD = 0.001f;
struct ScreenLine : MSlice<int>
{
Face *f1, *f2;
bool valid;
};
struct Bound
{
int v1, v2;
};
struct ScreenZone : MSlice<Bound>
{
Face *f;
ScreenZone() {}
void Init (Face* af, int b1, int b2) {
this->f = af;
MSlice<Bound>::Init(b1,b2,PK_TEMP);
}
ScreenZone (Face* f, int b1, int b2) : f(f), MSlice<Bound> (b1,b2,PK_TEMP) {}
};
struct ShowSectorInfo
{
FrameWindow b;
SBuffer filled;
MSlice<int16_t> cut;
Matrix3D m;
Point3D viewP, plt,dx,dy;
ScreenZone mainZ;
MArray<CheckPtr<Face> > faces;
MArray<CheckPtr<Sector> > nearSectors;
bool useTransparent;
};
struct TemporaryShowSectorInfo
{
Sector &s;
bool onlyAff;
const ScreenZone &curZ;
Point3D nTest;
TemporaryShowSectorInfo (Sector&s, bool onlyAff, const ScreenZone &curZ, Point3D nTest)
: s(s),onlyAff(onlyAff),curZ(curZ),nTest(nTest) {}
private :
TemporaryShowSectorInfo(const TemporaryShowSectorInfo&);
TemporaryShowSectorInfo&operator=(const TemporaryShowSectorInfo&);
};
void ShowSector(ShowSectorInfo& si, const TemporaryShowSectorInfo& tsi, bool cutCheck);
const int startDiseringX[4] = { -0x6000, +0x2000, -0x2000, +0x6000 };
const int startDiseringY[4] = { -0x2000, +0x6000, -0x6000, +0x2000 };
void LoopDith(Pixel* l, int count, Pixel* txrBeg, int shX, int mask, int tx, int dtx, int ty, int dty, int ord)
{
/*
assert (ord>=0 && ord<4);
int dsx = startDiseringX[ord], dsy = startDiseringY[ord];*/
switch (ord)
{
case 0 :
tx += 0x4000;
for (;;)
{
if (count-- == 0) break;
*l++ = txrBeg [ (((tx)>>16) + (((ty)>>16)<<shX)) & mask ];
tx += dtx;
ty += dty;
if (count-- == 0) break;
*l++ = txrBeg [ (((tx + 0x4000)>>16) + (((ty + 0xc000)>>16)<<shX)) & mask ];
tx += dtx;
ty += dty;
}
break;
case 1 :
tx += 0x4000;
for (;;)
{
if (count-- == 0) break;
*l++ = txrBeg [ (((tx + 0x4000)>>16) + (((ty + 0xc000)>>16)<<shX)) & mask ];
tx += dtx;
ty += dty;
if (count-- == 0) break;
*l++ = txrBeg [ (((tx)>>16) + (((ty)>>16)<<shX)) & mask ];
tx += dtx;
ty += dty;
}
break;
case 2 :
ty += 0x4000;
for (;;)
{
if (count-- == 0) break;
*l++ = txrBeg [ (((tx + 0xc000)>>16) + (((ty + 0x4000)>>16)<<shX)) & mask ];
tx += dtx;
ty += dty;
if (count-- == 0) break;
*l++ = txrBeg [ (((tx)>>16) + (((ty )>>16)<<shX)) & mask ];
tx += dtx;
ty += dty;
}
break;
case 3 :
ty += 0x4000;
for (;;)
{
if (count-- == 0) break;
*l++ = txrBeg [ (((tx)>>16) + (((ty )>>16)<<shX)) & mask ];
tx += dtx;
ty += dty;
if (count-- == 0) break;
*l++ = txrBeg [ (((tx + 0xc000)>>16) + (((ty + 0x4000)>>16)<<shX)) & mask ];
tx += dtx;
ty += dty;
}
break;
}
}
void LoopNoDith(Pixel* l, int count, TexturePixel* txrBeg, int tx, int dtx, int ty, int dty)
{
for(;;)
{
*l++ = Pixel(txrBeg [ ((tx>>16) + ((ty>>16)<<6)) & 4095 ]);
if (--count == 0)
break;
tx += dtx;
ty += dty;
}
}
void TransparentLoopNoDith(Pixel* l, int count, TexturePixel* txrBeg, int tx, int dtx, int ty, int dty)
{
for(;;)
{
TexturePixel src = txrBeg [ ((tx>>16) + ((ty>>16)<<6)) & 4095 ];
*l += Pixel(Pixel(src) - *l) >> (src >> 8);
++l;
if (--count == 0)
break;
tx += dtx;
ty += dty;
}
}
void RenderLine (FrameWindow::Line l, RenderPoint& p1, RenderPoint& p2, int tail, const RenderInfo &ri)
{
int dtx,dty;
if (p1.x<p2.x+tail-1)
{
const float dx = 1.0f / (p2.x-p1.x);
dtx = HackTrunc((p2.txw-p1.txw)*dx, 16);
dty = HackTrunc((p2.tyw-p1.tyw)*dx, 16);
} else
{
dtx = 0;
dty = 0;
}
int tx = HackTrunc(p1.txw, 16);
int ty = HackTrunc(p1.tyw, 16);
/*
if (ri.texture->Dithering())
LoopDith(&l[p1.x], p2.x+tail-p1.x, ri.texture->data(), ri.texture->ShX(), ri.texture->MaskAll(), tx, dtx, ty, dty, (p1.x&1) + ((p1.y&1)<<1));
else*/
if (ri.useTransparent)
TransparentLoopNoDith(&l[p1.x], p2.x+tail-p1.x, ri.texture->data(), tx, dtx, ty, dty);
else
LoopNoDith(&l[p1.x], p2.x+tail-p1.x, ri.texture->data(), tx, dtx, ty, dty);
}
void ComputeRenderInfo(ShowSectorInfo& si, const TemporaryShowSectorInfo& tsi, const ScreenZone &z, RenderInfo& ri)
{
TaktCounter tc(TIMER3);
const float den = 1.0f / (-z.f->Dist(tsi.nTest));//dist îòðèöàòåëüíî åñëè ìû âíóòðè
const float dxn = Dot(si.dx, z.f->n);
const float dyn = Dot(si.dy, z.f->n);
const float pltn = Dot(si.plt, z.f->n);
const float dxtx = Dot(si.dx, z.f->vtx);
const float dytx = Dot(si.dy, z.f->vtx);
const float plttx = Dot(si.plt, z.f->vtx);
const float nvtx = Dot(tsi.nTest, z.f->vtx);
const float dxty = Dot(si.dx, z.f->vty);
const float dyty = Dot(si.dy, z.f->vty);
const float pltty = Dot(si.plt, z.f->vty);
const float nvty = Dot(tsi.nTest, z.f->vty);
ri.w.vdx = dxn * den;
ri.w.vdy = dyn * den;
ri.w.vplt = pltn * den;
ri.tx.vdx = ri.w.vdx * (nvtx - z.f->vtxc) + dxtx;
ri.tx.vdy = ri.w.vdy * (nvtx - z.f->vtxc) + dytx;
ri.tx.vplt = ri.w.vplt * (nvtx - z.f->vtxc) + plttx;
ri.ty.vdx = ri.w.vdx * (nvty - z.f->vtyc) + dxty;
ri.ty.vdy = ri.w.vdy * (nvty - z.f->vtyc) + dyty;
ri.ty.vplt = ri.w.vplt * (nvty - z.f->vtyc) + pltty;
ri.w .Correct(si.b.SizeX(), si.b.SizeY());
ri.tx.Correct(si.b.SizeX(), si.b.SizeY());
ri.ty.Correct(si.b.SizeX(), si.b.SizeY());
ri.texture = z.f->texture;
assert(ri.texture);
ri.light = z.f->light;
ri.useTransparent = si.useTransparent;
}
void Fill (ShowSectorInfo& si, const TemporaryShowSectorInfo& tsi, const ScreenZone &z)
{
int y1 = z.Low(), y2 = z.High()-1;
if (y1>y2) return;
while (y1<=y2 && z[y1].v1 >= z[y1].v2)
++y1;
while (y1<=y2 && z[y2].v1 >= z[y2].v2)
--y2;
if (y1<=y2)
{
Sector* s = z.f->nextSector;
if (s)
{
if (s->inProcess==0)
ShowSector(si, TemporaryShowSectorInfo(*s, tsi.onlyAff, z, s->skybox ? Point3D(0.0f,0.0f,0.0f) : tsi.nTest), false);
} else
{
assert(z.f->texture);
FrameWindow::Line l = si.b[y1];
const int afflen = si.b.SizeX()<=400 ? 16 : 32; // äëèíà àôôèííûõ áëîêîâ
RenderInfo& ri = z.f->renderInfo;
bool computed = false;
for (int y = y1; y<=y2; ++y)
{
assert(z[y].v1>=0 && z[y].v2<=si.b.SizeX());
if (z[y].v1<z[y].v2)
{
int cutlines = si.useTransparent ?
si.filled.CutLineNoChange(z[y].v1, z[y].v2, y, si.cut):
si.filled.CutLine(z[y].v1, z[y].v2, y, si.cut);
for (int sbl=0; sbl<cutlines; ++sbl)
{
if (!computed)
{
ComputeRenderInfo(si, tsi, z, ri);
computed = true;
}
RenderPoint rp1(ri, si.cut[sbl*2 ] , y);
RenderPoint rp2(ri, si.cut[sbl*2+1]-1, y);
rp1.Precompute();
rp2.Precompute();
if (rp1.x<=rp2.x)
{
if (tsi.onlyAff)
{
RenderLine(l,rp1,rp2,1,ri);
} else for (;;)
{
const int nx = (rp1.x+afflen) & ~(afflen-1);
if (nx<=rp2.x)
{
RenderPoint mid(ri,nx,y);
mid.Precompute();
RenderLine(l,rp1,mid,0,ri);
rp1=mid;
} else
{
RenderLine(l,rp1,rp2,1,ri);
break;
}
}
}
}
}
si.b.ProcessLine(l);
}
}
}
}