removed need for gfx/menuplyr.lmp (this code wasn't even used anymore, since the...
[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                 // LordHavoc: note that color is not set here because it does not
336                 // matter with GL_REPLACE
337                 m.tex[1] = R_GetTexture(alphaskytexture);
338                 m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
339                 m.pointer_texcoord[1] = skysphere_texcoord2f;
340                 R_Mesh_State_Texture(&m);
341                 R_Mesh_TextureMatrix(1, &scroll2matrix);
342                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
343                 R_Mesh_TextureMatrix(1, &identitymatrix);
344         }
345         else
346         {
347                 // two pass
348                 R_Mesh_State_Texture(&m);
349                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
350
351                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
352                 m.tex[0] = R_GetTexture(alphaskytexture);
353                 R_Mesh_State_Texture(&m);
354                 R_Mesh_TextureMatrix(0, &scroll2matrix);
355                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
356         }
357         R_Mesh_TextureMatrix(0, &identitymatrix);
358 }
359
360 void R_Sky(void)
361 {
362         matrix4x4_t skymatrix;
363         if (skyrendermasked)
364         {
365                 Matrix4x4_CreateTranslate(&skymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2]);
366                 R_Mesh_Matrix(&skymatrix);
367                 if (skyrendersphere)
368                 {
369                         // this does not modify depth buffer
370                         R_SkySphere();
371                 }
372                 else if (skyrenderbox)
373                 {
374                         // this does not modify depth buffer
375                         R_SkyBox();
376                 }
377                 /* this will be skyroom someday
378                 else
379                 {
380                         // this modifies the depth buffer so we have to clear it afterward
381                         //R_SkyRoom();
382                         // clear the depthbuffer that was used while rendering the skyroom
383                         //qglClear(GL_DEPTH_BUFFER_BIT);
384                 }
385                 */
386         }
387 }
388
389 //===============================================================
390
391 /*
392 =============
393 R_InitSky
394
395 A sky texture is 256*128, with the right side being a masked overlay
396 ==============
397 */
398 void R_InitSky (qbyte *src, int bytesperpixel)
399 {
400         int i, j, p, r, g, b;
401         qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
402         unsigned trans[128*128], transpix, *rgba;
403         union
404         {
405                 int i;
406                 qbyte b[4];
407         }
408         transpixunion;
409
410         skyavailable_quake = true;
411
412         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
413         R_FreeTexturePool(&skytexturepool);
414         skytexturepool = R_AllocTexturePool();
415         solidskytexture = NULL;
416         alphaskytexture = NULL;
417
418         if (bytesperpixel == 4)
419         {
420                 transpixunion.i = 0;
421                 for (i = 0;i < 128;i++)
422                         for (j = 0;j < 128;j++)
423                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
424         }
425         else
426         {
427                 // make an average value for the back to avoid
428                 // a fringe on the top level
429                 r = g = b = 0;
430                 for (i=0 ; i<128 ; i++)
431                 {
432                         for (j=0 ; j<128 ; j++)
433                         {
434                                 p = src[i*256 + j + 128];
435                                 rgba = &palette_complete[p];
436                                 trans[(i*128) + j] = *rgba;
437                                 r += ((qbyte *)rgba)[0];
438                                 g += ((qbyte *)rgba)[1];
439                                 b += ((qbyte *)rgba)[2];
440                         }
441                 }
442
443                 transpixunion.i = 0;
444                 transpixunion.b[0] = r/(128*128);
445                 transpixunion.b[1] = g/(128*128);
446                 transpixunion.b[2] = b/(128*128);
447                 transpixunion.b[3] = 0;
448         }
449         transpix = transpixunion.i;
450
451         memcpy(skyupperlayerpixels, trans, 128*128*4);
452
453         solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
454
455         if (bytesperpixel == 4)
456         {
457                 for (i = 0;i < 128;i++)
458                         for (j = 0;j < 128;j++)
459                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j];
460         }
461         else
462         {
463                 for (i=0 ; i<128 ; i++)
464                 {
465                         for (j=0 ; j<128 ; j++)
466                         {
467                                 p = src[i*256 + j];
468                                 if (p == 0)
469                                         trans[(i*128) + j] = transpix;
470                                 else
471                                         trans[(i*128) + j] = palette_complete[p];
472                         }
473                 }
474         }
475
476         memcpy(skylowerlayerpixels, trans, 128*128*4);
477
478         alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
479 }
480
481 void R_ResetQuakeSky(void)
482 {
483         skyavailable_quake = false;
484 }
485
486 void R_ResetSkyBox(void)
487 {
488         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
489         skyname[0] = 0;
490         skyavailable_box = false;
491 }
492
493 static void r_sky_start(void)
494 {
495         skytexturepool = R_AllocTexturePool();
496         solidskytexture = NULL;
497         alphaskytexture = NULL;
498         R_LoadSkyBox();
499 }
500
501 static void r_sky_shutdown(void)
502 {
503         R_UnloadSkyBox();
504         R_FreeTexturePool(&skytexturepool);
505         solidskytexture = NULL;
506         alphaskytexture = NULL;
507 }
508
509 static void r_sky_newmap(void)
510 {
511 }
512
513 void R_Sky_Init(void)
514 {
515         Cmd_AddCommand ("loadsky", &LoadSky_f);
516         Cvar_RegisterVariable (&r_sky);
517         R_ResetSkyBox();
518         R_ResetQuakeSky();
519         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
520 }
521