varray_* rewritten to remove padding (varray_vertex3f, varray_texcoord2f, varray_texc...
[xonotic/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         if (!skysphereinitialized)
259         {
260                 skysphereinitialized = true;
261                 skyspherecalc();
262         }
263
264         // scroll speed for upper layer
265         speedscale = cl.time*8.0/128.0;
266         // wrap the scroll just to be extra kind to float accuracy
267         speedscale -= (int)speedscale;
268
269         memset(&m, 0, sizeof(m));
270         m.blendfunc1 = GL_ONE;
271         m.blendfunc2 = GL_ZERO;
272         m.depthdisable = true; // don't modify or read zbuffer
273         m.tex[0] = R_GetTexture(solidskytexture);
274         R_Mesh_State(&m);
275
276         GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
277
278         R_Mesh_GetSpace(skysphere_numverts);
279         R_Mesh_CopyVertex3f(skysphere_vertex3f, skysphere_numverts);
280         for (i = 0, out2f = varray_texcoord2f[0], in2f = skysphere_texcoord2f;i < skysphere_numverts;i++)
281         {
282                 *out2f++ = *in2f++ + speedscale;
283                 *out2f++ = *in2f++ + speedscale;
284         }
285         R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
286
287         m.blendfunc1 = GL_SRC_ALPHA;
288         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
289         m.tex[0] = R_GetTexture(alphaskytexture);
290         R_Mesh_State(&m);
291
292         // scroll the lower cloud layer twice as fast (just like quake did)
293         speedscale *= 2;
294
295         R_Mesh_GetSpace(skysphere_numverts);
296         R_Mesh_CopyVertex3f(skysphere_vertex3f, skysphere_numverts);
297         for (i = 0, out2f = varray_texcoord2f[0], in2f = skysphere_texcoord2f;i < skysphere_numverts;i++)
298         {
299                 *out2f++ = *in2f++ + speedscale;
300                 *out2f++ = *in2f++ + speedscale;
301         }
302         R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
303 }
304
305 void R_Sky(void)
306 {
307         matrix4x4_t skymatrix;
308         if (skyrendermasked)
309         {
310                 Matrix4x4_CreateTranslate(&skymatrix, r_origin[0], r_origin[1], r_origin[2]);
311                 R_Mesh_Matrix(&skymatrix);
312                 if (skyrendersphere)
313                 {
314                         // this does not modify depth buffer
315                         R_SkySphere();
316                 }
317                 else if (skyrenderbox)
318                 {
319                         // this does not modify depth buffer
320                         R_SkyBox();
321                 }
322                 /* this will be skyroom someday
323                 else
324                 {
325                         // this modifies the depth buffer so we have to clear it afterward
326                         //R_SkyRoom();
327                         // clear the depthbuffer that was used while rendering the skyroom
328                         //qglClear(GL_DEPTH_BUFFER_BIT);
329                 }
330                 */
331         }
332 }
333
334 //===============================================================
335
336 /*
337 =============
338 R_InitSky
339
340 A sky texture is 256*128, with the right side being a masked overlay
341 ==============
342 */
343 void R_InitSky (qbyte *src, int bytesperpixel)
344 {
345         int i, j, p, r, g, b;
346         qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
347         unsigned trans[128*128], transpix, *rgba;
348
349         skyavailable_quake = true;
350
351         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
352         R_FreeTexturePool(&skytexturepool);
353         skytexturepool = R_AllocTexturePool();
354         solidskytexture = NULL;
355         alphaskytexture = NULL;
356
357         if (bytesperpixel == 4)
358         {
359                 for (i = 0;i < 128;i++)
360                         for (j = 0;j < 128;j++)
361                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
362         }
363         else
364         {
365                 // make an average value for the back to avoid
366                 // a fringe on the top level
367                 r = g = b = 0;
368                 for (i=0 ; i<128 ; i++)
369                 {
370                         for (j=0 ; j<128 ; j++)
371                         {
372                                 p = src[i*256 + j + 128];
373                                 rgba = &palette_complete[p];
374                                 trans[(i*128) + j] = *rgba;
375                                 r += ((qbyte *)rgba)[0];
376                                 g += ((qbyte *)rgba)[1];
377                                 b += ((qbyte *)rgba)[2];
378                         }
379                 }
380
381                 ((qbyte *)&transpix)[0] = r/(128*128);
382                 ((qbyte *)&transpix)[1] = g/(128*128);
383                 ((qbyte *)&transpix)[2] = b/(128*128);
384                 ((qbyte *)&transpix)[3] = 0;
385         }
386
387         memcpy(skyupperlayerpixels, trans, 128*128*4);
388
389         solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
390
391         if (bytesperpixel == 4)
392         {
393                 for (i = 0;i < 128;i++)
394                         for (j = 0;j < 128;j++)
395                                 trans[(i*128) + j] = ((unsigned *)src)[i*256+j];
396         }
397         else
398         {
399                 for (i=0 ; i<128 ; i++)
400                 {
401                         for (j=0 ; j<128 ; j++)
402                         {
403                                 p = src[i*256 + j];
404                                 if (p == 0)
405                                         trans[(i*128) + j] = transpix;
406                                 else
407                                         trans[(i*128) + j] = palette_complete[p];
408                         }
409                 }
410         }
411
412         memcpy(skylowerlayerpixels, trans, 128*128*4);
413
414         alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
415 }
416
417 void R_ResetQuakeSky(void)
418 {
419         skyavailable_quake = false;
420 }
421
422 void R_ResetSkyBox(void)
423 {
424         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
425         skyname[0] = 0;
426         skyavailable_box = false;
427 }
428
429 static void r_sky_start(void)
430 {
431         skytexturepool = R_AllocTexturePool();
432         solidskytexture = NULL;
433         alphaskytexture = NULL;
434 }
435
436 static void r_sky_shutdown(void)
437 {
438         R_FreeTexturePool(&skytexturepool);
439         solidskytexture = NULL;
440         alphaskytexture = NULL;
441 }
442
443 static void r_sky_newmap(void)
444 {
445 }
446
447 void R_Sky_Init(void)
448 {
449         Cmd_AddCommand ("loadsky", &LoadSky_f);
450         Cvar_RegisterVariable (&r_sky);
451         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
452 }