implemented occlusion query support on corona rendering, this enables
[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", "enables sky rendering (black otherwise)"};
7 cvar_t r_skyscroll1 = {CVAR_SAVE, "r_skyscroll1", "1", "speed at which upper clouds layer scrolls in quake sky"};
8 cvar_t r_skyscroll2 = {CVAR_SAVE, "r_skyscroll2", "2", "speed at which lower clouds layer scrolls in quake sky"};
9 int skyrendernow;
10 int skyrendermasked;
11
12 static int skyrendersphere;
13 static int skyrenderbox;
14 static rtexturepool_t *skytexturepool;
15 static char skyname[MAX_QPATH];
16
17 typedef struct suffixinfo_s
18 {
19         const char *suffix;
20         qboolean flipx, flipy, flipdiagonal;
21 }
22 suffixinfo_t;
23 static const suffixinfo_t suffix[3][6] =
24 {
25         {
26                 {"px",   false, false, false},
27                 {"nx",   false, false, false},
28                 {"py",   false, false, false},
29                 {"ny",   false, false, false},
30                 {"pz",   false, false, false},
31                 {"nz",   false, false, false}
32         },
33         {
34                 {"posx", false, false, false},
35                 {"negx", false, false, false},
36                 {"posy", false, false, false},
37                 {"negy", false, false, false},
38                 {"posz", false, false, false},
39                 {"negz", false, false, false}
40         },
41         {
42                 {"rt",   false, false,  true},
43                 {"lf",    true,  true,  true},
44                 {"bk",   false,  true, false},
45                 {"ft",    true, false, false},
46                 {"up",   false, false,  true},
47                 {"dn",   false, false,  true}
48         }
49 };
50
51 static rtexture_t *skyboxside[6];
52
53 void R_SkyStartFrame(void)
54 {
55         skyrendernow = false;
56         skyrendersphere = false;
57         skyrenderbox = false;
58         skyrendermasked = false;
59         if (r_sky.integer && !(r_refdef.fogenabled && r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1] < (1.0f / 256.0f)))
60         {
61                 if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
62                         skyrenderbox = true;
63                 else if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.solidskytexture)
64                         skyrendersphere = true;
65                 // for depth-masked sky, render the sky on the first sky surface encountered
66                 skyrendernow = true;
67                 skyrendermasked = true;
68         }
69 }
70
71 /*
72 ==================
73 R_SetSkyBox
74 ==================
75 */
76 void R_UnloadSkyBox(void)
77 {
78         int i;
79         int c = 0;
80         for (i = 0;i < 6;i++)
81         {
82                 if (skyboxside[i])
83                 {
84                         R_FreeTexture(skyboxside[i]);
85                         c++;
86                 }
87                 skyboxside[i] = NULL;
88         }
89         if (c && developer_loading.integer)
90                 Con_Printf("unloading skybox\n");
91 }
92
93 int R_LoadSkyBox(void)
94 {
95         int i, j, success;
96         int indices[4] = {0,1,2,3};
97         char name[MAX_INPUTLINE];
98         unsigned char *image_buffer;
99         unsigned char *temp;
100
101         R_UnloadSkyBox();
102
103         if (!skyname[0])
104                 return true;
105
106         for (j=0; j<3; j++)
107         {
108                 success = 0;
109                 for (i=0; i<6; i++)
110                 {
111                         if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
112                         {
113                                 if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
114                                 {
115                                         if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
116                                         {
117                                                 if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_buffer = loadimagepixelsbgra(name, false, false)))
118                                                         continue;
119                                         }
120                                 }
121                         }
122                         temp = (unsigned char *)Mem_Alloc(tempmempool, image_width*image_height*4);
123                         Image_CopyMux (temp, image_buffer, image_width, image_height, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, indices);
124                         skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, temp, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_PRECACHE | (gl_texturecompression_sky.integer ? TEXF_COMPRESS : 0), NULL);
125                         Mem_Free(image_buffer);
126                         Mem_Free(temp);
127                         success++;
128                 }
129
130                 if (success)
131                         break;
132         }
133
134         if (j == 3)
135                 return false;
136
137         if (developer_loading.integer)
138                 Con_Printf("loading skybox \"%s\"\n", name);
139
140         return true;
141 }
142
143 int R_SetSkyBox(const char *sky)
144 {
145         if (strcmp(sky, skyname) == 0) // no change
146                 return true;
147
148         if (strlen(sky) > 1000)
149         {
150                 Con_Printf("sky name too long (%i, max is 1000)\n", (int)strlen(sky));
151                 return false;
152         }
153
154         strlcpy(skyname, sky, sizeof(skyname));
155
156         return R_LoadSkyBox();
157 }
158
159 // LordHavoc: added LoadSky console command
160 void LoadSky_f (void)
161 {
162         switch (Cmd_Argc())
163         {
164         case 1:
165                 if (skyname[0])
166                         Con_Printf("current sky: %s\n", skyname);
167                 else
168                         Con_Print("no skybox has been set\n");
169                 break;
170         case 2:
171                 if (R_SetSkyBox(Cmd_Argv(1)))
172                 {
173                         if (skyname[0])
174                                 Con_Printf("skybox set to %s\n", skyname);
175                         else
176                                 Con_Print("skybox disabled\n");
177                 }
178                 else
179                         Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
180                 break;
181         default:
182                 Con_Print("usage: loadsky skyname\n");
183                 break;
184         }
185 }
186
187 static const float skyboxvertex3f[6*4*3] =
188 {
189         // skyside[0]
190          16, -16,  16,
191          16, -16, -16,
192          16,  16, -16,
193          16,  16,  16,
194         // skyside[1]
195         -16,  16,  16,
196         -16,  16, -16,
197         -16, -16, -16,
198         -16, -16,  16,
199         // skyside[2]
200          16,  16,  16,
201          16,  16, -16,
202         -16,  16, -16,
203         -16,  16,  16,
204         // skyside[3]
205         -16, -16,  16,
206         -16, -16, -16,
207          16, -16, -16,
208          16, -16,  16,
209         // skyside[4]
210         -16, -16,  16,
211          16, -16,  16,
212          16,  16,  16,
213         -16,  16,  16,
214         // skyside[5]
215          16, -16, -16,
216         -16, -16, -16,
217         -16,  16, -16,
218          16,  16, -16
219 };
220
221 static const float skyboxtexcoord2f[6*4*2] =
222 {
223         // skyside[0]
224         0, 1,
225         1, 1,
226         1, 0,
227         0, 0,
228         // skyside[1]
229         1, 0,
230         0, 0,
231         0, 1,
232         1, 1,
233         // skyside[2]
234         1, 1,
235         1, 0,
236         0, 0,
237         0, 1,
238         // skyside[3]
239         0, 0,
240         0, 1,
241         1, 1,
242         1, 0,
243         // skyside[4]
244         0, 1,
245         1, 1,
246         1, 0,
247         0, 0,
248         // skyside[5]
249         0, 1,
250         1, 1,
251         1, 0,
252         0, 0
253 };
254
255 static const unsigned short skyboxelements[6*2*3] =
256 {
257         // skyside[3]
258          0,  1,  2,
259          0,  2,  3,
260         // skyside[1]
261          4,  5,  6,
262          4,  6,  7,
263         // skyside[0]
264          8,  9, 10,
265          8, 10, 11,
266         // skyside[2]
267         12, 13, 14,
268         12, 14, 15,
269         // skyside[4]
270         16, 17, 18,
271         16, 18, 19,
272         // skyside[5]
273         20, 21, 22,
274         20, 22, 23
275 };
276
277 static void R_SkyBox(void)
278 {
279         int i;
280         // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
281         GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
282         GL_BlendFunc(GL_ONE, GL_ZERO);
283         GL_CullFace(GL_NONE);
284         GL_DepthMask(false);
285         GL_DepthRange(0, 1);
286         GL_PolygonOffset(0, 0);
287         GL_DepthTest(false); // don't modify or read zbuffer
288         R_Mesh_VertexPointer(skyboxvertex3f, 0, 0);
289         R_Mesh_ColorPointer(NULL, 0, 0);
290         R_Mesh_ResetTextureState();
291         R_Mesh_TexCoordPointer(0, 2, skyboxtexcoord2f, 0, 0);
292         R_SetupGenericShader(true);
293         GL_LockArrays(0, 6*4);
294         for (i = 0;i < 6;i++)
295         {
296                 R_Mesh_TexBind(0, R_GetTexture(skyboxside[i]));
297                 R_Mesh_Draw(0, 6*4, i*2, 2, NULL, skyboxelements, 0, 0);
298         }
299
300         if(r_refdef.fogenabled)
301         {
302                 R_SetupGenericShader(false);
303                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
304                 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
305                 for (i = 0;i < 6;i++)
306                 {
307                         R_Mesh_TexBind(0, 0);
308                         R_Mesh_Draw(0, 6*4, i*2, 2, NULL, skyboxelements, 0, 0);
309                 }
310         }
311
312         GL_LockArrays(0, 0);
313 }
314
315 #define skygridx 32
316 #define skygridx1 (skygridx + 1)
317 #define skygridxrecip (1.0f / (skygridx))
318 #define skygridy 32
319 #define skygridy1 (skygridy + 1)
320 #define skygridyrecip (1.0f / (skygridy))
321 #define skysphere_numverts (skygridx1 * skygridy1)
322 #define skysphere_numtriangles (skygridx * skygridy * 2)
323 static float skysphere_vertex3f[skysphere_numverts * 3];
324 static float skysphere_texcoord2f[skysphere_numverts * 2];
325 static unsigned short skysphere_elements[skysphere_numtriangles * 3];
326
327 static void skyspherecalc(void)
328 {
329         int i, j;
330         unsigned short *e;
331         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
332         float dx, dy, dz;
333         dx = 16;
334         dy = 16;
335         dz = 16 / 3;
336         vertex3f = skysphere_vertex3f;
337         texcoord2f = skysphere_texcoord2f;
338         for (j = 0;j <= skygridy;j++)
339         {
340                 a = j * skygridyrecip;
341                 ax = cos(a * M_PI * 2);
342                 ay = -sin(a * M_PI * 2);
343                 for (i = 0;i <= skygridx;i++)
344                 {
345                         b = i * skygridxrecip;
346                         x = cos((b + 0.5) * M_PI);
347                         v[0] = ax*x * dx;
348                         v[1] = ay*x * dy;
349                         v[2] = -sin((b + 0.5) * M_PI) * dz;
350                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
351                         *texcoord2f++ = v[0] * length;
352                         *texcoord2f++ = v[1] * length;
353                         *vertex3f++ = v[0];
354                         *vertex3f++ = v[1];
355                         *vertex3f++ = v[2];
356                 }
357         }
358         e = skysphere_elements;
359         for (j = 0;j < skygridy;j++)
360         {
361                 for (i = 0;i < skygridx;i++)
362                 {
363                         *e++ =  j      * skygridx1 + i;
364                         *e++ =  j      * skygridx1 + i + 1;
365                         *e++ = (j + 1) * skygridx1 + i;
366
367                         *e++ =  j      * skygridx1 + i + 1;
368                         *e++ = (j + 1) * skygridx1 + i + 1;
369                         *e++ = (j + 1) * skygridx1 + i;
370                 }
371         }
372 }
373
374 static void R_SkySphere(void)
375 {
376         float speedscale;
377         static qboolean skysphereinitialized = false;
378         matrix4x4_t scroll1matrix, scroll2matrix;
379         if (!skysphereinitialized)
380         {
381                 skysphereinitialized = true;
382                 skyspherecalc();
383         }
384
385         // wrap the scroll values just to be extra kind to float accuracy
386
387         // scroll speed for upper layer
388         speedscale = r_refdef.scene.time*r_skyscroll1.value*8.0/128.0;
389         speedscale -= (int)speedscale;
390         Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
391         // scroll speed for lower layer (transparent layer)
392         speedscale = r_refdef.scene.time*r_skyscroll2.value*8.0/128.0;
393         speedscale -= (int)speedscale;
394         Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
395
396         // FIXME: fixed function path can't properly handle r_refdef.view.colorscale > 1
397         GL_Color(1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1 * r_refdef.view.colorscale, 1);
398         GL_BlendFunc(GL_ONE, GL_ZERO);
399         GL_CullFace(GL_NONE);
400         GL_DepthMask(true);
401         GL_DepthRange(0, 1);
402         GL_PolygonOffset(0, 0);
403         GL_DepthTest(false); // don't modify or read zbuffer
404         R_Mesh_VertexPointer(skysphere_vertex3f, 0, 0);
405         R_Mesh_ColorPointer(NULL, 0, 0);
406         R_Mesh_ResetTextureState();
407         R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.solidskytexture));
408         R_Mesh_TexCoordPointer(0, 2, skysphere_texcoord2f, 0, 0);
409         R_Mesh_TexMatrix(0, &scroll1matrix);
410         if (r_textureunits.integer >= 2 && r_refdef.view.colorscale == 1)
411         {
412                 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
413                 R_SetupGenericTwoTextureShader(GL_DECAL);
414                 R_Mesh_TexBind(1, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
415                 R_Mesh_TexCoordPointer(1, 2, skysphere_texcoord2f, 0, 0);
416                 R_Mesh_TexMatrix(1, &scroll2matrix);
417                 GL_LockArrays(0, skysphere_numverts);
418                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, NULL, skysphere_elements, 0, 0);
419                 GL_LockArrays(0, 0);
420                 R_Mesh_TexBind(1, 0);
421         }
422         else
423         {
424                 // two pass
425                 R_SetupGenericShader(true);
426                 GL_LockArrays(0, skysphere_numverts);
427                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, NULL, skysphere_elements, 0, 0);
428
429                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
430                 R_Mesh_TexBind(0, R_GetTexture(r_refdef.scene.worldmodel->brush.alphaskytexture));
431                 R_Mesh_TexMatrix(0, &scroll2matrix);
432                 GL_LockArrays(0, skysphere_numverts);
433                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, NULL, skysphere_elements, 0, 0);
434                 GL_LockArrays(0, 0);
435         }
436
437         if(r_refdef.fogenabled)
438         {
439                 R_SetupGenericShader(false);
440                 R_Mesh_TexBind(0, 0);
441                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
442                 GL_Color(r_refdef.fogcolor[0], r_refdef.fogcolor[1], r_refdef.fogcolor[2], 1 - r_refdef.fogmasktable[FOGMASKTABLEWIDTH-1]);
443                 GL_LockArrays(0, skysphere_numverts);
444                 R_Mesh_Draw(0, skysphere_numverts, 0, skysphere_numtriangles, NULL, skysphere_elements, 0, 0);
445                 GL_LockArrays(0, 0);
446         }
447 }
448
449 void R_Sky(void)
450 {
451         matrix4x4_t skymatrix;
452         if (skyrendermasked)
453         {
454                 Matrix4x4_CreateTranslate(&skymatrix, r_refdef.view.origin[0], r_refdef.view.origin[1], r_refdef.view.origin[2]);
455                 R_Mesh_Matrix(&skymatrix);
456                 if (skyrendersphere)
457                 {
458                         // this does not modify depth buffer
459                         R_SkySphere();
460                 }
461                 else if (skyrenderbox)
462                 {
463                         // this does not modify depth buffer
464                         R_SkyBox();
465                 }
466                 /* this will be skyroom someday
467                 else
468                 {
469                         // this modifies the depth buffer so we have to clear it afterward
470                         //R_SkyRoom();
471                         // clear the depthbuffer that was used while rendering the skyroom
472                         //GL_Clear(GL_DEPTH_BUFFER_BIT);
473                 }
474                 */
475                 GL_DepthRange(0, 1);
476                 GL_DepthTest(true);
477                 GL_DepthMask(true);
478         }
479 }
480
481 //===============================================================
482
483 void R_ResetSkyBox(void)
484 {
485         R_UnloadSkyBox();
486         skyname[0] = 0;
487         R_LoadSkyBox();
488 }
489
490 static void r_sky_start(void)
491 {
492         skytexturepool = R_AllocTexturePool();
493         R_LoadSkyBox();
494 }
495
496 static void r_sky_shutdown(void)
497 {
498         R_UnloadSkyBox();
499         R_FreeTexturePool(&skytexturepool);
500 }
501
502 static void r_sky_newmap(void)
503 {
504 }
505
506
507 void R_Sky_Init(void)
508 {
509         Cmd_AddCommand ("loadsky", &LoadSky_f, "load a skybox by basename (for example loadsky mtnsun_ loads mtnsun_ft.tga and so on)");
510         Cvar_RegisterVariable (&r_sky);
511         Cvar_RegisterVariable (&r_skyscroll1);
512         Cvar_RegisterVariable (&r_skyscroll2);
513         memset(&skyboxside, 0, sizeof(skyboxside));
514         skyname[0] = 0;
515         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
516 }
517