4fa301d2d857c32eb432cd227c7dc9cde8f045e6
[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 static void R_SkyBox(void)
120 {
121         rmeshstate_t m;
122
123 #define R_SkyBoxPolyVec(i,s,t,x,y,z) \
124         varray_vertex[i * 4 + 0] = (x) * 16.0f;\
125         varray_vertex[i * 4 + 1] = (y) * 16.0f;\
126         varray_vertex[i * 4 + 2] = (z) * 16.0f;\
127         varray_texcoord[0][i * 4 + 0] = (s);\
128         varray_texcoord[0][i * 4 + 1] = (t);
129
130         GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
131
132         memset(&m, 0, sizeof(m));
133         m.blendfunc1 = GL_ONE;
134         m.blendfunc2 = GL_ZERO;
135         m.depthdisable = true; // don't modify or read zbuffer
136
137         m.tex[0] = R_GetTexture(skyboxside[3]); // front
138         R_Mesh_State(&m);
139         R_Mesh_GetSpace(4);
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
146         m.tex[0] = R_GetTexture(skyboxside[1]); // back
147         R_Mesh_TextureState(&m);
148         R_Mesh_GetSpace(4);
149         R_SkyBoxPolyVec(0, 1, 0, -1,  1,  1);
150         R_SkyBoxPolyVec(1, 1, 1, -1,  1, -1);
151         R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
152         R_SkyBoxPolyVec(3, 0, 0, -1, -1,  1);
153         R_Mesh_Draw(4, 2, polygonelements);
154
155         m.tex[0] = R_GetTexture(skyboxside[0]); // right
156         R_Mesh_TextureState(&m);
157         R_Mesh_GetSpace(4);
158         R_SkyBoxPolyVec(0, 1, 0,  1,  1,  1);
159         R_SkyBoxPolyVec(1, 1, 1,  1,  1, -1);
160         R_SkyBoxPolyVec(2, 0, 1, -1,  1, -1);
161         R_SkyBoxPolyVec(3, 0, 0, -1,  1,  1);
162         R_Mesh_Draw(4, 2, polygonelements);
163
164         m.tex[0] = R_GetTexture(skyboxside[2]); // left
165         R_Mesh_TextureState(&m);
166         R_Mesh_GetSpace(4);
167         R_SkyBoxPolyVec(0, 1, 0, -1, -1,  1);
168         R_SkyBoxPolyVec(1, 1, 1, -1, -1, -1);
169         R_SkyBoxPolyVec(2, 0, 1,  1, -1, -1);
170         R_SkyBoxPolyVec(3, 0, 0,  1, -1,  1);
171         R_Mesh_Draw(4, 2, polygonelements);
172
173         m.tex[0] = R_GetTexture(skyboxside[4]); // up
174         R_Mesh_TextureState(&m);
175         R_Mesh_GetSpace(4);
176         R_SkyBoxPolyVec(0, 1, 0,  1, -1,  1);
177         R_SkyBoxPolyVec(1, 1, 1,  1,  1,  1);
178         R_SkyBoxPolyVec(2, 0, 1, -1,  1,  1);
179         R_SkyBoxPolyVec(3, 0, 0, -1, -1,  1);
180         R_Mesh_Draw(4, 2, polygonelements);
181
182         m.tex[0] = R_GetTexture(skyboxside[5]); // down
183         R_Mesh_TextureState(&m);
184         R_Mesh_GetSpace(4);
185         R_SkyBoxPolyVec(0, 1, 0,  1,  1, -1);
186         R_SkyBoxPolyVec(1, 1, 1,  1, -1, -1);
187         R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
188         R_SkyBoxPolyVec(3, 0, 0, -1,  1, -1);
189         R_Mesh_Draw(4, 2, polygonelements);
190 }
191
192 #define skygridx 32
193 #define skygridx1 (skygridx + 1)
194 #define skygridxrecip (1.0f / (skygridx))
195 #define skygridy 32
196 #define skygridy1 (skygridy + 1)
197 #define skygridyrecip (1.0f / (skygridy))
198 #define skysphere_numverts (skygridx1 * skygridy1)
199 #define skysphere_numtriangles (skygridx * skygridy * 2)
200 static float skysphere_vertex[skysphere_numverts * 4];
201 static float skysphere_texcoord[skysphere_numverts * 4];
202 static int skysphere_elements[skysphere_numtriangles * 3];
203
204 static void skyspherecalc(void)
205 {
206         int i, j, *e;
207         float a, b, x, ax, ay, v[3], length, *vertex, *texcoord;
208         float dx, dy, dz;
209         dx = 16;
210         dy = 16;
211         dz = 16 / 3;
212         vertex = skysphere_vertex;
213         texcoord = skysphere_texcoord;
214         for (j = 0;j <= skygridy;j++)
215         {
216                 a = j * skygridyrecip;
217                 ax = cos(a * M_PI * 2);
218                 ay = -sin(a * M_PI * 2);
219                 for (i = 0;i <= skygridx;i++)
220                 {
221                         b = i * skygridxrecip;
222                         x = cos((b + 0.5) * M_PI);
223                         v[0] = ax*x * dx;
224                         v[1] = ay*x * dy;
225                         v[2] = -sin((b + 0.5) * M_PI) * dz;
226                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
227                         *texcoord++ = v[0] * length;
228                         *texcoord++ = v[1] * length;
229                         *texcoord++ = 0;
230                         *texcoord++ = 0;
231                         *vertex++ = v[0];
232                         *vertex++ = v[1];
233                         *vertex++ = v[2];
234                         *vertex++ = 1;
235                 }
236         }
237         e = skysphere_elements;
238         for (j = 0;j < skygridy;j++)
239         {
240                 for (i = 0;i < skygridx;i++)
241                 {
242                         *e++ =  j      * skygridx1 + i;
243                         *e++ =  j      * skygridx1 + i + 1;
244                         *e++ = (j + 1) * skygridx1 + i;
245
246                         *e++ =  j      * skygridx1 + i + 1;
247                         *e++ = (j + 1) * skygridx1 + i + 1;
248                         *e++ = (j + 1) * skygridx1 + i;
249                 }
250                 i++;
251         }
252 }
253
254 static void R_SkySphere(void)
255 {
256         int i;
257         float speedscale, *t;
258         static qboolean skysphereinitialized = false;
259         rmeshstate_t m;
260         if (!skysphereinitialized)
261         {
262                 skysphereinitialized = true;
263                 skyspherecalc();
264         }
265
266         // scroll speed for upper layer
267         speedscale = cl.time*8.0/128.0;
268         // wrap the scroll just to be extra kind to float accuracy
269         speedscale -= (int)speedscale;
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         GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
279
280         R_Mesh_GetSpace(skysphere_numverts);
281         memcpy(varray_vertex, skysphere_vertex, skysphere_numverts * sizeof(float[4]));
282         memcpy(varray_texcoord[0], skysphere_texcoord, skysphere_numverts * sizeof(float[4]));
283         for (i = 0, t = varray_texcoord[0];i < skysphere_numverts;i++, t += 4)
284         {
285                 t[0] += speedscale;
286                 t[1] += speedscale;
287         }
288         R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_elements);
289
290         m.blendfunc1 = GL_SRC_ALPHA;
291         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
292         m.tex[0] = R_GetTexture(alphaskytexture);
293         R_Mesh_State(&m);
294
295         // scroll the lower cloud layer twice as fast (just like quake did)
296         speedscale *= 2;
297
298         R_Mesh_GetSpace(skysphere_numverts);
299         memcpy(varray_vertex, skysphere_vertex, skysphere_numverts * sizeof(float[4]));
300         memcpy(varray_texcoord[0], skysphere_texcoord, skysphere_numverts * sizeof(float[4]));
301         for (i = 0, t = varray_texcoord[0];i < skysphere_numverts;i++, t += 4)
302         {
303                 t[0] += speedscale;
304                 t[1] += speedscale;
305         }
306         R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_elements);
307 }
308
309 void R_Sky(void)
310 {
311         matrix4x4_t skymatrix;
312         if (skyrendermasked)
313         {
314                 Matrix4x4_CreateTranslate(&skymatrix, r_origin[0], r_origin[1], r_origin[2]);
315                 R_Mesh_Matrix(&skymatrix);
316                 if (skyrendersphere)
317                 {
318                         // this does not modify depth buffer
319                         R_SkySphere();
320                 }
321                 else if (skyrenderbox)
322                 {
323                         // this does not modify depth buffer
324                         R_SkyBox();
325                 }
326                 /* this will be skyroom someday
327                 else
328                 {
329                         // this modifies the depth buffer so we have to clear it afterward
330                         //R_SkyRoom();
331                         // clear the depthbuffer that was used while rendering the skyroom
332                         //qglClear(GL_DEPTH_BUFFER_BIT);
333                 }
334                 */
335         }
336 }
337
338 //===============================================================
339
340 /*
341 =============
342 R_InitSky
343
344 A sky texture is 256*128, with the right side being a masked overlay
345 ==============
346 */
347 void R_InitSky (qbyte *src, int bytesperpixel)
348 {
349         int i, j, p, r, g, b;
350         qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
351         unsigned trans[128*128], transpix, *rgba;
352
353         skyavailable_quake = true;
354
355         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
356         R_FreeTexturePool(&skytexturepool);
357         skytexturepool = R_AllocTexturePool();
358         solidskytexture = NULL;
359         alphaskytexture = NULL;
360
361         if (bytesperpixel == 4)
362         {
363                 for (i = 0;i < 128;i++)
364                         for (j = 0;j < 128;j++)
365                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
366         }
367         else
368         {
369                 // make an average value for the back to avoid
370                 // a fringe on the top level
371                 r = g = b = 0;
372                 for (i=0 ; i<128 ; i++)
373                 {
374                         for (j=0 ; j<128 ; j++)
375                         {
376                                 p = src[i*256 + j + 128];
377                                 rgba = &palette_complete[p];
378                                 trans[(i*128) + j] = *rgba;
379                                 r += ((qbyte *)rgba)[0];
380                                 g += ((qbyte *)rgba)[1];
381                                 b += ((qbyte *)rgba)[2];
382                         }
383                 }
384
385                 ((qbyte *)&transpix)[0] = r/(128*128);
386                 ((qbyte *)&transpix)[1] = g/(128*128);
387                 ((qbyte *)&transpix)[2] = b/(128*128);
388                 ((qbyte *)&transpix)[3] = 0;
389         }
390
391         memcpy(skyupperlayerpixels, trans, 128*128*4);
392
393         solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
394
395         if (bytesperpixel == 4)
396         {
397                 for (i = 0;i < 128;i++)
398                         for (j = 0;j < 128;j++)
399                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j];
400         }
401         else
402         {
403                 for (i=0 ; i<128 ; i++)
404                 {
405                         for (j=0 ; j<128 ; j++)
406                         {
407                                 p = src[i*256 + j];
408                                 if (p == 0)
409                                         trans[(i*128) + j] = transpix;
410                                 else
411                                         trans[(i*128) + j] = palette_complete[p];
412                         }
413                 }
414         }
415
416         memcpy(skylowerlayerpixels, trans, 128*128*4);
417
418         alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
419 }
420
421 void R_ResetQuakeSky(void)
422 {
423         skyavailable_quake = false;
424 }
425
426 void R_ResetSkyBox(void)
427 {
428         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
429         skyname[0] = 0;
430         skyavailable_box = false;
431 }
432
433 static void r_sky_start(void)
434 {
435         skytexturepool = R_AllocTexturePool();
436         solidskytexture = NULL;
437         alphaskytexture = NULL;
438 }
439
440 static void r_sky_shutdown(void)
441 {
442         R_FreeTexturePool(&skytexturepool);
443         solidskytexture = NULL;
444         alphaskytexture = NULL;
445 }
446
447 static void r_sky_newmap(void)
448 {
449 }
450
451 void R_Sky_Init(void)
452 {
453         Cmd_AddCommand ("loadsky", &LoadSky_f);
454         Cvar_RegisterVariable (&r_sky);
455         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
456 }