fixed cloud layers in quake sky loading
[divverent/darkplaces.git] / r_sky.c
1
2 #include "quakedef.h"
3 #include "image.h"
4
5 // FIXME: fix skybox after vid_restart
6 cvar_t r_sky = {CVAR_SAVE, "r_sky", "1"};
7 qboolean skyavailable_quake;
8 qboolean skyavailable_box;
9 int skyrendernow;
10 int skyrendermasked;
11
12 static rtexture_t *solidskytexture;
13 static rtexture_t *alphaskytexture;
14 static int skyrendersphere;
15 static int skyrenderbox;
16 static rtexturepool_t *skytexturepool;
17 static char skyname[256];
18 static char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
19 static rtexture_t *skyboxside[6];
20
21 void R_SkyStartFrame(void)
22 {
23         skyrendernow = false;
24         skyrendersphere = false;
25         skyrenderbox = false;
26         skyrendermasked = false;
27         if (r_sky.integer && !fogenabled)
28         {
29                 if (skyavailable_box)
30                         skyrenderbox = true;
31                 else if (skyavailable_quake)
32                         skyrendersphere = true;
33                 // for depth-masked sky, render the sky on the first sky surface encountered
34                 skyrendernow = true;
35                 skyrendermasked = true;
36         }
37 }
38
39 /*
40 ==================
41 R_SetSkyBox
42 ==================
43 */
44 void R_UnloadSkyBox(void)
45 {
46         int i;
47         for (i = 0;i < 6;i++)
48         {
49                 if (skyboxside[i])
50                         R_FreeTexture(skyboxside[i]);
51                 skyboxside[i] = NULL;;
52         }
53 }
54
55 void R_LoadSkyBox(void)
56 {
57         int i;
58         char name[1024];
59         qbyte *image_rgba;
60         R_UnloadSkyBox();
61         if (!skyname[0])
62                 return;
63         for (i = 0;i < 6;i++)
64         {
65                 if (snprintf(name, sizeof(name), "env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
66                 {
67                         if (snprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
68                         {
69                                 Con_Printf ("Couldn't load env/%s%s or gfx/env/%s%s\n", skyname, suf[i], skyname, suf[i]);
70                                 continue;
71                         }
72                 }
73                 skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, image_rgba, TEXTYPE_RGBA, TEXF_CLAMP | TEXF_PRECACHE, NULL);
74                 Mem_Free(image_rgba);
75         }
76 }
77
78 int R_SetSkyBox(const char *sky)
79 {
80         if (strcmp(sky, skyname) == 0) // no change
81                 return true;
82
83         if (strlen(sky) > 1000)
84         {
85                 Con_Printf ("sky name too long (%i, max is 1000)\n", strlen(sky));
86                 return false;
87         }
88
89         skyavailable_box = false;
90         strcpy(skyname, sky);
91
92         R_UnloadSkyBox();
93         R_LoadSkyBox();
94
95         if (!skyname[0])
96                 return true;
97
98         if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
99         {
100                 skyavailable_box = true;
101                 return true;
102         }
103         return false;
104 }
105
106 // LordHavoc: added LoadSky console command
107 void LoadSky_f (void)
108 {
109         switch (Cmd_Argc())
110         {
111         case 1:
112                 if (skyname[0])
113                         Con_Printf("current sky: %s\n", skyname);
114                 else
115                         Con_Printf("no skybox has been set\n");
116                 break;
117         case 2:
118                 if (R_SetSkyBox(Cmd_Argv(1)))
119                 {
120                         if (skyname[0])
121                                 Con_Printf("skybox set to %s\n", skyname);
122                         else
123                                 Con_Printf("skybox disabled\n");
124                 }
125                 else
126                         Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
127                 break;
128         default:
129                 Con_Printf("usage: loadsky skyname\n");
130                 break;
131         }
132 }
133
134 float skyboxvertex3f[6*4*3] =
135 {
136         // skyside[0]
137          16,  16,  16,
138          16,  16, -16,
139         -16,  16, -16,
140         -16,  16,  16,
141         // skyside[1]
142         -16,  16,  16,
143         -16,  16, -16,
144         -16, -16, -16,
145         -16, -16,  16,
146         // skyside[2]
147         -16, -16,  16,
148         -16, -16, -16,
149          16, -16, -16,
150          16, -16,  16,
151         // skyside[3]
152          16, -16,  16,
153          16, -16, -16,
154          16,  16, -16,
155          16,  16,  16,
156         // skyside[4]
157          16, -16,  16,
158          16,  16,  16,
159         -16,  16,  16,
160         -16, -16,  16,
161         // skyside[5]
162          16,  16, -16,
163          16, -16, -16,
164         -16, -16, -16,
165         -16,  16, -16
166 };
167
168 float skyboxtexcoord2f[6*4*2] =
169 {
170         // skyside[0]
171         1, 0,
172         1, 1,
173         0, 1,
174         0, 0,
175         // skyside[1]
176         1, 0,
177         1, 1,
178         0, 1,
179         0, 0,
180         // skyside[2]
181         1, 0,
182         1, 1,
183         0, 1,
184         0, 0,
185         // skyside[3]
186         1, 0,
187         1, 1,
188         0, 1,
189         0, 0,
190         // skyside[4]
191         1, 0,
192         1, 1,
193         0, 1,
194         0, 0,
195         // skyside[5]
196         1, 0,
197         1, 1,
198         0, 1,
199         0, 0
200 };
201
202 int skyboxelements[6*2*3] =
203 {
204         // skyside[3]
205          0,  1,  2,
206          0,  2,  3,
207         // skyside[1]
208          4,  5,  6,
209          4,  6,  7,
210         // skyside[0]
211          8,  9, 10,
212          8, 10, 11,
213         // skyside[2]
214         12, 13, 14,
215         12, 14, 15,
216         // skyside[4]
217         16, 17, 18,
218         16, 18, 19,
219         // skyside[5]
220         20, 21, 22,
221         20, 22, 23
222 };
223
224 static void R_SkyBox(void)
225 {
226         int i;
227         rmeshstate_t m;
228         GL_Color(1, 1, 1, 1);
229         memset(&m, 0, sizeof(m));
230         GL_BlendFunc(GL_ONE, GL_ZERO);
231         GL_DepthMask(true);
232         GL_DepthTest(false); // don't modify or read zbuffer
233         GL_VertexPointer(skyboxvertex3f);
234         m.pointer_texcoord[0] = skyboxtexcoord2f;
235         for (i = 0;i < 6;i++)
236         {
237                 m.tex[0] = R_GetTexture(skyboxside[i]);
238                 R_Mesh_State_Texture(&m);
239                 R_Mesh_Draw(6*4, 2, skyboxelements + i * 6);
240         }
241 }
242
243 #define skygridx 32
244 #define skygridx1 (skygridx + 1)
245 #define skygridxrecip (1.0f / (skygridx))
246 #define skygridy 32
247 #define skygridy1 (skygridy + 1)
248 #define skygridyrecip (1.0f / (skygridy))
249 #define skysphere_numverts (skygridx1 * skygridy1)
250 #define skysphere_numtriangles (skygridx * skygridy * 2)
251 static float skysphere_vertex3f[skysphere_numverts * 3];
252 static float skysphere_texcoord2f[skysphere_numverts * 2];
253 static int skysphere_element3i[skysphere_numtriangles * 3];
254
255 static void skyspherecalc(void)
256 {
257         int i, j, *e;
258         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
259         float dx, dy, dz;
260         dx = 16;
261         dy = 16;
262         dz = 16 / 3;
263         vertex3f = skysphere_vertex3f;
264         texcoord2f = skysphere_texcoord2f;
265         for (j = 0;j <= skygridy;j++)
266         {
267                 a = j * skygridyrecip;
268                 ax = cos(a * M_PI * 2);
269                 ay = -sin(a * M_PI * 2);
270                 for (i = 0;i <= skygridx;i++)
271                 {
272                         b = i * skygridxrecip;
273                         x = cos((b + 0.5) * M_PI);
274                         v[0] = ax*x * dx;
275                         v[1] = ay*x * dy;
276                         v[2] = -sin((b + 0.5) * M_PI) * dz;
277                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
278                         *texcoord2f++ = v[0] * length;
279                         *texcoord2f++ = v[1] * length;
280                         *vertex3f++ = v[0];
281                         *vertex3f++ = v[1];
282                         *vertex3f++ = v[2];
283                 }
284         }
285         e = skysphere_element3i;
286         for (j = 0;j < skygridy;j++)
287         {
288                 for (i = 0;i < skygridx;i++)
289                 {
290                         *e++ =  j      * skygridx1 + i;
291                         *e++ =  j      * skygridx1 + i + 1;
292                         *e++ = (j + 1) * skygridx1 + i;
293
294                         *e++ =  j      * skygridx1 + i + 1;
295                         *e++ = (j + 1) * skygridx1 + i + 1;
296                         *e++ = (j + 1) * skygridx1 + i;
297                 }
298         }
299 }
300
301 static void R_SkySphere(void)
302 {
303         float speedscale;
304         static qboolean skysphereinitialized = false;
305         rmeshstate_t m;
306         matrix4x4_t scroll1matrix, scroll2matrix, identitymatrix;
307         if (!skysphereinitialized)
308         {
309                 skysphereinitialized = true;
310                 skyspherecalc();
311         }
312
313         // scroll speed for upper layer
314         speedscale = cl.time*8.0/128.0;
315         // wrap the scroll just to be extra kind to float accuracy
316         speedscale -= (int)speedscale;
317
318         // scroll the lower cloud layer twice as fast (just like quake did)
319         Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
320         Matrix4x4_CreateTranslate(&scroll2matrix, speedscale * 2, speedscale * 2, 0);
321         Matrix4x4_CreateIdentity(&identitymatrix);
322
323         GL_VertexPointer(skysphere_vertex3f);
324         GL_Color(1, 1, 1, 1);
325         GL_BlendFunc(GL_ONE, GL_ZERO);
326         GL_DepthMask(true);
327         GL_DepthTest(false); // don't modify or read zbuffer
328         memset(&m, 0, sizeof(m));
329         m.tex[0] = R_GetTexture(solidskytexture);
330         m.pointer_texcoord[0] = skysphere_texcoord2f;
331         R_Mesh_TextureMatrix(0, &scroll1matrix);
332         if (r_textureunits.integer >= 2)
333         {
334                 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
335                 m.tex[1] = R_GetTexture(alphaskytexture);
336                 m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
337                 m.pointer_texcoord[1] = skysphere_texcoord2f;
338                 R_Mesh_State_Texture(&m);
339                 R_Mesh_TextureMatrix(1, &scroll2matrix);
340                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
341                 R_Mesh_TextureMatrix(1, &identitymatrix);
342         }
343         else
344         {
345                 // two pass
346                 R_Mesh_State_Texture(&m);
347                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
348
349                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
350                 m.tex[0] = R_GetTexture(alphaskytexture);
351                 R_Mesh_State_Texture(&m);
352                 R_Mesh_TextureMatrix(0, &scroll2matrix);
353                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
354         }
355         R_Mesh_TextureMatrix(0, &identitymatrix);
356 }
357
358 void R_Sky(void)
359 {
360         matrix4x4_t skymatrix;
361         if (skyrendermasked)
362         {
363                 Matrix4x4_CreateTranslate(&skymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2]);
364                 R_Mesh_Matrix(&skymatrix);
365                 if (skyrendersphere)
366                 {
367                         // this does not modify depth buffer
368                         R_SkySphere();
369                 }
370                 else if (skyrenderbox)
371                 {
372                         // this does not modify depth buffer
373                         R_SkyBox();
374                 }
375                 /* this will be skyroom someday
376                 else
377                 {
378                         // this modifies the depth buffer so we have to clear it afterward
379                         //R_SkyRoom();
380                         // clear the depthbuffer that was used while rendering the skyroom
381                         //qglClear(GL_DEPTH_BUFFER_BIT);
382                 }
383                 */
384         }
385 }
386
387 //===============================================================
388
389 /*
390 =============
391 R_InitSky
392
393 A sky texture is 256*128, with the right side being a masked overlay
394 ==============
395 */
396 void R_InitSky (qbyte *src, int bytesperpixel)
397 {
398         int i, j;
399         unsigned solidpixels[128*128], alphapixels[128*128];
400
401         skyavailable_quake = true;
402
403         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
404         R_FreeTexturePool(&skytexturepool);
405         skytexturepool = R_AllocTexturePool();
406         solidskytexture = NULL;
407         alphaskytexture = NULL;
408
409         if (bytesperpixel == 4)
410         {
411                 for (i = 0;i < 128;i++)
412                 {
413                         for (j = 0;j < 128;j++)
414                         {
415                                 solidpixels[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
416                                 alphapixels[(i*128) + j] = ((unsigned *)src)[i*256+j];
417                         }
418                 }
419         }
420         else
421         {
422                 // make an average value for the back to avoid
423                 // a fringe on the top level
424                 int p, r, g, b;
425                 union
426                 {
427                         unsigned int i;
428                         unsigned char b[4];
429                 }
430                 rgba;
431                 r = g = b = 0;
432                 for (i = 0;i < 128;i++)
433                 {
434                         for (j = 0;j < 128;j++)
435                         {
436                                 rgba.i = palette_complete[src[i*256 + j + 128]];
437                                 r += rgba.b[0];
438                                 g += rgba.b[1];
439                                 b += rgba.b[2];
440                         }
441                 }
442                 rgba.b[0] = r/(128*128);
443                 rgba.b[1] = g/(128*128);
444                 rgba.b[2] = b/(128*128);
445                 rgba.b[3] = 0;
446                 for (i = 0;i < 128;i++)
447                 {
448                         for (j = 0;j < 128;j++)
449                         {
450                                 solidpixels[(i*128) + j] = palette_complete[src[i*256 + j + 128]];
451                                 alphapixels[(i*128) + j] = (p = src[i*256 + j]) ? palette_complete[p] : rgba.i;
452                         }
453                 }
454         }
455
456         solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) solidpixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
457         alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) alphapixels, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
458 }
459
460 void R_ResetQuakeSky(void)
461 {
462         skyavailable_quake = false;
463 }
464
465 void R_ResetSkyBox(void)
466 {
467         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
468         skyname[0] = 0;
469         skyavailable_box = false;
470 }
471
472 static void r_sky_start(void)
473 {
474         skytexturepool = R_AllocTexturePool();
475         solidskytexture = NULL;
476         alphaskytexture = NULL;
477         R_LoadSkyBox();
478 }
479
480 static void r_sky_shutdown(void)
481 {
482         R_UnloadSkyBox();
483         R_FreeTexturePool(&skytexturepool);
484         solidskytexture = NULL;
485         alphaskytexture = NULL;
486 }
487
488 static void r_sky_newmap(void)
489 {
490 }
491
492 void R_Sky_Init(void)
493 {
494         Cmd_AddCommand ("loadsky", &LoadSky_f);
495         Cvar_RegisterVariable (&r_sky);
496         R_ResetSkyBox();
497         R_ResetQuakeSky();
498         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
499 }
500