]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cl_particles.c
use dynamic eye position-centered bouncegrid when rendering in dynamic
[xonotic/darkplaces.git] / cl_particles.c
index 08652af41bcc11091976fc477a264ec4aa745c9e..187934c32d4dd69bf81ea46a526896c0a6d197b4 100644 (file)
@@ -219,7 +219,7 @@ cvar_t cl_decals = {CVAR_SAVE, "cl_decals", "1", "enables decals (bullet holes,
 cvar_t cl_decals_visculling = {CVAR_SAVE, "cl_decals_visculling", "1", "perform a very cheap check if each decal is visible before drawing"};
 cvar_t cl_decals_time = {CVAR_SAVE, "cl_decals_time", "20", "how long before decals start to fade away"};
 cvar_t cl_decals_fadetime = {CVAR_SAVE, "cl_decals_fadetime", "1", "how long decals take to fade away"};
-cvar_t cl_decals_newsystem = {CVAR_SAVE, "cl_decals_newsystem", "0", "enables new advanced decal system"};
+cvar_t cl_decals_newsystem = {CVAR_SAVE, "cl_decals_newsystem", "1", "enables new advanced decal system"};
 cvar_t cl_decals_newsystem_intensitymultiplier = {CVAR_SAVE, "cl_decals_newsystem_intensitymultiplier", "2", "boosts intensity of decals (because the distance fade can make them hard to see otherwise)"};
 cvar_t cl_decals_newsystem_immediatebloodstain = {CVAR_SAVE, "cl_decals_newsystem_immediatebloodstain", "2", "0: no on-spawn blood stains; 1: on-spawn blood stains for pt_blood; 2: always use on-spawn blood stains"};
 cvar_t cl_decals_models = {CVAR_SAVE, "cl_decals_models", "0", "enables decals on animated models (if newsystem is also 1)"};
@@ -842,7 +842,7 @@ void CL_ParticleEffect_Fallback(int effectnameindex, float count, const vec3_t o
                {
                        // bloodhack checks if this effect's color matches regular or lightning blood and if so spawns a blood effect instead
                        if (count == 1024)
-                               CL_ParticleExplosion(center);
+                               CL_ParticleEffect(EFFECT_TE_EXPLOSION, 1, originmins, originmaxs, velocitymins, velocitymaxs, NULL, 0);
                        else if (cl_particles_blood_bloodhack.integer && !cl_particles_quake.integer && (palettecolor == 73 || palettecolor == 225))
                                CL_ParticleEffect(EFFECT_TE_BLOOD, count / 2.0f, originmins, originmaxs, velocitymins, velocitymaxs, NULL, 0);
                        else
@@ -1687,16 +1687,15 @@ void CL_ParticleExplosion (const vec3_t org)
                        {
                                for (i = 0;i < 512 * cl_particles_quality.value;i++)
                                {
-                                       int k;
+                                       int k = 0;
                                        vec3_t v, v2;
-                                       for (k = 0;k < 16;k++)
+                                       do
                                        {
                                                VectorRandom(v2);
                                                VectorMA(org, 128, v2, v);
                                                trace = CL_TraceLine(org, v, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, true, false, NULL, false);
-                                               if (trace.fraction >= 0.1)
-                                                       break;
                                        }
+                                       while (k < 16 && trace.fraction < 0.1f);
                                        VectorSubtract(trace.endpos, org, v2);
                                        VectorScale(v2, 2.0f, v2);
                                        CL_NewParticle(org, pt_spark, 0x903010, 0xFFD030, tex_particle, 1.0f, 0, lhrandom(0, 255), 512, 0, 0, org[0], org[1], org[2], v2[0], v2[1], v2[2], 0, 0, 0, 0, true, 0, 1, PBLEND_ADD, PARTICLE_SPARK, -1, -1, -1, 1, 1, 0, 0, NULL);
@@ -1827,6 +1826,8 @@ void CL_ParticleRain (const vec3_t mins, const vec3_t maxs, const vec3_t dir, in
 
 static cvar_t r_drawparticles = {0, "r_drawparticles", "1", "enables drawing of particles"};
 static cvar_t r_drawparticles_drawdistance = {CVAR_SAVE, "r_drawparticles_drawdistance", "2000", "particles further than drawdistance*size will not be drawn"};
+static cvar_t r_drawparticles_nearclip_min = {CVAR_SAVE, "r_drawparticles_nearclip_min", "4", "particles closer than drawnearclip_min will not be drawn"};
+static cvar_t r_drawparticles_nearclip_max = {CVAR_SAVE, "r_drawparticles_nearclip_max", "4", "particles closer than drawnearclip_min will be faded"};
 static cvar_t r_drawdecals = {0, "r_drawdecals", "1", "enables drawing of decals"};
 static cvar_t r_drawdecals_drawdistance = {CVAR_SAVE, "r_drawdecals_drawdistance", "500", "decals further than drawdistance*size will not be drawn"};
 
@@ -1982,6 +1983,7 @@ static void R_InitParticleTexture (void)
        char *buf;
        fs_offset_t filesize;
        char texturename[MAX_QPATH];
+       skinframe_t *sf;
 
        // a note: decals need to modulate (multiply) the background color to
        // properly darken it (stain), and they need to be able to alpha fade,
@@ -1993,7 +1995,7 @@ static void R_InitParticleTexture (void)
        // we invert it again during the blendfunc to make it work...
 
 #ifndef DUMPPARTICLEFONT
-       decalskinframe = R_SkinFrame_LoadExternal("particles/particlefont.tga", TEXF_ALPHA | TEXF_FORCELINEAR, false);
+       decalskinframe = R_SkinFrame_LoadExternal("particles/particlefont.tga", TEXF_ALPHA | TEXF_FORCELINEAR | TEXF_RGBMULTIPLYBYALPHA, false);
        if (decalskinframe)
        {
                particlefonttexture = decalskinframe->base;
@@ -2138,7 +2140,7 @@ static void R_InitParticleTexture (void)
                Image_WriteTGABGRA ("particles/particlefont.tga", PARTICLEFONTSIZE, PARTICLEFONTSIZE, particletexturedata);
 #endif
 
-               decalskinframe = R_SkinFrame_LoadInternalBGRA("particlefont", TEXF_ALPHA | TEXF_FORCELINEAR, particletexturedata, PARTICLEFONTSIZE, PARTICLEFONTSIZE);
+               decalskinframe = R_SkinFrame_LoadInternalBGRA("particlefont", TEXF_ALPHA | TEXF_FORCELINEAR | TEXF_RGBMULTIPLYBYALPHA, particletexturedata, PARTICLEFONTSIZE, PARTICLEFONTSIZE);
                particlefonttexture = decalskinframe->base;
 
                Mem_Free(particletexturedata);
@@ -2157,7 +2159,7 @@ static void R_InitParticleTexture (void)
        }
 
 #ifndef DUMPPARTICLEFONT
-       particletexture[tex_beam].texture = loadtextureimage(particletexturepool, "particles/nexbeam.tga", false, TEXF_ALPHA | TEXF_FORCELINEAR, true, r_texture_convertsRGB_particles.integer != 0);
+       particletexture[tex_beam].texture = loadtextureimage(particletexturepool, "particles/nexbeam.tga", false, TEXF_ALPHA | TEXF_FORCELINEAR | TEXF_RGBMULTIPLYBYALPHA, true, r_texture_convertsRGB_particles.integer != 0);
        if (!particletexture[tex_beam].texture)
 #endif
        {
@@ -2180,7 +2182,7 @@ static void R_InitParticleTexture (void)
 #ifdef DUMPPARTICLEFONT
                Image_WriteTGABGRA ("particles/nexbeam.tga", 64, 64, &data2[0][0][0]);
 #endif
-               particletexture[tex_beam].texture = R_LoadTexture2D(particletexturepool, "nexbeam", 16, 64, &data2[0][0][0], TEXTYPE_BGRA, TEXF_ALPHA | TEXF_FORCELINEAR, -1, NULL);
+               particletexture[tex_beam].texture = R_LoadTexture2D(particletexturepool, "nexbeam", 16, 64, &data2[0][0][0], TEXTYPE_BGRA, TEXF_ALPHA | TEXF_FORCELINEAR | TEXF_RGBMULTIPLYBYALPHA, -1, NULL);
        }
        particletexture[tex_beam].s1 = 0;
        particletexture[tex_beam].t1 = 0;
@@ -2209,9 +2211,11 @@ static void R_InitParticleTexture (void)
 
                        if (COM_ParseToken_Simple(&bufptr, true, false) && strcmp(com_token, "\n"))
                        {
+                               strlcpy(texturename, com_token, sizeof(texturename));
                                s1 = atof(com_token);
                                if (COM_ParseToken_Simple(&bufptr, true, false) && strcmp(com_token, "\n"))
                                {
+                                       texturename[0] = 0;
                                        t1 = atof(com_token);
                                        if (COM_ParseToken_Simple(&bufptr, true, false) && strcmp(com_token, "\n"))
                                        {
@@ -2226,10 +2230,7 @@ static void R_InitParticleTexture (void)
                                        }
                                }
                                else
-                               {
                                        s1 = 0;
-                                       strlcpy(texturename, com_token, sizeof(texturename));
-                               }
                        }
                        if (!texturename[0])
                        {
@@ -2241,7 +2242,13 @@ static void R_InitParticleTexture (void)
                                Con_Printf("particles/particlefont.txt: texnum %i outside valid range (0 to %i)\n", i, MAX_PARTICLETEXTURES);
                                continue;
                        }
-                       particletexture[i].texture = R_SkinFrame_LoadExternal(texturename, TEXF_ALPHA | TEXF_FORCELINEAR, false)->base;
+                       sf = R_SkinFrame_LoadExternal(texturename, TEXF_ALPHA | TEXF_FORCELINEAR | TEXF_RGBMULTIPLYBYALPHA, true);
+                       if(!sf)
+                       {
+                               // R_SkinFrame_LoadExternal already complained
+                               continue;
+                       }
+                       particletexture[i].texture = sf->base;
                        particletexture[i].s1 = s1;
                        particletexture[i].t1 = t1;
                        particletexture[i].s2 = s2;
@@ -2293,6 +2300,8 @@ void R_Particles_Init (void)
 
        Cvar_RegisterVariable(&r_drawparticles);
        Cvar_RegisterVariable(&r_drawparticles_drawdistance);
+       Cvar_RegisterVariable(&r_drawparticles_nearclip_min);
+       Cvar_RegisterVariable(&r_drawparticles_nearclip_max);
        Cvar_RegisterVariable(&r_drawdecals);
        Cvar_RegisterVariable(&r_drawdecals_drawdistance);
        R_RegisterModule("R_Particles", r_part_start, r_part_shutdown, r_part_newmap, NULL, NULL);
@@ -2310,7 +2319,7 @@ void R_DrawDecal_TransparentCallback(const entity_render_t *ent, const rtlight_t
        RSurf_ActiveWorldEntity();
 
        r_refdef.stats.drawndecals += numsurfaces;
-       R_Mesh_ResetTextureState();
+//     R_Mesh_ResetTextureState();
        GL_DepthMask(false);
        GL_DepthRange(0, 1);
        GL_PolygonOffset(0, 0);
@@ -2460,39 +2469,47 @@ void R_DrawParticle_TransparentCallback(const entity_render_t *ent, const rtligh
        particletexture_t *tex;
        float up2[3], v[3], right[3], up[3], fog, ifog, size, len, lenfactor, alpha;
        float ambient[3], diffuse[3], diffusenormal[3];
-       float spintime, spinrad, spincos, spinsin, spinm1, spinm2, spinm3, spinm4, baseright[3], baseup[3];
+       float palpha, spintime, spinrad, spincos, spinsin, spinm1, spinm2, spinm3, spinm4, baseright[3], baseup[3];
        vec4_t colormultiplier;
+       float minparticledist_start, minparticledist_end;
+       qboolean dofade;
 
        RSurf_ActiveWorldEntity();
 
        Vector4Set(colormultiplier, r_refdef.view.colorscale * (1.0 / 256.0f), r_refdef.view.colorscale * (1.0 / 256.0f), r_refdef.view.colorscale * (1.0 / 256.0f), cl_particles_alpha.value * (1.0 / 256.0f));
 
        r_refdef.stats.particles += numsurfaces;
-       R_Mesh_ResetTextureState();
+//     R_Mesh_ResetTextureState();
        GL_DepthMask(false);
        GL_DepthRange(0, 1);
        GL_PolygonOffset(0, 0);
        GL_DepthTest(true);
-       GL_AlphaTest(false);
        GL_CullFace(GL_NONE);
 
        spintime = r_refdef.scene.time;
 
+       minparticledist_start = DotProduct(r_refdef.view.origin, r_refdef.view.forward) + r_drawparticles_nearclip_min.value;
+       minparticledist_end = DotProduct(r_refdef.view.origin, r_refdef.view.forward) + r_drawparticles_nearclip_max.value;
+       dofade = (minparticledist_start < minparticledist_end);
+
        // first generate all the vertices at once
        for (surfacelistindex = 0, v3f = particle_vertex3f, t2f = particle_texcoord2f, c4f = particle_color4f;surfacelistindex < numsurfaces;surfacelistindex++, v3f += 3*4, t2f += 2*4, c4f += 4*4)
        {
                p = cl.particles + surfacelist[surfacelistindex];
 
                blendmode = (pblend_t)p->blendmode;
+               palpha = p->alpha;
+               if(dofade && p->orientation != PARTICLE_VBEAM && p->orientation != PARTICLE_HBEAM)
+                       palpha *= min(1, (DotProduct(p->org, r_refdef.view.forward)  - minparticledist_start) / (minparticledist_end - minparticledist_start));
+               alpha = palpha * colormultiplier[3];
+               // ensure alpha multiplier saturates properly
+               if (alpha > 1.0f)
+                       alpha = 1.0f;
 
                switch (blendmode)
                {
                case PBLEND_INVALID:
                case PBLEND_INVMOD:
-                       alpha = p->alpha * colormultiplier[3];
-                       // ensure alpha multiplier saturates properly
-                       if (alpha > 1.0f)
-                               alpha = 1.0f;
                        // additive and modulate can just fade out in fog (this is correct)
                        if (r_refdef.fogenabled)
                                alpha *= RSurf_FogVertex(p->org);
@@ -2501,13 +2518,9 @@ void R_DrawParticle_TransparentCallback(const entity_render_t *ent, const rtligh
                        c4f[0] = p->color[0] * alpha;
                        c4f[1] = p->color[1] * alpha;
                        c4f[2] = p->color[2] * alpha;
-                       c4f[3] = 1;
+                       c4f[3] = 0;
                        break;
                case PBLEND_ADD:
-                       alpha = p->alpha * colormultiplier[3];
-                       // ensure alpha multiplier saturates properly
-                       if (alpha > 1.0f)
-                               alpha = 1.0f;
                        // additive and modulate can just fade out in fog (this is correct)
                        if (r_refdef.fogenabled)
                                alpha *= RSurf_FogVertex(p->org);
@@ -2515,17 +2528,17 @@ void R_DrawParticle_TransparentCallback(const entity_render_t *ent, const rtligh
                        c4f[0] = p->color[0] * colormultiplier[0] * alpha;
                        c4f[1] = p->color[1] * colormultiplier[1] * alpha;
                        c4f[2] = p->color[2] * colormultiplier[2] * alpha;
-                       c4f[3] = 1;
+                       c4f[3] = 0;
                        break;
                case PBLEND_ALPHA:
                        c4f[0] = p->color[0] * colormultiplier[0];
                        c4f[1] = p->color[1] * colormultiplier[1];
                        c4f[2] = p->color[2] * colormultiplier[2];
-                       c4f[3] = p->alpha * colormultiplier[3];
+                       c4f[3] = alpha;
                        // note: lighting is not cheap!
                        if (particletype[p->typeindex].lighting)
                        {
-                               R_CompleteLightPoint(ambient, diffuse, diffusenormal, p->org, true);
+                               R_CompleteLightPoint(ambient, diffuse, diffusenormal, p->org, LP_LIGHTMAP | LP_RTWORLD | LP_DYNLIGHT);
                                c4f[0] *= (ambient[0] + 0.5 * diffuse[0]);
                                c4f[1] *= (ambient[1] + 0.5 * diffuse[1]);
                                c4f[2] *= (ambient[2] + 0.5 * diffuse[2]);
@@ -2539,6 +2552,8 @@ void R_DrawParticle_TransparentCallback(const entity_render_t *ent, const rtligh
                                c4f[1] = c4f[1] * fog + r_refdef.fogcolor[1] * ifog;
                                c4f[2] = c4f[2] * fog + r_refdef.fogcolor[2] * ifog;
                        }
+                       // for premultiplied alpha we have to apply the alpha to the color (after fog of course)
+                       VectorScale(c4f, alpha, c4f);
                        break;
                }
                // copy the color into the other three vertices
@@ -2672,36 +2687,38 @@ void R_DrawParticle_TransparentCallback(const entity_render_t *ent, const rtligh
        {
                p = cl.particles + surfacelist[surfacelistindex];
 
-               if (blendmode != p->blendmode)
-               {
-                       blendmode = (pblend_t)p->blendmode;
-                       switch(blendmode)
-                       {
-                       case PBLEND_ALPHA:
-                               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-                               break;
-                       case PBLEND_INVALID:
-                       case PBLEND_ADD:
-                               GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-                               break;
-                       case PBLEND_INVMOD:
-                               GL_BlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
-                               break;
-                       }
-               }
                if (texture != particletexture[p->texnum].texture)
                {
                        texture = particletexture[p->texnum].texture;
                        R_SetupShader_Generic(texture, NULL, GL_MODULATE, 1);
                }
 
-               // iterate until we find a change in settings
-               batchstart = surfacelistindex++;
-               for (;surfacelistindex < numsurfaces;surfacelistindex++)
+               if (p->blendmode == PBLEND_INVMOD)
                {
-                       p = cl.particles + surfacelist[surfacelistindex];
-                       if (blendmode != p->blendmode || texture != particletexture[p->texnum].texture)
-                               break;
+                       // inverse modulate blend - group these
+                       GL_BlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR);
+                       // iterate until we find a change in settings
+                       batchstart = surfacelistindex++;
+                       for (;surfacelistindex < numsurfaces;surfacelistindex++)
+                       {
+                               p = cl.particles + surfacelist[surfacelistindex];
+                               if (p->blendmode != PBLEND_INVMOD || texture != particletexture[p->texnum].texture)
+                                       break;
+                       }
+               }
+               else
+               {
+                       // additive or alpha blend - group these
+                       // (we can group these because we premultiplied the texture alpha)
+                       GL_BlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
+                       // iterate until we find a change in settings
+                       batchstart = surfacelistindex++;
+                       for (;surfacelistindex < numsurfaces;surfacelistindex++)
+                       {
+                               p = cl.particles + surfacelist[surfacelistindex];
+                               if (p->blendmode == PBLEND_INVMOD || texture != particletexture[p->texnum].texture)
+                                       break;
+                       }
                }
 
                batchcount = surfacelistindex - batchstart;
@@ -2713,7 +2730,7 @@ void R_DrawParticles (void)
 {
        int i, a;
        int drawparticles = r_drawparticles.integer;
-       float minparticledist;
+       float minparticledist_start;
        particle_t *p;
        float gravity, frametime, f, dist, oldorg[3];
        float drawdist2;
@@ -2728,7 +2745,7 @@ void R_DrawParticles (void)
        if (!cl.num_particles)
                return;
 
-       minparticledist = DotProduct(r_refdef.view.origin, r_refdef.view.forward) + 4.0f;
+       minparticledist_start = DotProduct(r_refdef.view.origin, r_refdef.view.forward) + r_drawparticles_nearclip_min.value;
        gravity = frametime * cl.movevars_gravity;
        update = frametime > 0;
        drawdist2 = r_drawparticles_drawdistance.value * r_refdef.view.quality;
@@ -2778,7 +2795,7 @@ void R_DrawParticles (void)
                                VectorCopy(p->org, oldorg);
                                VectorMA(p->org, frametime, p->vel, p->org);
 //                             if (p->bounce && cl.time >= p->delayedcollisions)
-                               if (p->bounce && cl_particles_collisions.integer)
+                               if (p->bounce && cl_particles_collisions.integer && VectorLength(p->vel))
                                {
                                        trace = CL_TraceLine(oldorg, p->org, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | ((p->typeindex == pt_rain || p->typeindex == pt_snow) ? SUPERCONTENTS_LIQUIDSMASK : 0), true, false, &hitent, false);
                                        // if the trace started in or hit something of SUPERCONTENTS_NODROP
@@ -2835,11 +2852,16 @@ void R_DrawParticles (void)
                                                        // anything else - bounce off solid
                                                        dist = DotProduct(p->vel, trace.plane.normal) * -p->bounce;
                                                        VectorMA(p->vel, dist, trace.plane.normal, p->vel);
-                                                       if (DotProduct(p->vel, p->vel) < 0.03)
-                                                               VectorClear(p->vel);
                                                }
                                        }
                                }
+
+                               if (VectorLength2(p->vel) < 0.03)
+                               {
+                                       if(p->orientation == PARTICLE_SPARK) // sparks are virtually invisible if very slow, so rather let them go off
+                                               goto killparticle;
+                                       VectorClear(p->vel);
+                               }
                        }
 
                        if (p->typeindex != pt_static)
@@ -2909,7 +2931,7 @@ void R_DrawParticles (void)
                                                                continue;
                                        }
                        // anything else just has to be in front of the viewer and visible at this distance
-                       if (DotProduct(p->org, r_refdef.view.forward) >= minparticledist && VectorDistance2(p->org, r_refdef.view.origin) < drawdist2 * (p->size * p->size))
+                       if (DotProduct(p->org, r_refdef.view.forward) >= minparticledist_start && VectorDistance2(p->org, r_refdef.view.origin) < drawdist2 * (p->size * p->size))
                                R_MeshQueue_AddTransparent(p->sortorigin, R_DrawParticle_TransparentCallback, NULL, i, NULL);
                        break;
                }