merged CL_ColorPointer into R_Mesh_State
[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"};
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 void R_UnloadSkyBox(void)
45 {
46         int i;
47         for (i = 0;i < 6;i++)
48         {
49                 if (skyboxside[i])
50                         R_FreeTexture(skyboxside[i]);
51                 skyboxside[i] = NULL;;
52         }
53 }
54
55 void R_LoadSkyBox(void)
56 {
57         int i;
58         char name[1024];
59         qbyte *image_rgba;
60         R_UnloadSkyBox();
61         if (!skyname[0])
62                 return;
63         for (i = 0;i < 6;i++)
64         {
65                 if (snprintf(name, sizeof(name), "%s_%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
66                 {
67                         if (snprintf(name, sizeof(name), "%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
68                         {
69                                 if (snprintf(name, sizeof(name), "env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
70                                 {
71                                         if (snprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
72                                         {
73                                                 Con_Printf("Couldn't load %s_%s or %s%s or env/%s%s or gfx/env/%s%s\n", skyname, suf[i], skyname, suf[i], skyname, suf[i], skyname, suf[i]);
74                                                 continue;
75                                         }
76                                 }
77                         }
78                 }
79                 skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, image_rgba, TEXTYPE_RGBA, TEXF_CLAMP | TEXF_PRECACHE, NULL);
80                 Mem_Free(image_rgba);
81         }
82 }
83
84 int R_SetSkyBox(const char *sky)
85 {
86         if (strcmp(sky, skyname) == 0) // no change
87                 return true;
88
89         if (strlen(sky) > 1000)
90         {
91                 Con_Printf("sky name too long (%i, max is 1000)\n", strlen(sky));
92                 return false;
93         }
94
95         skyavailable_box = false;
96         strcpy(skyname, sky);
97
98         R_UnloadSkyBox();
99         R_LoadSkyBox();
100
101         if (!skyname[0])
102                 return true;
103
104         if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
105         {
106                 skyavailable_box = true;
107                 return true;
108         }
109         return false;
110 }
111
112 // LordHavoc: added LoadSky console command
113 void LoadSky_f (void)
114 {
115         switch (Cmd_Argc())
116         {
117         case 1:
118                 if (skyname[0])
119                         Con_Printf("current sky: %s\n", skyname);
120                 else
121                         Con_Print("no skybox has been set\n");
122                 break;
123         case 2:
124                 if (R_SetSkyBox(Cmd_Argv(1)))
125                 {
126                         if (skyname[0])
127                                 Con_Printf("skybox set to %s\n", skyname);
128                         else
129                                 Con_Print("skybox disabled\n");
130                 }
131                 else
132                         Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
133                 break;
134         default:
135                 Con_Print("usage: loadsky skyname\n");
136                 break;
137         }
138 }
139
140 float skyboxvertex3f[6*4*3] =
141 {
142         // skyside[0]
143          16,  16,  16,
144          16,  16, -16,
145         -16,  16, -16,
146         -16,  16,  16,
147         // skyside[1]
148         -16,  16,  16,
149         -16,  16, -16,
150         -16, -16, -16,
151         -16, -16,  16,
152         // skyside[2]
153         -16, -16,  16,
154         -16, -16, -16,
155          16, -16, -16,
156          16, -16,  16,
157         // skyside[3]
158          16, -16,  16,
159          16, -16, -16,
160          16,  16, -16,
161          16,  16,  16,
162         // skyside[4]
163          16, -16,  16,
164          16,  16,  16,
165         -16,  16,  16,
166         -16, -16,  16,
167         // skyside[5]
168          16,  16, -16,
169          16, -16, -16,
170         -16, -16, -16,
171         -16,  16, -16
172 };
173
174 float skyboxtexcoord2f[6*4*2] =
175 {
176         // skyside[0]
177         1, 0,
178         1, 1,
179         0, 1,
180         0, 0,
181         // skyside[1]
182         1, 0,
183         1, 1,
184         0, 1,
185         0, 0,
186         // skyside[2]
187         1, 0,
188         1, 1,
189         0, 1,
190         0, 0,
191         // skyside[3]
192         1, 0,
193         1, 1,
194         0, 1,
195         0, 0,
196         // skyside[4]
197         1, 0,
198         1, 1,
199         0, 1,
200         0, 0,
201         // skyside[5]
202         1, 0,
203         1, 1,
204         0, 1,
205         0, 0
206 };
207
208 int skyboxelements[6*2*3] =
209 {
210         // skyside[3]
211          0,  1,  2,
212          0,  2,  3,
213         // skyside[1]
214          4,  5,  6,
215          4,  6,  7,
216         // skyside[0]
217          8,  9, 10,
218          8, 10, 11,
219         // skyside[2]
220         12, 13, 14,
221         12, 14, 15,
222         // skyside[4]
223         16, 17, 18,
224         16, 18, 19,
225         // skyside[5]
226         20, 21, 22,
227         20, 22, 23
228 };
229
230 static void R_SkyBox(void)
231 {
232         int i;
233         rmeshstate_t m;
234         GL_Color(1, 1, 1, 1);
235         memset(&m, 0, sizeof(m));
236         GL_BlendFunc(GL_ONE, GL_ZERO);
237         GL_DepthMask(true);
238         GL_DepthTest(false); // don't modify or read zbuffer
239         m.pointer_vertex = skyboxvertex3f;
240         m.pointer_texcoord[0] = skyboxtexcoord2f;
241         for (i = 0;i < 6;i++)
242         {
243                 m.tex[0] = R_GetTexture(skyboxside[i]);
244                 R_Mesh_State(&m);
245                 R_Mesh_Draw(6*4, 2, skyboxelements + i * 6);
246         }
247 }
248
249 #define skygridx 32
250 #define skygridx1 (skygridx + 1)
251 #define skygridxrecip (1.0f / (skygridx))
252 #define skygridy 32
253 #define skygridy1 (skygridy + 1)
254 #define skygridyrecip (1.0f / (skygridy))
255 #define skysphere_numverts (skygridx1 * skygridy1)
256 #define skysphere_numtriangles (skygridx * skygridy * 2)
257 static float skysphere_vertex3f[skysphere_numverts * 3];
258 static float skysphere_texcoord2f[skysphere_numverts * 2];
259 static int skysphere_element3i[skysphere_numtriangles * 3];
260
261 static void skyspherecalc(void)
262 {
263         int i, j, *e;
264         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
265         float dx, dy, dz;
266         dx = 16;
267         dy = 16;
268         dz = 16 / 3;
269         vertex3f = skysphere_vertex3f;
270         texcoord2f = skysphere_texcoord2f;
271         for (j = 0;j <= skygridy;j++)
272         {
273                 a = j * skygridyrecip;
274                 ax = cos(a * M_PI * 2);
275                 ay = -sin(a * M_PI * 2);
276                 for (i = 0;i <= skygridx;i++)
277                 {
278                         b = i * skygridxrecip;
279                         x = cos((b + 0.5) * M_PI);
280                         v[0] = ax*x * dx;
281                         v[1] = ay*x * dy;
282                         v[2] = -sin((b + 0.5) * M_PI) * dz;
283                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
284                         *texcoord2f++ = v[0] * length;
285                         *texcoord2f++ = v[1] * length;
286                         *vertex3f++ = v[0];
287                         *vertex3f++ = v[1];
288                         *vertex3f++ = v[2];
289                 }
290         }
291         e = skysphere_element3i;
292         for (j = 0;j < skygridy;j++)
293         {
294                 for (i = 0;i < skygridx;i++)
295                 {
296                         *e++ =  j      * skygridx1 + i;
297                         *e++ =  j      * skygridx1 + i + 1;
298                         *e++ = (j + 1) * skygridx1 + i;
299
300                         *e++ =  j      * skygridx1 + i + 1;
301                         *e++ = (j + 1) * skygridx1 + i + 1;
302                         *e++ = (j + 1) * skygridx1 + i;
303                 }
304         }
305 }
306
307 static void R_SkySphere(void)
308 {
309         float speedscale;
310         static qboolean skysphereinitialized = false;
311         rmeshstate_t m;
312         matrix4x4_t scroll1matrix, scroll2matrix, identitymatrix;
313         if (!skysphereinitialized)
314         {
315                 skysphereinitialized = true;
316                 skyspherecalc();
317         }
318
319         // scroll speed for upper layer
320         speedscale = cl.time*8.0/128.0;
321         // wrap the scroll just to be extra kind to float accuracy
322         speedscale -= (int)speedscale;
323
324         // scroll the lower cloud layer twice as fast (just like quake did)
325         Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
326         Matrix4x4_CreateTranslate(&scroll2matrix, speedscale * 2, speedscale * 2, 0);
327         Matrix4x4_CreateIdentity(&identitymatrix);
328
329         GL_Color(1, 1, 1, 1);
330         GL_BlendFunc(GL_ONE, GL_ZERO);
331         GL_DepthMask(true);
332         GL_DepthTest(false); // don't modify or read zbuffer
333         memset(&m, 0, sizeof(m));
334         m.pointer_vertex = skysphere_vertex3f;
335         m.tex[0] = R_GetTexture(solidskytexture);
336         m.pointer_texcoord[0] = skysphere_texcoord2f;
337         R_Mesh_TextureMatrix(0, &scroll1matrix);
338         if (r_textureunits.integer >= 2)
339         {
340                 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
341                 m.tex[1] = R_GetTexture(alphaskytexture);
342                 m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
343                 m.pointer_texcoord[1] = skysphere_texcoord2f;
344                 R_Mesh_State(&m);
345                 R_Mesh_TextureMatrix(1, &scroll2matrix);
346                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
347                 R_Mesh_TextureMatrix(1, &identitymatrix);
348         }
349         else
350         {
351                 // two pass
352                 R_Mesh_State(&m);
353                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
354
355                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
356                 m.tex[0] = R_GetTexture(alphaskytexture);
357                 R_Mesh_State(&m);
358                 R_Mesh_TextureMatrix(0, &scroll2matrix);
359                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
360         }
361         R_Mesh_TextureMatrix(0, &identitymatrix);
362 }
363
364 void R_Sky(void)
365 {
366         matrix4x4_t skymatrix;
367         if (skyrendermasked)
368         {
369                 Matrix4x4_CreateTranslate(&skymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2]);
370                 R_Mesh_Matrix(&skymatrix);
371                 if (skyrendersphere)
372                 {
373                         // this does not modify depth buffer
374                         R_SkySphere();
375                 }
376                 else if (skyrenderbox)
377                 {
378                         // this does not modify depth buffer
379                         R_SkyBox();
380                 }
381                 /* this will be skyroom someday
382                 else
383                 {
384                         // this modifies the depth buffer so we have to clear it afterward
385                         //R_SkyRoom();
386                         // clear the depthbuffer that was used while rendering the skyroom
387                         //GL_Clear(GL_DEPTH_BUFFER_BIT);
388                 }
389                 */
390         }
391 }
392
393 //===============================================================
394
395 /*
396 =============
397 R_InitSky
398
399 A sky texture is 256*128, with the right side being a masked overlay
400 ==============
401 */
402 void R_InitSky (qbyte *src, int bytesperpixel)
403 {
404         int i, j;
405         unsigned solidpixels[128*128], alphapixels[128*128];
406
407         skyavailable_quake = true;
408
409         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
410         R_FreeTexturePool(&skytexturepool);
411         skytexturepool = R_AllocTexturePool();
412         solidskytexture = NULL;
413         alphaskytexture = NULL;
414
415         if (bytesperpixel == 4)
416         {
417                 for (i = 0;i < 128;i++)
418                 {
419                         for (j = 0;j < 128;j++)
420                         {
421                                 solidpixels[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
422                                 alphapixels[(i*128) + j] = ((unsigned *)src)[i*256+j];
423                         }
424                 }
425         }
426         else
427         {
428                 // make an average value for the back to avoid
429                 // a fringe on the top level
430                 int p, r, g, b;
431                 union
432                 {
433                         unsigned int i;
434                         unsigned char b[4];
435                 }
436                 rgba;
437                 r = g = b = 0;
438                 for (i = 0;i < 128;i++)
439                 {
440                         for (j = 0;j < 128;j++)
441                         {
442                                 rgba.i = palette_complete[src[i*256 + j + 128]];
443                                 r += rgba.b[0];
444                                 g += rgba.b[1];
445                                 b += rgba.b[2];
446                         }
447                 }
448                 rgba.b[0] = r/(128*128);
449                 rgba.b[1] = g/(128*128);
450                 rgba.b[2] = b/(128*128);
451                 rgba.b[3] = 0;
452                 for (i = 0;i < 128;i++)
453                 {
454                         for (j = 0;j < 128;j++)
455                         {
456                                 solidpixels[(i*128) + j] = palette_complete[src[i*256 + j + 128]];
457                                 alphapixels[(i*128) + j] = (p = src[i*256 + j]) ? palette_complete[p] : rgba.i;
458                         }
459                 }
460         }
461
462         solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) solidpixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
463         alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) alphapixels, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
464 }
465
466 void R_ResetQuakeSky(void)
467 {
468         skyavailable_quake = false;
469 }
470
471 void R_ResetSkyBox(void)
472 {
473         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
474         skyname[0] = 0;
475         skyavailable_box = false;
476 }
477
478 static void r_sky_start(void)
479 {
480         skytexturepool = R_AllocTexturePool();
481         solidskytexture = NULL;
482         alphaskytexture = NULL;
483         R_LoadSkyBox();
484 }
485
486 static void r_sky_shutdown(void)
487 {
488         R_UnloadSkyBox();
489         R_FreeTexturePool(&skytexturepool);
490         solidskytexture = NULL;
491         alphaskytexture = NULL;
492 }
493
494 static void r_sky_newmap(void)
495 {
496 }
497
498 void R_Sky_Init(void)
499 {
500         Cmd_AddCommand ("loadsky", &LoadSky_f);
501         Cvar_RegisterVariable (&r_sky);
502         R_ResetSkyBox();
503         R_ResetQuakeSky();
504         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
505 }
506