m.wantoverbright is gone
[divverent/darkplaces.git] / r_sky.c
1
2 #include "quakedef.h"
3
4 cvar_t r_sky = {CVAR_SAVE, "r_sky", "1"};
5 qboolean skyavailable_quake;
6 qboolean skyavailable_box;
7 int skyrendernow;
8 int skyrendermasked;
9
10 static rtexture_t *solidskytexture;
11 static rtexture_t *alphaskytexture;
12 static int skyrendersphere;
13 static int skyrenderbox;
14 static rtexturepool_t *skytexturepool;
15 static char skyname[256];
16 static char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
17 static rtexture_t *skyboxside[6];
18
19 void R_SkyStartFrame(void)
20 {
21         skyrendernow = false;
22         skyrendersphere = false;
23         skyrenderbox = false;
24         skyrendermasked = false;
25         if (r_sky.integer && !fogenabled)
26         {
27                 if (skyavailable_box)
28                         skyrenderbox = true;
29                 else if (skyavailable_quake)
30                         skyrendersphere = true;
31                 // for depth-masked sky, render the sky on the first sky surface encountered
32                 skyrendernow = true;
33                 skyrendermasked = true;
34         }
35 }
36
37 /*
38 ==================
39 R_SetSkyBox
40 ==================
41 */
42 int R_SetSkyBox(const char *sky)
43 {
44         int i;
45         char name[1024];
46         qbyte *image_rgba;
47
48         if (strcmp(sky, skyname) == 0) // no change
49                 return true;
50
51         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
52         skyavailable_box = false;
53         skyname[0] = 0;
54
55         if (!sky[0])
56                 return true;
57
58         if (strlen(sky) > 1000)
59         {
60                 Con_Printf ("sky name too long (%i, max is 1000)\n", strlen(sky));
61                 return false;
62         }
63
64         for (i = 0;i < 6;i++)
65         {
66                 sprintf (name, "env/%s%s", sky, suf[i]);
67                 if (!(image_rgba = loadimagepixels(name, false, 0, 0)))
68                 {
69                         sprintf (name, "gfx/env/%s%s", sky, suf[i]);
70                         if (!(image_rgba = loadimagepixels(name, false, 0, 0)))
71                         {
72                                 Con_Printf ("Couldn't load env/%s%s or gfx/env/%s%s\n", sky, suf[i], sky, suf[i]);
73                                 continue;
74                         }
75                 }
76                 skyboxside[i] = R_LoadTexture(skytexturepool, va("skyboxside%d", i), image_width, image_height, image_rgba, TEXTYPE_RGBA, TEXF_PRECACHE);
77                 Mem_Free(image_rgba);
78         }
79
80         if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
81         {
82                 skyavailable_box = true;
83                 strcpy(skyname, sky);
84                 return true;
85         }
86         return false;
87 }
88
89 // LordHavoc: added LoadSky console command
90 void LoadSky_f (void)
91 {
92         switch (Cmd_Argc())
93         {
94         case 1:
95                 if (skyname[0])
96                         Con_Printf("current sky: %s\n", skyname);
97                 else
98                         Con_Printf("no skybox has been set\n");
99                 break;
100         case 2:
101                 if (R_SetSkyBox(Cmd_Argv(1)))
102                 {
103                         if (skyname[0])
104                                 Con_Printf("skybox set to %s\n", skyname);
105                         else
106                                 Con_Printf("skybox disabled\n");
107                 }
108                 else
109                         Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
110                 break;
111         default:
112                 Con_Printf("usage: loadsky skyname\n");
113                 break;
114         }
115 }
116
117 static void R_SkyBox(void)
118 {
119         rmeshstate_t m;
120
121 #define R_SkyBoxPolyVec(i,s,t,x,y,z) \
122         varray_vertex[i * 4 + 0] = (x) * 16.0f;\
123         varray_vertex[i * 4 + 1] = (y) * 16.0f;\
124         varray_vertex[i * 4 + 2] = (z) * 16.0f;\
125         varray_texcoord[0][i * 2 + 0] = (s) * (254.0f/256.0f) + (1.0f/256.0f);\
126         varray_texcoord[0][i * 2 + 1] = (t) * (254.0f/256.0f) + (1.0f/256.0f);
127
128         memset(&m, 0, sizeof(m));
129         m.blendfunc1 = GL_ONE;
130         m.blendfunc2 = GL_ZERO;
131         m.depthdisable = true; // don't modify or read zbuffer
132         m.tex[0] = R_GetTexture(skyboxside[3]); // front
133         R_Mesh_State(&m);
134
135         varray_color[0] = varray_color[4] = varray_color[8] = varray_color[12] = r_colorscale;
136         varray_color[1] = varray_color[5] = varray_color[9] = varray_color[13] = r_colorscale;
137         varray_color[2] = varray_color[6] = varray_color[10] = varray_color[14] = r_colorscale;
138         varray_color[3] = varray_color[7] = varray_color[11] = varray_color[15] = 1;
139         
140         R_SkyBoxPolyVec(0, 1, 0,  1, -1,  1);
141         R_SkyBoxPolyVec(1, 1, 1,  1, -1, -1);
142         R_SkyBoxPolyVec(2, 0, 1,  1,  1, -1);
143         R_SkyBoxPolyVec(3, 0, 0,  1,  1,  1);
144         R_Mesh_Draw(4, 2, polygonelements);
145         m.tex[0] = R_GetTexture(skyboxside[1]); // back
146         R_Mesh_State(&m);
147         R_SkyBoxPolyVec(0, 1, 0, -1,  1,  1);
148         R_SkyBoxPolyVec(1, 1, 1, -1,  1, -1);
149         R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
150         R_SkyBoxPolyVec(3, 0, 0, -1, -1,  1);
151         R_Mesh_Draw(4, 2, polygonelements);
152         m.tex[0] = R_GetTexture(skyboxside[0]); // right
153         R_Mesh_State(&m);
154         R_SkyBoxPolyVec(0, 1, 0,  1,  1,  1);
155         R_SkyBoxPolyVec(1, 1, 1,  1,  1, -1);
156         R_SkyBoxPolyVec(2, 0, 1, -1,  1, -1);
157         R_SkyBoxPolyVec(3, 0, 0, -1,  1,  1);
158         R_Mesh_Draw(4, 2, polygonelements);
159         m.tex[0] = R_GetTexture(skyboxside[2]); // left
160         R_Mesh_State(&m);
161         R_SkyBoxPolyVec(0, 1, 0, -1, -1,  1);
162         R_SkyBoxPolyVec(1, 1, 1, -1, -1, -1);
163         R_SkyBoxPolyVec(2, 0, 1,  1, -1, -1);
164         R_SkyBoxPolyVec(3, 0, 0,  1, -1,  1);
165         R_Mesh_Draw(4, 2, polygonelements);
166         m.tex[0] = R_GetTexture(skyboxside[4]); // up
167         R_Mesh_State(&m);
168         R_SkyBoxPolyVec(0, 1, 0,  1, -1,  1);
169         R_SkyBoxPolyVec(1, 1, 1,  1,  1,  1);
170         R_SkyBoxPolyVec(2, 0, 1, -1,  1,  1);
171         R_SkyBoxPolyVec(3, 0, 0, -1, -1,  1);
172         R_Mesh_Draw(4, 2, polygonelements);
173         m.tex[0] = R_GetTexture(skyboxside[5]); // down
174         R_Mesh_State(&m);
175         R_SkyBoxPolyVec(0, 1, 0,  1,  1, -1);
176         R_SkyBoxPolyVec(1, 1, 1,  1, -1, -1);
177         R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
178         R_SkyBoxPolyVec(3, 0, 0, -1,  1, -1);
179         R_Mesh_Draw(4, 2, polygonelements);
180 }
181
182 #define skygridx 32
183 #define skygridx1 (skygridx + 1)
184 #define skygridxrecip (1.0f / (skygridx))
185 #define skygridy 32
186 #define skygridy1 (skygridy + 1)
187 #define skygridyrecip (1.0f / (skygridy))
188
189 static float skysphere[skygridx1*skygridy1*5];
190 static int skysphereindices[skygridx*skygridy*6];
191 static void skyspherecalc(float *sphere, float dx, float dy, float dz)
192 {
193         float a, b, x, ax, ay, v[3], length;
194         int i, j, *index;
195         for (j = 0;j <= skygridy;j++)
196         {
197                 a = j * skygridyrecip;
198                 ax = cos(a * M_PI * 2);
199                 ay = -sin(a * M_PI * 2);
200                 for (i = 0;i <= skygridx;i++)
201                 {
202                         b = i * skygridxrecip;
203                         x = cos(b * M_PI * 2);
204                         v[0] = ax*x * dx;
205                         v[1] = ay*x * dy;
206                         v[2] = -sin(b * M_PI * 2) * dz;
207                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
208                         *sphere++ = v[0] * length;
209                         *sphere++ = v[1] * length;
210                         *sphere++ = v[0];
211                         *sphere++ = v[1];
212                         *sphere++ = v[2];
213                 }
214         }
215         index = skysphereindices;
216         for (j = 0;j < skygridy;j++)
217         {
218                 for (i = 0;i < skygridx;i++)
219                 {
220                         *index++ =  j      * skygridx1 + i;
221                         *index++ =  j      * skygridx1 + i + 1;
222                         *index++ = (j + 1) * skygridx1 + i;
223
224                         *index++ =  j      * skygridx1 + i + 1;
225                         *index++ = (j + 1) * skygridx1 + i + 1;
226                         *index++ = (j + 1) * skygridx1 + i;
227                 }
228                 i++;
229         }
230 }
231
232 static void skyspherearrays(float *v, float *t, float *c, float *source, float s, float colorscale)
233 {
234         int i;
235         for (i = 0;i < (skygridx1*skygridy1);i++, c += 4, t += 2, v += 4, source += 5)
236         {
237                 c[0] = colorscale;
238                 c[1] = colorscale;
239                 c[2] = colorscale;
240                 c[3] = 1;
241                 t[0] = source[0] + s;
242                 t[1] = source[1] + s;
243                 v[0] = source[2];
244                 v[1] = source[3];
245                 v[2] = source[4];
246         }
247 }
248
249 static void R_SkySphere(void)
250 {
251         int numverts, numtriangles;
252         float speedscale, speedscale2;
253         static qboolean skysphereinitialized = false;
254         rmeshstate_t m;
255         if (!skysphereinitialized)
256         {
257                 skysphereinitialized = true;
258                 skyspherecalc(skysphere, 16, 16, 16 / 3);
259         }
260
261         speedscale = cl.time*8.0/128.0;
262         speedscale -= (int)speedscale;
263         speedscale2 = cl.time*16.0/128.0;
264         speedscale2 -= (int)speedscale2;
265
266         numverts = skygridx1*skygridy1;
267         numtriangles = skygridx*skygridy*2;
268
269         R_Mesh_ResizeCheck(numverts);
270
271         memset(&m, 0, sizeof(m));
272         m.blendfunc1 = GL_ONE;
273         m.blendfunc2 = GL_ZERO;
274         m.depthdisable = true; // don't modify or read zbuffer
275         m.tex[0] = R_GetTexture(solidskytexture);
276         R_Mesh_State(&m);
277
278         skyspherearrays(varray_vertex, varray_texcoord[0], varray_color, skysphere, speedscale, r_colorscale);
279         R_Mesh_Draw(numverts, numtriangles, skysphereindices);
280
281         m.blendfunc1 = GL_SRC_ALPHA;
282         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
283         m.tex[0] = R_GetTexture(alphaskytexture);
284         R_Mesh_State(&m);
285
286         skyspherearrays(varray_vertex, varray_texcoord[0], varray_color, skysphere, speedscale2, r_colorscale);
287         R_Mesh_Draw(numverts, numtriangles, skysphereindices);
288 }
289
290 void R_Sky(void)
291 {
292         matrix4x4_t skymatrix;
293         if (skyrendermasked)
294         {
295                 Matrix4x4_CreateTranslate(&skymatrix, r_origin[0], r_origin[1], r_origin[2]);
296                 R_Mesh_Matrix(&skymatrix);
297                 if (skyrendersphere)
298                 {
299                         // this does not modify depth buffer
300                         R_SkySphere();
301                 }
302                 else if (skyrenderbox)
303                 {
304                         // this does not modify depth buffer
305                         R_SkyBox();
306                 }
307                 /* this will be skyroom someday
308                 else
309                 {
310                         // this modifies the depth buffer so we have to clear it afterward
311                         //R_SkyRoom();
312                         // clear the depthbuffer that was used while rendering the skyroom
313                         //R_Mesh_ClearDepth();
314                 }
315                 */
316         }
317 }
318
319 //===============================================================
320
321 /*
322 =============
323 R_InitSky
324
325 A sky texture is 256*128, with the right side being a masked overlay
326 ==============
327 */
328 void R_InitSky (qbyte *src, int bytesperpixel)
329 {
330         int i, j, p, r, g, b;
331         qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
332         unsigned trans[128*128], transpix, *rgba;
333
334         skyavailable_quake = true;
335
336         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
337         R_FreeTexturePool(&skytexturepool);
338         skytexturepool = R_AllocTexturePool();
339         solidskytexture = NULL;
340         alphaskytexture = NULL;
341
342         if (bytesperpixel == 4)
343         {
344                 for (i = 0;i < 128;i++)
345                         for (j = 0;j < 128;j++)
346                                 trans[(i*128) + j] = src[i*256+j+128];
347         }
348         else
349         {
350                 // make an average value for the back to avoid
351                 // a fringe on the top level
352                 r = g = b = 0;
353                 for (i=0 ; i<128 ; i++)
354                 {
355                         for (j=0 ; j<128 ; j++)
356                         {
357                                 p = src[i*256 + j + 128];
358                                 rgba = &d_8to24table[p];
359                                 trans[(i*128) + j] = *rgba;
360                                 r += ((qbyte *)rgba)[0];
361                                 g += ((qbyte *)rgba)[1];
362                                 b += ((qbyte *)rgba)[2];
363                         }
364                 }
365
366                 ((qbyte *)&transpix)[0] = r/(128*128);
367                 ((qbyte *)&transpix)[1] = g/(128*128);
368                 ((qbyte *)&transpix)[2] = b/(128*128);
369                 ((qbyte *)&transpix)[3] = 0;
370         }
371
372         memcpy(skyupperlayerpixels, trans, 128*128*4);
373
374         solidskytexture = R_LoadTexture (skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE);
375
376         if (bytesperpixel == 4)
377         {
378                 for (i = 0;i < 128;i++)
379                         for (j = 0;j < 128;j++)
380                                 trans[(i*128) + j] = src[i*256+j];
381         }
382         else
383         {
384                 for (i=0 ; i<128 ; i++)
385                 {
386                         for (j=0 ; j<128 ; j++)
387                         {
388                                 p = src[i*256 + j];
389                                 if (p == 0)
390                                         trans[(i*128) + j] = transpix;
391                                 else
392                                         trans[(i*128) + j] = d_8to24table[p];
393                         }
394                 }
395         }
396
397         memcpy(skylowerlayerpixels, trans, 128*128*4);
398
399         alphaskytexture = R_LoadTexture (skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
400 }
401
402 void R_ResetQuakeSky(void)
403 {
404         skyavailable_quake = false;
405 }
406
407 void R_ResetSkyBox(void)
408 {
409         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
410         skyname[0] = 0;
411         skyavailable_box = false;
412 }
413
414 static void r_sky_start(void)
415 {
416         skytexturepool = R_AllocTexturePool();
417         solidskytexture = NULL;
418         alphaskytexture = NULL;
419 }
420
421 static void r_sky_shutdown(void)
422 {
423         R_FreeTexturePool(&skytexturepool);
424         solidskytexture = NULL;
425         alphaskytexture = NULL;
426 }
427
428 static void r_sky_newmap(void)
429 {
430 }
431
432 void R_Sky_Init(void)
433 {
434         Cmd_AddCommand ("loadsky", &LoadSky_f);
435         Cvar_RegisterVariable (&r_sky);
436         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
437 }