]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - gl_rmain.c
eliminated qbyte type, now uses unsigned char throughout the engine for this purpose
[xonotic/darkplaces.git] / gl_rmain.c
index fc65bbc341efe2ea4acddc56bfab3c4009a185e0..b73ebca97b0b86d83e96198bb8cabd530c537dda 100644 (file)
@@ -71,9 +71,6 @@ matrix4x4_t r_view_matrix;
 //
 refdef_t r_refdef;
 
-// 8.8 fraction of base light value
-unsigned short d_lightstylevalue[256];
-
 cvar_t r_showtris = {0, "r_showtris", "0"};
 cvar_t r_drawentities = {0, "r_drawentities","1"};
 cvar_t r_drawviewmodel = {0, "r_drawviewmodel","1"};
@@ -121,6 +118,8 @@ rtexture_t *r_texture_black;
 rtexture_t *r_texture_notexture;
 rtexture_t *r_texture_whitecube;
 rtexture_t *r_texture_normalizationcube;
+rtexture_t *r_texture_fogattenuation;
+rtexture_t *r_texture_fogintensity;
 
 void R_ModulateColors(float *in, float *out, int verts, float r, float g, float b)
 {
@@ -151,6 +150,11 @@ void R_FillColors(float *out, int verts, float r, float g, float b, float a)
 
 vec3_t fogcolor;
 vec_t fogdensity;
+vec_t fogrange;
+vec_t fograngerecip;
+int fogtableindex;
+vec_t fogtabledistmultiplier;
+float fogtable[FOGTABLEWIDTH];
 float fog_density, fog_red, fog_green, fog_blue;
 qboolean fogenabled;
 qboolean oldgl_fogenable;
@@ -185,6 +189,12 @@ void R_UpdateFog(void)
        {
                fogenabled = true;
                fogdensity = -4000.0f / (fog_density * fog_density);
+               // this is the point where the fog reaches 0.9986 alpha, which we
+               // consider a good enough cutoff point for the texture
+               // (0.9986 * 256 == 255.6)
+               fogrange = 400 / fog_density;
+               fograngerecip = 1.0f / fogrange;
+               fogtabledistmultiplier = FOGTABLEWIDTH * fograngerecip;
                // fog color was already set
        }
        else
@@ -208,6 +218,9 @@ void FOG_clear(void)
 // FIXME: move this to client?
 void FOG_registercvars(void)
 {
+       int x;
+       double r, alpha;
+
        if (gamemode == GAME_NEHAHRA)
        {
                Cvar_RegisterVariable (&gl_fogenable);
@@ -218,11 +231,20 @@ void FOG_registercvars(void)
                Cvar_RegisterVariable (&gl_fogstart);
                Cvar_RegisterVariable (&gl_fogend);
        }
+
+       r = (-1.0/256.0) * (FOGTABLEWIDTH * FOGTABLEWIDTH);
+       for (x = 0;x < FOGTABLEWIDTH;x++)
+       {
+               alpha = exp(r / ((double)x*(double)x));
+               if (x == FOGTABLEWIDTH - 1)
+                       alpha = 1;
+               fogtable[x] = bound(0, alpha, 1);
+       }
 }
 
 static void R_BuildBlankTextures(void)
 {
-       qbyte data[4];
+       unsigned char data[4];
        data[0] = 128; // normal X
        data[1] = 128; // normal Y
        data[2] = 255; // normal Z
@@ -243,7 +265,7 @@ static void R_BuildBlankTextures(void)
 static void R_BuildNoTexture(void)
 {
        int x, y;
-       qbyte pix[16][16][4];
+       unsigned char pix[16][16][4];
        // this makes a light grey/dark grey checkerboard texture
        for (y = 0;y < 16;y++)
        {
@@ -270,7 +292,7 @@ static void R_BuildNoTexture(void)
 
 static void R_BuildWhiteCube(void)
 {
-       qbyte data[6*1*1*4];
+       unsigned char data[6*1*1*4];
        data[ 0] = 255;data[ 1] = 255;data[ 2] = 255;data[ 3] = 255;
        data[ 4] = 255;data[ 5] = 255;data[ 6] = 255;data[ 7] = 255;
        data[ 8] = 255;data[ 9] = 255;data[10] = 255;data[11] = 255;
@@ -286,7 +308,7 @@ static void R_BuildNormalizationCube(void)
        vec3_t v;
        vec_t s, t, intensity;
 #define NORMSIZE 64
-       qbyte data[6][NORMSIZE][NORMSIZE][4];
+       unsigned char data[6][NORMSIZE][NORMSIZE][4];
        for (side = 0;side < 6;side++)
        {
                for (y = 0;y < NORMSIZE;y++)
@@ -340,6 +362,34 @@ static void R_BuildNormalizationCube(void)
        r_texture_normalizationcube = R_LoadTextureCubeMap(r_main_texturepool, "normalcube", NORMSIZE, &data[0][0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_CLAMP, NULL);
 }
 
+static void R_BuildFogTexture(void)
+{
+       int x, b;
+       double r, alpha;
+#define FOGWIDTH 64
+       unsigned char data1[FOGWIDTH][4];
+       unsigned char data2[FOGWIDTH][4];
+       r = (-1.0/256.0) * (FOGWIDTH * FOGWIDTH);
+       for (x = 0;x < FOGWIDTH;x++)
+       {
+               alpha = exp(r / ((double)x*(double)x));
+               if (x == FOGWIDTH - 1)
+                       alpha = 1;
+               b = (int)(256.0 * alpha);
+               b = bound(0, b, 255);
+               data1[x][0] = 255 - b;
+               data1[x][1] = 255 - b;
+               data1[x][2] = 255 - b;
+               data1[x][3] = 255;
+               data2[x][0] = b;
+               data2[x][1] = b;
+               data2[x][2] = b;
+               data2[x][3] = 255;
+       }
+       r_texture_fogattenuation = R_LoadTexture2D(r_main_texturepool, "fogattenuation", FOGWIDTH, 1, &data1[0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
+       r_texture_fogintensity = R_LoadTexture2D(r_main_texturepool, "fogintensity", FOGWIDTH, 1, &data2[0][0], TEXTYPE_RGBA, TEXF_PRECACHE | TEXF_FORCELINEAR | TEXF_CLAMP, NULL);
+}
+
 void gl_main_start(void)
 {
        r_main_texturepool = R_AllocTexturePool();
@@ -352,6 +402,7 @@ void gl_main_start(void)
                R_BuildWhiteCube();
                R_BuildNormalizationCube();
        }
+       R_BuildFogTexture();
 }
 
 void gl_main_shutdown(void)
@@ -582,7 +633,6 @@ static void R_MarkEntities (void)
                for (i = 0;i < r_refdef.numentities;i++)
                {
                        ent = r_refdef.entities[i];
-                       Mod_CheckLoaded(ent->model);
                        // some of the renderer still relies on origin...
                        Matrix4x4_OriginFromMatrix(&ent->matrix, ent->origin);
                        // some of the renderer still relies on scale...
@@ -600,7 +650,6 @@ static void R_MarkEntities (void)
                for (i = 0;i < r_refdef.numentities;i++)
                {
                        ent = r_refdef.entities[i];
-                       Mod_CheckLoaded(ent->model);
                        // some of the renderer still relies on origin...
                        Matrix4x4_OriginFromMatrix(&ent->matrix, ent->origin);
                        // some of the renderer still relies on scale...
@@ -941,7 +990,6 @@ void R_RenderView(void)
        R_ClearScreen();
        R_Textures_Frame();
        R_UpdateFog();
-       R_UpdateLights();
        R_TimeReport("setup");
 
        qglDepthFunc(GL_LEQUAL);
@@ -1097,8 +1145,7 @@ void R_DrawBBoxMesh(vec3_t mins, vec3_t maxs, float cr, float cg, float cb, floa
        {
                for (i = 0, v = vertex, c = color;i < 8;i++, v += 4, c += 4)
                {
-                       VectorSubtract(v, r_vieworigin, diff);
-                       f2 = exp(fogdensity/DotProduct(diff, diff));
+                       f2 = VERTEXFOGTABLE(VectorDistance(v, r_vieworigin));
                        f1 = 1 - f2;
                        c[0] = c[0] * f1 + fogcolor[0] * f2;
                        c[1] = c[1] * f1 + fogcolor[1] * f2;
@@ -1149,7 +1196,7 @@ void R_DrawNoModelCallback(const void *calldata1, int calldata2)
 {
        const entity_render_t *ent = (entity_render_t *)calldata1;
        int i;
-       float f1, f2, *c, diff[3];
+       float f1, f2, *c;
        float color4f[6*4];
        rmeshstate_t m;
        R_Mesh_Matrix(&ent->matrix);
@@ -1177,8 +1224,7 @@ void R_DrawNoModelCallback(const void *calldata1, int calldata2)
        {
                memcpy(color4f, nomodelcolor4f, sizeof(float[6*4]));
                m.pointer_color = color4f;
-               VectorSubtract(ent->origin, r_vieworigin, diff);
-               f2 = exp(fogdensity/DotProduct(diff, diff));
+               f2 = VERTEXFOGTABLE(VectorDistance(ent->origin, r_vieworigin));
                f1 = 1 - f2;
                for (i = 0, c = color4f;i < 6;i++, c += 4)
                {
@@ -1241,16 +1287,14 @@ void R_CalcBeam_Vertex3f (float *vert, const vec3_t org1, const vec3_t org2, flo
 
 float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
 
-void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, int depthdisable, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2, float cr, float cg, float cb, float ca)
+void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, rtexture_t *fogtexture, int depthdisable, const vec3_t origin, const vec3_t left, const vec3_t up, float scalex1, float scalex2, float scaley1, float scaley2, float cr, float cg, float cb, float ca)
 {
-       float diff[3];
+       float fog = 0.0f, ifog;
        rmeshstate_t m;
 
        if (fogenabled)
-       {
-               VectorSubtract(origin, r_vieworigin, diff);
-               ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
-       }
+               fog = VERTEXFOGTABLE(VectorDistance(origin, r_vieworigin));
+       ifog = 1 - fog;
 
        R_Mesh_Matrix(&r_identitymatrix);
        GL_BlendFunc(blendfunc1, blendfunc2);
@@ -1275,8 +1319,16 @@ void R_DrawSprite(int blendfunc1, int blendfunc2, rtexture_t *texture, int depth
        m.pointer_texcoord[0] = spritetexcoord2f;
        m.pointer_vertex = varray_vertex3f;
        R_Mesh_State(&m);
-       GL_Color(cr, cg, cb, ca);
+       GL_Color(cr * ifog, cg * ifog, cb * ifog, ca);
        R_Mesh_Draw(0, 4, 2, polygonelements);
+
+       if (blendfunc2 == GL_ONE_MINUS_SRC_ALPHA)
+       {
+               R_Mesh_TexBind(0, R_GetTexture(fogtexture));
+               GL_BlendFunc(blendfunc1, GL_ONE);
+               GL_Color(fogcolor[0] * fog, fogcolor[1] * fog, fogcolor[2] * fog, ca);
+               R_Mesh_Draw(0, 4, 2, polygonelements);
+       }
 }
 
 int R_Mesh_AddVertex3f(rmesh_t *mesh, const float *v)
@@ -1459,7 +1511,7 @@ void R_UpdateTextureInfo(const entity_render_t *ent, texture_t *t)
                                        // would normally be baked into the lightmaptexture must be
                                        // applied to the color
                                        if (ent->model->type == mod_brushq3)
-                                               colorscale *= d_lightstylevalue[0] * (1.0f / 256.0f);
+                                               colorscale *= r_refdef.lightstylevalue[0] * (1.0f / 256.0f);
                                        // transparent and fullbright are not affected by r_lightmapintensity
                                        if (!(t->currentmaterialflags & MATERIALFLAG_TRANSPARENT))
                                                colorscale *= r_lightmapintensity;
@@ -1617,7 +1669,6 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
        int i;
        float f;
        float *v, *c, *c2;
-       vec3_t diff;
        if (lightmode >= 2)
        {
                // model lighting
@@ -1654,26 +1705,26 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
                {
                        for (i = 0, c = varray_color4f + 4 * surface->num_firstvertex;i < surface->num_vertices;i++, c += 4)
                        {
-                               const qbyte *lm = surface->lightmapinfo->samples + (surface->groupmesh->data_lightmapoffsets + surface->num_firstvertex)[i];
+                               const unsigned char *lm = surface->lightmapinfo->samples + (surface->groupmesh->data_lightmapoffsets + surface->num_firstvertex)[i];
                                if (lm)
                                {
-                                       float scale = d_lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
+                                       float scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[0]] * (1.0f / 32768.0f);
                                        VectorScale(lm, scale, c);
                                        if (surface->lightmapinfo->styles[1] != 255)
                                        {
                                                int size3 = ((surface->lightmapinfo->extents[0]>>4)+1)*((surface->lightmapinfo->extents[1]>>4)+1)*3;
                                                lm += size3;
-                                               scale = d_lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
+                                               scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[1]] * (1.0f / 32768.0f);
                                                VectorMA(c, scale, lm, c);
                                                if (surface->lightmapinfo->styles[2] != 255)
                                                {
                                                        lm += size3;
-                                                       scale = d_lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
+                                                       scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[2]] * (1.0f / 32768.0f);
                                                        VectorMA(c, scale, lm, c);
                                                        if (surface->lightmapinfo->styles[3] != 255)
                                                        {
                                                                lm += size3;
-                                                               scale = d_lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
+                                                               scale = r_refdef.lightstylevalue[surface->lightmapinfo->styles[3]] * (1.0f / 32768.0f);
                                                                VectorMA(c, scale, lm, c);
                                                        }
                                                }
@@ -1695,8 +1746,7 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
                {
                        for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (rsurface_lightmapcolor4f + 4 * surface->num_firstvertex), c2 = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4, c2 += 4)
                        {
-                               VectorSubtract(v, modelorg, diff);
-                               f = 1 - exp(fogdensity/DotProduct(diff, diff));
+                               f = 1 - VERTEXFOGTABLE(VectorDistance(v, modelorg));
                                c2[0] = c[0] * f;
                                c2[1] = c[1] * f;
                                c2[2] = c[2] * f;
@@ -1707,8 +1757,7 @@ void RSurf_SetColorPointer(const entity_render_t *ent, const msurface_t *surface
                {
                        for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c2 = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c2 += 4)
                        {
-                               VectorSubtract(v, modelorg, diff);
-                               f = 1 - exp(fogdensity/DotProduct(diff, diff));
+                               f = 1 - VERTEXFOGTABLE(VectorDistance(v, modelorg));
                                c2[0] = f;
                                c2[1] = f;
                                c2[2] = f;
@@ -1949,7 +1998,6 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                for (texturesurfaceindex = 0;texturesurfaceindex < texturenumsurfaces;texturesurfaceindex++)
                                {
                                        int i;
-                                       float diff[3];
                                        float f, *v, *c;
                                        surface = texturesurfacelist[texturesurfaceindex];
                                        RSurf_SetVertexPointer(ent, texture, surface, modelorg);
@@ -1958,8 +2006,7 @@ static void R_DrawTextureSurfaceList(const entity_render_t *ent, texture_t *text
                                        R_Mesh_ColorPointer(varray_color4f);
                                        for (i = 0, v = (rsurface_vertex3f + 3 * surface->num_firstvertex), c = (varray_color4f + 4 * surface->num_firstvertex);i < surface->num_vertices;i++, v += 3, c += 4)
                                        {
-                                               VectorSubtract(v, modelorg, diff);
-                                               f = exp(fogdensity/DotProduct(diff, diff));
+                                               f = VERTEXFOGTABLE(VectorDistance(v, modelorg));
                                                c[0] = layercolor[0];
                                                c[1] = layercolor[1];
                                                c[2] = layercolor[2];
@@ -2062,9 +2109,9 @@ void R_DrawSurfaces(entity_render_t *ent, qboolean skysurfaces)
        {
                for (i = 0;i < model->brushq1.light_styles;i++)
                {
-                       if (model->brushq1.light_stylevalue[i] != d_lightstylevalue[model->brushq1.light_style[i]])
+                       if (model->brushq1.light_stylevalue[i] != r_refdef.lightstylevalue[model->brushq1.light_style[i]])
                        {
-                               model->brushq1.light_stylevalue[i] = d_lightstylevalue[model->brushq1.light_style[i]];
+                               model->brushq1.light_stylevalue[i] = r_refdef.lightstylevalue[model->brushq1.light_style[i]];
                                if ((surfacechain = model->brushq1.light_styleupdatechains[i]))
                                        for (;(surface = *surfacechain);surfacechain++)
                                                surface->cached_dlight = true;