added R_ScrollTexCoord2f (copies while texture coordinates with an offset)
[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         int i;
254         float speedscale, *out2f;
255         const float *in2f;
256         static qboolean skysphereinitialized = false;
257         rmeshstate_t m;
258         rcachearrayrequest_t request;
259         if (!skysphereinitialized)
260         {
261                 skysphereinitialized = true;
262                 skyspherecalc();
263         }
264
265         // scroll speed for upper layer
266         speedscale = cl.time*8.0/128.0;
267         // wrap the scroll just to be extra kind to float accuracy
268         speedscale -= (int)speedscale;
269
270         if (r_colorscale == 1 && r_textureunits.integer >= 2)
271         {
272                 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
273                 // LordHavoc: note that color is not set here because it does not
274                 // matter with GL_REPLACE
275                 memset(&m, 0, sizeof(m));
276                 m.blendfunc1 = GL_ONE;
277                 m.blendfunc2 = GL_ZERO;
278                 m.depthdisable = true; // don't modify or read zbuffer
279                 m.tex[0] = R_GetTexture(solidskytexture);
280                 m.tex[1] = R_GetTexture(alphaskytexture);
281                 m.texcombinergb[0] = GL_REPLACE;
282                 m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
283                 if (gl_mesh_copyarrays.integer)
284                 {
285                         R_Mesh_State(&m);
286                         R_Mesh_GetSpace(skysphere_numverts);
287                         R_Mesh_CopyVertex3f(skysphere_vertex3f, skysphere_numverts);
288                         R_ScrollTexCoord2f(varray_texcoord2f[0], skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
289                         R_ScrollTexCoord2f(varray_texcoord2f[1], skysphere_texcoord2f, skysphere_numverts, speedscale*2, speedscale*2);
290                 }
291                 else
292                 {
293                         m.pointervertexcount = skysphere_numverts;
294                         m.pointer_vertex = skysphere_vertex3f;
295                         memset(&request, 0, sizeof(request));
296                         request.data_size = skysphere_numverts * sizeof(float[2]);
297                         request.id_pointer1 = skysphere_texcoord2f;
298                         request.id_number1 = CRC_Block((void *)&speedscale, sizeof(speedscale));
299                         if (R_Mesh_CacheArray(&request))
300                                 R_ScrollTexCoord2f(request.data, skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
301                         m.pointer_texcoord[0] = request.data;
302                         speedscale *= 2;
303                         request.id_number1 = CRC_Block((void *)&speedscale, sizeof(speedscale));
304                         if (R_Mesh_CacheArray(&request))
305                                 R_ScrollTexCoord2f(request.data, skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
306                         m.pointer_texcoord[1] = request.data;
307                         R_Mesh_State(&m);
308                 }
309                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
310         }
311         else
312         {
313                 // two pass
314                 GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
315                 memset(&m, 0, sizeof(m));
316                 m.blendfunc1 = GL_ONE;
317                 m.blendfunc2 = GL_ZERO;
318                 m.depthdisable = true; // don't modify or read zbuffer
319                 m.tex[0] = R_GetTexture(solidskytexture);
320                 if (gl_mesh_copyarrays.integer)
321                 {
322                         R_Mesh_State(&m);
323                         R_Mesh_GetSpace(skysphere_numverts);
324                         R_Mesh_CopyVertex3f(skysphere_vertex3f, skysphere_numverts);
325                         R_ScrollTexCoord2f(varray_texcoord2f[0], skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
326                 }
327                 else
328                 {
329                         m.pointervertexcount = skysphere_numverts;
330                         m.pointer_vertex = skysphere_vertex3f;
331                         memset(&request, 0, sizeof(request));
332                         request.data_size = skysphere_numverts * sizeof(float[2]);
333                         request.id_pointer1 = skysphere_texcoord2f;
334                         request.id_number1 = CRC_Block((void *)&speedscale, sizeof(speedscale));
335                         if (R_Mesh_CacheArray(&request))
336                                 R_ScrollTexCoord2f(request.data, skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
337                         m.pointer_texcoord[0] = request.data;
338                         R_Mesh_State(&m);
339                 }
340                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
341
342                 // scroll the lower cloud layer twice as fast (just like quake did)
343                 speedscale *= 2;
344
345                 m.blendfunc1 = GL_SRC_ALPHA;
346                 m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
347                 m.tex[0] = R_GetTexture(alphaskytexture);
348                 if (gl_mesh_copyarrays.integer)
349                 {
350                         R_Mesh_State(&m);
351                         R_Mesh_GetSpace(skysphere_numverts);
352                         R_Mesh_CopyVertex3f(skysphere_vertex3f, skysphere_numverts);
353                         R_ScrollTexCoord2f(varray_texcoord2f[0], skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
354                 }
355                 else
356                 {
357                         m.pointervertexcount = skysphere_numverts;
358                         m.pointer_vertex = skysphere_vertex3f;
359                         request.data_size = skysphere_numverts * sizeof(float[2]);
360                         request.id_pointer1 = skysphere_texcoord2f;
361                         request.id_number1 = CRC_Block((void *)&speedscale, sizeof(speedscale));
362                         if (R_Mesh_CacheArray(&request))
363                                 R_ScrollTexCoord2f(request.data, skysphere_texcoord2f, skysphere_numverts, speedscale, speedscale);
364                         m.pointer_texcoord[0] = request.data;
365                         R_Mesh_State(&m);
366                 }
367                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
368         }
369 }
370
371 void R_Sky(void)
372 {
373         matrix4x4_t skymatrix;
374         if (skyrendermasked)
375         {
376                 Matrix4x4_CreateTranslate(&skymatrix, r_origin[0], r_origin[1], r_origin[2]);
377                 R_Mesh_Matrix(&skymatrix);
378                 if (skyrendersphere)
379                 {
380                         // this does not modify depth buffer
381                         R_SkySphere();
382                 }
383                 else if (skyrenderbox)
384                 {
385                         // this does not modify depth buffer
386                         R_SkyBox();
387                 }
388                 /* this will be skyroom someday
389                 else
390                 {
391                         // this modifies the depth buffer so we have to clear it afterward
392                         //R_SkyRoom();
393                         // clear the depthbuffer that was used while rendering the skyroom
394                         //R_Mesh_EndBatch();
395                         //qglClear(GL_DEPTH_BUFFER_BIT);
396                 }
397                 */
398         }
399 }
400
401 //===============================================================
402
403 /*
404 =============
405 R_InitSky
406
407 A sky texture is 256*128, with the right side being a masked overlay
408 ==============
409 */
410 void R_InitSky (qbyte *src, int bytesperpixel)
411 {
412         int i, j, p, r, g, b;
413         qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
414         unsigned trans[128*128], transpix, *rgba;
415
416         skyavailable_quake = true;
417
418         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
419         R_FreeTexturePool(&skytexturepool);
420         skytexturepool = R_AllocTexturePool();
421         solidskytexture = NULL;
422         alphaskytexture = NULL;
423
424         if (bytesperpixel == 4)
425         {
426                 for (i = 0;i < 128;i++)
427                         for (j = 0;j < 128;j++)
428                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
429         }
430         else
431         {
432                 // make an average value for the back to avoid
433                 // a fringe on the top level
434                 r = g = b = 0;
435                 for (i=0 ; i<128 ; i++)
436                 {
437                         for (j=0 ; j<128 ; j++)
438                         {
439                                 p = src[i*256 + j + 128];
440                                 rgba = &palette_complete[p];
441                                 trans[(i*128) + j] = *rgba;
442                                 r += ((qbyte *)rgba)[0];
443                                 g += ((qbyte *)rgba)[1];
444                                 b += ((qbyte *)rgba)[2];
445                         }
446                 }
447
448                 ((qbyte *)&transpix)[0] = r/(128*128);
449                 ((qbyte *)&transpix)[1] = g/(128*128);
450                 ((qbyte *)&transpix)[2] = b/(128*128);
451                 ((qbyte *)&transpix)[3] = 0;
452         }
453
454         memcpy(skyupperlayerpixels, trans, 128*128*4);
455
456         solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
457
458         if (bytesperpixel == 4)
459         {
460                 for (i = 0;i < 128;i++)
461                         for (j = 0;j < 128;j++)
462                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j];
463         }
464         else
465         {
466                 for (i=0 ; i<128 ; i++)
467                 {
468                         for (j=0 ; j<128 ; j++)
469                         {
470                                 p = src[i*256 + j];
471                                 if (p == 0)
472                                         trans[(i*128) + j] = transpix;
473                                 else
474                                         trans[(i*128) + j] = palette_complete[p];
475                         }
476                 }
477         }
478
479         memcpy(skylowerlayerpixels, trans, 128*128*4);
480
481         alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
482 }
483
484 void R_ResetQuakeSky(void)
485 {
486         skyavailable_quake = false;
487 }
488
489 void R_ResetSkyBox(void)
490 {
491         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
492         skyname[0] = 0;
493         skyavailable_box = false;
494 }
495
496 static void r_sky_start(void)
497 {
498         skytexturepool = R_AllocTexturePool();
499         solidskytexture = NULL;
500         alphaskytexture = NULL;
501 }
502
503 static void r_sky_shutdown(void)
504 {
505         R_FreeTexturePool(&skytexturepool);
506         solidskytexture = NULL;
507         alphaskytexture = NULL;
508 }
509
510 static void r_sky_newmap(void)
511 {
512 }
513
514 void R_Sky_Init(void)
515 {
516         Cmd_AddCommand ("loadsky", &LoadSky_f);
517         Cvar_RegisterVariable (&r_sky);
518         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
519 }