gl backend now has functions for color array and flat color states (GL_UseColorArray...
[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 * 2 + 0] = (s) * (254.0f/256.0f) + (1.0f/256.0f);\
126         varray_texcoord[0][i * 2 + 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
186 static float skysphere[skygridx1*skygridy1*5];
187 static int skysphereindices[skygridx*skygridy*6];
188 static void skyspherecalc(float *sphere, float dx, float dy, float dz)
189 {
190         float a, b, x, ax, ay, v[3], length;
191         int i, j, *index;
192         for (j = 0;j <= skygridy;j++)
193         {
194                 a = j * skygridyrecip;
195                 ax = cos(a * M_PI * 2);
196                 ay = -sin(a * M_PI * 2);
197                 for (i = 0;i <= skygridx;i++)
198                 {
199                         b = i * skygridxrecip;
200                         x = cos(b * M_PI * 2);
201                         v[0] = ax*x * dx;
202                         v[1] = ay*x * dy;
203                         v[2] = -sin(b * M_PI * 2) * dz;
204                         length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
205                         *sphere++ = v[0] * length;
206                         *sphere++ = v[1] * length;
207                         *sphere++ = v[0];
208                         *sphere++ = v[1];
209                         *sphere++ = v[2];
210                 }
211         }
212         index = skysphereindices;
213         for (j = 0;j < skygridy;j++)
214         {
215                 for (i = 0;i < skygridx;i++)
216                 {
217                         *index++ =  j      * skygridx1 + i;
218                         *index++ =  j      * skygridx1 + i + 1;
219                         *index++ = (j + 1) * skygridx1 + i;
220
221                         *index++ =  j      * skygridx1 + i + 1;
222                         *index++ = (j + 1) * skygridx1 + i + 1;
223                         *index++ = (j + 1) * skygridx1 + i;
224                 }
225                 i++;
226         }
227 }
228
229 static void skyspherearrays(float *v, float *t, float *source, float s)
230 {
231         int i;
232         for (i = 0;i < (skygridx1*skygridy1);i++, t += 2, v += 4, source += 5)
233         {
234                 t[0] = source[0] + s;
235                 t[1] = source[1] + s;
236                 v[0] = source[2];
237                 v[1] = source[3];
238                 v[2] = source[4];
239         }
240 }
241
242 static void R_SkySphere(void)
243 {
244         int numverts, numtriangles;
245         float speedscale, speedscale2;
246         static qboolean skysphereinitialized = false;
247         rmeshstate_t m;
248         if (!skysphereinitialized)
249         {
250                 skysphereinitialized = true;
251                 skyspherecalc(skysphere, 16, 16, 16 / 3);
252         }
253
254         speedscale = cl.time*8.0/128.0;
255         speedscale -= (int)speedscale;
256         speedscale2 = cl.time*16.0/128.0;
257         speedscale2 -= (int)speedscale2;
258
259         numverts = skygridx1*skygridy1;
260         numtriangles = skygridx*skygridy*2;
261
262         R_Mesh_ResizeCheck(numverts);
263
264         memset(&m, 0, sizeof(m));
265         m.blendfunc1 = GL_ONE;
266         m.blendfunc2 = GL_ZERO;
267         m.depthdisable = true; // don't modify or read zbuffer
268         m.tex[0] = R_GetTexture(solidskytexture);
269         R_Mesh_State(&m);
270
271         GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
272
273         skyspherearrays(varray_vertex, varray_texcoord[0], skysphere, speedscale);
274         R_Mesh_Draw(numverts, numtriangles, skysphereindices);
275
276         m.blendfunc1 = GL_SRC_ALPHA;
277         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
278         m.tex[0] = R_GetTexture(alphaskytexture);
279         R_Mesh_State(&m);
280
281         skyspherearrays(varray_vertex, varray_texcoord[0], skysphere, speedscale2);
282         R_Mesh_Draw(numverts, numtriangles, skysphereindices);
283 }
284
285 void R_Sky(void)
286 {
287         matrix4x4_t skymatrix;
288         if (skyrendermasked)
289         {
290                 Matrix4x4_CreateTranslate(&skymatrix, r_origin[0], r_origin[1], r_origin[2]);
291                 R_Mesh_Matrix(&skymatrix);
292                 if (skyrendersphere)
293                 {
294                         // this does not modify depth buffer
295                         R_SkySphere();
296                 }
297                 else if (skyrenderbox)
298                 {
299                         // this does not modify depth buffer
300                         R_SkyBox();
301                 }
302                 /* this will be skyroom someday
303                 else
304                 {
305                         // this modifies the depth buffer so we have to clear it afterward
306                         //R_SkyRoom();
307                         // clear the depthbuffer that was used while rendering the skyroom
308                         //R_Mesh_ClearDepth();
309                 }
310                 */
311         }
312 }
313
314 //===============================================================
315
316 /*
317 =============
318 R_InitSky
319
320 A sky texture is 256*128, with the right side being a masked overlay
321 ==============
322 */
323 void R_InitSky (qbyte *src, int bytesperpixel)
324 {
325         int i, j, p, r, g, b;
326         qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
327         unsigned trans[128*128], transpix, *rgba;
328
329         skyavailable_quake = true;
330
331         // flush skytexturepool so we won't build up a leak from uploading textures multiple times
332         R_FreeTexturePool(&skytexturepool);
333         skytexturepool = R_AllocTexturePool();
334         solidskytexture = NULL;
335         alphaskytexture = NULL;
336
337         if (bytesperpixel == 4)
338         {
339                 for (i = 0;i < 128;i++)
340                         for (j = 0;j < 128;j++)
341                                 trans[(i*128) + j] = src[i*256+j+128];
342         }
343         else
344         {
345                 // make an average value for the back to avoid
346                 // a fringe on the top level
347                 r = g = b = 0;
348                 for (i=0 ; i<128 ; i++)
349                 {
350                         for (j=0 ; j<128 ; j++)
351                         {
352                                 p = src[i*256 + j + 128];
353                                 rgba = &d_8to24table[p];
354                                 trans[(i*128) + j] = *rgba;
355                                 r += ((qbyte *)rgba)[0];
356                                 g += ((qbyte *)rgba)[1];
357                                 b += ((qbyte *)rgba)[2];
358                         }
359                 }
360
361                 ((qbyte *)&transpix)[0] = r/(128*128);
362                 ((qbyte *)&transpix)[1] = g/(128*128);
363                 ((qbyte *)&transpix)[2] = b/(128*128);
364                 ((qbyte *)&transpix)[3] = 0;
365         }
366
367         memcpy(skyupperlayerpixels, trans, 128*128*4);
368
369         solidskytexture = R_LoadTexture (skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE);
370
371         if (bytesperpixel == 4)
372         {
373                 for (i = 0;i < 128;i++)
374                         for (j = 0;j < 128;j++)
375                                 trans[(i*128) + j] = src[i*256+j];
376         }
377         else
378         {
379                 for (i=0 ; i<128 ; i++)
380                 {
381                         for (j=0 ; j<128 ; j++)
382                         {
383                                 p = src[i*256 + j];
384                                 if (p == 0)
385                                         trans[(i*128) + j] = transpix;
386                                 else
387                                         trans[(i*128) + j] = d_8to24table[p];
388                         }
389                 }
390         }
391
392         memcpy(skylowerlayerpixels, trans, 128*128*4);
393
394         alphaskytexture = R_LoadTexture (skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
395 }
396
397 void R_ResetQuakeSky(void)
398 {
399         skyavailable_quake = false;
400 }
401
402 void R_ResetSkyBox(void)
403 {
404         skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
405         skyname[0] = 0;
406         skyavailable_box = false;
407 }
408
409 static void r_sky_start(void)
410 {
411         skytexturepool = R_AllocTexturePool();
412         solidskytexture = NULL;
413         alphaskytexture = NULL;
414 }
415
416 static void r_sky_shutdown(void)
417 {
418         R_FreeTexturePool(&skytexturepool);
419         solidskytexture = NULL;
420         alphaskytexture = NULL;
421 }
422
423 static void r_sky_newmap(void)
424 {
425 }
426
427 void R_Sky_Init(void)
428 {
429         Cmd_AddCommand ("loadsky", &LoadSky_f);
430         Cvar_RegisterVariable (&r_sky);
431         R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
432 }