-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolydraw.cpp
More file actions
274 lines (212 loc) · 7.38 KB
/
polydraw.cpp
File metadata and controls
274 lines (212 loc) · 7.38 KB
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
#include <stdint.h>
#include "vec.h"
#include "poly.h"
// zpizzheno from fatmap2.zip by MRI\Doomsday just because
// I didn't have ANY time to write my own tmapper :)
static vec3bx * max_vtx; // Max y veci (ending veci)
static vec3bx * start_vtx, * end_vtx; // First and last veci in array
static vec3bx * right_vtx, * left_vtx; // Current right and left veci
static long right_height, left_height;
static long right_x, right_dxdy, left_x, left_dxdy;
static long left_u, left_dudy, left_v, left_dvdy;
static long _dudx, _dvdx;
// edge buffer
struct edge_buffer_t {
uint8_t *p;
int length;
int u, v;
int dudx, dvdx;
int l, dldx;
};
edge_buffer_t edgebuf[256];
// line renderers
#pragma aux __linedrawcall "*_" parm caller [esi] [eax] [ecx] \
value [eax] modify [eax ebx ecx edx esi edi]
#define __edgebufdrawcall __declspec(__pragma("__linedrawcall"))
// esi eax esi
// edgebuf color length
typedef void __edgebufdrawcall (*edgebufdraw_proc_t) (edge_buffer_t*, int32_t, uint32_t);
extern "C" {
void __edgebufdrawcall edge_draw_flat_mov_a(edge_buffer_t*, int32_t, uint32_t);
void __edgebufdrawcall edge_draw_flat_avg_a(edge_buffer_t*, int32_t, uint32_t);
};
void edge_draw_flat_mov(edge_buffer_t *edgebuf, int color, int length) {
do {
_asm {
mov esi, [edgebuf]
mov eax, [color]
mov edi, [esi + 0*4]
mov ecx, [esi + 1*4]
rep stosb
}
edgebuf++;
} while (--length);
}
static void RightSection(void)
{
// Walk backwards trough the veci array
vec3bx * v2, * v1 = right_vtx;
if(right_vtx > start_vtx) v2 = right_vtx-1;
else v2 = end_vtx; // Wrap to end of array
right_vtx = v2;
// v1 = top veci
// v2 = bottom veci
// Calculate number of scanlines in this section
right_height = ceilx(v2->p.y) - ceilx(v1->p.y);
if(right_height <= 0) return;
// Guard against possible div overflows
if(right_height > 1) {
// OK, no worries, we have a section that is at least
// one pixel high. Calculate slope as usual.
long height = v2->p.y - v1->p.y;
right_dxdy = idiv16(v2->p.x - v1->p.x, height);
}
else {
// Height is less or equal to one pixel.
// Calculate slope = width * 1/height
// using 18:14 bit precision to avoid overflows.
long inv_height = (0x10000 << 14) / (v2->p.y - v1->p.y);
right_dxdy = imul14(v2->p.x - v1->p.x, inv_height);
}
// Prestep initial values
long prestep = (ceilx(v1->p.y) << 16) - v1->p.y;
right_x = v1->p.x + imul16(prestep, right_dxdy);
}
static void LeftSection(void)
{
// Walk forward trough the veci array
vec3bx * v2, * v1 = left_vtx;
if(left_vtx < end_vtx) v2 = left_vtx+1;
else v2 = start_vtx; // Wrap to start of array
left_vtx = v2;
// v1 = top vecx
// v2 = bottom vecx
// Calculate number of scanlines in this section
left_height = ceilx(v2->p.y) - ceilx(v1->p.y);
if(left_height <= 0) return;
// Guard against possible div overflows
if(left_height > 1) {
// OK, no worries, we have a section that is at least
// one pixel high. Calculate slope as usual.
long height = v2->p.y - v1->p.y;
left_dxdy = idiv16(v2->p.x - v1->p.x, height);
//left_dudy = idiv16(v2->u - v1->u, height);
//left_dvdy = idiv16(v2->v - v1->v, height);
}
else {
// Height is less or equal to one pixel.
// Calculate slope = width * 1/height
// using 18:14 bit precision to avoid overflows.
long inv_height = (0x10000 << 14) / (v2->p.y - v1->p.y);
left_dxdy = imul14(v2->p.x - v1->p.x, inv_height);
//left_dudy = imul14(v2->u - v1->u, inv_height);
//left_dvdy = imul14(v2->v - v1->v, inv_height);
}
// Prestep initial values
long prestep = (ceilx(v1->p.y) << 16) - v1->p.y;
left_x = v1->p.x + imul16(prestep, left_dxdy);
//left_u = v1->u + imul16(prestep, left_dudy);
//left_v = v1->v + imul16(prestep, left_dvdy);
}
namespace __tmap {
static void *dst;
static float TopClip, BotClip, LeftClip, RightClip;
static long dst_pitch;
void DrawFlatPoly(vec3bx * vtx, int vertices, int color, int style)
{
edge_buffer_t *eb = edgebuf;
start_vtx = vtx; // First vertex in array
// Search trough the vtx array to find min y, max y
// and the location of these structures.
vec3bx * min_vtx = vtx;
max_vtx = vtx;
long min_y = vtx->p.y;
long max_y = vtx->p.y;
vtx++;
for(int n=1; n<vertices; n++) {
if(vtx->p.y < min_y) {
min_y = vtx->p.y;
min_vtx = vtx;
}
else
if(vtx->p.y > max_y) {
max_y = vtx->p.y;
max_vtx = vtx;
}
vtx++;
}
// OK, now we know where in the array we should start and
// where to end while scanning the edges of the polygon
left_vtx = min_vtx; // Left side starting vertex
right_vtx = min_vtx; // Right side starting vertex
end_vtx = vtx-1; // Last vertex in array
// Search for the first usable right section
do {
if(right_vtx == max_vtx) return;
RightSection();
} while(right_height <= 0);
// Search for the first usable left section
do {
if(left_vtx == max_vtx) return;
LeftSection();
} while(left_height <= 0);
unsigned char *destptr = ((unsigned char*)dst + (ceilx(min_y) * dst_pitch));
for(;;)
{
long x1 = ceilx(left_x);
long width = ceilx(right_x) - x1;
unsigned char *p = destptr + x1;
#if 0
if (width > 0) // here double-sided polys are rejected
_asm {
mov eax, color
mov edi, p
mov ecx, width
rep stosb // don't! write your own mapper instead!
}
#else
if (width > 0) {
eb->p = p;
eb->length = width;
eb++;
}
#endif
destptr += dst_pitch;
// Scan the right side
if(--right_height <= 0) { // End of this section?
do {
if(right_vtx == max_vtx) goto draw_edgebuf;
RightSection();
} while(right_height <= 0);
}
else
right_x += right_dxdy;
// Scan the left side
if(--left_height <= 0) { // End of this section?
do {
if(left_vtx == max_vtx) goto draw_edgebuf;
LeftSection();
} while(left_height <= 0);
}
else
left_x += left_dxdy;
}
draw_edgebuf:
// render edge buffer
int length = eb - edgebuf;
if (length > 0) {
edgebufdraw_proc_t proc = edge_draw_flat_avg_a;
proc(edgebuf, color * 0x01010101, length);
}
}
}
// ---------------------------------------------
using namespace __tmap;
void tmap_init(void *buf, int xres, int yres, int pitch) {
dst = buf;
TopClip = 0; BotClip = yres; LeftClip = 0; RightClip = xres;
dst_pitch = pitch;
}
void facedraw_flat(facelist_t *f) {
DrawFlatPoly(&f->v[0], 3, f->c, f->style);
}