1 /*
2 Copyright (C) 1997-2001 Id Software, Inc.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 See the GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 */
20 // r_rast.c
21
22 #include "sw_local.h"
23
24 #define MAXLEFTCLIPEDGES 100
25
26 // !!! if these are changed, they must be changed in asm_draw.h too !!!
27 #define FULLY_CLIPPED_CACHED 0x80000000
28 #define FRAMECOUNT_MASK 0x7FFFFFFF
29
30 unsigned int cacheoffset;
31
32 int c_faceclip; // number of faces clipped
33
34
35 clipplane_t *entity_clipplanes;
36 clipplane_t view_clipplanes[4];
37 clipplane_t world_clipplanes[16];
38
39 medge_t *r_pedge;
40
41 qboolean r_leftclipped, r_rightclipped;
42 static qboolean makeleftedge, makerightedge;
43 qboolean r_nearzionly;
44
45 int sintable[CYCLE*2];
46 int intsintable[CYCLE*2];
47 int blanktable[CYCLE*2]; // PGM
48
49 mvertex_t r_leftenter, r_leftexit;
50 mvertex_t r_rightenter, r_rightexit;
51
52 typedef struct
53 {
54 float u,v;
55 int ceilv;
56 } evert_t;
57
58 int r_emitted;
59 float r_nearzi;
60 float r_u1, r_v1, r_lzi1;
61 int r_ceilv1;
62
63 qboolean r_lastvertvalid;
64 int r_skyframe;
65
66 msurface_t *r_skyfaces;
67 mplane_t r_skyplanes[6];
68 mtexinfo_t r_skytexinfo[6];
69 mvertex_t *r_skyverts;
70 medge_t *r_skyedges;
71 int *r_skysurfedges;
72
73 // I just copied this data from a box map...
74 int skybox_planes[12] = {2,-128, 0,-128, 2,128, 1,128, 0,128, 1,-128};
75
76 int box_surfedges[24] = { 1,2,3,4, -1,5,6,7, 8,9,-6,10, -2,-7,-9,11,
77 12,-3,-11,-8, -12,-10,-5,-4};
78 int box_edges[24] = { 1,2, 2,3, 3,4, 4,1, 1,5, 5,6, 6,2, 7,8, 8,6, 5,7, 8,3, 7,4};
79
80 int box_faces[6] = {0,0,2,2,2,0};
81
82 vec3_t box_vecs[6][2] = {
83 { {0,-1,0}, {-1,0,0} },
84 { {0,1,0}, {0,0,-1} },
85 { {0,-1,0}, {1,0,0} },
86 { {1,0,0}, {0,0,-1} },
87 { {0,-1,0}, {0,0,-1} },
88 { {-1,0,0}, {0,0,-1} }
89 };
90
91 float box_verts[8][3] = {
92 {-1,-1,-1},
93 {-1,1,-1},
94 {1,1,-1},
95 {1,-1,-1},
96 {-1,-1,1},
97 {-1,1,1},
98 {1,-1,1},
99 {1,1,1}
100 };
101
102 // down, west, up, north, east, south
103 // {"rt", "bk", "lf", "ft", "up", "dn"};
104
105 /*
106 ================
107 R_InitSkyBox
108
109 ================
110 */
R_InitSkyBox(void)111 void R_InitSkyBox (void)
112 {
113 int i;
114 extern model_t *loadmodel;
115
116 r_skyfaces = loadmodel->surfaces + loadmodel->numsurfaces;
117 loadmodel->numsurfaces += 6;
118 r_skyverts = loadmodel->vertexes + loadmodel->numvertexes;
119 loadmodel->numvertexes += 8;
120 r_skyedges = loadmodel->edges + loadmodel->numedges;
121 loadmodel->numedges += 12;
122 r_skysurfedges = loadmodel->surfedges + loadmodel->numsurfedges;
123 loadmodel->numsurfedges += 24;
124 if (loadmodel->numsurfaces > MAX_MAP_FACES
125 || loadmodel->numvertexes > MAX_MAP_VERTS
126 || loadmodel->numedges > MAX_MAP_EDGES)
127 Com_Error (ERR_DROP, "InitSkyBox: map overflow");
128
129 memset (r_skyfaces, 0, 6*sizeof(*r_skyfaces));
130 for (i=0 ; i<6 ; i++)
131 {
132 r_skyplanes[i].normal[skybox_planes[i*2]] = 1;
133 r_skyplanes[i].dist = skybox_planes[i*2+1];
134
135 VectorCopy (box_vecs[i][0], r_skytexinfo[i].vecs[0]);
136 VectorCopy (box_vecs[i][1], r_skytexinfo[i].vecs[1]);
137
138 r_skyfaces[i].plane = &r_skyplanes[i];
139 r_skyfaces[i].numedges = 4;
140 r_skyfaces[i].flags = box_faces[i] | SURF_DRAWSKYBOX;
141 r_skyfaces[i].firstedge = loadmodel->numsurfedges-24+i*4;
142 r_skyfaces[i].texinfo = &r_skytexinfo[i];
143 r_skyfaces[i].texturemins[0] = -128;
144 r_skyfaces[i].texturemins[1] = -128;
145 r_skyfaces[i].extents[0] = 256;
146 r_skyfaces[i].extents[1] = 256;
147 }
148
149 for (i=0 ; i<24 ; i++)
150 if (box_surfedges[i] > 0)
151 r_skysurfedges[i] = loadmodel->numedges-13 + box_surfedges[i];
152 else
153 r_skysurfedges[i] = - (loadmodel->numedges-13 + -box_surfedges[i]);
154
155 for(i=0 ; i<12 ; i++)
156 {
157 r_skyedges[i].v[0] = loadmodel->numvertexes-9+box_edges[i*2+0];
158 r_skyedges[i].v[1] = loadmodel->numvertexes-9+box_edges[i*2+1];
159 r_skyedges[i].cachededgeoffset = 0;
160 }
161 }
162
163 /*
164 ================
165 R_EmitSkyBox
166 ================
167 */
R_EmitSkyBox(void)168 void R_EmitSkyBox (void)
169 {
170 int i, j;
171 int oldkey;
172
173 if (insubmodel)
174 return; // submodels should never have skies
175 if (r_skyframe == r_framecount)
176 return; // already set this frame
177
178 r_skyframe = r_framecount;
179
180 // set the eight fake vertexes
181 for (i=0 ; i<8 ; i++)
182 for (j=0 ; j<3 ; j++)
183 r_skyverts[i].position[j] = r_origin[j] + box_verts[i][j]*128;
184
185 // set the six fake planes
186 for (i=0 ; i<6 ; i++)
187 if (skybox_planes[i*2+1] > 0)
188 r_skyplanes[i].dist = r_origin[skybox_planes[i*2]]+128;
189 else
190 r_skyplanes[i].dist = r_origin[skybox_planes[i*2]]-128;
191
192 // fix texture offseets
193 for (i=0 ; i<6 ; i++)
194 {
195 r_skytexinfo[i].vecs[0][3] = -DotProduct (r_origin, r_skytexinfo[i].vecs[0]);
196 r_skytexinfo[i].vecs[1][3] = -DotProduct (r_origin, r_skytexinfo[i].vecs[1]);
197 }
198
199 // emit the six faces
200 oldkey = r_currentkey;
201 r_currentkey = 0x7ffffff0;
202 for (i=0 ; i<6 ; i++)
203 {
204 R_RenderFace (r_skyfaces + i, 15);
205 }
206 r_currentkey = oldkey; // bsp sorting order
207 }
208
209
210 #if !id386
211
212 /*
213 ================
214 R_EmitEdge
215 ================
216 */
R_EmitEdge(mvertex_t * pv0,mvertex_t * pv1)217 void R_EmitEdge (mvertex_t *pv0, mvertex_t *pv1)
218 {
219 edge_t *edge, *pcheck;
220 int u_check;
221 float u, u_step;
222 vec3_t local, transformed;
223 float *world;
224 int v, v2, ceilv0;
225 float scale, lzi0, u0, v0;
226 int side;
227
228 if (r_lastvertvalid)
229 {
230 u0 = r_u1;
231 v0 = r_v1;
232 lzi0 = r_lzi1;
233 ceilv0 = r_ceilv1;
234 }
235 else
236 {
237 world = &pv0->position[0];
238
239 // transform and project
240 VectorSubtract (world, modelorg, local);
241 TransformVector (local, transformed);
242
243 if (transformed[2] < NEAR_CLIP)
244 transformed[2] = NEAR_CLIP;
245
246 lzi0 = 1.0 / transformed[2];
247
248 // FIXME: build x/yscale into transform?
249 scale = xscale * lzi0;
250 u0 = (xcenter + scale*transformed[0]);
251 if (u0 < r_refdef.fvrectx_adj)
252 u0 = r_refdef.fvrectx_adj;
253 if (u0 > r_refdef.fvrectright_adj)
254 u0 = r_refdef.fvrectright_adj;
255
256 scale = yscale * lzi0;
257 v0 = (ycenter - scale*transformed[1]);
258 if (v0 < r_refdef.fvrecty_adj)
259 v0 = r_refdef.fvrecty_adj;
260 if (v0 > r_refdef.fvrectbottom_adj)
261 v0 = r_refdef.fvrectbottom_adj;
262
263 ceilv0 = (int) ceil(v0);
264 }
265
266 world = &pv1->position[0];
267
268 // transform and project
269 VectorSubtract (world, modelorg, local);
270 TransformVector (local, transformed);
271
272 if (transformed[2] < NEAR_CLIP)
273 transformed[2] = NEAR_CLIP;
274
275 r_lzi1 = 1.0 / transformed[2];
276
277 scale = xscale * r_lzi1;
278 r_u1 = (xcenter + scale*transformed[0]);
279 if (r_u1 < r_refdef.fvrectx_adj)
280 r_u1 = r_refdef.fvrectx_adj;
281 if (r_u1 > r_refdef.fvrectright_adj)
282 r_u1 = r_refdef.fvrectright_adj;
283
284 scale = yscale * r_lzi1;
285 r_v1 = (ycenter - scale*transformed[1]);
286 if (r_v1 < r_refdef.fvrecty_adj)
287 r_v1 = r_refdef.fvrecty_adj;
288 if (r_v1 > r_refdef.fvrectbottom_adj)
289 r_v1 = r_refdef.fvrectbottom_adj;
290
291 if (r_lzi1 > lzi0)
292 lzi0 = r_lzi1;
293
294 if (lzi0 > r_nearzi) // for mipmap finding
295 r_nearzi = lzi0;
296
297 // for right edges, all we want is the effect on 1/z
298 if (r_nearzionly)
299 return;
300
301 r_emitted = 1;
302
303 r_ceilv1 = (int) ceil(r_v1);
304
305
306 // create the edge
307 if (ceilv0 == r_ceilv1)
308 {
309 // we cache unclipped horizontal edges as fully clipped
310 if (cacheoffset != 0x7FFFFFFF)
311 {
312 cacheoffset = FULLY_CLIPPED_CACHED |
313 (r_framecount & FRAMECOUNT_MASK);
314 }
315
316 return; // horizontal edge
317 }
318
319 side = ceilv0 > r_ceilv1;
320
321 edge = edge_p++;
322
323 edge->owner = r_pedge;
324
325 edge->nearzi = lzi0;
326
327 if (side == 0)
328 {
329 // trailing edge (go from p1 to p2)
330 v = ceilv0;
331 v2 = r_ceilv1 - 1;
332
333 edge->surfs[0] = surface_p - surfaces;
334 edge->surfs[1] = 0;
335
336 u_step = ((r_u1 - u0) / (r_v1 - v0));
337 u = u0 + ((float)v - v0) * u_step;
338 }
339 else
340 {
341 // leading edge (go from p2 to p1)
342 v2 = ceilv0 - 1;
343 v = r_ceilv1;
344
345 edge->surfs[0] = 0;
346 edge->surfs[1] = surface_p - surfaces;
347
348 u_step = ((u0 - r_u1) / (v0 - r_v1));
349 u = r_u1 + ((float)v - r_v1) * u_step;
350 }
351
352 edge->u_step = u_step*0x100000;
353 edge->u = u*0x100000 + 0xFFFFF;
354
355 // we need to do this to avoid stepping off the edges if a very nearly
356 // horizontal edge is less than epsilon above a scan, and numeric error causes
357 // it to incorrectly extend to the scan, and the extension of the line goes off
358 // the edge of the screen
359 // FIXME: is this actually needed?
360 if (edge->u < r_refdef.vrect_x_adj_shift20)
361 edge->u = r_refdef.vrect_x_adj_shift20;
362 if (edge->u > r_refdef.vrectright_adj_shift20)
363 edge->u = r_refdef.vrectright_adj_shift20;
364
365 //
366 // sort the edge in normally
367 //
368 u_check = edge->u;
369 if (edge->surfs[0])
370 u_check++; // sort trailers after leaders
371
372 if (!newedges[v] || newedges[v]->u >= u_check)
373 {
374 edge->next = newedges[v];
375 newedges[v] = edge;
376 }
377 else
378 {
379 pcheck = newedges[v];
380 while (pcheck->next && pcheck->next->u < u_check)
381 pcheck = pcheck->next;
382 edge->next = pcheck->next;
383 pcheck->next = edge;
384 }
385
386 edge->nextremove = removeedges[v2];
387 removeedges[v2] = edge;
388 }
389
390
391 /*
392 ================
393 R_ClipEdge
394 ================
395 */
R_ClipEdge(mvertex_t * pv0,mvertex_t * pv1,clipplane_t * clip)396 void R_ClipEdge (mvertex_t *pv0, mvertex_t *pv1, clipplane_t *clip)
397 {
398 float d0, d1, f;
399 mvertex_t clipvert;
400
401 if (clip)
402 {
403 do
404 {
405 d0 = DotProduct (pv0->position, clip->normal) - clip->dist;
406 d1 = DotProduct (pv1->position, clip->normal) - clip->dist;
407
408 if (d0 >= 0)
409 {
410 // point 0 is unclipped
411 if (d1 >= 0)
412 {
413 // both points are unclipped
414 continue;
415 }
416
417 // only point 1 is clipped
418
419 // we don't cache clipped edges
420 cacheoffset = 0x7FFFFFFF;
421
422 f = d0 / (d0 - d1);
423 clipvert.position[0] = pv0->position[0] +
424 f * (pv1->position[0] - pv0->position[0]);
425 clipvert.position[1] = pv0->position[1] +
426 f * (pv1->position[1] - pv0->position[1]);
427 clipvert.position[2] = pv0->position[2] +
428 f * (pv1->position[2] - pv0->position[2]);
429
430 if (clip->leftedge)
431 {
432 r_leftclipped = qtrue;
433 r_leftexit = clipvert;
434 }
435 else if (clip->rightedge)
436 {
437 r_rightclipped = qtrue;
438 r_rightexit = clipvert;
439 }
440
441 R_ClipEdge (pv0, &clipvert, clip->next);
442 return;
443 }
444 else
445 {
446 // point 0 is clipped
447 if (d1 < 0)
448 {
449 // both points are clipped
450 // we do cache fully clipped edges
451 if (!r_leftclipped)
452 cacheoffset = FULLY_CLIPPED_CACHED |
453 (r_framecount & FRAMECOUNT_MASK);
454 return;
455 }
456
457 // only point 0 is clipped
458 r_lastvertvalid = qfalse;
459
460 // we don't cache partially clipped edges
461 cacheoffset = 0x7FFFFFFF;
462
463 f = d0 / (d0 - d1);
464 clipvert.position[0] = pv0->position[0] +
465 f * (pv1->position[0] - pv0->position[0]);
466 clipvert.position[1] = pv0->position[1] +
467 f * (pv1->position[1] - pv0->position[1]);
468 clipvert.position[2] = pv0->position[2] +
469 f * (pv1->position[2] - pv0->position[2]);
470
471 if (clip->leftedge)
472 {
473 r_leftclipped = qtrue;
474 r_leftenter = clipvert;
475 }
476 else if (clip->rightedge)
477 {
478 r_rightclipped = qtrue;
479 r_rightenter = clipvert;
480 }
481
482 R_ClipEdge (&clipvert, pv1, clip->next);
483 return;
484 }
485 } while ((clip = clip->next) != NULL);
486 }
487
488 // add the edge
489 R_EmitEdge (pv0, pv1);
490 }
491
492 #endif // !id386
493
494
495 /*
496 ================
497 R_EmitCachedEdge
498 ================
499 */
R_EmitCachedEdge(void)500 void R_EmitCachedEdge (void)
501 {
502 edge_t *pedge_t;
503
504 pedge_t = (edge_t *)((unsigned long)r_edges + r_pedge->cachededgeoffset);
505
506 if (!pedge_t->surfs[0])
507 pedge_t->surfs[0] = surface_p - surfaces;
508 else
509 pedge_t->surfs[1] = surface_p - surfaces;
510
511 if (pedge_t->nearzi > r_nearzi) // for mipmap finding
512 r_nearzi = pedge_t->nearzi;
513
514 r_emitted = 1;
515 }
516
517
518 /*
519 ================
520 R_RenderFace
521 ================
522 */
R_RenderFace(msurface_t * fa,int clipflags)523 void R_RenderFace (msurface_t *fa, int clipflags)
524 {
525 int i, lindex;
526 unsigned mask;
527 mplane_t *pplane;
528 float distinv;
529 vec3_t p_normal;
530 medge_t *pedges, tedge;
531 clipplane_t *pclip;
532
533 // translucent surfaces are not drawn by the edge renderer
534 if (fa->texinfo->flags & (SURF_TRANS33|SURF_TRANS66))
535 {
536 fa->nextalphasurface = r_alpha_surfaces;
537 r_alpha_surfaces = fa;
538 return;
539 }
540
541 // sky surfaces encountered in the world will cause the
542 // environment box surfaces to be emited
543 if ( fa->texinfo->flags & SURF_SKY )
544 {
545 R_EmitSkyBox ();
546 return;
547 }
548
549 // skip out if no more surfs
550 if ((surface_p) >= surf_max)
551 {
552 r_outofsurfaces++;
553 return;
554 }
555
556 // ditto if not enough edges left, or switch to auxedges if possible
557 if ((edge_p + fa->numedges + 4) >= edge_max)
558 {
559 r_outofedges += fa->numedges;
560 return;
561 }
562
563 c_faceclip++;
564
565 // set up clip planes
566 pclip = NULL;
567
568 for (i=3, mask = 0x08 ; i>=0 ; i--, mask >>= 1)
569 {
570 if (clipflags & mask)
571 {
572 view_clipplanes[i].next = pclip;
573 pclip = &view_clipplanes[i];
574 }
575 }
576
577 // push the edges through
578 r_emitted = 0;
579 r_nearzi = 0;
580 r_nearzionly = qfalse;
581 makeleftedge = makerightedge = qfalse;
582 pedges = currentmodel->edges;
583 r_lastvertvalid = qfalse;
584
585 for (i=0 ; i<fa->numedges ; i++)
586 {
587 lindex = currentmodel->surfedges[fa->firstedge + i];
588
589 if (lindex > 0)
590 {
591 r_pedge = &pedges[lindex];
592
593 // if the edge is cached, we can just reuse the edge
594 if (!insubmodel)
595 {
596 if (r_pedge->cachededgeoffset & FULLY_CLIPPED_CACHED)
597 {
598 if ((r_pedge->cachededgeoffset & FRAMECOUNT_MASK) ==
599 r_framecount)
600 {
601 r_lastvertvalid = qfalse;
602 continue;
603 }
604 }
605 else
606 {
607 if ((((unsigned long)edge_p - (unsigned long)r_edges) >
608 r_pedge->cachededgeoffset) &&
609 (((edge_t *)((unsigned long)r_edges +
610 r_pedge->cachededgeoffset))->owner == r_pedge))
611 {
612 R_EmitCachedEdge ();
613 r_lastvertvalid = qfalse;
614 continue;
615 }
616 }
617 }
618
619 // assume it's cacheable
620 cacheoffset = (byte *)edge_p - (byte *)r_edges;
621 r_leftclipped = r_rightclipped = qfalse;
622 R_ClipEdge (&r_pcurrentvertbase[r_pedge->v[0]],
623 &r_pcurrentvertbase[r_pedge->v[1]],
624 pclip);
625 r_pedge->cachededgeoffset = cacheoffset;
626
627 if (r_leftclipped)
628 makeleftedge = qtrue;
629 if (r_rightclipped)
630 makerightedge = qtrue;
631 r_lastvertvalid = qtrue;
632 }
633 else
634 {
635 lindex = -lindex;
636 r_pedge = &pedges[lindex];
637 // if the edge is cached, we can just reuse the edge
638 if (!insubmodel)
639 {
640 if (r_pedge->cachededgeoffset & FULLY_CLIPPED_CACHED)
641 {
642 if ((r_pedge->cachededgeoffset & FRAMECOUNT_MASK) ==
643 r_framecount)
644 {
645 r_lastvertvalid = qfalse;
646 continue;
647 }
648 }
649 else
650 {
651 // it's cached if the cached edge is valid and is owned
652 // by this medge_t
653 if ((((unsigned long)edge_p - (unsigned long)r_edges) >
654 r_pedge->cachededgeoffset) &&
655 (((edge_t *)((unsigned long)r_edges +
656 r_pedge->cachededgeoffset))->owner == r_pedge))
657 {
658 R_EmitCachedEdge ();
659 r_lastvertvalid = qfalse;
660 continue;
661 }
662 }
663 }
664
665 // assume it's cacheable
666 cacheoffset = (byte *)edge_p - (byte *)r_edges;
667 r_leftclipped = r_rightclipped = qfalse;
668 R_ClipEdge (&r_pcurrentvertbase[r_pedge->v[1]],
669 &r_pcurrentvertbase[r_pedge->v[0]],
670 pclip);
671 r_pedge->cachededgeoffset = cacheoffset;
672
673 if (r_leftclipped)
674 makeleftedge = qtrue;
675 if (r_rightclipped)
676 makerightedge = qtrue;
677 r_lastvertvalid = qtrue;
678 }
679 }
680
681 // if there was a clip off the left edge, add that edge too
682 // FIXME: faster to do in screen space?
683 // FIXME: share clipped edges?
684 if (makeleftedge)
685 {
686 r_pedge = &tedge;
687 r_lastvertvalid = qfalse;
688 R_ClipEdge (&r_leftexit, &r_leftenter, pclip->next);
689 }
690
691 // if there was a clip off the right edge, get the right r_nearzi
692 if (makerightedge)
693 {
694 r_pedge = &tedge;
695 r_lastvertvalid = qfalse;
696 r_nearzionly = qtrue;
697 R_ClipEdge (&r_rightexit, &r_rightenter, view_clipplanes[1].next);
698 }
699
700 // if no edges made it out, return without posting the surface
701 if (!r_emitted)
702 return;
703
704 r_polycount++;
705
706 surface_p->msurf = fa;
707 surface_p->nearzi = r_nearzi;
708 surface_p->flags = fa->flags;
709 surface_p->insubmodel = insubmodel;
710 surface_p->spanstate = 0;
711 surface_p->entity = currententity;
712 surface_p->key = r_currentkey++;
713 surface_p->spans = NULL;
714
715 pplane = fa->plane;
716 // FIXME: cache this?
717 TransformVector (pplane->normal, p_normal);
718 // FIXME: cache this?
719 distinv = 1.0 / (pplane->dist - DotProduct (modelorg, pplane->normal));
720
721 surface_p->d_zistepu = p_normal[0] * xscaleinv * distinv;
722 surface_p->d_zistepv = -p_normal[1] * yscaleinv * distinv;
723 surface_p->d_ziorigin = p_normal[2] * distinv -
724 xcenter * surface_p->d_zistepu -
725 ycenter * surface_p->d_zistepv;
726
727 surface_p++;
728 }
729
730
731 /*
732 ================
733 R_RenderBmodelFace
734 ================
735 */
R_RenderBmodelFace(bedge_t * pedges,msurface_t * psurf)736 void R_RenderBmodelFace (bedge_t *pedges, msurface_t *psurf)
737 {
738 int i;
739 unsigned mask;
740 mplane_t *pplane;
741 float distinv;
742 vec3_t p_normal;
743 medge_t tedge;
744 clipplane_t *pclip;
745
746 if (psurf->texinfo->flags & (SURF_TRANS33|SURF_TRANS66))
747 {
748 psurf->nextalphasurface = r_alpha_surfaces;
749 r_alpha_surfaces = psurf;
750 return;
751 }
752
753 // skip out if no more surfs
754 if (surface_p >= surf_max)
755 {
756 r_outofsurfaces++;
757 return;
758 }
759
760 // ditto if not enough edges left, or switch to auxedges if possible
761 if ((edge_p + psurf->numedges + 4) >= edge_max)
762 {
763 r_outofedges += psurf->numedges;
764 return;
765 }
766
767 c_faceclip++;
768
769 // this is a dummy to give the caching mechanism someplace to write to
770 r_pedge = &tedge;
771
772 // set up clip planes
773 pclip = NULL;
774
775 for (i=3, mask = 0x08 ; i>=0 ; i--, mask >>= 1)
776 {
777 if (r_clipflags & mask)
778 {
779 view_clipplanes[i].next = pclip;
780 pclip = &view_clipplanes[i];
781 }
782 }
783
784 // push the edges through
785 r_emitted = 0;
786 r_nearzi = 0;
787 r_nearzionly = qfalse;
788 makeleftedge = makerightedge = qfalse;
789 // FIXME: keep clipped bmodel edges in clockwise order so last vertex caching
790 // can be used?
791 r_lastvertvalid = qfalse;
792
793 for ( ; pedges ; pedges = pedges->pnext)
794 {
795 r_leftclipped = r_rightclipped = qfalse;
796 R_ClipEdge (pedges->v[0], pedges->v[1], pclip);
797
798 if (r_leftclipped)
799 makeleftedge = qtrue;
800 if (r_rightclipped)
801 makerightedge = qtrue;
802 }
803
804 // if there was a clip off the left edge, add that edge too
805 // FIXME: faster to do in screen space?
806 // FIXME: share clipped edges?
807 if (makeleftedge)
808 {
809 r_pedge = &tedge;
810 R_ClipEdge (&r_leftexit, &r_leftenter, pclip->next);
811 }
812
813 // if there was a clip off the right edge, get the right r_nearzi
814 if (makerightedge)
815 {
816 r_pedge = &tedge;
817 r_nearzionly = qtrue;
818 R_ClipEdge (&r_rightexit, &r_rightenter, view_clipplanes[1].next);
819 }
820
821 // if no edges made it out, return without posting the surface
822 if (!r_emitted)
823 return;
824
825 r_polycount++;
826
827 surface_p->msurf = psurf;
828 surface_p->nearzi = r_nearzi;
829 surface_p->flags = psurf->flags;
830 surface_p->insubmodel = qtrue;
831 surface_p->spanstate = 0;
832 surface_p->entity = currententity;
833 surface_p->key = r_currentbkey;
834 surface_p->spans = NULL;
835
836 pplane = psurf->plane;
837 // FIXME: cache this?
838 TransformVector (pplane->normal, p_normal);
839 // FIXME: cache this?
840 distinv = 1.0 / (pplane->dist - DotProduct (modelorg, pplane->normal));
841
842 surface_p->d_zistepu = p_normal[0] * xscaleinv * distinv;
843 surface_p->d_zistepv = -p_normal[1] * yscaleinv * distinv;
844 surface_p->d_ziorigin = p_normal[2] * distinv -
845 xcenter * surface_p->d_zistepu -
846 ycenter * surface_p->d_zistepv;
847
848 surface_p++;
849 }
850
851