renamed polygonelements, skyboxelements, skysphereelements, to add 3s
[divverent/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 rtexture_t *skyboxside[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 && !(r_refdef.fogenabled && r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1] < (1.0f / 256.0f)))
63         {
64                 if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
65                         skyrenderbox = true;
66                 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.solidskytexture)
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 (skyboxside[i])
84                 {
85                         R_FreeTexture(skyboxside[i]);
86                         c++;
87                 }
88                 skyboxside[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                         skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, temp, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PRECACHE | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), NULL);
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         // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
304         GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
305         GL_BlendFunc(GL_ONE, GL_ZERO);
306         GL_CullFace(GL_NONE);
307         GL_DepthMask(false);
308         GL_DepthRange(0, 1);
309         GL_PolygonOffset(0, 0);
310         GL_DepthTest(false); // don't modify or read zbuffer
311         R_Mesh_VertexPointer(skyboxvertex3f, 0, 0);
312         R_Mesh_ColorPointer(NULL, 0, 0);
313         R_Mesh_ResetTextureState();
314         R_Mesh_TexCoordPointer(0, 2, skyboxtexcoord2f, 0, 0);
315         R_SetupGenericShader(true);
316         GL_LockArrays(0, 6*4);
317         for (i = 0;i < 6;i++)
318         {
319                 R_Mesh_TexBind(0, R_GetTexture(skyboxside[i]));
320                 R_Mesh_Draw(0, 6*4, i*2, 2, skyboxelement3i, skyboxelement3s, 0, 0);
321         }
322
323         if(r_refdef.fogenabled)
324         {
325                 R_SetupGenericShader(false);
326                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
327                 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
328                 for (i = 0;i < 6;i++)
329                 {
330                         R_Mesh_TexBind(0, 0);
331                         R_Mesh_Draw(0, 6*4, i*2, 2, skyboxelement3i, skyboxelement3s, 0, 0);
332                 }
333         }
334
335         GL_LockArrays(0, 0);
336 }
337
338 #define skygridx 32
339 #define skygridx1 (skygridx + 1)
340 #define skygridxrecip (1.0f / (skygridx))
341 #define skygridy 32
342 #define skygridy1 (skygridy + 1)
343 #define skygridyrecip (1.0f / (skygridy))
344 #define skysphere_numverts (skygridx1 * skygridy1)
345 #define skysphere_numtriangles (skygridx * skygridy * 2)
346 static float skysphere_vertex3f[skysphere_numverts * 3];
347 static float skysphere_texcoord2f[skysphere_numverts * 2];
348 static int skysphere_element3i[skysphere_numtriangles * 3];
349 static unsigned short skysphere_element3s[skysphere_numtriangles * 3];
350
351 static void skyspherecalc(void)
352 {
353         int i, j;
354         unsigned short *e;
355         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
356         float dx, dy, dz;
357         dx = 16.0f;
358         dy = 16.0f;
359         dz = 16.0f / 3.0f;
360         vertex3f = skysphere_vertex3f;
361         texcoord2f = skysphere_texcoord2f;
362         for (j = 0;j <= skygridy;j++)
363         {
364                 a = j * skygridyrecip;
365                 ax = cos(a * M_PI * 2);
366                 ay = -sin(a * M_PI * 2);
367                 for (i = 0;i <= skygridx;i++)
368                 {
369                         b = i * skygridxrecip;
370                         x = cos((b + 0.5) * M_PI);
371                         v[0] = ax*x * dx;
372                         v[1] = ay*x * dy;
373                         v[2] = -sin((b + 0.5) * M_PI) * dz;
374                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
375                         *texcoord2f++ = v[0] * length;
376                         *texcoord2f++ = v[1] * length;
377                         *vertex3f++ = v[0];
378                         *vertex3f++ = v[1];
379                         *vertex3f++ = v[2];
380                 }
381         }
382         e = skysphere_element3s;
383         for (j = 0;j < skygridy;j++)
384         {
385                 for (i = 0;i < skygridx;i++)
386                 {
387                         *e++ =  j      * skygridx1 + i;
388                         *e++ =  j      * skygridx1 + i + 1;
389                         *e++ = (j + 1) * skygridx1 + i;
390
391                         *e++ =  j      * skygridx1 + i + 1;
392                         *e++ = (j + 1) * skygridx1 + i + 1;
393                         *e++ = (j + 1) * skygridx1 + i;
394                 }
395         }
396         for (i = 0;i < skysphere_numtriangles*3;i++)
397                 skysphere_element3i[i] = skysphere_element3s[i];
398 }
399
400 static void R_SkySphere(void)
401 {
402         double speedscale;
403         static qboolean skysphereinitialized = false;
404         matrix4x4_t scroll1matrix, scroll2matrix;
405         if (!skysphereinitialized)
406         {
407                 skysphereinitialized = true;
408                 skyspherecalc();
409         }
410
411         // wrap the scroll values just to be extra kind to float accuracy
412
413         // scroll speed for upper layer
414         speedscale = r_refdef.scene.time*r_skyscroll1.value*8.0/128.0;
415         speedscale -= floor(speedscale);
416         Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
417         // scroll speed for lower layer (transparent layer)
418         speedscale = r_refdef.scene.time*r_skyscroll2.value*8.0/128.0;
419         speedscale -= floor(speedscale);
420         Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
421
422         // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
423         GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
424         GL_BlendFunc(GL_ONE, GL_ZERO);
425         GL_CullFace(GL_NONE);
426         GL_DepthMask(true);
427         GL_DepthRange(0, 1);
428         GL_PolygonOffset(0, 0);
429         GL_DepthTest(false); // don't modify or read zbuffer
430         R_Mesh_VertexPointer(skysphere_vertex3f, 0, 0);
431         R_Mesh_ColorPointer(NULL, 0, 0);
432         R_Mesh_ResetTextureState();
433         R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.solidskytexture));
434         R_Mesh_TexCoordPointer(0, 2, skysphere_texcoord2f, 0, 0);
435         R_Mesh_TexMatrix(0, &scroll1matrix);
436         if (r_textureunits.integer >= 2 && r_refdef.view.colorscale == 1)
437         {
438                 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
439                 R_SetupGenericTwoTextureShader(GL_DECAL);
440                 R_Mesh_TexBind(1, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
441                 R_Mesh_TexCoordPointer(1, 2, skysphere_texcoord2f, 0, 0);
442                 R_Mesh_TexMatrix(1, &scroll2matrix);
443                 GL_LockArrays(0, skysphere_numverts);
444                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, 0, 0);
445                 GL_LockArrays(0, 0);
446                 R_Mesh_TexBind(1, 0);
447         }
448         else
449         {
450                 // two pass
451                 R_SetupGenericShader(true);
452                 GL_LockArrays(0, skysphere_numverts);
453                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, 0, 0);
454
455                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
456                 R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
457                 R_Mesh_TexMatrix(0, &scroll2matrix);
458                 GL_LockArrays(0, skysphere_numverts);
459                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, 0, 0);
460                 GL_LockArrays(0, 0);
461         }
462
463         if(r_refdef.fogenabled)
464         {
465                 R_SetupGenericShader(false);
466                 R_Mesh_TexBind(0, 0);
467                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
468                 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
469                 GL_LockArrays(0, skysphere_numverts);
470                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, skysphere_element3i, skysphere_element3s, 0, 0);
471                 GL_LockArrays(0, 0);
472         }
473 }
474
475 void R_Sky(void)
476 {
477         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));
478         Matrix4x4_Invert_Simple(&skyinversematrix, &skymatrix);
479
480         R_Mesh_Matrix(&skymatrix);
481         if (skyrendersphere)
482         {
483                 // this does not modify depth buffer
484                 R_SkySphere();
485         }
486         else if (skyrenderbox)
487         {
488                 // this does not modify depth buffer
489                 R_SkyBox();
490         }
491         /* this will be skyroom someday
492         else
493         {
494                 // this modifies the depth buffer so we have to clear it afterward
495                 //R_SkyRoom();
496                 // clear the depthbuffer that was used while rendering the skyroom
497                 //GL_Clear(GL_DEPTH_BUFFER_BIT);
498         }
499         */
500         GL_DepthRange(0, 1);
501         GL_DepthTest(true);
502         GL_DepthMask(true);
503 }
504
505 //===============================================================
506
507 void R_ResetSkyBox(void)
508 {
509         R_UnloadSkyBox();
510         skyname[0] = 0;
511         R_LoadSkyBox();
512 }
513
514 static void r_sky_start(void)
515 {
516         skytexturepool = R_AllocTexturePool();
517         R_LoadSkyBox();
518 }
519
520 static void r_sky_shutdown(void)
521 {
522         R_UnloadSkyBox();
523         R_FreeTexturePool(&skytexturepool);
524 }
525
526 static void r_sky_newmap(void)
527 {
528 }
529
530
531 void R_Sky_Init(void)
532 {
533         Cmd_AddCommand ("loadsky", &LoadSky_f, "load a skybox by basename (for example loadsky mtnsun_ loads mtnsun_ft.tga and so on)");
534         Cvar_RegisterVariable (&r_sky);
535         Cvar_RegisterVariable (&r_skyscroll1);
536         Cvar_RegisterVariable (&r_skyscroll2);
537         memset(&skyboxside, 0, sizeof(skyboxside));
538         skyname[0] = 0;
539         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
540 }
541