forked from viciious/d32xr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr_phase3.c
292 lines (242 loc) · 6.81 KB
/
r_phase3.c
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
/*
CALICO
Renderer phase 3 - Sprite prep
*/
#include "doomdef.h"
#include "r_local.h"
static void R_PrepMobj(mobj_t* thing) ATTR_DATA_CACHE_ALIGN;
static void R_PrepPSprite(pspdef_t* psp) ATTR_DATA_CACHE_ALIGN;
void R_SpritePrep(void) ATTR_DATA_CACHE_ALIGN __attribute__((noinline));
//
// Project vissprite for potentially visible actor
//
static void R_PrepMobj(mobj_t *thing)
{
fixed_t tr_x, tr_y;
fixed_t gxt, gyt, gzt;
fixed_t tx, tz, x1, x2;
fixed_t xscale;
fixed_t texmid;
spritedef_t *sprdef;
spriteframe_t *sprframe;
VINT *sprlump;
angle_t ang;
unsigned int rot;
boolean flip;
int lump;
patch_t *patch;
vissprite_t *vis;
// transform origin relative to viewpoint
tr_x = thing->x - vd.viewx;
tr_y = thing->y - vd.viewy;
gxt = FixedMul(tr_x, vd.viewcos);
gyt = FixedMul(tr_y, vd.viewsin);
gyt = -gyt;
tz = gxt - gyt;
// thing is behind view plane?
if(tz < MINZ)
return;
gxt = FixedMul(tr_x, vd.viewsin);
gxt = -gxt;
gyt = FixedMul(tr_y, vd.viewcos);
tx = -(gyt + gxt);
// too far off the side?
if(tx > (tz << 2) || tx < -(tz<<2))
return;
// check sprite for validity
if(/*thing->sprite < 0 || */thing->sprite >= NUMSPRITES)
return;
sprdef = &sprites[thing->sprite];
// check frame for validity
if ((thing->frame & FF_FRAMEMASK) >= sprdef->numframes)
return;
sprframe = &spriteframes[sprdef->firstframe + (thing->frame & FF_FRAMEMASK)];
sprlump = &spritelumps[sprframe->lump];
if(sprlump[1] != -1)
{
// select proper rotation depending on player's view point
ang = R_PointToAngle2(vd.viewx, vd.viewy, thing->x, thing->y);
rot = (ang - thing->angle + (unsigned int)(ANG45 / 2)*9) >> 29;
lump = sprlump[rot];
}
else
{
// sprite has a single view for all rotations
lump = sprlump[0];
}
flip = false;
if (lump < 0)
{
lump = -(lump + 1);
flip = true;
}
patch = W_POINTLUMPNUM(lump);
xscale = FixedDiv(PROJECTION, tz);
gzt = thing->z - vd.viewz;
// calculate edges of the shape
tx -= ((fixed_t)BIGSHORT(patch->leftoffset)) << FRACBITS;
x1 = FixedMul(tx, xscale);
x1 = (centerXFrac + x1) / FRACUNIT;
// off the right side?
if (x1 > viewportWidth)
return;
tx += ((fixed_t)BIGSHORT(patch->width) << FRACBITS);
x2 = FixedMul(tx, xscale);
x2 = ((centerXFrac + x2) / FRACUNIT) - 1;
// off the left side
if (x2 < 0)
return;
// killough 4/9/98: clip things which are out of view due to height
tz = FixedMul(gzt, xscale);
if (tz > centerYFrac)
return;
texmid = gzt + ((fixed_t)BIGSHORT(patch->topoffset) << FRACBITS);
tz = FixedMul(texmid, xscale);
if (tz < viewportHeight - centerYFrac)
return;
// get a new vissprite
if(vd.vissprite_p >= vd.vissprites + MAXVISSPRITES - NUMPSPRITES)
return; // too many visible sprites already, leave room for psprites
vis = (vissprite_t *)vd.vissprite_p;
vd.vissprite_p++;
vis->patchnum = lump;
#ifndef MARS
vis->pixels = R_CheckPixels(lump + 1);
#endif
vis->x1 = x1 < 0 ? 0 : x1;
vis->x2 = x2 >= viewportWidth ? viewportWidth - 1 : x2;
vis->gx = thing->x / FRACUNIT;
vis->gy = thing->y / FRACUNIT;
vis->xscale = xscale;
vis->xiscale = FixedDiv(FRACUNIT, xscale);
vis->yscale = FixedMul(xscale, stretch);
vis->texturemid = texmid;
vis->startfrac = 0;
if(flip)
vis->xiscale = -vis->xiscale;
if (vis->xiscale < 0)
vis->startfrac = ((fixed_t)BIGSHORT(patch->width) << FRACBITS) - 1;
if (x1 < 0)
vis->startfrac += vis->xiscale * -x1;
if (thing->flags & MF_SHADOW)
{
vis->colormap = -vd.fuzzcolormap;
}
else if (vd.fixedcolormap)
{
vis->colormap = vd.fixedcolormap;
}
else
{
if (thing->frame & FF_FULLBRIGHT)
vis->colormap = 255;
else
vis->colormap = thing->subsector->sector->lightlevel;
vis->colormap = HWLIGHT(vis->colormap);
}
vis->colormaps = dc_colormaps;
}
//
// Project player weapon sprite
//
static void R_PrepPSprite(pspdef_t *psp)
{
spritedef_t *sprdef;
spriteframe_t *sprframe;
VINT *sprlump;
int lump;
patch_t *patch;
vissprite_t *vis;
fixed_t center, xscale;
fixed_t topoffset, texmid;
fixed_t tx, x1, x2;
const state_t* state;
state = &states[psp->state];
sprdef = &sprites[state->sprite];
sprframe = &spriteframes[sprdef->firstframe + (state->frame & FF_FRAMEMASK)];
sprlump = &spritelumps[sprframe->lump];
lump = sprlump[0];
patch = W_POINTLUMPNUM(lump);
xscale = weaponXScale;
center = centerXFrac - 80 * weaponXScale;
if (!lowResMode)
center >>= 1;
tx = psp->sx + center;
topoffset = (((fixed_t)BIGSHORT(patch->topoffset) - weaponYpos) << FRACBITS) - psp->sy;
texmid = BASEYCENTER * FRACUNIT + topoffset;
// calculate edges of the shape
tx = tx - (((fixed_t)BIGSHORT(patch->leftoffset)) << FRACBITS);
x1 = FixedMul(tx, xscale);
tx = ((fixed_t)BIGSHORT(patch->width) << FRACBITS);
x2 = FixedMul(tx, xscale);
x2 += x1;
x1 /= FRACUNIT;
x2 /= FRACUNIT;
x2 -= 1;
// off the right side?
if (x1 > viewportWidth)
return;
// off the left side
if (x2 < 0)
return;
// store information in vissprite
if(vd.vissprite_p == vd.vissprites + MAXVISSPRITES)
return; // out of vissprites
vis = (vissprite_t *)vd.vissprite_p;
vd.vissprite_p++;
vis->patchnum = lump;
#ifndef MARS
vis->pixels = R_CheckPixels(lump + 1);
#endif
vis->x1 = x1 < 0 ? 0 : x1;
vis->x2 = x2 >= viewportWidth ? viewportWidth - 1 : x2;
vis->xscale = xscale;
vis->yscale = FRACUNIT;
vis->xiscale = FixedDiv(FRACUNIT, xscale);
vis->texturemid = texmid;
vis->startfrac = 0;
if (x1 < 0)
vis->startfrac = vis->xiscale * -x1;
if (vd.fixedcolormap)
{
vis->colormap = vd.fixedcolormap;
}
else
{
if (state->frame & FF_FULLBRIGHT)
vis->colormap = 255;
else
vis->colormap = vd.lightlevel;
vis->colormap = HWLIGHT(vis->colormap);
}
vis->colormaps = dc_colormaps;
}
//
// Process actors in all visible subsectors
//
void R_SpritePrep(void)
{
sector_t **pse = vd.vissectors;
pspdef_t *psp;
int i;
while(pse < vd.lastvissector)
{
sector_t *se = *pse;
mobj_t *thing = se->thinglist;
while(thing) // walk sector thing list
{
R_PrepMobj(thing);
thing = thing->snext;
}
++pse;
}
// remember end of actor vissprites
vd.lastsprite_p = vd.vissprite_p;
// draw player weapon sprites
for(i = 0, psp = vd.viewplayer->psprites; i < NUMPSPRITES; i++, psp++)
{
if(psp->state)
R_PrepPSprite(psp);
}
}
// EOF