1/*
2Copyright (C) 1996-1997 Id Software, Inc.
3
4This program is free software; you can redistribute it and/or
5modify it under the terms of the GNU General Public License
6as published by the Free Software Foundation; either version 2
7of the License, or (at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18
19*/
20// d_sky.c
21
22#include "quakedef.h"
23#include "r_local.h"
24#include "d_local.h"
25
26#define SKY_SPAN_SHIFT	5
27#define SKY_SPAN_MAX	(1 << SKY_SPAN_SHIFT)
28
29
30/*
31=================
32D_Sky_uv_To_st
33=================
34*/
35void D_Sky_uv_To_st (int u, int v, fixed16_t *s, fixed16_t *t)
36{
37	float	wu, wv, temp;
38	vec3_t	end;
39
40	if (r_refdef.vrect.width >= r_refdef.vrect.height)
41		temp = (float)r_refdef.vrect.width;
42	else
43		temp = (float)r_refdef.vrect.height;
44
45	wu = 8192.0 * (float)(u-((int)vid.width>>1)) / temp;
46	wv = 8192.0 * (float)(((int)vid.height>>1)-v) / temp;
47
48	end[0] = 4096*vpn[0] + wu*vright[0] + wv*vup[0];
49	end[1] = 4096*vpn[1] + wu*vright[1] + wv*vup[1];
50	end[2] = 4096*vpn[2] + wu*vright[2] + wv*vup[2];
51	end[2] *= 3;
52	VectorNormalize (end);
53
54	temp = skytime*skyspeed;	// TODO: add D_SetupFrame & set this there
55	*s = (int)((temp + 6*(SKYSIZE/2-1)*end[0]) * 0x10000);
56	*t = (int)((temp + 6*(SKYSIZE/2-1)*end[1]) * 0x10000);
57}
58
59
60/*
61=================
62D_DrawSkyScans8
63=================
64*/
65void D_DrawSkyScans8 (espan_t *pspan)
66{
67	int				count, spancount, u, v;
68	unsigned char	*pdest;
69	fixed16_t		s, t, snext, tnext, sstep, tstep;
70	int				spancountminus1;
71
72	sstep = 0;	// keep compiler happy
73	tstep = 0;	// ditto
74
75	do
76	{
77		pdest = (unsigned char *)((byte *)d_viewbuffer +
78				(screenwidth * pspan->v) + pspan->u);
79
80		count = pspan->count;
81
82	// calculate the initial s & t
83		u = pspan->u;
84		v = pspan->v;
85		D_Sky_uv_To_st (u, v, &s, &t);
86
87		do
88		{
89			if (count >= SKY_SPAN_MAX)
90				spancount = SKY_SPAN_MAX;
91			else
92				spancount = count;
93
94			count -= spancount;
95
96			if (count)
97			{
98				u += spancount;
99
100			// calculate s and t at far end of span,
101			// calculate s and t steps across span by shifting
102				D_Sky_uv_To_st (u, v, &snext, &tnext);
103
104				sstep = (snext - s) >> SKY_SPAN_SHIFT;
105				tstep = (tnext - t) >> SKY_SPAN_SHIFT;
106			}
107			else
108			{
109			// calculate s and t at last pixel in span,
110			// calculate s and t steps across span by division
111				spancountminus1 = (float)(spancount - 1);
112
113				if (spancountminus1 > 0)
114				{
115					u += spancountminus1;
116					D_Sky_uv_To_st (u, v, &snext, &tnext);
117
118					sstep = (snext - s) / spancountminus1;
119					tstep = (tnext - t) / spancountminus1;
120				}
121			}
122
123			do
124			{
125				*pdest++ = r_skysource[((t & R_SKY_TMASK) >> 8) +
126						((s & R_SKY_SMASK) >> 16)];
127				s += sstep;
128				t += tstep;
129			} while (--spancount > 0);
130
131			s = snext;
132			t = tnext;
133
134		} while (count > 0);
135
136	} while ((pspan = pspan->pnext) != NULL);
137}
138
139