fix some warnings I kept forgetting
[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 static void R_SkyBox(void)
120 {
121         rmeshstate_t m;
122
123 #define R_SkyBoxPolyVec(i,s,t,x,y,z) \
124         varray_vertex3f[i * 3 + 0] = (x) * 16.0f;\
125         varray_vertex3f[i * 3 + 1] = (y) * 16.0f;\
126         varray_vertex3f[i * 3 + 2] = (z) * 16.0f;\
127         varray_texcoord2f[0][i * 2 + 0] = (s);\
128         varray_texcoord2f[0][i * 2 + 1] = (t);
129
130         GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
131
132         memset(&m, 0, sizeof(m));
133         m.blendfunc1 = GL_ONE;
134         m.blendfunc2 = GL_ZERO;
135         m.depthdisable = true; // don't modify or read zbuffer
136
137         m.tex[0] = R_GetTexture(skyboxside[3]); // front
138         R_Mesh_State(&m);
139         R_Mesh_GetSpace(4);
140         R_SkyBoxPolyVec(0, 1, 0,  1, -1,  1);
141         R_SkyBoxPolyVec(1, 1, 1,  1, -1, -1);
142         R_SkyBoxPolyVec(2, 0, 1,  1,  1, -1);
143         R_SkyBoxPolyVec(3, 0, 0,  1,  1,  1);
144         R_Mesh_Draw(4, 2, polygonelements);
145
146         m.tex[0] = R_GetTexture(skyboxside[1]); // back
147         R_Mesh_TextureState(&m);
148         R_Mesh_GetSpace(4);
149         R_SkyBoxPolyVec(0, 1, 0, -1,  1,  1);
150         R_SkyBoxPolyVec(1, 1, 1, -1,  1, -1);
151         R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
152         R_SkyBoxPolyVec(3, 0, 0, -1, -1,  1);
153         R_Mesh_Draw(4, 2, polygonelements);
154
155         m.tex[0] = R_GetTexture(skyboxside[0]); // right
156         R_Mesh_TextureState(&m);
157         R_Mesh_GetSpace(4);
158         R_SkyBoxPolyVec(0, 1, 0,  1,  1,  1);
159         R_SkyBoxPolyVec(1, 1, 1,  1,  1, -1);
160         R_SkyBoxPolyVec(2, 0, 1, -1,  1, -1);
161         R_SkyBoxPolyVec(3, 0, 0, -1,  1,  1);
162         R_Mesh_Draw(4, 2, polygonelements);
163
164         m.tex[0] = R_GetTexture(skyboxside[2]); // left
165         R_Mesh_TextureState(&m);
166         R_Mesh_GetSpace(4);
167         R_SkyBoxPolyVec(0, 1, 0, -1, -1,  1);
168         R_SkyBoxPolyVec(1, 1, 1, -1, -1, -1);
169         R_SkyBoxPolyVec(2, 0, 1,  1, -1, -1);
170         R_SkyBoxPolyVec(3, 0, 0,  1, -1,  1);
171         R_Mesh_Draw(4, 2, polygonelements);
172
173         m.tex[0] = R_GetTexture(skyboxside[4]); // up
174         R_Mesh_TextureState(&m);
175         R_Mesh_GetSpace(4);
176         R_SkyBoxPolyVec(0, 1, 0,  1, -1,  1);
177         R_SkyBoxPolyVec(1, 1, 1,  1,  1,  1);
178         R_SkyBoxPolyVec(2, 0, 1, -1,  1,  1);
179         R_SkyBoxPolyVec(3, 0, 0, -1, -1,  1);
180         R_Mesh_Draw(4, 2, polygonelements);
181
182         m.tex[0] = R_GetTexture(skyboxside[5]); // down
183         R_Mesh_TextureState(&m);
184         R_Mesh_GetSpace(4);
185         R_SkyBoxPolyVec(0, 1, 0,  1,  1, -1);
186         R_SkyBoxPolyVec(1, 1, 1,  1, -1, -1);
187         R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
188         R_SkyBoxPolyVec(3, 0, 0, -1,  1, -1);
189         R_Mesh_Draw(4, 2, polygonelements);
190 }
191
192 #define skygridx 32
193 #define skygridx1 (skygridx + 1)
194 #define skygridxrecip (1.0f / (skygridx))
195 #define skygridy 32
196 #define skygridy1 (skygridy + 1)
197 #define skygridyrecip (1.0f / (skygridy))
198 #define skysphere_numverts (skygridx1 * skygridy1)
199 #define skysphere_numtriangles (skygridx * skygridy * 2)
200 static float skysphere_vertex3f[skysphere_numverts * 3];
201 static float skysphere_texcoord2f[skysphere_numverts * 2];
202 static int skysphere_element3i[skysphere_numtriangles * 3];
203
204 static void skyspherecalc(void)
205 {
206         int i, j, *e;
207         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
208         float dx, dy, dz;
209         dx = 16;
210         dy = 16;
211         dz = 16 / 3;
212         vertex3f = skysphere_vertex3f;
213         texcoord2f = skysphere_texcoord2f;
214         for (j = 0;j <= skygridy;j++)
215         {
216                 a = j * skygridyrecip;
217                 ax = cos(a * M_PI * 2);
218                 ay = -sin(a * M_PI * 2);
219                 for (i = 0;i <= skygridx;i++)
220                 {
221                         b = i * skygridxrecip;
222                         x = cos((b + 0.5) * M_PI);
223                         v[0] = ax*x * dx;
224                         v[1] = ay*x * dy;
225                         v[2] = -sin((b + 0.5) * M_PI) * dz;
226                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
227                         *texcoord2f++ = v[0] * length;
228                         *texcoord2f++ = v[1] * length;
229                         *vertex3f++ = v[0];
230                         *vertex3f++ = v[1];
231                         *vertex3f++ = v[2];
232                 }
233         }
234         e = skysphere_element3i;
235         for (j = 0;j < skygridy;j++)
236         {
237                 for (i = 0;i < skygridx;i++)
238                 {
239                         *e++ =  j      * skygridx1 + i;
240                         *e++ =  j      * skygridx1 + i + 1;
241                         *e++ = (j + 1) * skygridx1 + i;
242
243                         *e++ =  j      * skygridx1 + i + 1;
244                         *e++ = (j + 1) * skygridx1 + i + 1;
245                         *e++ = (j + 1) * skygridx1 + i;
246                 }
247                 i++;
248         }
249 }
250
251 static void R_SkySphere(void)
252 {
253         float speedscale;
254         static qboolean skysphereinitialized = false;
255         rmeshstate_t m;
256         rcachearrayrequest_t request;
257         if (!skysphereinitialized)
258         {
259                 skysphereinitialized = true;
260                 skyspherecalc();
261         }
262
263         // scroll speed for upper layer
264         speedscale = cl.time*8.0/128.0;
265         // wrap the scroll just to be extra kind to float accuracy
266         speedscale -= (int)speedscale;
267
268         if (r_colorscale == 1 && r_textureunits.integer >= 2)
269         {
270                 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
271                 // LordHavoc: note that color is not set here because it does not
272                 // matter with GL_REPLACE
273                 memset(&m, 0, sizeof(m));
274                 m.blendfunc1 = GL_ONE;
275                 m.blendfunc2 = GL_ZERO;
276                 m.depthdisable = true; // don't modify or read zbuffer
277                 m.tex[0] = R_GetTexture(solidskytexture);
278                 m.tex[1] = R_GetTexture(alphaskytexture);
279                 m.texcombinergb[0] = GL_REPLACE;
280                 m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
281                 if (gl_mesh_copyarrays.integer)
282                 {
283                         R_Mesh_State(&m);
284                         R_Mesh_GetSpace(skysphere_numverts);
285                         R_Mesh_CopyVertex3f(skysphere_vertex3f, skysphere_numverts);
286                         R_ScrollTexCoord2f(varray_texcoord2f[0], skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
287                         R_ScrollTexCoord2f(varray_texcoord2f[1], skysphere_texcoord2f, skysphere_numverts, speedscale*2, speedscale*2);
288                 }
289                 else
290                 {
291                         m.pointervertexcount = skysphere_numverts;
292                         m.pointer_vertex = skysphere_vertex3f;
293                         memset(&request, 0, sizeof(request));
294                         request.data_size = skysphere_numverts * sizeof(float[2]);
295                         request.id_pointer1 = skysphere_texcoord2f;
296                         request.id_number1 = CRC_Block((void *)&speedscale, sizeof(speedscale));
297                         if (R_Mesh_CacheArray(&request))
298                                 R_ScrollTexCoord2f(request.data, skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
299                         m.pointer_texcoord[0] = request.data;
300                         speedscale *= 2;
301                         request.id_number1 = CRC_Block((void *)&speedscale, sizeof(speedscale));
302                         if (R_Mesh_CacheArray(&request))
303                                 R_ScrollTexCoord2f(request.data, skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
304                         m.pointer_texcoord[1] = request.data;
305                         R_Mesh_State(&m);
306                 }
307                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
308         }
309         else
310         {
311                 // two pass
312                 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
313                 memset(&m, 0, sizeof(m));
314                 m.blendfunc1 = GL_ONE;
315                 m.blendfunc2 = GL_ZERO;
316                 m.depthdisable = true; // don't modify or read zbuffer
317                 m.tex[0] = R_GetTexture(solidskytexture);
318                 if (gl_mesh_copyarrays.integer)
319                 {
320                         R_Mesh_State(&m);
321                         R_Mesh_GetSpace(skysphere_numverts);
322                         R_Mesh_CopyVertex3f(skysphere_vertex3f, skysphere_numverts);
323                         R_ScrollTexCoord2f(varray_texcoord2f[0], skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
324                 }
325                 else
326                 {
327                         m.pointervertexcount = skysphere_numverts;
328                         m.pointer_vertex = skysphere_vertex3f;
329                         memset(&request, 0, sizeof(request));
330                         request.data_size = skysphere_numverts * sizeof(float[2]);
331                         request.id_pointer1 = skysphere_texcoord2f;
332                         request.id_number1 = CRC_Block((void *)&speedscale, sizeof(speedscale));
333                         if (R_Mesh_CacheArray(&request))
334                                 R_ScrollTexCoord2f(request.data, skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
335                         m.pointer_texcoord[0] = request.data;
336                         R_Mesh_State(&m);
337                 }
338                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
339
340                 // scroll the lower cloud layer twice as fast (just like quake did)
341                 speedscale *= 2;
342
343                 m.blendfunc1 = GL_SRC_ALPHA;
344                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
345                 m.tex[0] = R_GetTexture(alphaskytexture);
346                 if (gl_mesh_copyarrays.integer)
347                 {
348                         R_Mesh_State(&m);
349                         R_Mesh_GetSpace(skysphere_numverts);
350                         R_Mesh_CopyVertex3f(skysphere_vertex3f, skysphere_numverts);
351                         R_ScrollTexCoord2f(varray_texcoord2f[0], skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
352                 }
353                 else
354                 {
355                         m.pointervertexcount = skysphere_numverts;
356                         m.pointer_vertex = skysphere_vertex3f;
357                         request.data_size = skysphere_numverts * sizeof(float[2]);
358                         request.id_pointer1 = skysphere_texcoord2f;
359                         request.id_number1 = CRC_Block((void *)&speedscale, sizeof(speedscale));
360                         if (R_Mesh_CacheArray(&request))
361                                 R_ScrollTexCoord2f(request.data, skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
362                         m.pointer_texcoord[0] = request.data;
363                         R_Mesh_State(&m);
364                 }
365                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
366         }
367 }
368
369 void R_Sky(void)
370 {
371         matrix4x4_t skymatrix;
372         if (skyrendermasked)
373         {
374                 Matrix4x4_CreateTranslate(&skymatrix, r_origin[0], r_origin[1], r_origin[2]);
375                 R_Mesh_Matrix(&skymatrix);
376                 if (skyrendersphere)
377                 {
378                         // this does not modify depth buffer
379                         R_SkySphere();
380                 }
381                 else if (skyrenderbox)
382                 {
383                         // this does not modify depth buffer
384                         R_SkyBox();
385                 }
386                 /* this will be skyroom someday
387                 else
388                 {
389                         // this modifies the depth buffer so we have to clear it afterward
390                         //R_SkyRoom();
391                         // clear the depthbuffer that was used while rendering the skyroom
392                         //R_Mesh_EndBatch();
393                         //qglClear(GL_DEPTH_BUFFER_BIT);
394                 }
395                 */
396         }
397 }
398
399 //===============================================================
400
401 /*
402 =============
403 R_InitSky
404
405 A sky texture is 256*128, with the right side being a masked overlay
406 ==============
407 */
408 void R_InitSky (qbyte *src, int bytesperpixel)
409 {
410         int i, j, p, r, g, b;
411         qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
412         unsigned trans[128*128], transpix, *rgba;
413
414         skyavailable_quake = true;
415
416         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
417         R_FreeTexturePool(&skytexturepool);
418         skytexturepool = R_AllocTexturePool();
419         solidskytexture = NULL;
420         alphaskytexture = NULL;
421
422         if (bytesperpixel == 4)
423         {
424                 for (i = 0;i < 128;i++)
425                         for (j = 0;j < 128;j++)
426                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
427         }
428         else
429         {
430                 // make an average value for the back to avoid
431                 // a fringe on the top level
432                 r = g = b = 0;
433                 for (i=0 ; i<128 ; i++)
434                 {
435                         for (j=0 ; j<128 ; j++)
436                         {
437                                 p = src[i*256 + j + 128];
438                                 rgba = &palette_complete[p];
439                                 trans[(i*128) + j] = *rgba;
440                                 r += ((qbyte *)rgba)[0];
441                                 g += ((qbyte *)rgba)[1];
442                                 b += ((qbyte *)rgba)[2];
443                         }
444                 }
445
446                 ((qbyte *)&transpix)[0] = r/(128*128);
447                 ((qbyte *)&transpix)[1] = g/(128*128);
448                 ((qbyte *)&transpix)[2] = b/(128*128);
449                 ((qbyte *)&transpix)[3] = 0;
450         }
451
452         memcpy(skyupperlayerpixels, trans, 128*128*4);
453
454         solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
455
456         if (bytesperpixel == 4)
457         {
458                 for (i = 0;i < 128;i++)
459                         for (j = 0;j < 128;j++)
460                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j];
461         }
462         else
463         {
464                 for (i=0 ; i<128 ; i++)
465                 {
466                         for (j=0 ; j<128 ; j++)
467                         {
468                                 p = src[i*256 + j];
469                                 if (p == 0)
470                                         trans[(i*128) + j] = transpix;
471                                 else
472                                         trans[(i*128) + j] = palette_complete[p];
473                         }
474                 }
475         }
476
477         memcpy(skylowerlayerpixels, trans, 128*128*4);
478
479         alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
480 }
481
482 void R_ResetQuakeSky(void)
483 {
484         skyavailable_quake = false;
485 }
486
487 void R_ResetSkyBox(void)
488 {
489         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
490         skyname[0] = 0;
491         skyavailable_box = false;
492 }
493
494 static void r_sky_start(void)
495 {
496         skytexturepool = R_AllocTexturePool();
497         solidskytexture = NULL;
498         alphaskytexture = NULL;
499 }
500
501 static void r_sky_shutdown(void)
502 {
503         R_FreeTexturePool(&skytexturepool);
504         solidskytexture = NULL;
505         alphaskytexture = NULL;
506 }
507
508 static void r_sky_newmap(void)
509 {
510 }
511
512 void R_Sky_Init(void)
513 {
514         Cmd_AddCommand ("loadsky", &LoadSky_f);
515         Cvar_RegisterVariable (&r_sky);
516         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
517 }