]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_sky.c
added a FIXME note about skyboxes and vid_restart
[xonotic/darkplaces.git] / r_sky.c
diff --git a/r_sky.c b/r_sky.c
index 25fa9fd5ea73a8e9b5b8fe2fad054bbd31f207c5..d5b21c0d0ef348660dbdb8674b292a5c4975d3d0 100644 (file)
--- a/r_sky.c
+++ b/r_sky.c
@@ -1,6 +1,8 @@
 
 #include "quakedef.h"
+#include "image.h"
 
+// FIXME: fix skybox after vid_restart
 cvar_t r_sky = {CVAR_SAVE, "r_sky", "1"};
 qboolean skyavailable_quake;
 qboolean skyavailable_box;
@@ -73,7 +75,7 @@ int R_SetSkyBox(const char *sky)
                                continue;
                        }
                }
-               skyboxside[i] = R_LoadTexture(skytexturepool, va("skyboxside%d", i), image_width, image_height, image_rgba, TEXTYPE_RGBA, TEXF_PRECACHE);
+               skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, image_rgba, TEXTYPE_RGBA, TEXF_CLAMP | TEXF_PRECACHE, NULL);
                Mem_Free(image_rgba);
        }
 
@@ -122,8 +124,8 @@ static void R_SkyBox(void)
        varray_vertex[i * 4 + 0] = (x) * 16.0f;\
        varray_vertex[i * 4 + 1] = (y) * 16.0f;\
        varray_vertex[i * 4 + 2] = (z) * 16.0f;\
-       varray_texcoord[0][i * 2 + 0] = (s) * (254.0f/256.0f) + (1.0f/256.0f);\
-       varray_texcoord[0][i * 2 + 1] = (t) * (254.0f/256.0f) + (1.0f/256.0f);
+       varray_texcoord[0][i * 4 + 0] = (s);\
+       varray_texcoord[0][i * 4 + 1] = (t);
 
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_ONE;
@@ -132,10 +134,7 @@ static void R_SkyBox(void)
        m.tex[0] = R_GetTexture(skyboxside[3]); // front
        R_Mesh_State(&m);
 
-       varray_color[0] = varray_color[4] = varray_color[8] = varray_color[12] = r_colorscale;
-       varray_color[1] = varray_color[5] = varray_color[9] = varray_color[13] = r_colorscale;
-       varray_color[2] = varray_color[6] = varray_color[10] = varray_color[14] = r_colorscale;
-       varray_color[3] = varray_color[7] = varray_color[11] = varray_color[15] = 1;
+       GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
        
        R_SkyBoxPolyVec(0, 1, 0,  1, -1,  1);
        R_SkyBoxPolyVec(1, 1, 1,  1, -1, -1);
@@ -185,13 +184,22 @@ static void R_SkyBox(void)
 #define skygridy 32
 #define skygridy1 (skygridy + 1)
 #define skygridyrecip (1.0f / (skygridy))
+#define skysphere_numverts (skygridx1 * skygridy1)
+#define skysphere_numtriangles (skygridx * skygridy * 2)
+static float skysphere_vertex[skysphere_numverts * 4];
+static float skysphere_texcoord[skysphere_numverts * 4];
+static int skysphere_elements[skysphere_numtriangles * 3];
 
-static float skysphere[skygridx1*skygridy1*5];
-static int skysphereindices[skygridx*skygridy*6];
-static void skyspherecalc(float *sphere, float dx, float dy, float dz)
+static void skyspherecalc(void)
 {
-       float a, b, x, ax, ay, v[3], length;
-       int i, j, *index;
+       int i, j, *e;
+       float a, b, x, ax, ay, v[3], length, *vertex, *texcoord;
+       float dx, dy, dz;
+       dx = 16;
+       dy = 16;
+       dz = 16 / 3;
+       vertex = skysphere_vertex;
+       texcoord = skysphere_texcoord;
        for (j = 0;j <= skygridy;j++)
        {
                a = j * skygridyrecip;
@@ -200,73 +208,56 @@ static void skyspherecalc(float *sphere, float dx, float dy, float dz)
                for (i = 0;i <= skygridx;i++)
                {
                        b = i * skygridxrecip;
-                       x = cos(b * M_PI * 2);
+                       x = cos((b + 0.5) * M_PI);
                        v[0] = ax*x * dx;
                        v[1] = ay*x * dy;
-                       v[2] = -sin(b * M_PI * 2) * dz;
+                       v[2] = -sin((b + 0.5) * M_PI) * dz;
                        length = 3.0f / sqrt(v[0]*v[0]+v[1]*v[1]+(v[2]*v[2]*9));
-                       *sphere++ = v[0] * length;
-                       *sphere++ = v[1] * length;
-                       *sphere++ = v[0];
-                       *sphere++ = v[1];
-                       *sphere++ = v[2];
+                       *texcoord++ = v[0] * length;
+                       *texcoord++ = v[1] * length;
+                       *texcoord++ = 0;
+                       *texcoord++ = 0;
+                       *vertex++ = v[0];
+                       *vertex++ = v[1];
+                       *vertex++ = v[2];
+                       *vertex++ = 1;
                }
        }
-       index = skysphereindices;
+       e = skysphere_elements;
        for (j = 0;j < skygridy;j++)
        {
                for (i = 0;i < skygridx;i++)
                {
-                       *index++ =  j      * skygridx1 + i;
-                       *index++ =  j      * skygridx1 + i + 1;
-                       *index++ = (j + 1) * skygridx1 + i;
+                       *e++ =  j      * skygridx1 + i;
+                       *e++ =  j      * skygridx1 + i + 1;
+                       *e++ = (j + 1) * skygridx1 + i;
 
-                       *index++ =  j      * skygridx1 + i + 1;
-                       *index++ = (j + 1) * skygridx1 + i + 1;
-                       *index++ = (j + 1) * skygridx1 + i;
+                       *e++ =  j      * skygridx1 + i + 1;
+                       *e++ = (j + 1) * skygridx1 + i + 1;
+                       *e++ = (j + 1) * skygridx1 + i;
                }
                i++;
        }
 }
 
-static void skyspherearrays(float *v, float *t, float *c, float *source, float s, float colorscale)
-{
-       int i;
-       for (i = 0;i < (skygridx1*skygridy1);i++, c += 4, t += 2, v += 4, source += 5)
-       {
-               c[0] = colorscale;
-               c[1] = colorscale;
-               c[2] = colorscale;
-               c[3] = 1;
-               t[0] = source[0] + s;
-               t[1] = source[1] + s;
-               v[0] = source[2];
-               v[1] = source[3];
-               v[2] = source[4];
-       }
-}
-
 static void R_SkySphere(void)
 {
-       int numverts, numtriangles;
-       float speedscale, speedscale2;
+       int i;
+       float speedscale, *t;
        static qboolean skysphereinitialized = false;
        rmeshstate_t m;
        if (!skysphereinitialized)
        {
                skysphereinitialized = true;
-               skyspherecalc(skysphere, 16, 16, 16 / 3);
+               skyspherecalc();
        }
 
+       // scroll speed for upper layer
        speedscale = cl.time*8.0/128.0;
+       // wrap the scroll just to be extra kind to float accuracy
        speedscale -= (int)speedscale;
-       speedscale2 = cl.time*16.0/128.0;
-       speedscale2 -= (int)speedscale2;
 
-       numverts = skygridx1*skygridy1;
-       numtriangles = skygridx*skygridy*2;
-
-       R_Mesh_ResizeCheck(numverts);
+       R_Mesh_ResizeCheck(skysphere_numverts);
 
        memset(&m, 0, sizeof(m));
        m.blendfunc1 = GL_ONE;
@@ -275,16 +266,29 @@ static void R_SkySphere(void)
        m.tex[0] = R_GetTexture(solidskytexture);
        R_Mesh_State(&m);
 
-       skyspherearrays(varray_vertex, varray_texcoord[0], varray_color, skysphere, speedscale, r_colorscale);
-       R_Mesh_Draw(numverts, numtriangles, skysphereindices);
+       GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
+
+       memcpy(varray_vertex, skysphere_vertex, skysphere_numverts * sizeof(float[4]));
+       memcpy(varray_texcoord[0], skysphere_texcoord, skysphere_numverts * sizeof(float[4]));
+       for (i = 0, t = varray_texcoord[0];i < skysphere_numverts;i++, t += 4)
+       {
+               t[0] += speedscale;
+               t[1] += speedscale;
+       }
+       R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_elements);
 
        m.blendfunc1 = GL_SRC_ALPHA;
        m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        m.tex[0] = R_GetTexture(alphaskytexture);
        R_Mesh_State(&m);
 
-       skyspherearrays(varray_vertex, varray_texcoord[0], varray_color, skysphere, speedscale2, r_colorscale);
-       R_Mesh_Draw(numverts, numtriangles, skysphereindices);
+       // scroll it again, this makes the lower cloud layer scroll twice as fast (just like quake did)
+       for (i = 0, t = varray_texcoord[0];i < skysphere_numverts;i++, t += 4)
+       {
+               t[0] += speedscale;
+               t[1] += speedscale;
+       }
+       R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_elements);
 }
 
 void R_Sky(void)
@@ -310,7 +314,7 @@ void R_Sky(void)
                        // this modifies the depth buffer so we have to clear it afterward
                        //R_SkyRoom();
                        // clear the depthbuffer that was used while rendering the skyroom
-                       //R_Mesh_ClearDepth();
+                       //qglClear(GL_DEPTH_BUFFER_BIT);
                }
                */
        }
@@ -355,7 +359,7 @@ void R_InitSky (qbyte *src, int bytesperpixel)
                        for (j=0 ; j<128 ; j++)
                        {
                                p = src[i*256 + j + 128];
-                               rgba = &d_8to24table[p];
+                               rgba = &palette_complete[p];
                                trans[(i*128) + j] = *rgba;
                                r += ((qbyte *)rgba)[0];
                                g += ((qbyte *)rgba)[1];
@@ -371,7 +375,7 @@ void R_InitSky (qbyte *src, int bytesperpixel)
 
        memcpy(skyupperlayerpixels, trans, 128*128*4);
 
-       solidskytexture = R_LoadTexture (skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE);
+       solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
 
        if (bytesperpixel == 4)
        {
@@ -389,14 +393,14 @@ void R_InitSky (qbyte *src, int bytesperpixel)
                                if (p == 0)
                                        trans[(i*128) + j] = transpix;
                                else
-                                       trans[(i*128) + j] = d_8to24table[p];
+                                       trans[(i*128) + j] = palette_complete[p];
                        }
                }
        }
 
        memcpy(skylowerlayerpixels, trans, 128*128*4);
 
-       alphaskytexture = R_LoadTexture (skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
+       alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
 }
 
 void R_ResetQuakeSky(void)