]> git.xonotic.org Git - xonotic/darkplaces.git/blob - r_sky.c
limit vertex lighting path (GL11/GL13) to 4 passes per light rather than
[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 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 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
102         R_UnloadSkyBox();
103
104         if (!skyname[0])
105                 return true;
106
107         for (j=0; j<3; j++)
108         {
109                 success = 0;
110                 for (i=0; i<6; i++)
111                 {
112                         if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
113                         {
114                                 if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
115                                 {
116                                         if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
117                                         {
118                                                 if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
119                                                         continue;
120                                         }
121                                 }
122                         }
123                         temp = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
124                         Image_CopyMux (temp, image_buffer, image_width, image_height, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, indices);
125                         skyboxskinframe[i] = R_SkinFrame_LoadInternalBGRA(va("skyboxside%d", i), TEXF_CLAMP | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), temp, image_width, image_height);
126                         Mem_Free(image_buffer);
127                         Mem_Free(temp);
128                         success++;
129                 }
130
131                 if (success)
132                         break;
133         }
134
135         if (j == 3)
136                 return false;
137
138         if (developer_loading.integer)
139                 Con_Printf("loading skybox \"%s\"\n", name);
140
141         return true;
142 }
143
144 int R_SetSkyBox(const char *sky)
145 {
146         if (strcmp(sky, skyname) == 0) // no change
147                 return true;
148
149         if (strlen(sky) > 1000)
150         {
151                 Con_Printf("sky name too long (%i, max is 1000)\n", (int)strlen(sky));
152                 return false;
153         }
154
155         strlcpy(skyname, sky, sizeof(skyname));
156
157         return R_LoadSkyBox();
158 }
159
160 // LordHavoc: added LoadSky console command
161 void LoadSky_f (void)
162 {
163         switch (Cmd_Argc())
164         {
165         case 1:
166                 if (skyname[0])
167                         Con_Printf("current sky: %s\n", skyname);
168                 else
169                         Con_Print("no skybox has been set\n");
170                 break;
171         case 2:
172                 if (R_SetSkyBox(Cmd_Argv(1)))
173                 {
174                         if (skyname[0])
175                                 Con_Printf("skybox set to %s\n", skyname);
176                         else
177                                 Con_Print("skybox disabled\n");
178                 }
179                 else
180                         Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
181                 break;
182         default:
183                 Con_Print("usage: loadsky skyname\n");
184                 break;
185         }
186 }
187
188 static const float skyboxvertex3f[6*4*3] =
189 {
190         // skyside[0]
191          16, -16,  16,
192          16, -16, -16,
193          16,  16, -16,
194          16,  16,  16,
195         // skyside[1]
196         -16,  16,  16,
197         -16,  16, -16,
198         -16, -16, -16,
199         -16, -16,  16,
200         // skyside[2]
201          16,  16,  16,
202          16,  16, -16,
203         -16,  16, -16,
204         -16,  16,  16,
205         // skyside[3]
206         -16, -16,  16,
207         -16, -16, -16,
208          16, -16, -16,
209          16, -16,  16,
210         // skyside[4]
211         -16, -16,  16,
212          16, -16,  16,
213          16,  16,  16,
214         -16,  16,  16,
215         // skyside[5]
216          16, -16, -16,
217         -16, -16, -16,
218         -16,  16, -16,
219          16,  16, -16
220 };
221
222 static const float skyboxtexcoord2f[6*4*2] =
223 {
224         // skyside[0]
225         0, 1,
226         1, 1,
227         1, 0,
228         0, 0,
229         // skyside[1]
230         1, 0,
231         0, 0,
232         0, 1,
233         1, 1,
234         // skyside[2]
235         1, 1,
236         1, 0,
237         0, 0,
238         0, 1,
239         // skyside[3]
240         0, 0,
241         0, 1,
242         1, 1,
243         1, 0,
244         // skyside[4]
245         0, 1,
246         1, 1,
247         1, 0,
248         0, 0,
249         // skyside[5]
250         0, 1,
251         1, 1,
252         1, 0,
253         0, 0
254 };
255
256 static const int skyboxelement3i[6*2*3] =
257 {
258         // skyside[3]
259          0,  1,  2,
260          0,  2,  3,
261         // skyside[1]
262          4,  5,  6,
263          4,  6,  7,
264         // skyside[0]
265          8,  9, 10,
266          8, 10, 11,
267         // skyside[2]
268         12, 13, 14,
269         12, 14, 15,
270         // skyside[4]
271         16, 17, 18,
272         16, 18, 19,
273         // skyside[5]
274         20, 21, 22,
275         20, 22, 23
276 };
277
278 static const unsigned short skyboxelement3s[6*2*3] =
279 {
280         // skyside[3]
281          0,  1,  2,
282          0,  2,  3,
283         // skyside[1]
284          4,  5,  6,
285          4,  6,  7,
286         // skyside[0]
287          8,  9, 10,
288          8, 10, 11,
289         // skyside[2]
290         12, 13, 14,
291         12, 14, 15,
292         // skyside[4]
293         16, 17, 18,
294         16, 18, 19,
295         // skyside[5]
296         20, 21, 22,
297         20, 22, 23
298 };
299
300 static void R_SkyBox(void)
301 {
302         int i;
303         RSurf_ActiveCustomEntity(&skymatrix, &skyinversematrix, 0, 0, 1, 1, 1, 1, 6*4, skyboxvertex3f, skyboxtexcoord2f, NULL, NULL, NULL, NULL, 6*2, skyboxelement3i, skyboxelement3s, false, false);
304         for (i = 0;i < 6;i++)
305                 R_DrawCustomSurface(skyboxskinframe[i], &identitymatrix, MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST, i*4, 4, i*2, 2, false, false);
306 }
307
308 #define skygridx 32
309 #define skygridx1 (skygridx + 1)
310 #define skygridxrecip (1.0f / (skygridx))
311 #define skygridy 32
312 #define skygridy1 (skygridy + 1)
313 #define skygridyrecip (1.0f / (skygridy))
314 #define skysphere_numverts (skygridx1 * skygridy1)
315 #define skysphere_numtriangles (skygridx * skygridy * 2)
316 static float skysphere_vertex3f[skysphere_numverts * 3];
317 static float skysphere_texcoord2f[skysphere_numverts * 2];
318 static int skysphere_element3i[skysphere_numtriangles * 3];
319 static unsigned short skysphere_element3s[skysphere_numtriangles * 3];
320
321 static void skyspherecalc(void)
322 {
323         int i, j;
324         unsigned short *e;
325         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
326         float dx, dy, dz;
327         dx = 16.0f;
328         dy = 16.0f;
329         dz = 16.0f / 3.0f;
330         vertex3f = skysphere_vertex3f;
331         texcoord2f = skysphere_texcoord2f;
332         for (j = 0;j <= skygridy;j++)
333         {
334                 a = j * skygridyrecip;
335                 ax = cos(a * M_PI * 2);
336                 ay = -sin(a * M_PI * 2);
337                 for (i = 0;i <= skygridx;i++)
338                 {
339                         b = i * skygridxrecip;
340                         x = cos((b + 0.5) * M_PI);
341                         v[0] = ax*x * dx;
342                         v[1] = ay*x * dy;
343                         v[2] = -sin((b + 0.5) * M_PI) * dz;
344                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
345                         *texcoord2f++ = v[0] * length;
346                         *texcoord2f++ = v[1] * length;
347                         *vertex3f++ = v[0];
348                         *vertex3f++ = v[1];
349                         *vertex3f++ = v[2];
350                 }
351         }
352         e = skysphere_element3s;
353         for (j = 0;j < skygridy;j++)
354         {
355                 for (i = 0;i < skygridx;i++)
356                 {
357                         *e++ =  j      * skygridx1 + i;
358                         *e++ =  j      * skygridx1 + i + 1;
359                         *e++ = (j + 1) * skygridx1 + i;
360
361                         *e++ =  j      * skygridx1 + i + 1;
362                         *e++ = (j + 1) * skygridx1 + i + 1;
363                         *e++ = (j + 1) * skygridx1 + i;
364                 }
365         }
366         for (i = 0;i < skysphere_numtriangles*3;i++)
367                 skysphere_element3i[i] = skysphere_element3s[i];
368 }
369
370 static void R_SkySphere(void)
371 {
372         double speedscale;
373         static qboolean skysphereinitialized = false;
374         matrix4x4_t scroll1matrix, scroll2matrix;
375         if (!skysphereinitialized)
376         {
377                 skysphereinitialized = true;
378                 skyspherecalc();
379         }
380
381         // wrap the scroll values just to be extra kind to float accuracy
382
383         // scroll speed for upper layer
384         speedscale = r_refdef.scene.time*r_skyscroll1.value*8.0/128.0;
385         speedscale -= floor(speedscale);
386         Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
387         // scroll speed for lower layer (transparent layer)
388         speedscale = r_refdef.scene.time*r_skyscroll2.value*8.0/128.0;
389         speedscale -= floor(speedscale);
390         Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
391
392         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);
393         R_DrawCustomSurface(r_refdef.scene.worldmodel->brush.solidskyskinframe, &scroll1matrix, MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST                                            , 0, skysphere_numverts, 0, skysphere_numtriangles, false, false);
394         R_DrawCustomSurface(r_refdef.scene.worldmodel->brush.alphaskyskinframe, &scroll2matrix, MATERIALFLAG_FULLBRIGHT | MATERIALFLAG_NOCULLFACE | MATERIALFLAG_NODEPTHTEST | MATERIALFLAG_ALPHA | MATERIALFLAG_BLENDED, 0, skysphere_numverts, 0, skysphere_numtriangles, false, false);
395 }
396
397 void R_Sky(void)
398 {
399         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));
400         Matrix4x4_Invert_Simple(&skyinversematrix, &skymatrix);
401
402         if (skyrendersphere)
403         {
404                 // this does not modify depth buffer
405                 R_SkySphere();
406         }
407         else if (skyrenderbox)
408         {
409                 // this does not modify depth buffer
410                 R_SkyBox();
411         }
412         /* this will be skyroom someday
413         else
414         {
415                 // this modifies the depth buffer so we have to clear it afterward
416                 //R_SkyRoom();
417                 // clear the depthbuffer that was used while rendering the skyroom
418                 //GL_Clear(GL_DEPTH_BUFFER_BIT);
419         }
420         */
421 }
422
423 //===============================================================
424
425 void R_ResetSkyBox(void)
426 {
427         R_UnloadSkyBox();
428         skyname[0] = 0;
429         R_LoadSkyBox();
430 }
431
432 static void r_sky_start(void)
433 {
434         skytexturepool = R_AllocTexturePool();
435         R_LoadSkyBox();
436 }
437
438 static void r_sky_shutdown(void)
439 {
440         R_UnloadSkyBox();
441         R_FreeTexturePool(&skytexturepool);
442 }
443
444 static void r_sky_newmap(void)
445 {
446 }
447
448
449 void R_Sky_Init(void)
450 {
451         Cmd_AddCommand ("loadsky", &LoadSky_f, "load a skybox by basename (for example loadsky mtnsun_ loads mtnsun_ft.tga and so on)");
452         Cvar_RegisterVariable (&r_sky);
453         Cvar_RegisterVariable (&r_skyscroll1);
454         Cvar_RegisterVariable (&r_skyscroll2);
455         memset(&skyboxskinframe, 0, sizeof(skyboxskinframe));
456         skyname[0] = 0;
457         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
458 }
459