1 /*
2 sw32_r_draw.c
3
4 (description)
5
6 Copyright (C) 1996-1997 Id Software, Inc.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16
17 See the GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to:
21
22 Free Software Foundation, Inc.
23 59 Temple Place - Suite 330
24 Boston, MA 02111-1307, USA
25
26 */
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #define NH_DEFINE
32 #include "namehack.h"
33
34 #include "QF/render.h"
35
36 #include "compat.h"
37 #include "r_internal.h"
38
39 #define MAXLEFTCLIPEDGES 100
40
41 // !!! if these are changed, they must be changed in asm_draw.h too !!!
42 #define FULLY_CLIPPED_CACHED 0x80000000
43 #define FRAMECOUNT_MASK 0x7FFFFFFF
44
45 static unsigned int cacheoffset;
46
47 int sw32_c_faceclip; // number of faces clipped
48
49 zpointdesc_t sw32_r_zpointdesc;
50
51 static polydesc_t r_polydesc;
52
53 clipplane_t sw32_view_clipplanes[4];
54
55 medge_t *sw32_r_pedge;
56
57 qboolean sw32_r_leftclipped, sw32_r_rightclipped;
58 static qboolean makeleftedge, makerightedge;
59 qboolean sw32_r_nearzionly;
60
61 int sw32_sintable[SIN_BUFFER_SIZE];
62 int sw32_intsintable[SIN_BUFFER_SIZE];
63
64 mvertex_t sw32_r_leftenter, sw32_r_leftexit;
65 mvertex_t sw32_r_rightenter, sw32_r_rightexit;
66
67 typedef struct {
68 float u, v;
69 int ceilv;
70 } evert_t;
71
72 int sw32_r_emitted;
73 float sw32_r_nearzi;
74 float sw32_r_u1, sw32_r_v1, sw32_r_lzi1;
75 int sw32_r_ceilv1;
76
77 qboolean sw32_r_lastvertvalid;
78
79
80 void
sw32_R_EmitEdge(mvertex_t * pv0,mvertex_t * pv1)81 sw32_R_EmitEdge (mvertex_t *pv0, mvertex_t *pv1)
82 {
83 edge_t *edge, *pcheck;
84 int u_check;
85 float u, u_step;
86 vec3_t local, transformed;
87 float *world;
88 int v, v2, ceilv0;
89 float scale, lzi0, u0, v0;
90 int side;
91
92 if (sw32_r_lastvertvalid) {
93 u0 = sw32_r_u1;
94 v0 = sw32_r_v1;
95 lzi0 = sw32_r_lzi1;
96 ceilv0 = sw32_r_ceilv1;
97 } else {
98 world = &pv0->position[0];
99
100 // transform and project
101 VectorSubtract (world, modelorg, local);
102 sw32_TransformVector (local, transformed);
103
104 if (transformed[2] < NEAR_CLIP)
105 transformed[2] = NEAR_CLIP;
106
107 lzi0 = 1.0 / transformed[2];
108
109 // FIXME: build x/sw32_yscale into transform?
110 scale = sw32_xscale * lzi0;
111 u0 = (sw32_xcenter + scale * transformed[0]);
112 if (u0 < r_refdef.fvrectx_adj)
113 u0 = r_refdef.fvrectx_adj;
114 if (u0 > r_refdef.fvrectright_adj)
115 u0 = r_refdef.fvrectright_adj;
116
117 scale = sw32_yscale * lzi0;
118 v0 = (sw32_ycenter - scale * transformed[1]);
119 if (v0 < r_refdef.fvrecty_adj)
120 v0 = r_refdef.fvrecty_adj;
121 if (v0 > r_refdef.fvrectbottom_adj)
122 v0 = r_refdef.fvrectbottom_adj;
123
124 ceilv0 = (int) ceil (v0);
125 }
126
127 world = &pv1->position[0];
128
129 // transform and project
130 VectorSubtract (world, modelorg, local);
131 sw32_TransformVector (local, transformed);
132
133 if (transformed[2] < NEAR_CLIP)
134 transformed[2] = NEAR_CLIP;
135
136 sw32_r_lzi1 = 1.0 / transformed[2];
137
138 scale = sw32_xscale * sw32_r_lzi1;
139 sw32_r_u1 = (sw32_xcenter + scale * transformed[0]);
140 if (sw32_r_u1 < r_refdef.fvrectx_adj)
141 sw32_r_u1 = r_refdef.fvrectx_adj;
142 if (sw32_r_u1 > r_refdef.fvrectright_adj)
143 sw32_r_u1 = r_refdef.fvrectright_adj;
144
145 scale = sw32_yscale * sw32_r_lzi1;
146 sw32_r_v1 = (sw32_ycenter - scale * transformed[1]);
147 if (sw32_r_v1 < r_refdef.fvrecty_adj)
148 sw32_r_v1 = r_refdef.fvrecty_adj;
149 if (sw32_r_v1 > r_refdef.fvrectbottom_adj)
150 sw32_r_v1 = r_refdef.fvrectbottom_adj;
151
152 if (sw32_r_lzi1 > lzi0)
153 lzi0 = sw32_r_lzi1;
154
155 if (lzi0 > sw32_r_nearzi) // for mipmap finding
156 sw32_r_nearzi = lzi0;
157
158 // for right edges, all we want is the effect on 1/z
159 if (sw32_r_nearzionly)
160 return;
161
162 sw32_r_emitted = 1;
163
164 sw32_r_ceilv1 = (int) ceil (sw32_r_v1);
165
166 // create the edge
167 if (ceilv0 == sw32_r_ceilv1) {
168 // we cache unclipped horizontal edges as fully clipped
169 if (cacheoffset != 0x7FFFFFFF) {
170 cacheoffset = FULLY_CLIPPED_CACHED |
171 (r_framecount & FRAMECOUNT_MASK);
172 }
173
174 return; // horizontal edge
175 }
176
177 side = ceilv0 > sw32_r_ceilv1;
178
179 edge = sw32_edge_p++;
180
181 edge->owner = sw32_r_pedge;
182
183 edge->nearzi = lzi0;
184
185 if (side == 0) {
186 // trailing edge (go from p1 to p2)
187 v = ceilv0;
188 v2 = sw32_r_ceilv1 - 1;
189
190 edge->surfs[0] = sw32_surface_p - sw32_surfaces;
191 edge->surfs[1] = 0;
192
193 u_step = ((sw32_r_u1 - u0) / (sw32_r_v1 - v0));
194 u = u0 + ((float) v - v0) * u_step;
195 } else {
196 // leading edge (go from p2 to p1)
197 v2 = ceilv0 - 1;
198 v = sw32_r_ceilv1;
199
200 edge->surfs[0] = 0;
201 edge->surfs[1] = sw32_surface_p - sw32_surfaces;
202
203 u_step = ((u0 - sw32_r_u1) / (v0 - sw32_r_v1));
204 u = sw32_r_u1 + ((float) v - sw32_r_v1) * u_step;
205 }
206
207 edge->u_step = u_step * 0x100000;
208 edge->u = u * 0x100000 + 0xFFFFF;
209
210 // we need to do this to avoid stepping off the edges if a very nearly
211 // horizontal edge is less than epsilon above a scan, and numeric error
212 // causes it to incorrectly extend to the scan, and the extension of the
213 // line goes off the edge of the screen
214 // FIXME: is this actually needed?
215 if (edge->u < r_refdef.vrect_x_adj_shift20)
216 edge->u = r_refdef.vrect_x_adj_shift20;
217 if (edge->u > r_refdef.vrectright_adj_shift20)
218 edge->u = r_refdef.vrectright_adj_shift20;
219
220 // sort the edge in normally
221 u_check = edge->u;
222 if (edge->surfs[0])
223 u_check++; // sort trailers after leaders
224
225 if (!sw32_newedges[v] || sw32_newedges[v]->u >= u_check) {
226 edge->next = sw32_newedges[v];
227 sw32_newedges[v] = edge;
228 } else {
229 pcheck = sw32_newedges[v];
230 while (pcheck->next && pcheck->next->u < u_check)
231 pcheck = pcheck->next;
232 edge->next = pcheck->next;
233 pcheck->next = edge;
234 }
235
236 edge->nextremove = sw32_removeedges[v2];
237 sw32_removeedges[v2] = edge;
238 }
239
240
241 void
sw32_R_ClipEdge(mvertex_t * pv0,mvertex_t * pv1,clipplane_t * clip)242 sw32_R_ClipEdge (mvertex_t *pv0, mvertex_t *pv1, clipplane_t *clip)
243 {
244 float d0, d1, f;
245 mvertex_t clipvert;
246
247 if (clip) {
248 do {
249 d0 = DotProduct (pv0->position, clip->normal) - clip->dist;
250 d1 = DotProduct (pv1->position, clip->normal) - clip->dist;
251
252 if (d0 >= 0) {
253 // point 0 is unclipped
254 if (d1 >= 0) {
255 // both points are unclipped
256 continue;
257 }
258 // only point 1 is clipped
259
260 // we don't cache clipped edges
261 cacheoffset = 0x7FFFFFFF;
262
263 f = d0 / (d0 - d1);
264 clipvert.position[0] = pv0->position[0] +
265 f * (pv1->position[0] - pv0->position[0]);
266 clipvert.position[1] = pv0->position[1] +
267 f * (pv1->position[1] - pv0->position[1]);
268 clipvert.position[2] = pv0->position[2] +
269 f * (pv1->position[2] - pv0->position[2]);
270
271 if (clip->leftedge) {
272 sw32_r_leftclipped = true;
273 sw32_r_leftexit = clipvert;
274 } else if (clip->rightedge) {
275 sw32_r_rightclipped = true;
276 sw32_r_rightexit = clipvert;
277 }
278
279 sw32_R_ClipEdge (pv0, &clipvert, clip->next);
280 return;
281 } else {
282 // point 0 is clipped
283 if (d1 < 0) {
284 // both points are clipped
285 // we do cache fully clipped edges
286 if (!sw32_r_leftclipped)
287 cacheoffset = FULLY_CLIPPED_CACHED |
288 (r_framecount & FRAMECOUNT_MASK);
289 return;
290 }
291 // only point 0 is clipped
292 sw32_r_lastvertvalid = false;
293
294 // we don't cache partially clipped edges
295 cacheoffset = 0x7FFFFFFF;
296
297 f = d0 / (d0 - d1);
298 clipvert.position[0] = pv0->position[0] +
299 f * (pv1->position[0] - pv0->position[0]);
300 clipvert.position[1] = pv0->position[1] +
301 f * (pv1->position[1] - pv0->position[1]);
302 clipvert.position[2] = pv0->position[2] +
303 f * (pv1->position[2] - pv0->position[2]);
304
305 if (clip->leftedge) {
306 sw32_r_leftclipped = true;
307 sw32_r_leftenter = clipvert;
308 } else if (clip->rightedge) {
309 sw32_r_rightclipped = true;
310 sw32_r_rightenter = clipvert;
311 }
312
313 sw32_R_ClipEdge (&clipvert, pv1, clip->next);
314 return;
315 }
316 } while ((clip = clip->next) != NULL);
317 }
318 // add the edge
319 sw32_R_EmitEdge (pv0, pv1);
320 }
321
322
323 static void
R_EmitCachedEdge(void)324 R_EmitCachedEdge (void)
325 {
326 edge_t *pedge_t;
327
328 pedge_t = (edge_t *) ((intptr_t) sw32_r_edges + sw32_r_pedge->cachededgeoffset);
329
330 if (!pedge_t->surfs[0])
331 pedge_t->surfs[0] = sw32_surface_p - sw32_surfaces;
332 else
333 pedge_t->surfs[1] = sw32_surface_p - sw32_surfaces;
334
335 if (pedge_t->nearzi > sw32_r_nearzi) // for mipmap finding
336 sw32_r_nearzi = pedge_t->nearzi;
337
338 sw32_r_emitted = 1;
339 }
340
341
342 void
sw32_R_RenderFace(msurface_t * fa,int clipflags)343 sw32_R_RenderFace (msurface_t *fa, int clipflags)
344 {
345 int i, lindex;
346 unsigned int mask;
347 plane_t *pplane;
348 float distinv;
349 vec3_t p_normal;
350 medge_t *pedges, tedge;
351 clipplane_t *pclip;
352
353 // skip out if no more surfs
354 if ((sw32_surface_p) >= sw32_surf_max) {
355 sw32_r_outofsurfaces++;
356 return;
357 }
358 // ditto if not enough edges left, or switch to auxedges if possible
359 if ((sw32_edge_p + fa->numedges + 4) >= sw32_edge_max) {
360 sw32_r_outofedges += fa->numedges;
361 return;
362 }
363
364 sw32_c_faceclip++;
365
366 // set up clip planes
367 pclip = NULL;
368
369 for (i = 3, mask = 0x08; i >= 0; i--, mask >>= 1) {
370 if (clipflags & mask) {
371 sw32_view_clipplanes[i].next = pclip;
372 pclip = &sw32_view_clipplanes[i];
373 }
374 }
375
376 // push the edges through
377 sw32_r_emitted = 0;
378 sw32_r_nearzi = 0;
379 sw32_r_nearzionly = false;
380 makeleftedge = makerightedge = false;
381 pedges = currententity->model->edges;
382 sw32_r_lastvertvalid = false;
383
384 for (i = 0; i < fa->numedges; i++) {
385 lindex = currententity->model->surfedges[fa->firstedge + i];
386
387 if (lindex > 0) {
388 sw32_r_pedge = &pedges[lindex];
389
390 // if the edge is cached, we can just reuse the edge
391 if (!insubmodel) {
392 if (sw32_r_pedge->cachededgeoffset & FULLY_CLIPPED_CACHED) {
393 if ((sw32_r_pedge->cachededgeoffset & FRAMECOUNT_MASK) ==
394 (unsigned int) r_framecount) {
395 sw32_r_lastvertvalid = false;
396 continue;
397 }
398 } else {
399 if ((((uintptr_t) sw32_edge_p - (uintptr_t) sw32_r_edges) >
400 sw32_r_pedge->cachededgeoffset) &&
401 (((edge_t *) ((intptr_t) sw32_r_edges +
402 sw32_r_pedge->cachededgeoffset))->owner ==
403 sw32_r_pedge)) {
404 R_EmitCachedEdge ();
405 sw32_r_lastvertvalid = false;
406 continue;
407 }
408 }
409 }
410 // assume it's cacheable
411 cacheoffset = (byte *) sw32_edge_p - (byte *) sw32_r_edges;
412 sw32_r_leftclipped = sw32_r_rightclipped = false;
413 sw32_R_ClipEdge (&r_pcurrentvertbase[sw32_r_pedge->v[0]],
414 &r_pcurrentvertbase[sw32_r_pedge->v[1]], pclip);
415 sw32_r_pedge->cachededgeoffset = cacheoffset;
416
417 if (sw32_r_leftclipped)
418 makeleftedge = true;
419 if (sw32_r_rightclipped)
420 makerightedge = true;
421 sw32_r_lastvertvalid = true;
422 } else {
423 lindex = -lindex;
424 sw32_r_pedge = &pedges[lindex];
425 // if the edge is cached, we can just reuse the edge
426 if (!insubmodel) {
427 if (sw32_r_pedge->cachededgeoffset & FULLY_CLIPPED_CACHED) {
428 if ((sw32_r_pedge->cachededgeoffset & FRAMECOUNT_MASK) ==
429 (unsigned int) r_framecount) {
430 sw32_r_lastvertvalid = false;
431 continue;
432 }
433 } else {
434 // it's cached if the cached edge is valid and is owned
435 // by this medge_t
436 if ((((uintptr_t) sw32_edge_p - (uintptr_t) sw32_r_edges) >
437 sw32_r_pedge->cachededgeoffset) &&
438 (((edge_t *) ((intptr_t) sw32_r_edges +
439 sw32_r_pedge->cachededgeoffset))->owner ==
440 sw32_r_pedge)) {
441 R_EmitCachedEdge ();
442 sw32_r_lastvertvalid = false;
443 continue;
444 }
445 }
446 }
447 // assume it's cacheable
448 cacheoffset = (byte *) sw32_edge_p - (byte *) sw32_r_edges;
449 sw32_r_leftclipped = sw32_r_rightclipped = false;
450 sw32_R_ClipEdge (&r_pcurrentvertbase[sw32_r_pedge->v[1]],
451 &r_pcurrentvertbase[sw32_r_pedge->v[0]], pclip);
452 sw32_r_pedge->cachededgeoffset = cacheoffset;
453
454 if (sw32_r_leftclipped)
455 makeleftedge = true;
456 if (sw32_r_rightclipped)
457 makerightedge = true;
458 sw32_r_lastvertvalid = true;
459 }
460 }
461
462 // if there was a clip off the left edge, add that edge too
463 // FIXME: faster to do in screen space?
464 // FIXME: share clipped edges?
465 if (makeleftedge) {
466 sw32_r_pedge = &tedge;
467 sw32_r_lastvertvalid = false;
468 sw32_R_ClipEdge (&sw32_r_leftexit, &sw32_r_leftenter, pclip->next);
469 }
470 // if there was a clip off the right edge, get the right sw32_r_nearzi
471 if (makerightedge) {
472 sw32_r_pedge = &tedge;
473 sw32_r_lastvertvalid = false;
474 sw32_r_nearzionly = true;
475 sw32_R_ClipEdge (&sw32_r_rightexit, &sw32_r_rightenter, sw32_view_clipplanes[1].next);
476 }
477 // if no edges made it out, return without posting the surface
478 if (!sw32_r_emitted)
479 return;
480
481 sw32_r_polycount++;
482
483 sw32_surface_p->data = (void *) fa;
484 sw32_surface_p->nearzi = sw32_r_nearzi;
485 sw32_surface_p->flags = fa->flags;
486 sw32_surface_p->insubmodel = insubmodel;
487 sw32_surface_p->spanstate = 0;
488 sw32_surface_p->entity = currententity;
489 sw32_surface_p->key = sw32_r_currentkey++;
490 sw32_surface_p->spans = NULL;
491
492 pplane = fa->plane;
493 // FIXME: cache this?
494 sw32_TransformVector (pplane->normal, p_normal);
495 // FIXME: cache this?
496 distinv = 1.0 / (pplane->dist - DotProduct (modelorg, pplane->normal));
497 distinv = min (distinv, 1.0);
498
499 sw32_surface_p->d_zistepu = p_normal[0] * sw32_xscaleinv * distinv;
500 sw32_surface_p->d_zistepv = -p_normal[1] * sw32_yscaleinv * distinv;
501 sw32_surface_p->d_ziorigin = p_normal[2] * distinv -
502 sw32_xcenter * sw32_surface_p->d_zistepu - sw32_ycenter * sw32_surface_p->d_zistepv;
503
504 //JDC VectorCopy (r_worldmodelorg, sw32_surface_p->modelorg);
505 sw32_surface_p++;
506 }
507
508
509 void
sw32_R_RenderBmodelFace(bedge_t * pedges,msurface_t * psurf)510 sw32_R_RenderBmodelFace (bedge_t *pedges, msurface_t *psurf)
511 {
512 int i;
513 unsigned int mask;
514 plane_t *pplane;
515 float distinv;
516 vec3_t p_normal;
517 medge_t tedge;
518 clipplane_t *pclip;
519
520 // skip out if no more surfs
521 if (sw32_surface_p >= sw32_surf_max) {
522 sw32_r_outofsurfaces++;
523 return;
524 }
525 // ditto if not enough edges left, or switch to auxedges if possible
526 if ((sw32_edge_p + psurf->numedges + 4) >= sw32_edge_max) {
527 sw32_r_outofedges += psurf->numedges;
528 return;
529 }
530
531 sw32_c_faceclip++;
532
533 // this is a dummy to give the caching mechanism someplace to write to
534 sw32_r_pedge = &tedge;
535
536 // set up clip planes
537 pclip = NULL;
538
539 for (i = 3, mask = 0x08; i >= 0; i--, mask >>= 1) {
540 if (sw32_r_clipflags & mask) {
541 sw32_view_clipplanes[i].next = pclip;
542 pclip = &sw32_view_clipplanes[i];
543 }
544 }
545
546 // push the edges through
547 sw32_r_emitted = 0;
548 sw32_r_nearzi = 0;
549 sw32_r_nearzionly = false;
550 makeleftedge = makerightedge = false;
551 // FIXME: keep clipped bmodel edges in clockwise order so last vertex
552 // caching can be used?
553 sw32_r_lastvertvalid = false;
554
555 for (; pedges; pedges = pedges->pnext) {
556 sw32_r_leftclipped = sw32_r_rightclipped = false;
557 sw32_R_ClipEdge (pedges->v[0], pedges->v[1], pclip);
558
559 if (sw32_r_leftclipped)
560 makeleftedge = true;
561 if (sw32_r_rightclipped)
562 makerightedge = true;
563 }
564
565 // if there was a clip off the left edge, add that edge too
566 // FIXME: faster to do in screen space?
567 // FIXME: share clipped edges?
568 if (makeleftedge) {
569 sw32_r_pedge = &tedge;
570 sw32_R_ClipEdge (&sw32_r_leftexit, &sw32_r_leftenter, pclip->next);
571 }
572 // if there was a clip off the right edge, get the right sw32_r_nearzi
573 if (makerightedge) {
574 sw32_r_pedge = &tedge;
575 sw32_r_nearzionly = true;
576 sw32_R_ClipEdge (&sw32_r_rightexit, &sw32_r_rightenter, sw32_view_clipplanes[1].next);
577 }
578 // if no edges made it out, return without posting the surface
579 if (!sw32_r_emitted)
580 return;
581
582 sw32_r_polycount++;
583
584 sw32_surface_p->data = (void *) psurf;
585 sw32_surface_p->nearzi = sw32_r_nearzi;
586 sw32_surface_p->flags = psurf->flags;
587 sw32_surface_p->insubmodel = true;
588 sw32_surface_p->spanstate = 0;
589 sw32_surface_p->entity = currententity;
590 sw32_surface_p->key = sw32_r_currentbkey;
591 sw32_surface_p->spans = NULL;
592
593 pplane = psurf->plane;
594 // FIXME: cache this?
595 sw32_TransformVector (pplane->normal, p_normal);
596 // FIXME: cache this?
597 distinv = 1.0 / (pplane->dist - DotProduct (modelorg, pplane->normal));
598 distinv = min (distinv, 1.0);
599
600 sw32_surface_p->d_zistepu = p_normal[0] * sw32_xscaleinv * distinv;
601 sw32_surface_p->d_zistepv = -p_normal[1] * sw32_yscaleinv * distinv;
602 sw32_surface_p->d_ziorigin = p_normal[2] * distinv -
603 sw32_xcenter * sw32_surface_p->d_zistepu - sw32_ycenter * sw32_surface_p->d_zistepv;
604
605 //JDC VectorCopy (r_worldmodelorg, sw32_surface_p->modelorg);
606 sw32_surface_p++;
607 }
608
609
610 void
sw32_R_RenderPoly(msurface_t * fa,int clipflags)611 sw32_R_RenderPoly (msurface_t *fa, int clipflags)
612 {
613 int i, lindex, lnumverts, s_axis, t_axis;
614 float dist, lastdist, lzi, scale, u, v, frac;
615 unsigned int mask;
616 vec3_t local, transformed;
617 clipplane_t *pclip;
618 medge_t *pedges;
619 plane_t *pplane;
620 mvertex_t verts[2][100]; // FIXME: do real number
621 polyvert_t pverts[100]; // FIXME: do real number, safely
622 int vertpage, newverts, newpage, lastvert;
623 qboolean visible;
624
625 // FIXME: clean this up and make it faster
626 // FIXME: guard against running out of vertices
627
628 s_axis = t_axis = 0; // keep compiler happy
629
630 // set up clip planes
631 pclip = NULL;
632
633 for (i = 3, mask = 0x08; i >= 0; i--, mask >>= 1) {
634 if (clipflags & mask) {
635 sw32_view_clipplanes[i].next = pclip;
636 pclip = &sw32_view_clipplanes[i];
637 }
638 }
639
640 // reconstruct the polygon
641 // FIXME: these should be precalculated and loaded off disk
642 pedges = currententity->model->edges;
643 lnumverts = fa->numedges;
644 vertpage = 0;
645
646 for (i = 0; i < lnumverts; i++) {
647 lindex = currententity->model->surfedges[fa->firstedge + i];
648
649 if (lindex > 0) {
650 sw32_r_pedge = &pedges[lindex];
651 verts[0][i] = r_pcurrentvertbase[sw32_r_pedge->v[0]];
652 } else {
653 sw32_r_pedge = &pedges[-lindex];
654 verts[0][i] = r_pcurrentvertbase[sw32_r_pedge->v[1]];
655 }
656 }
657
658 // clip the polygon, done if not visible
659 while (pclip) {
660 lastvert = lnumverts - 1;
661 lastdist = DotProduct (verts[vertpage][lastvert].position,
662 pclip->normal) - pclip->dist;
663
664 visible = false;
665 newverts = 0;
666 newpage = vertpage ^ 1;
667
668 for (i = 0; i < lnumverts; i++) {
669 dist = DotProduct (verts[vertpage][i].position, pclip->normal) -
670 pclip->dist;
671
672 if ((lastdist > 0) != (dist > 0)) {
673 frac = dist / (dist - lastdist);
674 verts[newpage][newverts].position[0] =
675 verts[vertpage][i].position[0] +
676 ((verts[vertpage][lastvert].position[0] -
677 verts[vertpage][i].position[0]) * frac);
678 verts[newpage][newverts].position[1] =
679 verts[vertpage][i].position[1] +
680 ((verts[vertpage][lastvert].position[1] -
681 verts[vertpage][i].position[1]) * frac);
682 verts[newpage][newverts].position[2] =
683 verts[vertpage][i].position[2] +
684 ((verts[vertpage][lastvert].position[2] -
685 verts[vertpage][i].position[2]) * frac);
686 newverts++;
687 }
688
689 if (dist >= 0) {
690 verts[newpage][newverts] = verts[vertpage][i];
691 newverts++;
692 visible = true;
693 }
694
695 lastvert = i;
696 lastdist = dist;
697 }
698
699 if (!visible || (newverts < 3))
700 return;
701
702 lnumverts = newverts;
703 vertpage ^= 1;
704 pclip = pclip->next;
705 }
706
707 // transform and project, remembering the z values at the vertices and
708 // sw32_r_nearzi, and extract the s and t coordinates at the vertices
709 pplane = fa->plane;
710 switch (pplane->type) {
711 case PLANE_X:
712 case PLANE_ANYX:
713 s_axis = 1;
714 t_axis = 2;
715 break;
716 case PLANE_Y:
717 case PLANE_ANYY:
718 s_axis = 0;
719 t_axis = 2;
720 break;
721 case PLANE_Z:
722 case PLANE_ANYZ:
723 s_axis = 0;
724 t_axis = 1;
725 break;
726 }
727
728 sw32_r_nearzi = 0;
729
730 for (i = 0; i < lnumverts; i++) {
731 // transform and project
732 VectorSubtract (verts[vertpage][i].position, modelorg, local);
733 sw32_TransformVector (local, transformed);
734
735 if (transformed[2] < NEAR_CLIP)
736 transformed[2] = NEAR_CLIP;
737
738 lzi = 1.0 / transformed[2];
739
740 if (lzi > sw32_r_nearzi) // for mipmap finding
741 sw32_r_nearzi = lzi;
742
743 // FIXME: build x/sw32_yscale into transform?
744 scale = sw32_xscale * lzi;
745 u = (sw32_xcenter + scale * transformed[0]);
746 if (u < r_refdef.fvrectx_adj)
747 u = r_refdef.fvrectx_adj;
748 if (u > r_refdef.fvrectright_adj)
749 u = r_refdef.fvrectright_adj;
750
751 scale = sw32_yscale * lzi;
752 v = (sw32_ycenter - scale * transformed[1]);
753 if (v < r_refdef.fvrecty_adj)
754 v = r_refdef.fvrecty_adj;
755 if (v > r_refdef.fvrectbottom_adj)
756 v = r_refdef.fvrectbottom_adj;
757
758 pverts[i].u = u;
759 pverts[i].v = v;
760 pverts[i].zi = lzi;
761 pverts[i].s = verts[vertpage][i].position[s_axis];
762 pverts[i].t = verts[vertpage][i].position[t_axis];
763 }
764
765 // build the polygon descriptor, including fa, sw32_r_nearzi, and u, v, s, t,
766 // and z for each vertex
767 r_polydesc.numverts = lnumverts;
768 r_polydesc.nearzi = sw32_r_nearzi;
769 r_polydesc.pcurrentface = fa;
770 r_polydesc.pverts = pverts;
771
772 // draw the polygon
773 sw32_D_DrawPoly ();
774 }
775
776
777 void
sw32_R_ZDrawSubmodelPolys(model_t * pmodel)778 sw32_R_ZDrawSubmodelPolys (model_t *pmodel)
779 {
780 int i, numsurfaces;
781 msurface_t *psurf;
782 float dot;
783 plane_t *pplane;
784
785 psurf = &pmodel->surfaces[pmodel->firstmodelsurface];
786 numsurfaces = pmodel->nummodelsurfaces;
787
788 for (i = 0; i < numsurfaces; i++, psurf++) {
789 // find which side of the node we are on
790 pplane = psurf->plane;
791
792 dot = DotProduct (modelorg, pplane->normal) - pplane->dist;
793
794 // draw the polygon
795 if (((psurf->flags & SURF_PLANEBACK) && (dot < -BACKFACE_EPSILON)) ||
796 (!(psurf->flags & SURF_PLANEBACK) && (dot > BACKFACE_EPSILON))) {
797 // FIXME: use bounding-box-based frustum clipping info?
798 sw32_R_RenderPoly (psurf, 15);
799 }
800 }
801 }
802