Filter:   InfoImg
download gl_sky_clip.c
Language: C
License: GPL
Copyright: (C) 1996-1997 Id Software, Inc.
LOC: 536
Project Info
QuakeForge(quake)
Server: SourceForge
Type: cvs
...ake\quake\newtree32\source\
   .cvsignore
   .gdbinit
   .indent.pro
   buildnum.c
   cd_dos.c
   cd_linux.c
   cd_null.c
   cd_sdl.c
   cd_sgi.c
   cd_win.c
   checksum.c
   cl_cam.c
   cl_cmd.c
   cl_cvar.c
   cl_demo.c
   cl_ents.c
   cl_input.c
   cl_main.c
   cl_misc.c
   cl_parse.c
   cl_pred.c
   cl_slist.c
   cl_sys_sdl.c
   cl_sys_unix.c
   cl_sys_win.c
   cl_tent.c
   cmd.c
   com.c
   console.c
   context_x11.c
   crc.c
   cvar.c
   d_edge.c
   d_fill.c
   d_init.c
   d_modech.c
   d_part.c
   d_polyse.c
   d_scan.c
   d_sky.c
   d_sprite.c
   d_surf.c
   d_vars.c
   d_zpoint.c
   dga_check.c
   dirent.c
   draw.c
   fbset.c
   fbset_modes_y.y
   fnmatch.c
   fractalnoise.c
   gl_draw.c
   gl_dyn_fires.c
   gl_dyn_part.c
   gl_dyn_textures.c
   gl_mesh.c
   gl_model_alias.c
   gl_model_brush.c
   gl_model_fullbright.c
   gl_model_sprite.c
   gl_ngraph.c
   gl_rlight.c
   gl_rmain.c
   gl_rmisc.c
   gl_rsurf.c
   gl_screen.c
   gl_skin.c
   gl_sky.c
   gl_sky_clip.c
   gl_view.c
   gl_warp.c
   hash.c
   in_fbdev.c
   in_ggi.c
   in_null.c
   in_sdl.c
   in_svgalib.c
   in_win.c
   in_x11.c
   info.c
   joy_linux.c
   joy_null.c
   joy_win.c
   keys.c
   link.c
   locs.c
   Makefile.am
   makefile.mgw
   makefile.win
   mathlib.c
   mdfour.c
   menu.c
   model.c
   model_alias.c
   model_brush.c
   model_sprite.c
   msg.c
   net_chan.c
   net_com.c
   net_packetlog.c
   net_udp.c
   net_udp6.c
   nonintel.c
   pcx.c
   pmove.c
   pmovetst.c
   pr_edict.c
   pr_exec.c
   pr_offs.c
   qargs.c
   qendian.c
   qfgl_ext.c
   quakefs.c
   quakeio.c
   r_aclip.c
   r_alias.c
   r_bsp.c
   r_draw.c
   r_edge.c
   r_efrag.c
   r_light.c
   r_main.c
   r_misc.c
   r_part.c
   r_sky.c
   r_sprite.c
   r_surf.c
   r_vars.c
   r_view.c
   sbar.c
   screen.c
   sizebuf.c
   skin.c
   snd_alsa_0_5.c
   snd_alsa_0_6.c
   snd_disk.c
   snd_dma.c
   snd_mem.c
   snd_mix.c
   snd_null.c
   snd_oss.c
   snd_sdl.c
   snd_sgi.c
   snd_sun.c
   snd_win.c
   sv_ccmds.c
   sv_cvar.c
   sv_ents.c
   sv_init.c
   sv_main.c
   sv_misc.c
   sv_model.c
   sv_move.c
   sv_nchan.c
   sv_phys.c
   sv_pr_cmds.c
   sv_progs.c
   sv_send.c
   sv_sys_unix.c
   sv_sys_win.c
   sv_user.c
   sw_model_alias.c
   sw_model_brush.c
   sw_model_sprite.c
   sw_skin.c
   sw_view.c
   sys_null.c
   sys_unix.c
   sys_win.c
   teamplay.c
   tga.c
   va.c
   ver_check.c
   vid.c
   vid_3dfxsvga.c
   vid_common_gl.c
   vid_fbdev.c
   vid_ggi.c
   vid_glx.c
   vid_mgl.c
   vid_null.c
   vid_sdl.c
   vid_sgl.c
   vid_svgalib.c
   vid_wgl.c
   vid_win.c
   vid_x11.c
   wad.c
   world.c
   zone.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
	gl_sky_clip.c

	sky polygons

	Copyright (C) 1996-1997  Id Software, Inc.

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

	See the GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to:

		Free Software Foundation, Inc.
		59 Temple Place - Suite 330
		Boston, MA  02111-1307, USA

	$Id: gl_sky_clip.c,v 1.1.1.1 2001/03/06 09:34:00 lordhavoc Exp $
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#include <stdarg.h>

#include "console.h"
#include "glquake.h"
#include "sys.h"
#include "view.h"

extern qboolean skyloaded;
extern vec5_t skyvec[6][4];

#define BOX_WIDTH 2056

/* cube face to sky texture offset conversion */
static const int skytex_offs[] = { 3, 0, 4, 1, 2, 5 };

/* clockwise loop through the cube faces adjoining the current face */
static const int face_loop[6][5] = {
	{1, 2, 4, 5, 1},
	{0, 5, 3, 2, 0},
	{0, 1, 3, 4, 0},
	{1, 5, 4, 2, 1},
	{0, 2, 3, 5, 0},
	{0, 4, 3, 1, 0},
};

/* convert axis and face distance into face */
static const int faces_table[3][6] = {
	{-1, 0, 0, -1, 3, 3},
	{-1, 4, 4, -1, 1, 1},
	{-1, 2, 2, -1, 5, 5},
};

/* convert face magic bit mask to index into visit array */
static const int faces_bit_magic[] = { 2, 1, -1, 0, 3, -1, 4, -1 };

/* axis the cube face cuts (also index into vec3_t and n % 3 for 0 <= n < 6) */
static const int face_axis[] = { 0, 1, 2, 0, 1, 2 };

/* offset on the axis the cube face cuts */
static const vec_t face_offset[] = { 1024, 1024, 1024, -1024, -1024, -1024 };

/* cube face */
struct face_def {
	int         tex;					// texture to bind to
	glpoly_t    poly;					// describe the polygon of this face
	float       verts[32][VERTEXSIZE];
};

struct visit_def {
	int         face;					// face being visited
	int         leave;					// vertex departed through
};

/* our cube */
struct box_def {
	/* keep track of which cube faces we visit and in what order */
	struct visit_def visited_faces[9];
	int         face_visits[6];
	int         face_count;
	/* the cube faces */
	struct face_def face[6];
};

/*
	determine_face

	return the face of the cube which v hits first
	0	+x
	1	+y
	2	+z
	3	-x
	4	-y
	5	-z
	Also scales v so it touches that face.
*/
static int
determine_face (vec3_t v)
{
	float       a[3];
	float       m;
	int         i = 0;

	m = a[0] = fabs (v[0]);
	a[1] = fabs (v[1]);
	a[2] = fabs (v[2]);
	if (a[1] > m) {
		m = a[1];
		i = 1;
	}
	if (a[2] > m) {
		m = a[2];
		i = 2;
	}
	if (!m) {
		Sys_Error ("%s speared by sky poly edge\n", name->string);
	}
	if (v[i] < 0)
		i += 3;
	VectorScale (v, 1024 / m, v);
	return i;
}

/*
	find_intersect (for want of a better name)

	finds the point of intersection of the plane formed by the eye and the two
	points on the cube and the edge of the cube defined by the two faces.
	Currently, this will break if the two points are not on adjoining cube
	faces (ie either on opposing faces or the same face).

	The equation for the point of intersection of a line and a plane is:

		        (x - p).n
		y = x - _________ v
		           v.n

	where n is the normal to the plane, p is a point on the plane, x is a
	point on the line, and v is the direction vector of the line. n is found
	by (x1 - e) cross (x2 - e) and p is taken to be e (e = eye coords) for
	simplicity. However, because e is at 0,0,0, this simplifies to n = x1
	cross x2 and p = 0,0,0, so the equation above simplifies to:

		        x.n
		y = x - ___ v
		        v.n
*/
static int
find_intersect (int face1, vec3_t x1, int face2, vec3_t x2, vec3_t y)
{
	vec3_t      n;						// normal to the plane formed by the

	// eye and the two points on the cube.

	vec3_t      x = { 0, 0, 0 };		// point on cube edge of adjoining

	// faces. always on an axis plane.

	vec3_t      v = { 0, 0, 0 };		// direction vector of cube edge.

	// always +ve

	vec_t       x_n, v_n;				// x.n and v.n
	int         axis;
	vec3_t      t;

	x[face_axis[face1]] = face_offset[face1];
	x[face_axis[face2]] = face_offset[face2];

	axis = 3 - ((face_axis[face1]) + (face_axis[face2]));
	v[axis] = 1;

	CrossProduct (x1, x2, n);

	x_n = DotProduct (x, n);
	v_n = DotProduct (v, n);
	VectorScale (v, x_n / v_n, t);
	VectorSubtract (x, t, y);

	return axis;
}

/*
	find_cube_vertex

	get the coords of the vertex common to the three specified faces of the
	cube. NOTE: this WILL break if the three faces do not share a common
	vertex. ie works = ((face1 % 3 != face2 % 3)
	                    && (face2 % 3 != face3 % 3)
						&& (face1 % 3 != face3 % 3))
*/
static void
find_cube_vertex (int face1, int face2, int face3, vec3_t v)
{
	v[face_axis[face1]] = face_offset[face1];
	v[face_axis[face2]] = face_offset[face2];
	v[face_axis[face3]] = face_offset[face3];
}

/*
	set_vertex

	add the vertex to the polygon describing the face of the cube. Offsets
	the vertex relative to r_refdef.vieworg so the cube is always centered
	on the player and also calculates the texture coordinates of the vertex
	(wish I could find a cleaner way of calculating s and t).
*/
static void
set_vertex (struct box_def *box, int face, int ind, vec3_t v)
{
	VectorCopy (v, box->face[face].poly.verts[ind]);
	VectorAdd (v, r_refdef.vieworg, box->face[face].poly.verts[ind]);
	switch (face) {
		case 0:
			box->face[face].poly.verts[ind][3] = (1024 - v[1] + 4) / BOX_WIDTH;
			box->face[face].poly.verts[ind][4] = (1024 - v[2] + 4) / BOX_WIDTH;
			break;
		case 1:
			box->face[face].poly.verts[ind][3] = (1024 + v[0] + 4) / BOX_WIDTH;
			box->face[face].poly.verts[ind][4] = (1024 - v[2] + 4) / BOX_WIDTH;
			break;
		case 2:
			box->face[face].poly.verts[ind][3] = (1024 + v[0] + 4) / BOX_WIDTH;
			box->face[face].poly.verts[ind][4] = (1024 + v[1] + 4) / BOX_WIDTH;
			break;
		case 3:
			box->face[face].poly.verts[ind][3] = (1024 + v[1] + 4) / BOX_WIDTH;
			box->face[face].poly.verts[ind][4] = (1024 - v[2] + 4) / BOX_WIDTH;
			break;
		case 4:
			box->face[face].poly.verts[ind][3] = (1024 - v[0] + 4) / BOX_WIDTH;
			box->face[face].poly.verts[ind][4] = (1024 - v[2] + 4) / BOX_WIDTH;
			break;
		case 5:
			box->face[face].poly.verts[ind][3] = (1024 + v[0] + 4) / BOX_WIDTH;
			box->face[face].poly.verts[ind][4] = (1024 - v[1] + 4) / BOX_WIDTH;
			break;
	}
}

/*
 	add_vertex

	append a vertex to the poly vertex list.
*/
static void
add_vertex (struct box_def *box, int face, vec3_t v)
{
	set_vertex (box, face, box->face[face].poly.numverts++, v);
}

/*
	insert_cube_vertices

	insert the given cube vertices into the vertex list of the poly in the
	correct location.
*/
static void
insert_cube_vertices (struct box_def *box, struct visit_def visit, int count,
					  ...)
{
	int         i;
	vec3_t    **v;
	va_list     args;
	int         face = visit.face;
	int         ind = visit.leave + 1;

#ifdef __BORLANDC__
// This is fix for borland alloca "feature" which fails to restore stack
// correcly if calling function doesn't have any references to local variables.
        char dummy[5];

        dummy[0]=0;
#endif

	va_start (args, count);
	v = (vec3_t **) alloca (count * sizeof (vec3_t *));

	for (i = 0; i < count; i++) {
		v[i] = va_arg (args, vec3_t *);
	}
	va_end (args);

	if (ind == box->face[face].poly.numverts) {
		// the vertex the sky poly left this cube fase through is very
		// conveniently the last vertex of the face poly. this means we
		// can just append the vetexen
		for (i = 0; i < count; i++)
			add_vertex (box, face, *v[i]);
	} else {
		// we have to insert the cube vertices into the face poly
		// vertex list
		glpoly_t   *p = &box->face[face].poly;
		int         c = p->numverts - ind;
		const int   vert_size = sizeof (p->verts[0]);

		memmove (p->verts[ind + count], p->verts[ind], c * vert_size);
		p->numverts += count;
		for (i = 0; i < count; i++)
			set_vertex (box, face, ind + i, *v[i]);
	}
}

/*
	cross_cube_edge

	add the vertex formed by the poly edge crossing the cube edge to the
	polygon for the two faces on that edge. Actually, the two faces define
	the edge :). The poly edge is going from face 1 to face 2 (for
	enter/leave purposes).
*/
static void
cross_cube_edge (struct box_def *box, int face1, vec3_t v1, int face2,
				 vec3_t v2)
{
	vec3_t      l;
	int         axis;
	int         face = -1;

	axis = find_intersect (face1, v1, face2, v2, l);
	if (l[axis] > 1024)
		face = axis;
	else if (l[axis] < -1024)
		face = axis + 3;
	if (face >= 0) {
		vec3_t      x;

		VectorAdd (v1, v2, x);
		VectorScale (x, 0.5, x);
		cross_cube_edge (box, face1, v1, face, x);
		cross_cube_edge (box, face, x, face2, v2);
	} else {
		struct visit_def *visit = box->visited_faces;

		visit[box->face_count - 1].leave = box->face[face1].poly.numverts;
		visit[box->face_count].face = face2;
		box->face_count++;
		box->face_visits[face2]++;

		add_vertex (box, face1, l);
		add_vertex (box, face2, l);
	}
}

/*
	process_corners

	egad, veddy complicated :)
*/
static void
process_corners (struct box_def *box)
{
	struct visit_def *visit = box->visited_faces;
	int         max_visit = 0;
	int         i;
	int         center = -1;

	if (visit[box->face_count - 1].face == visit[0].face) {
		box->face_count--;
	}

	for (i = 0; i < 6; i++) {
		if (max_visit < box->face_visits[i]) {
			max_visit = box->face_visits[i];
			center = i;
		}
	}

	switch (box->face_count) {
		case 1:
		case 2:
		case 8:
			// no corners
			return;
		case 3:
			// one corner, no edges
			{
				vec3_t      v;

				find_cube_vertex (visit[0].face, visit[1].face, visit[2].face,
								  v);
				insert_cube_vertices (box, visit[0], 1, v);
				insert_cube_vertices (box, visit[1], 1, v);
				insert_cube_vertices (box, visit[2], 1, v);
			}
			break;
		case 4:
			if (max_visit > 1)
				return;
			if (abs (visit[2].face - visit[0].face) == 3
				&& abs (visit[3].face - visit[1].face) == 3) {
				// 4 vertices
				int         sum, diff;
				vec3_t      v[4];

				sum =
					visit[0].face + visit[1].face + visit[2].face +
					visit[3].face;
				diff = visit[1].face - visit[0].face;
				sum %= 3;
				diff = (diff + 6) % 6;

				center = faces_table[sum][diff];
				for (i = 0; i < 4; i++) {
					find_cube_vertex (visit[i].face, visit[(i + 1) & 3].face,
									  center, v[i]);
					add_vertex (box, center, v[i]);
				}
				for (i = 0; i < 4; i++)
					insert_cube_vertices (box, visit[i], 2, v[i],
										  v[(i - 1) & 3]);
			} else {
				// 2 vertices
				int         l_f, t_f, r_f, b_f;
				vec3_t      v_l, v_r;

				if (abs (visit[2].face - visit[0].face) == 3) {
					l_f = 0;
					t_f = 1;
					r_f = 2;
					b_f = 3;
				} else if (abs (visit[3].face - visit[1].face) == 3) {
					l_f = 1;
					t_f = 2;
					r_f = 3;
					b_f = 0;
				} else {
					return;
				}
				find_cube_vertex (visit[l_f].face, visit[t_f].face,
								  visit[b_f].face, v_l);
				find_cube_vertex (visit[r_f].face, visit[t_f].face,
								  visit[b_f].face, v_r);

				insert_cube_vertices (box, visit[t_f], 2, v_r, v_l);
				insert_cube_vertices (box, visit[b_f], 2, v_l, v_r);

				insert_cube_vertices (box, visit[l_f], 1, v_l);
				insert_cube_vertices (box, visit[r_f], 1, v_r);
			}
			break;
		case 5:
			if (max_visit > 1) {
				// one vertex
				vec3_t      v;

				for (i = 0; i < 4; i++) {
					// don't need to check the 5th visit
					if (visit[(i + 2) % 5].face == visit[(i + 4) % 5].face)
						break;
				}
				find_cube_vertex (visit[i].face, visit[(i + 1) % 5].face,
								  visit[(i + 2) % 5].face, v);
				insert_cube_vertices (box, visit[i], 1, v);
				insert_cube_vertices (box, visit[(i + 1) % 5], 1, v);
				insert_cube_vertices (box, visit[(i + 4) % 5], 1, v);

			} else {
				// 3 vertices
				unsigned int sel =
					(((abs (visit[2].face - visit[0].face) == 3) << 2) |
					 ((abs (visit[3].face - visit[1].face) == 3) << 1)
					 | ((abs (visit[4].face - visit[2].face) == 3) << 0));
				vec3_t      v[3];

				center = faces_bit_magic[sel];
				// printf ("%02o %d  %d %d %d %d %d\n", sel, center,
				// visit[0].face,
				// visit[1].face, visit[2].face, visit[3].face,
				// visit[4].face);
				for (i = 0; i < 3; i++)
					find_cube_vertex (visit[center].face,
									  visit[(center + 1 + i) % 5].face,
									  visit[(center + 2 + i) % 5].face, v[i]);
				insert_cube_vertices (box, visit[center], 3, v[0], v[1], v[2]);
				insert_cube_vertices (box, visit[(center + 1) % 5], 1, v[0]);
				insert_cube_vertices (box, visit[(center + 2) % 5], 2, v[1],
									  v[0]);
				insert_cube_vertices (box, visit[(center + 3) % 5], 2, v[2],
									  v[1]);
				insert_cube_vertices (box, visit[(center + 4) % 5], 1, v[2]);
			}
			break;
		case 6:
			if (max_visit > 2)
				return;
			for (i = 0; i < 5; i++) {
				// don't need to check the last point
				if (visit[(i + 3) % 6].face == visit[(i + 5) % 6].face
					|| visit[(i + 2) % 6].face == visit[(i + 5) % 6].face)
					break;
			}
			if (visit[(i + 3) % 6].face == visit[(i + 5) % 6].face) {
				// adjacant vertices
				vec3_t      v[2];

				find_cube_vertex (visit[i].face, visit[(i + 1) % 6].face,
								  visit[(i + 2) % 6].face, v[0]);
				find_cube_vertex (visit[(i + 1) % 6].face,
								  visit[(i + 2) % 6].face,
								  visit[(i + 3) % 6].face, v[1]);

				insert_cube_vertices (box, visit[(i + 5) % 6], 2, v[2], v[1]);

				insert_cube_vertices (box, visit[i], 1, v[0]);
				insert_cube_vertices (box, visit[(i + 1) % 6], 2, v[1], v[0]);
				insert_cube_vertices (box, visit[(i + 2) % 6], 1, v[1]);
			} else {
				// opposing vertices
				vec3_t      v[2];

				find_cube_vertex (visit[i].face, visit[(i + 1) % 6].face,
								  visit[(i + 2) % 6].face, v[0]);
				find_cube_vertex (visit[(i + 3) % 6].face,
								  visit[(i + 4) % 6].face,
								  visit[(i + 5) % 6].face, v[1]);

				insert_cube_vertices (box, visit[i], 1, v[0]);
				insert_cube_vertices (box, visit[(i + 1) % 6], 1, v[0]);

				insert_cube_vertices (box, visit[(i + 3) % 6], 1, v[1]);
				insert_cube_vertices (box, visit[(i + 4) % 6], 1, v[1]);

				insert_cube_vertices (box, visit[(i + 2) % 6], 1, v[1]);
				insert_cube_vertices (box, visit[(i + 5) % 6], 1, v[0]);
			}
			break;
		case 7:
			for (i = 0; i < 6; i++) {
				// don't need to check the last point
				if (visit[(i + 2) % 6].face == visit[(i + 4) % 6].face
					&& visit[(i + 4) % 6].face == visit[(i + 6) % 6].face)
					break;
			}
			{
				vec3_t      v;

				find_cube_vertex (visit[i].face, visit[(i + 1) % 6].face,
								  visit[(i + 2) % 6].face, v);

				insert_cube_vertices (box, visit[i], 1, v);
				insert_cube_vertices (box, visit[(i + 1) % 7], 1, v);
				insert_cube_vertices (box, visit[(i + 6) % 7], 1, v);
			}
			break;
	}
}

/*
	render_box

	draws all faces of the cube with 3 or more vertices.
*/
static void
render_box (struct box_def *box)
{
	int         i, j;

	for (i = 0; i < 6; i++) {
		if (box->face[i].poly.numverts <= 2)
			continue;
		glBindTexture (GL_TEXTURE_2D, box->face[i].tex);
		glBegin (GL_POLYGON);
		for (j = 0; j < box->face[i].poly.numverts; j++) {
			glTexCoord2fv (box->face[i].poly.verts[j] + 3);
			glVertex3fv (box->face[i].poly.verts[j]);
		}
		glEnd ();
	}
}

void
R_DrawSkyBoxPoly (glpoly_t *poly)
{
	int         i;
	struct box_def box;

	/* projected vertex and face of the previous sky poly vertex */
	vec3_t      last_v;
	int         prev_face;

	/* projected vertex and face of the current sky poly vertex */
	vec3_t      v;
	int         face;

	memset (&box, 0, sizeof (box));
	for (i = 0; i < 6; i++) {
		box.face[i].tex = SKY_TEX + skytex_offs[i];
	}

	if (poly->numverts >= 32) {
		Sys_Error ("too many verts!");
	}

	VectorSubtract (poly->verts[poly->numverts - 1], r_refdef.vieworg, last_v);
	prev_face = determine_face (last_v);

	box.visited_faces[0].face = prev_face;
	box.face_count = 1;

	for (i = 0; i < poly->numverts; i++) {
		VectorSubtract (poly->verts[i], r_refdef.vieworg, v);
		face = determine_face (v);
		if (face != prev_face) {
			if ((face_axis[face]) == (face_axis[prev_face])) {
				int         x_face;
				vec3_t      x;

				VectorAdd (v, last_v, x);
				VectorScale (x, 0.5, x);
				x_face = determine_face (x);

				cross_cube_edge (&box, prev_face, last_v, x_face, x);
				cross_cube_edge (&box, x_face, x, face, v);
			} else {
				cross_cube_edge (&box, prev_face, last_v, face, v);
			}
		}
		add_vertex (&box, face, v);

		VectorCopy (v, last_v);
		prev_face = face;
	}

	process_corners (&box);

	render_box (&box);
}

void
R_DrawSkyDomePoly (glpoly_t *poly)
{
	int         i;

	glBegin (GL_POLYGON);
	for (i = 0; i < poly->numverts; i++) {
		glVertex3fv (poly->verts[i]);
	}
	glEnd ();
}

void
R_DrawSkyChain (msurface_t *sky_chain)
{
	msurface_t *sc = sky_chain;
	float l = 1 / (256 * brightness->value);
	
	glColor3f (lighthalf_v[0] * l, lighthalf_v[1] * l, lighthalf_v[2] * l);
	if (skyloaded) {
		glDepthRange (gldepthmax, gldepthmax);
		while (sc) {
			glpoly_t   *p = sc->polys;

			while (p) {
				R_DrawSkyBoxPoly (p);
				p = p->next;
			}
			sc = sc->texturechain;
		}
		glDepthRange (gldepthmin, gldepthmax);
	} else {
		glDisable (GL_BLEND);
		glDisable (GL_TEXTURE_2D);
		glColor3f (0, 0, 0);
		while (sc) {
			glpoly_t   *p = sc->polys;

			while (p) {
				R_DrawSkyDomePoly (p);
				p = p->next;
			}
			sc = sc->texturechain;
		}
		glEnable (GL_TEXTURE_2D);
		glEnable (GL_BLEND);
	}
#if 0
	// seems to work, but this is the wrong place to do it.
	glColor4f (1,1,1,0);
	sc = sky_chain;
	while (sc) {
		glpoly_t   *p = sc->polys;

		while (p) {
			int i;
			glBegin (GL_POLYGON);
			for (i = 0; i < p->numverts; i++) {
				glVertex3fv (p->verts[i]);
			}
			glEnd ();
			p = p->next;
		}
		sc = sc->texturechain;
	}
#endif
	glColor3ubv (lighthalf_v);
#if 0
	glDisable (GL_TEXTURE_2D);
	sc = sky_chain;
	glColor3f (1, 1, 1);
	while (sc) {
		glpoly_t   *p = sc->polys;

		while (p) {
			int         i;

			glBegin (GL_LINE_LOOP);
			for (i = 0; i < p->numverts; i++) {
				glVertex3fv (p->verts[i]);
			}
			glEnd ();
			p = p->next;
		}
		sc = sc->texturechain;
	}
	sc = sky_chain;
	glColor3f (0, 1, 0);
	glBegin (GL_POINTS);
	while (sc) {
		glpoly_t   *p = sc->polys;

		while (p) {
			int         i;
			vec3_t      x, c = { 0, 0, 0 };

			for (i = 0; i < p->numverts; i++) {
				VectorSubtract (p->verts[i], r_refdef.vieworg, x);
				VectorAdd (x, c, c);
			}
			VectorScale (c, 1.0 / p->numverts, c);
			VectorAdd (c, r_refdef.vieworg, c);
			glVertex3fv (c);
			p = p->next;
		}
		sc = sc->texturechain;
	}
	glEnd ();
	if (skyloaded) {
		int         i, j;

		glColor3f (1, 0, 0);
		for (i = 0; i < 6; i++) {
			vec3_t      v;

			glBegin (GL_LINE_LOOP);
			for (j = 0; j < 4; j++) {
				memcpy (v, &skyvec[i][j][2], sizeof (v));
				VectorAdd (v, r_refdef.vieworg, v);
				glVertex3fv (v);
			}
			glEnd ();
		}
	}
	glColor3ubv (lighthalf_v);
	glEnable (GL_TEXTURE_2D);
#endif
}