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