]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_sky.c
added polygonoffset as a texture property, used on submodels (such as
[xonotic/darkplaces.git] / r_sky.c
diff --git a/r_sky.c b/r_sky.c
index a830e85b391de20ebb21a493622738c18d1db172..51caa6cecb4cfb926f8744dabb02c5087b50b050 100644 (file)
--- a/r_sky.c
+++ b/r_sky.c
@@ -3,19 +3,51 @@
 #include "image.h"
 
 // FIXME: fix skybox after vid_restart
-cvar_t r_sky = {CVAR_SAVE, "r_sky", "1"};
-qboolean skyavailable_quake;
-qboolean skyavailable_box;
+cvar_t r_sky = {CVAR_SAVE, "r_sky", "1", "enables sky rendering (black otherwise)"};
+cvar_t r_skyscroll1 = {CVAR_SAVE, "r_skyscroll1", "1", "speed at which upper clouds layer scrolls in quake sky"};
+cvar_t r_skyscroll2 = {CVAR_SAVE, "r_skyscroll2", "2", "speed at which lower clouds layer scrolls in quake sky"};
 int skyrendernow;
 int skyrendermasked;
 
-static rtexture_t *solidskytexture;
-static rtexture_t *alphaskytexture;
 static int skyrendersphere;
 static int skyrenderbox;
 static rtexturepool_t *skytexturepool;
-static char skyname[256];
-static char *suf[6] = {"rt", "bk", "lf", "ft", "up", "dn"};
+static char skyname[MAX_QPATH];
+
+typedef struct suffixinfo_s
+{
+       char *suffix;
+       qboolean flipx, flipy, flipdiagonal;
+}
+suffixinfo_t;
+static suffixinfo_t suffix[3][6] =
+{
+       {
+               {"px",   false, false, false},
+               {"nx",   false, false, false},
+               {"py",   false, false, false},
+               {"ny",   false, false, false},
+               {"pz",   false, false, false},
+               {"nz",   false, false, false}
+       },
+       {
+               {"posx", false, false, false},
+               {"negx", false, false, false},
+               {"posy", false, false, false},
+               {"negy", false, false, false},
+               {"posz", false, false, false},
+               {"negz", false, false, false}
+       },
+       {
+               {"rt",   false, false,  true},
+               {"lf",    true,  true,  true},
+               {"bk",   false,  true, false},
+               {"ft",    true, false, false},
+               {"up",   false, false,  true},
+               {"dn",   false, false,  true}
+       }
+};
+
 static rtexture_t *skyboxside[6];
 
 void R_SkyStartFrame(void)
@@ -24,11 +56,11 @@ void R_SkyStartFrame(void)
        skyrendersphere = false;
        skyrenderbox = false;
        skyrendermasked = false;
-       if (r_sky.integer && !fogenabled)
+       if (r_sky.integer && !r_refdef.fogenabled)
        {
-               if (skyavailable_box)
+               if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
                        skyrenderbox = true;
-               else if (skyavailable_quake)
+               else if (r_refdef.worldmodel && r_refdef.worldmodel->brush.solidskytexture)
                        skyrendersphere = true;
                // for depth-masked sky, render the sky on the first sky surface encountered
                skyrendernow = true;
@@ -48,37 +80,55 @@ void R_UnloadSkyBox(void)
        {
                if (skyboxside[i])
                        R_FreeTexture(skyboxside[i]);
-               skyboxside[i] = NULL;;
+               skyboxside[i] = NULL;
        }
 }
 
-void R_LoadSkyBox(void)
+int R_LoadSkyBox(void)
 {
-       int i;
-       char name[1024];
-       qbyte *image_rgba;
+       int i, j, success;
+       int indices[4] = {0,1,2,3};
+       char name[MAX_INPUTLINE];
+       unsigned char *image_rgba;
+       unsigned char *temp;
+
        R_UnloadSkyBox();
+
        if (!skyname[0])
-               return;
-       for (i = 0;i < 6;i++)
+               return true;
+
+       for (j=0; j<3; j++)
        {
-               if (snprintf(name, sizeof(name), "%s_%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
+               success = 0;
+               for (i=0; i<6; i++)
                {
-                       if (snprintf(name, sizeof(name), "%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
+                       if (dpsnprintf(name, sizeof(name), "%s_%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0)))
                        {
-                               if (snprintf(name, sizeof(name), "env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
+                               if (dpsnprintf(name, sizeof(name), "%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0)))
                                {
-                                       if (snprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suf[i]) >= (int)sizeof(name) || !(image_rgba = loadimagepixels(name, false, 0, 0)))
+                                       if (dpsnprintf(name, sizeof(name), "env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0)))
                                        {
-                                               Con_Printf("Couldn't load %s_%s or %s%s or env/%s%s or gfx/env/%s%s\n", skyname, suf[i], skyname, suf[i], skyname, suf[i], skyname, suf[i]);
-                                               continue;
+                                               if (dpsnprintf(name, sizeof(name), "gfx/env/%s%s", skyname, suffix[j][i].suffix) < 0 || !(image_rgba = loadimagepixels(name, false, 0, 0)))
+                                                       continue;
                                        }
                                }
                        }
+                       temp = (unsigned char *)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);
+                       success++;
                }
-               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);
+
+               if (success)
+                       break;
        }
+
+       if (j == 3)
+               return false;
+
+       return true;
 }
 
 int R_SetSkyBox(const char *sky)
@@ -88,25 +138,13 @@ int R_SetSkyBox(const char *sky)
 
        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", (int)strlen(sky));
                return false;
        }
 
-       skyavailable_box = false;
-       strcpy(skyname, sky);
-
-       R_UnloadSkyBox();
-       R_LoadSkyBox();
+       strlcpy(skyname, sky, sizeof(skyname));
 
-       if (!skyname[0])
-               return true;
-
-       if (skyboxside[0] || skyboxside[1] || skyboxside[2] || skyboxside[3] || skyboxside[4] || skyboxside[5])
-       {
-               skyavailable_box = true;
-               return true;
-       }
-       return false;
+       return R_LoadSkyBox();
 }
 
 // LordHavoc: added LoadSky console command
@@ -140,68 +178,68 @@ 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[3]
        -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,
        // 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,
+       1, 1,
+       1, 0,
        0, 0,
        // skyside[1]
        1, 0,
-       1, 1,
-       0, 1,
        0, 0,
+       0, 1,
+       1, 1,
        // skyside[2]
-       1, 0,
        1, 1,
-       0, 1,
-       0, 0,
-       // skyside[3]
        1, 0,
-       1, 1,
+       0, 0,
        0, 1,
+       // skyside[3]
        0, 0,
-       // skyside[4]
-       1, 0,
+       0, 1,
        1, 1,
+       1, 0,
+       // skyside[4]
        0, 1,
+       1, 1,
+       1, 0,
        0, 0,
        // skyside[5]
-       1, 0,
-       1, 1,
        0, 1,
+       1, 1,
+       1, 0,
        0, 0
 };
 
@@ -230,20 +268,22 @@ int skyboxelements[6*2*3] =
 static void R_SkyBox(void)
 {
        int i;
-       rmeshstate_t m;
-       GL_Color(1, 1, 1, 1);
-       memset(&m, 0, sizeof(m));
+       // FIXME: fixed function path can't properly handle r_view.colorscale > 1
+       GL_Color(1 * r_view.colorscale, 1 * r_view.colorscale, 1 * r_view.colorscale, 1);
        GL_BlendFunc(GL_ONE, GL_ZERO);
-       GL_DepthMask(true);
+       GL_DepthMask(false);
+       GL_DepthRange(0, 1);
+       GL_PolygonOffset(0, 0);
        GL_DepthTest(false); // don't modify or read zbuffer
-       m.pointer_vertex = skyboxvertex3f;
-       m.pointer_texcoord[0] = skyboxtexcoord2f;
+       R_Mesh_VertexPointer(skyboxvertex3f, 0, 0);
+       R_Mesh_ColorPointer(NULL, 0, 0);
+       R_Mesh_ResetTextureState();
+       R_Mesh_TexCoordPointer(0, 2, skyboxtexcoord2f, 0, 0);
        GL_LockArrays(0, 6*4);
        for (i = 0;i < 6;i++)
        {
-               m.tex[0] = R_GetTexture(skyboxside[i]);
-               R_Mesh_State(&m);
-               R_Mesh_Draw(6*4, 2, skyboxelements + i * 6);
+               R_Mesh_TexBind(0, R_GetTexture(skyboxside[i]));
+               R_Mesh_Draw(0, 6*4, 2, skyboxelements + i * 6, 0, 0);
        }
        GL_LockArrays(0, 0);
 }
@@ -310,7 +350,6 @@ static void R_SkySphere(void)
 {
        float speedscale;
        static qboolean skysphereinitialized = false;
-       rmeshstate_t m;
        matrix4x4_t scroll1matrix, scroll2matrix;
        if (!skysphereinitialized)
        {
@@ -318,50 +357,52 @@ static void R_SkySphere(void)
                skyspherecalc();
        }
 
+       // wrap the scroll values just to be extra kind to float accuracy
+
        // scroll speed for upper layer
-       speedscale = cl.time*8.0/128.0;
-       // wrap the scroll just to be extra kind to float accuracy
+       speedscale = r_refdef.time*r_skyscroll1.value*8.0/128.0;
        speedscale -= (int)speedscale;
-
-       // 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);
+       // scroll speed for lower layer (transparent layer)
+       speedscale = r_refdef.time*r_skyscroll2.value*8.0/128.0;
+       speedscale -= (int)speedscale;
+       Matrix4x4_CreateTranslate(&scroll2matrix, speedscale, speedscale, 0);
 
-       GL_Color(1, 1, 1, 1);
+       // FIXME: fixed function path can't properly handle r_view.colorscale > 1
+       GL_Color(1 * r_view.colorscale, 1 * r_view.colorscale, 1 * r_view.colorscale, 1);
        GL_BlendFunc(GL_ONE, GL_ZERO);
        GL_DepthMask(true);
+       GL_DepthRange(0, 1);
+       GL_PolygonOffset(0, 0);
        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;
-       m.texmatrix[0] = scroll1matrix;
-       if (r_textureunits.integer >= 2)
+       R_Mesh_VertexPointer(skysphere_vertex3f, 0, 0);
+       R_Mesh_ColorPointer(NULL, 0, 0);
+       R_Mesh_ResetTextureState();
+       R_Mesh_TexBind(0, R_GetTexture(r_refdef.worldmodel->brush.solidskytexture));
+       R_Mesh_TexCoordPointer(0, 2, skysphere_texcoord2f, 0, 0);
+       R_Mesh_TexMatrix(0, &scroll1matrix);
+       if (r_textureunits.integer >= 2 && r_view.colorscale == 1)
        {
                // one pass using GL_DECAL or GL_INTERPOLATE_ARB for alpha layer
-               m.tex[1] = R_GetTexture(alphaskytexture);
-               m.texcombinergb[1] = gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL;
-               m.pointer_texcoord[1] = skysphere_texcoord2f;
-               m.texmatrix[1] = scroll2matrix;
-               R_Mesh_State(&m);
+               R_Mesh_TexBind(1, R_GetTexture(r_refdef.worldmodel->brush.alphaskytexture));
+               R_Mesh_TexCombine(1, gl_combine.integer ? GL_INTERPOLATE_ARB : GL_DECAL, GL_MODULATE, 1, 1);
+               R_Mesh_TexCoordPointer(1, 2, skysphere_texcoord2f, 0, 0);
+               R_Mesh_TexMatrix(1, &scroll2matrix);
                GL_LockArrays(0, skysphere_numverts);
-               R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
+               R_Mesh_Draw(0, skysphere_numverts, skysphere_numtriangles, skysphere_element3i, 0, 0);
                GL_LockArrays(0, 0);
        }
        else
        {
                // two pass
-               R_Mesh_State(&m);
                GL_LockArrays(0, skysphere_numverts);
-               R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
-               GL_LockArrays(0, 0);
+               R_Mesh_Draw(0, skysphere_numverts, skysphere_numtriangles, skysphere_element3i, 0, 0);
 
                GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-               m.tex[0] = R_GetTexture(alphaskytexture);
-               m.texmatrix[0] = scroll2matrix;
-               R_Mesh_State(&m);
+               R_Mesh_TexBind(0, R_GetTexture(r_refdef.worldmodel->brush.alphaskytexture));
+               R_Mesh_TexMatrix(0, &scroll2matrix);
                GL_LockArrays(0, skysphere_numverts);
-               R_Mesh_Draw(skysphere_numverts, skysphere_numtriangles, skysphere_element3i);
+               R_Mesh_Draw(0, skysphere_numverts, skysphere_numtriangles, skysphere_element3i, 0, 0);
                GL_LockArrays(0, 0);
        }
 }
@@ -371,7 +412,7 @@ void R_Sky(void)
        matrix4x4_t skymatrix;
        if (skyrendermasked)
        {
-               Matrix4x4_CreateTranslate(&skymatrix, r_vieworigin[0], r_vieworigin[1], r_vieworigin[2]);
+               Matrix4x4_CreateTranslate(&skymatrix, r_view.origin[0], r_view.origin[1], r_view.origin[2]);
                R_Mesh_Matrix(&skymatrix);
                if (skyrendersphere)
                {
@@ -392,99 +433,24 @@ void R_Sky(void)
                        //GL_Clear(GL_DEPTH_BUFFER_BIT);
                }
                */
+               GL_DepthRange(0, 1);
+               GL_DepthTest(true);
+               GL_DepthMask(true);
        }
 }
 
 //===============================================================
 
-/*
-=============
-R_InitSky
-
-A sky texture is 256*128, with the right side being a masked overlay
-==============
-*/
-void R_InitSky (qbyte *src, int bytesperpixel)
-{
-       int i, j;
-       unsigned solidpixels[128*128], alphapixels[128*128];
-
-       skyavailable_quake = true;
-
-       // flush skytexturepool so we won't build up a leak from uploading textures multiple times
-       R_FreeTexturePool(&skytexturepool);
-       skytexturepool = R_AllocTexturePool();
-       solidskytexture = NULL;
-       alphaskytexture = NULL;
-
-       if (bytesperpixel == 4)
-       {
-               for (i = 0;i < 128;i++)
-               {
-                       for (j = 0;j < 128;j++)
-                       {
-                               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 (j = 0;j < 128;j++)
-                       {
-                               rgba.i = palette_complete[src[i*256 + j + 128]];
-                               r += rgba.b[0];
-                               g += rgba.b[1];
-                               b += rgba.b[2];
-                       }
-               }
-               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++)
-                       {
-                               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;
-                       }
-               }
-       }
-
-       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)
-{
-       skyavailable_quake = false;
-}
-
 void R_ResetSkyBox(void)
 {
-       skyboxside[0] = skyboxside[1] = skyboxside[2] = skyboxside[3] = skyboxside[4] = skyboxside[5] = NULL;
+       R_UnloadSkyBox();
        skyname[0] = 0;
-       skyavailable_box = false;
+       R_LoadSkyBox();
 }
 
 static void r_sky_start(void)
 {
        skytexturepool = R_AllocTexturePool();
-       solidskytexture = NULL;
-       alphaskytexture = NULL;
        R_LoadSkyBox();
 }
 
@@ -492,20 +458,21 @@ static void r_sky_shutdown(void)
 {
        R_UnloadSkyBox();
        R_FreeTexturePool(&skytexturepool);
-       solidskytexture = NULL;
-       alphaskytexture = NULL;
 }
 
 static void r_sky_newmap(void)
 {
 }
 
+
 void R_Sky_Init(void)
 {
-       Cmd_AddCommand ("loadsky", &LoadSky_f);
+       Cmd_AddCommand ("loadsky", &LoadSky_f, "load a skybox by basename (for example loadsky mtnsun_ loads mtnsun_ft.tga and so on)");
        Cvar_RegisterVariable (&r_sky);
-       R_ResetSkyBox();
-       R_ResetQuakeSky();
+       Cvar_RegisterVariable (&r_skyscroll1);
+       Cvar_RegisterVariable (&r_skyscroll2);
+       memset(&skyboxside, 0, sizeof(skyboxside));
+       skyname[0] = 0;
        R_RegisterModule("R_Sky", r_sky_start, r_sky_shutdown, r_sky_newmap);
 }