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