major overhaul for thread-safety - many global variables and static
[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", "enables sky rendering (black otherwise)"};
7 cvar_t r_skyscroll1 = {CVAR_SAVE, "r_skyscroll1", "1", "speed at which upper clouds layer scrolls in quake sky"};
8 cvar_t r_skyscroll2 = {CVAR_SAVE, "r_skyscroll2", "2", "speed at which lower clouds layer scrolls in quake sky"};
9 int skyrenderlater;
10 int skyrendermasked;
11
12 static int skyrendersphere;
13 static int skyrenderbox;
14 static rtexturepool_t *skytexturepool;
15 static char skyname[MAX_QPATH];
16 static matrix4x4_t skymatrix;
17 static matrix4x4_t skyinversematrix;
18
19 typedef struct suffixinfo_s
20 {
21         const char *suffix;
22         qboolean flipx, flipy, flipdiagonal;
23 }
24 suffixinfo_t;
25 static const suffixinfo_t suffix[3][6] =
26 {
27         {
28                 {"px",   false, false, false},
29                 {"nx",   false, false, false},
30                 {"py",   false, false, false},
31                 {"ny",   false, false, false},
32                 {"pz",   false, false, false},
33                 {"nz",   false, false, false}
34         },
35         {
36                 {"posx", false, false, false},
37                 {"negx", false, false, false},
38                 {"posy", false, false, false},
39                 {"negy", false, false, false},
40                 {"posz", false, false, false},
41                 {"negz", false, false, false}
42         },
43         {
44                 {"rt",   false, false,  true},
45                 {"lf",    true,  true,  true},
46                 {"bk",   false,  true, false},
47                 {"ft",    true, false, false},
48                 {"up",   false, false,  true},
49                 {"dn",   false, false,  true}
50         }
51 };
52
53 static skinframe_t *skyboxskinframe[6];
54
55 void R_SkyStartFrame(void)
56 {
57         skyrendersphere = false;
58         skyrenderbox = false;
59         skyrendermasked = false;
60         // for depth-masked sky, we need to know whether any sky was rendered
61         skyrenderlater = false;
62         if (r_sky.integer)
63         {
64                 if (skyboxskinframe[0] || skyboxskinframe[1] || skyboxskinframe[2] || skyboxskinframe[3] || skyboxskinframe[4] || skyboxskinframe[5])
65                         skyrenderbox = true;
66                 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.solidskyskinframe)
67                         skyrendersphere = true;
68                 skyrendermasked = true;
69         }
70 }
71
72 /*
73 ==================
74 R_SetSkyBox
75 ==================
76 */
77 static void R_UnloadSkyBox(void)
78 {
79         int i;
80         int c = 0;
81         for (i = 0;i < 6;i++)
82         {
83                 if (skyboxskinframe[i])
84                 {
85                         // TODO: make a R_SkinFrame_Purge for single skins...
86                         c++;
87                 }
88                 skyboxskinframe[i] = NULL;
89         }
90         if (c && developer_loading.integer)
91                 Con_Printf("unloading skybox\n");
92 }
93
94 static int R_LoadSkyBox(void)
95 {
96         int i, j, success;
97         int indices[4] = {0,1,2,3};
98         char name[MAX_INPUTLINE];
99         unsigned char *image_buffer;
100         unsigned char *temp;
101         char vabuf[1024];
102
103         R_UnloadSkyBox();
104
105         if (!skyname[0])
106                 return true;
107
108         for (j=0; j<3; j++)
109         {
110                 success = 0;
111                 for (i=0; i<6; i++)
112                 {
113                         if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
114                         {
115                                 if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
116                                 {
117                                         if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
118                                         {
119                                                 if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false, false, NULL)))
120                                                         continue;
121                                         }
122                                 }
123                         }
124                         temp = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
125                         Image_CopyMux (temp, image_buffer, image_width, image_height, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, indices);
126                         skyboxskinframe[i] = R_SkinFrame_LoadInternalBGRA(va(vabuf, sizeof(vabuf), "skyboxside%d", i), TEXF_CLAMP | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), temp, image_width, image_height, vid.sRGB3D);
127                         Mem_Free(image_buffer);
128                         Mem_Free(temp);
129                         success++;
130                 }
131
132                 if (success)
133                         break;
134         }
135
136         if (j == 3)
137                 return false;
138
139         if (developer_loading.integer)
140                 Con_Printf("loading skybox \"%s\"\n", name);
141
142         return true;
143 }
144
145 int R_SetSkyBox(const char *sky)
146 {
147         if (strcmp(sky, skyname) == 0) // no change
148                 return true;
149
150         if (strlen(sky) > 1000)
151         {
152                 Con_Printf("sky name too long (%i, max is 1000)\n", (int)strlen(sky));
153                 return false;
154         }
155
156         strlcpy(skyname, sky, sizeof(skyname));
157
158         return R_LoadSkyBox();
159 }
160
161 // LordHavoc: added LoadSky console command
162 static void LoadSky_f (void)
163 {
164         switch (Cmd_Argc())
165         {
166         case 1:
167                 if (skyname[0])
168                         Con_Printf("current sky: %s\n", skyname);
169                 else
170                         Con_Print("no skybox has been set\n");
171                 break;
172         case 2:
173                 if (R_SetSkyBox(Cmd_Argv(1)))
174                 {
175                         if (skyname[0])
176                                 Con_Printf("skybox set to %s\n", skyname);
177                         else
178                                 Con_Print("skybox disabled\n");
179                 }
180                 else
181                         Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
182                 break;
183         default:
184                 Con_Print("usage: loadsky skyname\n");
185                 break;
186         }
187 }
188
189 static const float skyboxvertex3f[6*4*3] =
190 {
191         // skyside[0]
192          16, -16,  16,
193          16, -16, -16,
194          16,  16, -16,
195          16,  16,  16,
196         // skyside[1]
197         -16,  16,  16,
198         -16,  16, -16,
199         -16, -16, -16,
200         -16, -16,  16,
201         // skyside[2]
202          16,  16,  16,
203          16,  16, -16,
204         -16,  16, -16,
205         -16,  16,  16,
206         // skyside[3]
207         -16, -16,  16,
208         -16, -16, -16,
209          16, -16, -16,
210          16, -16,  16,
211         // skyside[4]
212         -16, -16,  16,
213          16, -16,  16,
214          16,  16,  16,
215         -16,  16,  16,
216         // skyside[5]
217          16, -16, -16,
218         -16, -16, -16,
219         -16,  16, -16,
220          16,  16, -16
221 };
222
223 static const float skyboxtexcoord2f[6*4*2] =
224 {
225         // skyside[0]
226         0, 1,
227         1, 1,
228         1, 0,
229         0, 0,
230         // skyside[1]
231         1, 0,
232         0, 0,
233         0, 1,
234         1, 1,
235         // skyside[2]
236         1, 1,
237         1, 0,
238         0, 0,
239         0, 1,
240         // skyside[3]
241         0, 0,
242         0, 1,
243         1, 1,
244         1, 0,
245         // skyside[4]
246         0, 1,
247         1, 1,
248         1, 0,
249         0, 0,
250         // skyside[5]
251         0, 1,
252         1, 1,
253         1, 0,
254         0, 0
255 };
256
257 static const int skyboxelement3i[6*2*3] =
258 {
259         // skyside[3]
260          0,  1,  2,
261          0,  2,  3,
262         // skyside[1]
263          4,  5,  6,
264          4,  6,  7,
265         // skyside[0]
266          8,  9, 10,
267          8, 10, 11,
268         // skyside[2]
269         12, 13, 14,
270         12, 14, 15,
271         // skyside[4]
272         16, 17, 18,
273         16, 18, 19,
274         // skyside[5]
275         20, 21, 22,
276         20, 22, 23
277 };
278
279 static const unsigned short skyboxelement3s[6*2*3] =
280 {
281         // skyside[3]
282          0,  1,  2,
283          0,  2,  3,
284         // skyside[1]
285          4,  5,  6,
286          4,  6,  7,
287         // skyside[0]
288          8,  9, 10,
289          8, 10, 11,
290         // skyside[2]
291         12, 13, 14,
292         12, 14, 15,
293         // skyside[4]
294         16, 17, 18,
295         16, 18, 19,
296         // skyside[5]
297         20, 21, 22,
298         20, 22, 23
299 };
300
301 static void R_SkyBox(void)
302 {
303         int i;
304         RSurf_ActiveCustomEntity(&skymatrix, &skyinversematrix, 0, 0, 1, 1, 1, 1, 6*4, skyboxvertex3f, skyboxtexcoord2f, NULL, NULL, NULL, NULL, 6*2, skyboxelement3i, skyboxelement3s, false, false);
305         for (i = 0;i < 6;i++)
306                 if(skyboxskinframe[i])
307                         R_DrawCustomSurface(skyboxskinframe[i], &identitymatrix, MATERIALFLAG_SKY | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST, i*4, 4, i*2, 2, false, false);
308 }
309
310 #define skygridx 32
311 #define skygridx1 (skygridx + 1)
312 #define skygridxrecip (1.0f / (skygridx))
313 #define skygridy 32
314 #define skygridy1 (skygridy + 1)
315 #define skygridyrecip (1.0f / (skygridy))
316 #define skysphere_numverts (skygridx1 * skygridy1)
317 #define skysphere_numtriangles (skygridx * skygridy * 2)
318 static float skysphere_vertex3f[skysphere_numverts * 3];
319 static float skysphere_texcoord2f[skysphere_numverts * 2];
320 static int skysphere_element3i[skysphere_numtriangles * 3];
321 static unsigned short skysphere_element3s[skysphere_numtriangles * 3];
322
323 static void skyspherecalc(void)
324 {
325         int i, j;
326         unsigned short *e;
327         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
328         float dx, dy, dz;
329         dx = 16.0f;
330         dy = 16.0f;
331         dz = 16.0f / 3.0f;
332         vertex3f = skysphere_vertex3f;
333         texcoord2f = skysphere_texcoord2f;
334         for (j = 0;j <= skygridy;j++)
335         {
336                 a = j * skygridyrecip;
337                 ax = cos(a * M_PI * 2);
338                 ay = -sin(a * M_PI * 2);
339                 for (i = 0;i <= skygridx;i++)
340                 {
341                         b = i * skygridxrecip;
342                         x = cos((b + 0.5) * M_PI);
343                         v[0] = ax*x * dx;
344                         v[1] = ay*x * dy;
345                         v[2] = -sin((b + 0.5) * M_PI) * dz;
346                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
347                         *texcoord2f++ = v[0] * length;
348                         *texcoord2f++ = v[1] * length;
349                         *vertex3f++ = v[0];
350                         *vertex3f++ = v[1];
351                         *vertex3f++ = v[2];
352                 }
353         }
354         e = skysphere_element3s;
355         for (j = 0;j < skygridy;j++)
356         {
357                 for (i = 0;i < skygridx;i++)
358                 {
359                         *e++ =  j      * skygridx1 + i;
360                         *e++ =  j      * skygridx1 + i + 1;
361                         *e++ = (j + 1) * skygridx1 + i;
362
363                         *e++ =  j      * skygridx1 + i + 1;
364                         *e++ = (j + 1) * skygridx1 + i + 1;
365                         *e++ = (j + 1) * skygridx1 + i;
366                 }
367         }
368         for (i = 0;i < skysphere_numtriangles*3;i++)
369                 skysphere_element3i[i] = skysphere_element3s[i];
370 }
371
372 static void R_SkySphere(void)
373 {
374         double speedscale;
375         static qboolean skysphereinitialized = false;
376         matrix4x4_t scroll1matrix, scroll2matrix;
377         if (!skysphereinitialized)
378         {
379                 skysphereinitialized = true;
380                 skyspherecalc();
381         }
382
383         // wrap the scroll values just to be extra kind to float accuracy
384
385         // scroll speed for upper layer
386         speedscale = r_refdef.scene.time*r_skyscroll1.value*8.0/128.0;
387         speedscale -= floor(speedscale);
388         Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
389         // scroll speed for lower layer (transparent layer)
390         speedscale = r_refdef.scene.time*r_skyscroll2.value*8.0/128.0;
391         speedscale -= floor(speedscale);
392         Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
393
394         RSurf_ActiveCustomEntity(&skymatrix, &skyinversematrix, 0, 0, 1, 1, 1, 1, skysphere_numverts, skysphere_vertex3f, skysphere_texcoord2f, NULL, NULL, NULL, NULL, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, false, false);
395         R_DrawCustomSurface(r_refdef.scene.worldmodel->brush.solidskyskinframe, &scroll1matrix, MATERIALFLAG_SKY | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST                                            , 0, skysphere_numverts, 0, skysphere_numtriangles, false, false);
396         R_DrawCustomSurface(r_refdef.scene.worldmodel->brush.alphaskyskinframe, &scroll2matrix, MATERIALFLAG_SKY | MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED, 0, skysphere_numverts, 0, skysphere_numtriangles, false, false);
397 }
398
399 void R_Sky(void)
400 {
401         Matrix4x4_CreateFromQuakeEntity(&skymatrix, r_refdef.view.origin[0], r_refdef.view.origin[1], r_refdef.view.origin[2], 0, 0, 0, r_refdef.farclip * (0.5f / 16.0f));
402         Matrix4x4_Invert_Simple(&skyinversematrix, &skymatrix);
403
404         if (skyrendersphere)
405         {
406                 // this does not modify depth buffer
407                 R_SkySphere();
408         }
409         else if (skyrenderbox)
410         {
411                 // this does not modify depth buffer
412                 R_SkyBox();
413         }
414         /* this will be skyroom someday
415         else
416         {
417                 // this modifies the depth buffer so we have to clear it afterward
418                 //R_SkyRoom();
419                 // clear the depthbuffer that was used while rendering the skyroom
420                 //GL_Clear(GL_DEPTH_BUFFER_BIT);
421         }
422         */
423 }
424
425 //===============================================================
426
427 void R_ResetSkyBox(void)
428 {
429         R_UnloadSkyBox();
430         skyname[0] = 0;
431         R_LoadSkyBox();
432 }
433
434 static void r_sky_start(void)
435 {
436         skytexturepool = R_AllocTexturePool();
437         R_LoadSkyBox();
438 }
439
440 static void r_sky_shutdown(void)
441 {
442         R_UnloadSkyBox();
443         R_FreeTexturePool(&skytexturepool);
444 }
445
446 static void r_sky_newmap(void)
447 {
448 }
449
450
451 void R_Sky_Init(void)
452 {
453         Cmd_AddCommand ("loadsky", &LoadSky_f, "load a skybox by basename (for example loadsky mtnsun_ loads mtnsun_ft.tga and so on)");
454         Cvar_RegisterVariable (&r_sky);
455         Cvar_RegisterVariable (&r_skyscroll1);
456         Cvar_RegisterVariable (&r_skyscroll2);
457         memset(&skyboxskinframe, 0, sizeof(skyboxskinframe));
458         skyname[0] = 0;
459         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap, NULL, NULL);
460 }
461