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