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