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