]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_sky.c
Tomaz added support for loading a cubemap as skybox
[xonotic/darkplaces.git] / r_sky.c
diff --git a/r_sky.c b/r_sky.c
index ae126cf66d724c3d6249d94342a241cc89aed62e..5e76d2a1d44b4af39d78f1f1720550a65f43df52 100644 (file)
--- a/r_sky.c
+++ b/r_sky.c
@@ -15,7 +15,33 @@ static int skyrendersphere;
 static int skyrenderbox;
 static rtexturepool_t *skytexturepool;
 static char skyname[256];
-static char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
+
+typedef struct suffixinfo_s
+{
+       char *suffix;
+       qboolean flipx, flipy, flipdiagonal;
+}
+suffixinfo_t;
+static suffixinfo_t suffix[2][6] =
+{
+       {
+               {"rt", false, false, false},
+               {"bk", false, false, false},
+               {"lf", false, false, false},
+               {"ft", false, false, false},
+               {"up", false, false, false},
+               {"dn", false, false, false},
+       },
+       {
+               {"px", false,  true,  true},
+               {"ny", false, false, false},
+               {"nx",  true, false,  true},
+               {"py",  true,  true, false},
+               {"pz", false,  true,  true},
+               {"nz", false,  true,  true}
+       }
+};
+
 static rtexture_t *skyboxside[6];
 
 void R_SkyStartFrame(void)
@@ -41,48 +67,87 @@ void R_SkyStartFrame(void)
 R_SetSkyBox
 ==================
 */
-int R_SetSkyBox(const char *sky)
+void R_UnloadSkyBox(void)
 {
        int i;
+       for (i = 0;i < 6;i++)
+       {
+               if (skyboxside[i])
+                       R_FreeTexture(skyboxside[i]);
+               skyboxside[i] = NULL;
+       }
+}
+
+void R_LoadSkyBox(void)
+{
+       int i, j;
+       int indices[4] = {0,1,2,3};
        char name[1024];
        qbyte *image_rgba;
+       qbyte *temp;
+       R_UnloadSkyBox();
+       if (!skyname[0])
+               return;
 
-       if (strcmp(sky, skyname) == 0) // no change
-               return true;
+       for (j=0; j<2; j++)
+       {
+               for (i=0; i<6; i++)
+               {
+                       if (snprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
+                       {
+                               if (snprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
+                               {
+                                       if (snprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
+                                       {
+                                               if (snprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
+                                                       continue;
+                                       }
+                               }
+                       }
+                       temp = Mem_Alloc(tempmempool, image_width*image_height*4);
+                       Image_CopyMux (temp, image_rgba, image_width, image_height, suffix[j][i].flipx, suffix[j][i].flipy, suffix[j][i].flipdiagonal, 4, 4, indices);
+                       skyboxside[i] = R_LoadTexture2D(skytexturepool, va("skyboxside%d", i), image_width, image_height, temp, TEXTYPE_RGBA, TEXF_CLAMP | TEXF_PRECACHE, NULL);
+                       Mem_Free(image_rgba);
+                       Mem_Free(temp);
+               }
 
-       skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
-       skyavailable_box = false;
-       skyname[0] = 0;
+               for (i=0; i<6; i++)
+               {
+                       if (skyboxside[i] == NULL)
+                       {
+                               R_UnloadSkyBox();
+                               break;
+                       }
+               }
+
+               if (skyboxside[0] != NULL)
+                       return;
+       }
+}
 
-       if (!sky[0])
+int R_SetSkyBox(const char *sky)
+{
+       if (strcmp(sky, skyname) == 0) // no change
                return true;
 
        if (strlen(sky) > 1000)
        {
-               Con_Printf ("sky name too long (%i, max is 1000)\n", strlen(sky));
+               Con_Printf("sky name too long (%i, max is 1000)\n", strlen(sky));
                return false;
        }
 
-       for (i = 0;i < 6;i++)
-       {
-               sprintf (name, "env/%s%s", sky, suf[i]);
-               if (!(image_rgba = loadimagepixels(name, false, 0, 0)))
-               {
-                       sprintf (name, "gfx/env/%s%s", sky, suf[i]);
-                       if (!(image_rgba = loadimagepixels(name, false, 0, 0)))
-                       {
-                               Con_Printf ("Couldn't load env/%s%s or gfx/env/%s%s\n", sky, suf[i], sky, suf[i]);
-                               continue;
-                       }
-               }
-               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);
-       }
+       skyavailable_box = false;
+       strcpy(skyname, sky);
+
+       R_UnloadSkyBox();
+       R_LoadSkyBox();
+
+       if (!skyname[0])
+               return true;
 
        if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
        {
                skyavailable_box = true;
-               strcpy(skyname, sky);
                return true;
        }
        return false;
@@ -97,7 +162,7 @@ void LoadSky_f (void)
                if (skyname[0])
                        Con_Printf("current sky: %s\n", skyname);
                else
-                       Con_Printf("no skybox has been set\n");
+                       Con_Print("no skybox has been set\n");
                break;
        case 2:
                if (R_SetSkyBox(Cmd_Argv(1)))
@@ -105,13 +170,13 @@ void LoadSky_f (void)
                        if (skyname[0])
                                Con_Printf("skybox set to %s\n", skyname);
                        else
-                               Con_Printf("skybox disabled\n");
+                               Con_Print("skybox disabled\n");
                }
                else
                        Con_Printf("failed to load skybox %s\n", Cmd_Argv(1));
                break;
        default:
-               Con_Printf("usage: loadsky skyname\n");
+               Con_Print("usage: loadsky skyname\n");
                break;
        }
 }
@@ -119,69 +184,69 @@ void LoadSky_f (void)
 float skyboxvertex3f[6*4*3] =
 {
        // skyside[0]
-        16,  16,  16,
+        16, -16,  16,
+        16, -16, -16,
         16,  16, -16,
-       -16,  16, -16,
-       -16,  16,  16,
+        16,  16,  16,
        // skyside[1]
-       -16,  16,  16,
-       -16,  16, -16,
-       -16, -16, -16,
-       -16, -16,  16,
-       // skyside[2]
        -16, -16,  16,
        -16, -16, -16,
         16, -16, -16,
         16, -16,  16,
+       // skyside[2]
+       -16,  16,  16,
+       -16,  16, -16,
+       -16, -16, -16,
+       -16, -16,  16,
        // skyside[3]
-        16, -16,  16,
-        16, -16, -16,
-        16,  16, -16,
         16,  16,  16,
+        16,  16, -16,
+       -16,  16, -16,
+       -16,  16,  16,
        // skyside[4]
+       -16, -16,  16,
         16, -16,  16,
         16,  16,  16,
        -16,  16,  16,
-       -16, -16,  16,
        // skyside[5]
-        16,  16, -16,
         16, -16, -16,
        -16, -16, -16,
-       -16,  16, -16
+       -16,  16, -16,
+        16,  16, -16
 };
 
 float skyboxtexcoord2f[6*4*2] =
 {
        // skyside[0]
-       1, 0,
-       1, 1,
-       0, 1,
        0, 0,
-       // skyside[1]
-       1, 0,
-       1, 1,
        0, 1,
-       0, 0,
-       // skyside[2]
-       1, 0,
        1, 1,
-       0, 1,
-       0, 0,
-       // skyside[3]
        1, 0,
-       1, 1,
-       0, 1,
+       // skyside[1]
        0, 0,
-       // skyside[4]
-       1, 0,
+       0, 1,
        1, 1,
+       1, 0,
+       // skyside[2]
+       0, 0,
        0, 1,
+       1, 1,
+       1, 0,
+       // skyside[3]
        0, 0,
-       // skyside[5]
+       0, 1,
+       1, 1,
        1, 0,
+       // skyside[4]
+       0, 0,
+       0, 1,
        1, 1,
+       1, 0,
+       // skyside[5]
+       0, 0,
        0, 1,
-       0, 0
+       1, 1,
+       1, 0
 };
 
 int skyboxelements[6*2*3] =
@@ -210,19 +275,21 @@ static void R_SkyBox(void)
 {
        int i;
        rmeshstate_t m;
-       GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
+       GL_Color(1, 1, 1, 1);
        memset(&m, 0, sizeof(m));
        GL_BlendFunc(GL_ONE, GL_ZERO);
        GL_DepthMask(true);
        GL_DepthTest(false); // don't modify or read zbuffer
-       GL_VertexPointer(skyboxvertex3f);
+       m.pointer_vertex = skyboxvertex3f;
        m.pointer_texcoord[0] = skyboxtexcoord2f;
+       GL_LockArrays(0, 6*4);
        for (i = 0;i < 6;i++)
        {
                m.tex[0] = R_GetTexture(skyboxside[i]);
-               R_Mesh_State_Texture(&m);
+               R_Mesh_State(&m);
                R_Mesh_Draw(6*4, 2, skyboxelements + i * 6);
        }
+       GL_LockArrays(0, 0);
 }
 
 #define skygridx 32
@@ -288,7 +355,7 @@ static void R_SkySphere(void)
        float speedscale;
        static qboolean skysphereinitialized = false;
        rmeshstate_t m;
-       matrix4x4_t scroll1matrix, scroll2matrix, identitymatrix;
+       matrix4x4_t scroll1matrix, scroll2matrix;
        if (!skysphereinitialized)
        {
                skysphereinitialized = true;
@@ -303,43 +370,44 @@ static void R_SkySphere(void)
        // scroll the lower cloud layer twice as fast (just like quake did)
        Matrix4x4_CreateTranslate(&scroll1matrix, speedscale, speedscale, 0);
        Matrix4x4_CreateTranslate(&scroll2matrix, speedscale * 2, speedscale * 2, 0);
-       Matrix4x4_CreateIdentity(&identitymatrix);
 
-       GL_VertexPointer(skysphere_vertex3f);
-       GL_Color(r_colorscale, r_colorscale, r_colorscale, 1);
+       GL_Color(1, 1, 1, 1);
        GL_BlendFunc(GL_ONE, GL_ZERO);
        GL_DepthMask(true);
        GL_DepthTest(false); // don't modify or read zbuffer
        memset(&m, 0, sizeof(m));
+       m.pointer_vertex = skysphere_vertex3f;
        m.tex[0] = R_GetTexture(solidskytexture);
        m.pointer_texcoord[0] = skysphere_texcoord2f;
-       R_Mesh_TextureMatrix(0, &scroll1matrix);
-       if (r_colorscale == 1 && r_textureunits.integer >= 2)
+       m.texmatrix[0] = scroll1matrix;
+       if (r_textureunits.integer >= 2)
        {
                // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
-               // LordHavoc: note that color is not set here because it does not
-               // matter with GL_REPLACE
                m.tex[1] = R_GetTexture(alphaskytexture);
                m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
                m.pointer_texcoord[1] = skysphere_texcoord2f;
-               R_Mesh_State_Texture(&m);
-               R_Mesh_TextureMatrix(1, &scroll2matrix);
+               m.texmatrix[1] = scroll2matrix;
+               R_Mesh_State(&m);
+               GL_LockArrays(0, skysphere_numverts);
                R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
-               R_Mesh_TextureMatrix(1, &identitymatrix);
+               GL_LockArrays(0, 0);
        }
        else
        {
                // two pass
-               R_Mesh_State_Texture(&m);
+               R_Mesh_State(&m);
+               GL_LockArrays(0, skysphere_numverts);
                R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
+               GL_LockArrays(0, 0);
 
                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                m.tex[0] = R_GetTexture(alphaskytexture);
-               R_Mesh_State_Texture(&m);
-               R_Mesh_TextureMatrix(0, &scroll2matrix);
+               m.texmatrix[0] = scroll2matrix;
+               R_Mesh_State(&m);
+               GL_LockArrays(0, skysphere_numverts);
                R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
+               GL_LockArrays(0, 0);
        }
-       R_Mesh_TextureMatrix(0, &identitymatrix);
 }
 
 void R_Sky(void)
@@ -347,7 +415,7 @@ void R_Sky(void)
        matrix4x4_t skymatrix;
        if (skyrendermasked)
        {
-               Matrix4x4_CreateTranslate(&skymatrix, r_origin[0], r_origin[1], r_origin[2]);
+               Matrix4x4_CreateTranslate(&skymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2]);
                R_Mesh_Matrix(&skymatrix);
                if (skyrendersphere)
                {
@@ -365,7 +433,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
-                       //qglClear(GL_DEPTH_BUFFER_BIT);
+                       //GL_Clear(GL_DEPTH_BUFFER_BIT);
                }
                */
        }
@@ -382,15 +450,8 @@ A sky texture is 256*128, with the right side being a masked overlay
 */
 void R_InitSky (qbyte *src, int bytesperpixel)
 {
-       int i, j, p, r, g, b;
-       qbyte skyupperlayerpixels[128*128*4], skylowerlayerpixels[128*128*4];
-       unsigned trans[128*128], transpix, *rgba;
-       union
-       {
-               int i;
-               qbyte b[4];
-       }
-       transpixunion;
+       int i, j;
+       unsigned solidpixels[128*128], alphapixels[128*128];
 
        skyavailable_quake = true;
 
@@ -402,65 +463,53 @@ void R_InitSky (qbyte *src, int bytesperpixel)
 
        if (bytesperpixel == 4)
        {
-               transpixunion.i = 0;
                for (i = 0;i < 128;i++)
+               {
                        for (j = 0;j < 128;j++)
-                               trans[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
+                       {
+                               solidpixels[(i*128) + j] = ((unsigned *)src)[i*256+j+128];
+                               alphapixels[(i*128) + j] = ((unsigned *)src)[i*256+j];
+                       }
+               }
        }
        else
        {
                // make an average value for the back to avoid
                // a fringe on the top level
+               int p, r, g, b;
+               union
+               {
+                       unsigned int i;
+                       unsigned char b[4];
+               }
+               rgba;
                r = g = b = 0;
-               for (i=0 ; i<128 ; i++)
+               for (i = 0;i < 128;i++)
                {
-                       for (j=0 ; j<128 ; j++)
+                       for (j = 0;j < 128;j++)
                        {
-                               p = src[i*256 + j + 128];
-                               rgba = &palette_complete[p];
-                               trans[(i*128) + j] = *rgba;
-                               r += ((qbyte *)rgba)[0];
-                               g += ((qbyte *)rgba)[1];
-                               b += ((qbyte *)rgba)[2];
+                               rgba.i = palette_complete[src[i*256 + j + 128]];
+                               r += rgba.b[0];
+                               g += rgba.b[1];
+                               b += rgba.b[2];
                        }
                }
-
-               transpixunion.i = 0;
-               transpixunion.b[0] = r/(128*128);
-               transpixunion.b[1] = g/(128*128);
-               transpixunion.b[2] = b/(128*128);
-               transpixunion.b[3] = 0;
-       }
-       transpix = transpixunion.i;
-
-       memcpy(skyupperlayerpixels, trans, 128*128*4);
-
-       solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
-
-       if (bytesperpixel == 4)
-       {
+               rgba.b[0] = r/(128*128);
+               rgba.b[1] = g/(128*128);
+               rgba.b[2] = b/(128*128);
+               rgba.b[3] = 0;
                for (i = 0;i < 128;i++)
-                       for (j = 0;j < 128;j++)
-                               trans[(i*128) + j] = ((unsigned *)src)[i*256+j];
-       }
-       else
-       {
-               for (i=0 ; i<128 ; i++)
                {
-                       for (j=0 ; j<128 ; j++)
+                       for (j = 0;j < 128;j++)
                        {
-                               p = src[i*256 + j];
-                               if (p == 0)
-                                       trans[(i*128) + j] = transpix;
-                               else
-                                       trans[(i*128) + j] = palette_complete[p];
+                               solidpixels[(i*128) + j] = palette_complete[src[i*256 + j + 128]];
+                               alphapixels[(i*128) + j] = (p = src[i*256 + j]) ? palette_complete[p] : rgba.i;
                        }
                }
        }
 
-       memcpy(skylowerlayerpixels, trans, 128*128*4);
-
-       alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) trans, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
+       solidskytexture = R_LoadTexture2D(skytexturepool, "sky_solidtexture", 128, 128, (qbyte *) solidpixels, TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
+       alphaskytexture = R_LoadTexture2D(skytexturepool, "sky_alphatexture", 128, 128, (qbyte *) alphapixels, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
 }
 
 void R_ResetQuakeSky(void)
@@ -480,10 +529,12 @@ static void r_sky_start(void)
        skytexturepool = R_AllocTexturePool();
        solidskytexture = NULL;
        alphaskytexture = NULL;
+       R_LoadSkyBox();
 }
 
 static void r_sky_shutdown(void)
 {
+       R_UnloadSkyBox();
        R_FreeTexturePool(&skytexturepool);
        solidskytexture = NULL;
        alphaskytexture = NULL;
@@ -497,6 +548,8 @@ void R_Sky_Init(void)
 {
        Cmd_AddCommand ("loadsky", &LoadSky_f);
        Cvar_RegisterVariable (&r_sky);
+       R_ResetSkyBox();
+       R_ResetQuakeSky();
        R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
 }