fix skybox loading, _ft and _bk were swapped (and all others had to be flipped accord...
[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 cvar_t r_skyscroll1 = {CVAR_SAVE, "r_skyscroll1", "1"};
8 cvar_t r_skyscroll2 = {CVAR_SAVE, "r_skyscroll2", "2"};
9 int skyrendernow;
10 int skyrendermasked;
11
12 static int skyrendersphere;
13 static int skyrenderbox;
14 static rtexturepool_t *skytexturepool;
15 static char skyname[256];
16
17 typedef struct suffixinfo_s
18 {
19         char *suffix;
20         qboolean flipx, flipy, flipdiagonal;
21 }
22 suffixinfo_t;
23 static 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 && !fogenabled)
60         {
61                 if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
62                         skyrenderbox = true;
63                 else if (cl.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         for (i = 0;i < 6;i++)
80         {
81                 if (skyboxside[i])
82                         R_FreeTexture(skyboxside[i]);
83                 skyboxside[i] = NULL;
84         }
85 }
86
87 void R_LoadSkyBox(void)
88 {
89         int i, j, success;
90         int indices[4] = {0,1,2,3};
91         char name[1024];
92         qbyte *image_rgba;
93         qbyte *temp;
94
95         R_UnloadSkyBox();
96
97         if (!skyname[0])
98                 return;
99
100         for (j=0; j<3; j++)
101         {
102                 success = 0;
103                 for (i=0; i<6; i++)
104                 {
105                         if (snprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
106                         {
107                                 if (snprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
108                                 {
109                                         if (snprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
110                                         {
111                                                 if (snprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
112                                                         continue;
113                                         }
114                                 }
115                         }
116                         temp = Mem_Alloc(tempmempool, image_width*image_height*4);
117                         Image_CopyMux (temp, image_rgba, image_width, image_height, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, indices);
118                         skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, temp, TEXTYPE_RGBA, TEXF_CLAMP | TEXF_PRECACHE, NULL);
119                         Mem_Free(image_rgba);
120                         Mem_Free(temp);
121                         success++;
122                 }
123
124                 if (success)
125                         break;
126         }
127
128         if (j == 3)
129                 Con_Printf ("Failed to load %s as skybox\n", skyname);
130 }
131
132 int R_SetSkyBox(const char *sky)
133 {
134         if (strcmp(sky, skyname) == 0) // no change
135                 return true;
136
137         if (strlen(sky) > 1000)
138         {
139                 Con_Printf("sky name too long (%i, max is 1000)\n", strlen(sky));
140                 return false;
141         }
142
143         strcpy(skyname, sky);
144
145         R_LoadSkyBox();
146
147         if (!skyname[0])
148                 return true;
149         return false;
150 }
151
152 // LordHavoc: added LoadSky console command
153 void LoadSky_f (void)
154 {
155         switch (Cmd_Argc())
156         {
157         case 1:
158                 if (skyname[0])
159                         Con_Printf("current sky: %s\n", skyname);
160                 else
161                         Con_Print("no skybox has been set\n");
162                 break;
163         case 2:
164                 if (R_SetSkyBox(Cmd_Argv(1)))
165                 {
166                         if (skyname[0])
167                                 Con_Printf("skybox set to %s\n", skyname);
168                         else
169                                 Con_Print("skybox disabled\n");
170                 }
171                 else
172                         Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
173                 break;
174         default:
175                 Con_Print("usage: loadsky skyname\n");
176                 break;
177         }
178 }
179
180 float skyboxvertex3f[6*4*3] =
181 {
182         // skyside[0]
183          16, -16,  16,
184          16, -16, -16,
185          16,  16, -16,
186          16,  16,  16,
187         // skyside[1]
188         -16,  16,  16,
189         -16,  16, -16,
190         -16, -16, -16,
191         -16, -16,  16,
192         // skyside[2]
193          16,  16,  16,
194          16,  16, -16,
195         -16,  16, -16,
196         -16,  16,  16,
197         // skyside[3]
198         -16, -16,  16,
199         -16, -16, -16,
200          16, -16, -16,
201          16, -16,  16,
202         // skyside[4]
203         -16, -16,  16,
204          16, -16,  16,
205          16,  16,  16,
206         -16,  16,  16,
207         // skyside[5]
208          16, -16, -16,
209         -16, -16, -16,
210         -16,  16, -16,
211          16,  16, -16
212 };
213
214 float skyboxtexcoord2f[6*4*2] =
215 {
216         // skyside[0]
217         0, 1,
218         1, 1,
219         1, 0,
220         0, 0,
221         // skyside[1]
222         1, 0,
223         0, 0,
224         0, 1,
225         1, 1,
226         // skyside[2]
227         1, 1,
228         1, 0,
229         0, 0,
230         0, 1,
231         // skyside[3]
232         0, 0,
233         0, 1,
234         1, 1,
235         1, 0,
236         // skyside[4]
237         0, 1,
238         1, 1,
239         1, 0,
240         0, 0,
241         // skyside[5]
242         0, 1,
243         1, 1,
244         1, 0,
245         0, 0
246 };
247
248 int skyboxelements[6*2*3] =
249 {
250         // skyside[3]
251          0,  1,  2,
252          0,  2,  3,
253         // skyside[1]
254          4,  5,  6,
255          4,  6,  7,
256         // skyside[0]
257          8,  9, 10,
258          8, 10, 11,
259         // skyside[2]
260         12, 13, 14,
261         12, 14, 15,
262         // skyside[4]
263         16, 17, 18,
264         16, 18, 19,
265         // skyside[5]
266         20, 21, 22,
267         20, 22, 23
268 };
269
270 static void R_SkyBox(void)
271 {
272         int i;
273         rmeshstate_t m;
274         GL_Color(1, 1, 1, 1);
275         memset(&m, 0, sizeof(m));
276         GL_BlendFunc(GL_ONE, GL_ZERO);
277         GL_DepthMask(false);
278         GL_DepthTest(false); // don't modify or read zbuffer
279         m.pointer_vertex = skyboxvertex3f;
280         m.pointer_texcoord[0] = skyboxtexcoord2f;
281         GL_LockArrays(0, 6*4);
282         for (i = 0;i < 6;i++)
283         {
284                 m.tex[0] = R_GetTexture(skyboxside[i]);
285                 R_Mesh_State(&m);
286                 R_Mesh_Draw(6*4, 2, skyboxelements + i * 6);
287         }
288         GL_LockArrays(0, 0);
289 }
290
291 #define skygridx 32
292 #define skygridx1 (skygridx + 1)
293 #define skygridxrecip (1.0f / (skygridx))
294 #define skygridy 32
295 #define skygridy1 (skygridy + 1)
296 #define skygridyrecip (1.0f / (skygridy))
297 #define skysphere_numverts (skygridx1 * skygridy1)
298 #define skysphere_numtriangles (skygridx * skygridy * 2)
299 static float skysphere_vertex3f[skysphere_numverts * 3];
300 static float skysphere_texcoord2f[skysphere_numverts * 2];
301 static int skysphere_element3i[skysphere_numtriangles * 3];
302
303 static void skyspherecalc(void)
304 {
305         int i, j, *e;
306         float a, b, x, ax, ay, v[3], length, *vertex3f, *texcoord2f;
307         float dx, dy, dz;
308         dx = 16;
309         dy = 16;
310         dz = 16 / 3;
311         vertex3f = skysphere_vertex3f;
312         texcoord2f = skysphere_texcoord2f;
313         for (j = 0;j <= skygridy;j++)
314         {
315                 a = j * skygridyrecip;
316                 ax = cos(a * M_PI * 2);
317                 ay = -sin(a * M_PI * 2);
318                 for (i = 0;i <= skygridx;i++)
319                 {
320                         b = i * skygridxrecip;
321                         x = cos((b + 0.5) * M_PI);
322                         v[0] = ax*x * dx;
323                         v[1] = ay*x * dy;
324                         v[2] = -sin((b + 0.5) * M_PI) * dz;
325                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
326                         *texcoord2f++ = v[0] * length;
327                         *texcoord2f++ = v[1] * length;
328                         *vertex3f++ = v[0];
329                         *vertex3f++ = v[1];
330                         *vertex3f++ = v[2];
331                 }
332         }
333         e = skysphere_element3i;
334         for (j = 0;j < skygridy;j++)
335         {
336                 for (i = 0;i < skygridx;i++)
337                 {
338                         *e++ =  j      * skygridx1 + i;
339                         *e++ =  j      * skygridx1 + i + 1;
340                         *e++ = (j + 1) * skygridx1 + i;
341
342                         *e++ =  j      * skygridx1 + i + 1;
343                         *e++ = (j + 1) * skygridx1 + i + 1;
344                         *e++ = (j + 1) * skygridx1 + i;
345                 }
346         }
347 }
348
349 static void R_SkySphere(void)
350 {
351         float speedscale;
352         static qboolean skysphereinitialized = false;
353         rmeshstate_t m;
354         matrix4x4_t scroll1matrix, scroll2matrix;
355         if (!skysphereinitialized)
356         {
357                 skysphereinitialized = true;
358                 skyspherecalc();
359         }
360
361         // wrap the scroll values just to be extra kind to float accuracy
362
363         // scroll speed for upper layer
364         speedscale = cl.time*r_skyscroll1.value*8.0/128.0;
365         speedscale -= (int)speedscale;
366         Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
367         // scroll speed for lower layer (transparent layer)
368         speedscale = cl.time*r_skyscroll2.value*8.0/128.0;
369         speedscale -= (int)speedscale;
370         Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
371
372         GL_Color(1, 1, 1, 1);
373         GL_BlendFunc(GL_ONE, GL_ZERO);
374         GL_DepthMask(true);
375         GL_DepthTest(false); // don't modify or read zbuffer
376         memset(&m, 0, sizeof(m));
377         m.pointer_vertex = skysphere_vertex3f;
378         m.tex[0] = R_GetTexture(cl.worldmodel->brush.solidskytexture);
379         m.pointer_texcoord[0] = skysphere_texcoord2f;
380         m.texmatrix[0] = scroll1matrix;
381         if (r_textureunits.integer >= 2)
382         {
383                 // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
384                 m.tex[1] = R_GetTexture(cl.worldmodel->brush.alphaskytexture);
385                 m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
386                 m.pointer_texcoord[1] = skysphere_texcoord2f;
387                 m.texmatrix[1] = scroll2matrix;
388                 R_Mesh_State(&m);
389                 GL_LockArrays(0, skysphere_numverts);
390                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
391                 GL_LockArrays(0, 0);
392         }
393         else
394         {
395                 // two pass
396                 R_Mesh_State(&m);
397                 GL_LockArrays(0, skysphere_numverts);
398                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
399                 GL_LockArrays(0, 0);
400
401                 GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
402                 m.tex[0] = R_GetTexture(cl.worldmodel->brush.alphaskytexture);
403                 m.texmatrix[0] = scroll2matrix;
404                 R_Mesh_State(&m);
405                 GL_LockArrays(0, skysphere_numverts);
406                 R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
407                 GL_LockArrays(0, 0);
408         }
409 }
410
411 void R_Sky(void)
412 {
413         matrix4x4_t skymatrix;
414         if (skyrendermasked)
415         {
416                 Matrix4x4_CreateTranslate(&skymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2]);
417                 R_Mesh_Matrix(&skymatrix);
418                 if (skyrendersphere)
419                 {
420                         // this does not modify depth buffer
421                         R_SkySphere();
422                 }
423                 else if (skyrenderbox)
424                 {
425                         // this does not modify depth buffer
426                         R_SkyBox();
427                 }
428                 /* this will be skyroom someday
429                 else
430                 {
431                         // this modifies the depth buffer so we have to clear it afterward
432                         //R_SkyRoom();
433                         // clear the depthbuffer that was used while rendering the skyroom
434                         //GL_Clear(GL_DEPTH_BUFFER_BIT);
435                 }
436                 */
437         }
438 }
439
440 //===============================================================
441
442 void R_ResetSkyBox(void)
443 {
444         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
445         skyname[0] = 0;
446         R_LoadSkyBox();
447 }
448
449 static void r_sky_start(void)
450 {
451         skytexturepool = R_AllocTexturePool();
452         R_LoadSkyBox();
453 }
454
455 static void r_sky_shutdown(void)
456 {
457         R_UnloadSkyBox();
458         R_FreeTexturePool(&skytexturepool);
459 }
460
461 static void r_sky_newmap(void)
462 {
463 }
464
465
466 void R_Sky_Init(void)
467 {
468         Cmd_AddCommand ("loadsky", &LoadSky_f);
469         Cvar_RegisterVariable (&r_sky);
470         Cvar_RegisterVariable (&r_skyscroll1);
471         Cvar_RegisterVariable (&r_skyscroll2);
472         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
473         skyname[0] = 0;
474         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
475 }
476