most of Q2's keyboard handling ported over - what this means: keypad is now separatel...
[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_vertex[i * 4 + 0] = (x) * 16.0f;\
125         varray_vertex[i * 4 + 1] = (y) * 16.0f;\
126         varray_vertex[i * 4 + 2] = (z) * 16.0f;\
127         varray_texcoord[0][i * 4 + 0] = (s);\
128         varray_texcoord[0][i * 4 + 1] = (t);
129
130         memset(&m, 0, sizeof(m));
131         m.blendfunc1 = GL_ONE;
132         m.blendfunc2 = GL_ZERO;
133         m.depthdisable = true; // don't modify or read zbuffer
134         m.tex[0] = R_GetTexture(skyboxside[3]); // front
135         R_Mesh_State(&m);
136
137         GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
138         
139         R_SkyBoxPolyVec(0, 1, 0,  1, -1,  1);
140         R_SkyBoxPolyVec(1, 1, 1,  1, -1, -1);
141         R_SkyBoxPolyVec(2, 0, 1,  1,  1, -1);
142         R_SkyBoxPolyVec(3, 0, 0,  1,  1,  1);
143         R_Mesh_Draw(4, 2, polygonelements);
144         m.tex[0] = R_GetTexture(skyboxside[1]); // back
145         R_Mesh_State(&m);
146         R_SkyBoxPolyVec(0, 1, 0, -1,  1,  1);
147         R_SkyBoxPolyVec(1, 1, 1, -1,  1, -1);
148         R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
149         R_SkyBoxPolyVec(3, 0, 0, -1, -1,  1);
150         R_Mesh_Draw(4, 2, polygonelements);
151         m.tex[0] = R_GetTexture(skyboxside[0]); // right
152         R_Mesh_State(&m);
153         R_SkyBoxPolyVec(0, 1, 0,  1,  1,  1);
154         R_SkyBoxPolyVec(1, 1, 1,  1,  1, -1);
155         R_SkyBoxPolyVec(2, 0, 1, -1,  1, -1);
156         R_SkyBoxPolyVec(3, 0, 0, -1,  1,  1);
157         R_Mesh_Draw(4, 2, polygonelements);
158         m.tex[0] = R_GetTexture(skyboxside[2]); // left
159         R_Mesh_State(&m);
160         R_SkyBoxPolyVec(0, 1, 0, -1, -1,  1);
161         R_SkyBoxPolyVec(1, 1, 1, -1, -1, -1);
162         R_SkyBoxPolyVec(2, 0, 1,  1, -1, -1);
163         R_SkyBoxPolyVec(3, 0, 0,  1, -1,  1);
164         R_Mesh_Draw(4, 2, polygonelements);
165         m.tex[0] = R_GetTexture(skyboxside[4]); // up
166         R_Mesh_State(&m);
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         m.tex[0] = R_GetTexture(skyboxside[5]); // down
173         R_Mesh_State(&m);
174         R_SkyBoxPolyVec(0, 1, 0,  1,  1, -1);
175         R_SkyBoxPolyVec(1, 1, 1,  1, -1, -1);
176         R_SkyBoxPolyVec(2, 0, 1, -1, -1, -1);
177         R_SkyBoxPolyVec(3, 0, 0, -1,  1, -1);
178         R_Mesh_Draw(4, 2, polygonelements);
179 }
180
181 #define skygridx 32
182 #define skygridx1 (skygridx + 1)
183 #define skygridxrecip (1.0f / (skygridx))
184 #define skygridy 32
185 #define skygridy1 (skygridy + 1)
186 #define skygridyrecip (1.0f / (skygridy))
187 #define skysphere_numverts (skygridx1 * skygridy1)
188 #define skysphere_numtriangles (skygridx * skygridy * 2)
189 static float skysphere_vertex[skysphere_numverts * 4];
190 static float skysphere_texcoord[skysphere_numverts * 4];
191 static int skysphere_elements[skysphere_numtriangles * 3];
192
193 static void skyspherecalc(void)
194 {
195         int i, j, *e;
196         float a, b, x, ax, ay, v[3], length, *vertex, *texcoord;
197         float dx, dy, dz;
198         dx = 16;
199         dy = 16;
200         dz = 16 / 3;
201         vertex = skysphere_vertex;
202         texcoord = skysphere_texcoord;
203         for (j = 0;j <= skygridy;j++)
204         {
205                 a = j * skygridyrecip;
206                 ax = cos(a * M_PI * 2);
207                 ay = -sin(a * M_PI * 2);
208                 for (i = 0;i <= skygridx;i++)
209                 {
210                         b = i * skygridxrecip;
211                         x = cos((b + 0.5) * M_PI);
212                         v[0] = ax*x * dx;
213                         v[1] = ay*x * dy;
214                         v[2] = -sin((b + 0.5) * M_PI) * dz;
215                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
216                         *texcoord++ = v[0] * length;
217                         *texcoord++ = v[1] * length;
218                         *texcoord++ = 0;
219                         *texcoord++ = 0;
220                         *vertex++ = v[0];
221                         *vertex++ = v[1];
222                         *vertex++ = v[2];
223                         *vertex++ = 1;
224                 }
225         }
226         e = skysphere_elements;
227         for (j = 0;j < skygridy;j++)
228         {
229                 for (i = 0;i < skygridx;i++)
230                 {
231                         *e++ =  j      * skygridx1 + i;
232                         *e++ =  j      * skygridx1 + i + 1;
233                         *e++ = (j + 1) * skygridx1 + i;
234
235                         *e++ =  j      * skygridx1 + i + 1;
236                         *e++ = (j + 1) * skygridx1 + i + 1;
237                         *e++ = (j + 1) * skygridx1 + i;
238                 }
239                 i++;
240         }
241 }
242
243 static void R_SkySphere(void)
244 {
245         int i;
246         float speedscale, *t;
247         static qboolean skysphereinitialized = false;
248         rmeshstate_t m;
249         if (!skysphereinitialized)
250         {
251                 skysphereinitialized = true;
252                 skyspherecalc();
253         }
254
255         // scroll speed for upper layer
256         speedscale = cl.time*8.0/128.0;
257         // wrap the scroll just to be extra kind to float accuracy
258         speedscale -= (int)speedscale;
259
260         R_Mesh_ResizeCheck(skysphere_numverts);
261
262         memset(&m, 0, sizeof(m));
263         m.blendfunc1 = GL_ONE;
264         m.blendfunc2 = GL_ZERO;
265         m.depthdisable = true; // don't modify or read zbuffer
266         m.tex[0] = R_GetTexture(solidskytexture);
267         R_Mesh_State(&m);
268
269         GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
270
271         memcpy(varray_vertex, skysphere_vertex, skysphere_numverts * sizeof(float[4]));
272         memcpy(varray_texcoord[0], skysphere_texcoord, skysphere_numverts * sizeof(float[4]));
273         for (i = 0, t = varray_texcoord[0];i < skysphere_numverts;i++, t += 4)
274         {
275                 t[0] += speedscale;
276                 t[1] += speedscale;
277         }
278         R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_elements);
279
280         m.blendfunc1 = GL_SRC_ALPHA;
281         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
282         m.tex[0] = R_GetTexture(alphaskytexture);
283         R_Mesh_State(&m);
284
285         // scroll it again, this makes the lower cloud layer scroll twice as fast (just like quake did)
286         for (i = 0, t = varray_texcoord[0];i < skysphere_numverts;i++, t += 4)
287         {
288                 t[0] += speedscale;
289                 t[1] += speedscale;
290         }
291         R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_elements);
292 }
293
294 void R_Sky(void)
295 {
296         matrix4x4_t skymatrix;
297         if (skyrendermasked)
298         {
299                 Matrix4x4_CreateTranslate(&skymatrix, r_origin[0], r_origin[1], r_origin[2]);
300                 R_Mesh_Matrix(&skymatrix);
301                 if (skyrendersphere)
302                 {
303                         // this does not modify depth buffer
304                         R_SkySphere();
305                 }
306                 else if (skyrenderbox)
307                 {
308                         // this does not modify depth buffer
309                         R_SkyBox();
310                 }
311                 /* this will be skyroom someday
312                 else
313                 {
314                         // this modifies the depth buffer so we have to clear it afterward
315                         //R_SkyRoom();
316                         // clear the depthbuffer that was used while rendering the skyroom
317                         //qglClear(GL_DEPTH_BUFFER_BIT);
318                 }
319                 */
320         }
321 }
322
323 //===============================================================
324
325 /*
326 =============
327 R_InitSky
328
329 A sky texture is 256*128, with the right side being a masked overlay
330 ==============
331 */
332 void R_InitSky (qbyte *src, int bytesperpixel)
333 {
334         int i, j, p, r, g, b;
335         qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
336         unsigned trans[128*128], transpix, *rgba;
337
338         skyavailable_quake = true;
339
340         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
341         R_FreeTexturePool(&skytexturepool);
342         skytexturepool = R_AllocTexturePool();
343         solidskytexture = NULL;
344         alphaskytexture = NULL;
345
346         if (bytesperpixel == 4)
347         {
348                 for (i = 0;i < 128;i++)
349                         for (j = 0;j < 128;j++)
350                                 trans[(i*128) + j] = src[i*256+j+128];
351         }
352         else
353         {
354                 // make an average value for the back to avoid
355                 // a fringe on the top level
356                 r = g = b = 0;
357                 for (i=0 ; i<128 ; i++)
358                 {
359                         for (j=0 ; j<128 ; j++)
360                         {
361                                 p = src[i*256 + j + 128];
362                                 rgba = &palette_complete[p];
363                                 trans[(i*128) + j] = *rgba;
364                                 r += ((qbyte *)rgba)[0];
365                                 g += ((qbyte *)rgba)[1];
366                                 b += ((qbyte *)rgba)[2];
367                         }
368                 }
369
370                 ((qbyte *)&transpix)[0] = r/(128*128);
371                 ((qbyte *)&transpix)[1] = g/(128*128);
372                 ((qbyte *)&transpix)[2] = b/(128*128);
373                 ((qbyte *)&transpix)[3] = 0;
374         }
375
376         memcpy(skyupperlayerpixels, trans, 128*128*4);
377
378         solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
379
380         if (bytesperpixel == 4)
381         {
382                 for (i = 0;i < 128;i++)
383                         for (j = 0;j < 128;j++)
384                                 trans[(i*128) + j] = src[i*256+j];
385         }
386         else
387         {
388                 for (i=0 ; i<128 ; i++)
389                 {
390                         for (j=0 ; j<128 ; j++)
391                         {
392                                 p = src[i*256 + j];
393                                 if (p == 0)
394                                         trans[(i*128) + j] = transpix;
395                                 else
396                                         trans[(i*128) + j] = palette_complete[p];
397                         }
398                 }
399         }
400
401         memcpy(skylowerlayerpixels, trans, 128*128*4);
402
403         alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
404 }
405
406 void R_ResetQuakeSky(void)
407 {
408         skyavailable_quake = false;
409 }
410
411 void R_ResetSkyBox(void)
412 {
413         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
414         skyname[0] = 0;
415         skyavailable_box = false;
416 }
417
418 static void r_sky_start(void)
419 {
420         skytexturepool = R_AllocTexturePool();
421         solidskytexture = NULL;
422         alphaskytexture = NULL;
423 }
424
425 static void r_sky_shutdown(void)
426 {
427         R_FreeTexturePool(&skytexturepool);
428         solidskytexture = NULL;
429         alphaskytexture = NULL;
430 }
431
432 static void r_sky_newmap(void)
433 {
434 }
435
436 void R_Sky_Init(void)
437 {
438         Cmd_AddCommand ("loadsky", &LoadSky_f);
439         Cvar_RegisterVariable (&r_sky);
440         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
441 }