]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cl_particles.c
reduced default max particles from 16384 to 8192
[xonotic/darkplaces.git] / cl_particles.c
index ba62bcecff480426f80e6b5961297d29ab477bbf..9a0655491227626329973afbbce97c8383c9b32d 100644 (file)
@@ -21,7 +21,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include "quakedef.h"
 #include "cl_collision.h"
 
-#define MAX_PARTICLES                  16384   // default max # of particles at one time
+#define MAX_PARTICLES                  8192    // default max # of particles at one time
 #define ABSOLUTE_MIN_PARTICLES 512             // no fewer than this no matter what's on the command line
 
 typedef enum
@@ -35,7 +35,7 @@ ptype_t;
 #define P_ORIENTATION_FIRSTBIT (P_TEXNUM_FIRSTBIT + P_TEXNUM_BITS)
 #define P_ORIENTATION_BITS 2
 #define P_FLAGS_FIRSTBIT (P_ORIENTATION_FIRSTBIT + P_ORIENTATION_BITS)
-#define P_DYNLIGHT (1 << (P_FLAGS_FIRSTBIT + 0))
+//#define P_DYNLIGHT (1 << (P_FLAGS_FIRSTBIT + 0))
 #define P_ADDITIVE (1 << (P_FLAGS_FIRSTBIT + 1))
 
 typedef struct particle_s
@@ -179,8 +179,8 @@ void CL_Particles_Init (void)
                partflags = ((porientation) << P_ORIENTATION_FIRSTBIT) | ((ptex) << P_TEXNUM_FIRSTBIT);\
                if (padditive)\
                        partflags |= P_ADDITIVE;\
-               if (plight)\
-                       partflags |= P_DYNLIGHT;\
+               /*if (plight)*/\
+               /*      partflags |= P_DYNLIGHT;*/\
                tempcolor = (pcolor1);\
                tempcolor2 = (pcolor2);\
                cr2 = ((tempcolor2) >> 16) & 0xFF;\
@@ -349,7 +349,7 @@ void CL_ParticleExplosion (vec3_t org, int smoke)
 {
        int i;
        if (cl_stainmaps.integer)
-               R_Stain(org, 96, 80, 80, 80, 128, 176, 176, 176, 128);
+               R_Stain(org, 96, 80, 80, 80, 64, 176, 176, 176, 64);
 
        i = Mod_PointInLeaf(org, cl.worldmodel)->contents;
        if ((i == CONTENTS_SLIME || i == CONTENTS_WATER) && cl_particles.integer && cl_particles_bubbles.integer)
@@ -391,7 +391,7 @@ CL_BlobExplosion
 void CL_BlobExplosion (vec3_t org)
 {
        if (cl_stainmaps.integer)
-               R_Stain(org, 96, 80, 80, 80, 128, 176, 176, 176, 128);
+               R_Stain(org, 96, 80, 80, 80, 64, 176, 176, 176, 64);
 
        if (cl_explosions.integer)
                R_NewExplosion(org);
@@ -432,7 +432,7 @@ void CL_SparkShower (vec3_t org, vec3_t dir, int count)
        if (!cl_particles.integer) return;
 
        if (cl_stainmaps.integer)
-               R_Stain(org, 32, 96, 96, 96, 32, 128, 128, 128, 32);
+               R_Stain(org, 32, 96, 96, 96, 2, 128, 128, 128, 2);
 
        if (cl_particles_bulletimpacts.integer)
        {
@@ -455,7 +455,7 @@ void CL_SparkShower (vec3_t org, vec3_t dir, int count)
 void CL_PlasmaBurn (vec3_t org)
 {
        if (cl_stainmaps.integer)
-               R_Stain(org, 48, 96, 96, 96, 48, 128, 128, 128, 48);
+               R_Stain(org, 48, 96, 96, 96, 3, 128, 128, 128, 3);
 }
 
 static float bloodcount = 0;
@@ -860,7 +860,7 @@ void CL_MoveParticles (void)
                                {
                                        // assume it's blood (lame, but...)
                                        if (cl_stainmaps.integer)
-                                               R_Stain(v, 64, 32, 16, 16, p->alpha * p->scalex * (1.0f / 100.0f), 192, 48, 48, p->alpha * p->scalex * (1.0f / 100.0f));
+                                               R_Stain(v, 32, 32, 16, 16, p->alpha * p->scalex * (1.0f / 400.0f), 192, 48, 48, p->alpha * p->scalex * (1.0f / 400.0f));
                                        p->die = -1;
                                        freeparticles[j++] = p;
                                        continue;
@@ -991,14 +991,20 @@ void CL_MoveParticles (void)
        }
 }
 
+#define MAX_PARTICLETEXTURES 64
+// particletexture_t is a rectangle in the particlefonttexture
+typedef struct
+{
+       float s1, t1, s2, t2;
+}
+particletexture_t;
+
 static rtexturepool_t *particletexturepool;
 
 static rtexture_t *particlefonttexture;
-// [0] is normal, [1] is fog, they may be the same
-static particletexture_t particletexture[MAX_PARTICLETEXTURES][2];
+static particletexture_t particletexture[MAX_PARTICLETEXTURES];
 
 static cvar_t r_drawparticles = {0, "r_drawparticles", "1"};
-static cvar_t r_particles_lighting = {0, "r_particles_lighting", "0"};
 
 static qbyte shadebubble(float dx, float dy, vec3_t light)
 {
@@ -1033,15 +1039,15 @@ static qbyte shadebubble(float dx, float dy, vec3_t light)
                return 0;
 }
 
-static void setuptex(int cltexnum, int fog, int rtexnum, qbyte *data, qbyte *particletexturedata)
+static void setuptex(int cltexnum, int rtexnum, qbyte *data, qbyte *particletexturedata)
 {
        int basex, basey, y;
        basex = ((rtexnum >> 0) & 7) * 32;
        basey = ((rtexnum >> 3) & 7) * 32;
-       particletexture[cltexnum][fog].s1 = (basex + 1) / 256.0f;
-       particletexture[cltexnum][fog].t1 = (basey + 1) / 256.0f;
-       particletexture[cltexnum][fog].s2 = (basex + 31) / 256.0f;
-       particletexture[cltexnum][fog].t2 = (basey + 31) / 256.0f;
+       particletexture[cltexnum].s1 = (basex + 1) / 256.0f;
+       particletexture[cltexnum].t1 = (basey + 1) / 256.0f;
+       particletexture[cltexnum].s2 = (basex + 31) / 256.0f;
+       particletexture[cltexnum].t2 = (basey + 31) / 256.0f;
        for (y = 0;y < 32;y++)
                memcpy(particletexturedata + ((basey + y) * 256 + basex) * 4, data + y * 32 * 4, 32 * 4);
 }
@@ -1085,8 +1091,7 @@ static void R_InitParticleTexture (void)
                }
                while (m < 224);
 
-               setuptex(i + 0, 0, i + 0, &data[0][0][0], particletexturedata);
-               setuptex(i + 0, 1, i + 0, &data[0][0][0], particletexturedata);
+               setuptex(i + 0, i + 0, &data[0][0][0], particletexturedata);
        }
 
        // rain splash
@@ -1106,8 +1111,7 @@ static void R_InitParticleTexture (void)
                                data[y][x][3] = (int) f;
                        }
                }
-               setuptex(i + 8, 0, i + 16, &data[0][0][0], particletexturedata);
-               setuptex(i + 8, 1, i + 16, &data[0][0][0], particletexturedata);
+               setuptex(i + 8, i + 16, &data[0][0][0], particletexturedata);
        }
 
        // normal particle
@@ -1123,8 +1127,7 @@ static void R_InitParticleTexture (void)
                        data[y][x][3] = (qbyte) d;
                }
        }
-       setuptex(24, 0, 32, &data[0][0][0], particletexturedata);
-       setuptex(24, 1, 32, &data[0][0][0], particletexturedata);
+       setuptex(24, 32, &data[0][0][0], particletexturedata);
 
        // rain
        light[0] = 1;light[1] = 1;light[2] = 1;
@@ -1137,8 +1140,7 @@ static void R_InitParticleTexture (void)
                        data[y][x][3] = shadebubble((x - 16) * (1.0 / 8.0), y < 24 ? (y - 24) * (1.0 / 24.0) : (y - 24) * (1.0 / 8.0), light);
                }
        }
-       setuptex(25, 0, 33, &data[0][0][0], particletexturedata);
-       setuptex(25, 1, 33, &data[0][0][0], particletexturedata);
+       setuptex(25, 33, &data[0][0][0], particletexturedata);
 
        // bubble
        light[0] = 1;light[1] = 1;light[2] = 1;
@@ -1151,8 +1153,7 @@ static void R_InitParticleTexture (void)
                        data[y][x][3] = shadebubble((x - 16) * (1.0 / 16.0), (y - 16) * (1.0 / 16.0), light);
                }
        }
-       setuptex(26, 0, 34, &data[0][0][0], particletexturedata);
-       setuptex(26, 1, 34, &data[0][0][0], particletexturedata);
+       setuptex(26, 34, &data[0][0][0], particletexturedata);
 
        particlefonttexture = R_LoadTexture (particletexturepool, "particlefont", 256, 256, particletexturedata, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
 }
@@ -1175,69 +1176,43 @@ static void r_part_newmap(void)
 void R_Particles_Init (void)
 {
        Cvar_RegisterVariable(&r_drawparticles);
-       Cvar_RegisterVariable(&r_particles_lighting);
        R_RegisterModule("R_Particles", r_part_start, r_part_shutdown, r_part_newmap);
 }
 
 int partindexarray[6] = {0, 1, 2, 0, 2, 3};
 
-void R_DrawParticles (void)
+void R_DrawParticleCallback(const void *calldata1, int calldata2)
 {
-       int i, lighting, dynlight, additive, texnum, orientation;
-       float minparticledist, org[3], uprightangles[3], up2[3], right2[3], v[3], right[3], up[3], tvxyz[4][4], tvst[4][2], fog, ifog, fogvec[3];
-       mleaf_t *leaf;
-       particletexture_t *tex, *texfog;
-       rmeshinfo_t m;
-       particle_t *p;
-
-       // LordHavoc: early out conditions
-       if ((!cl_numparticles) || (!r_drawparticles.integer))
-               return;
-
-       lighting = r_particles_lighting.integer;
-       if (!r_dynamic.integer)
-               lighting = 0;
+       int additive, texnum, orientation;
+       float org[3], up2[3], right2[3], v[3], right[3], up[3], fog, ifog, fogvec[3], cr, cg, cb, ca;
+       particletexture_t *tex;
+       rmeshbufferinfo_t m;
+       const particle_t *p = calldata1;
+
+       VectorCopy(p->org, org);
+       orientation = (p->flags >> P_ORIENTATION_FIRSTBIT) & ((1 << P_ORIENTATION_BITS) - 1);
+       texnum = (p->flags >> P_TEXNUM_FIRSTBIT) & ((1 << P_TEXNUM_BITS) - 1);
+       //dynlight = p->flags & P_DYNLIGHT;
+       additive = p->flags & P_ADDITIVE;
 
-       c_particles += cl_numparticles;
-
-       uprightangles[0] = 0;
-       uprightangles[1] = r_refdef.viewangles[1];
-       uprightangles[2] = 0;
-       AngleVectors (uprightangles, NULL, right2, up2);
-
-       minparticledist = DotProduct(r_origin, vpn) + 16.0f;
-
-       // LordHavoc: this meshinfo must match up with R_Mesh_DrawDecal
-       // LordHavoc: the commented out lines are hardwired behavior in R_Mesh_DrawDecal
        memset(&m, 0, sizeof(m));
-       m.transparent = true;
        m.blendfunc1 = GL_SRC_ALPHA;
-       m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+       if (additive)
+               m.blendfunc2 = GL_ONE;
+       else
+               m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
        m.numtriangles = 2;
-       m.index = partindexarray;
        m.numverts = 4;
-       m.vertex = &tvxyz[0][0];
-       m.vertexstep = sizeof(float[4]);
        m.tex[0] = R_GetTexture(particlefonttexture);
-       m.texcoords[0] = &tvst[0][0];
-       m.texcoordstep[0] = sizeof(float[2]);
-
-       for (i = 0, p = particles;i < cl_numparticles;i++, p++)
+       Matrix4x4_CreateIdentity(&m.matrix);
+       if (R_Mesh_Draw_GetBuffer(&m, false))
        {
-               // LordHavoc: only render if not too close
-               if (DotProduct(p->org, vpn) < minparticledist)
-                       continue;
-
-               // LordHavoc: check if it's in a visible leaf
-               leaf = Mod_PointInLeaf(p->org, cl.worldmodel);
-               if (leaf->visframe != r_framecount)
-                       continue;
-
-               VectorCopy(p->org, org);
-               orientation = (p->flags >> P_ORIENTATION_FIRSTBIT) & ((1 << P_ORIENTATION_BITS) - 1);
-               texnum = (p->flags >> P_TEXNUM_FIRSTBIT) & ((1 << P_TEXNUM_BITS) - 1);
-               dynlight = p->flags & P_DYNLIGHT;
-               additive = p->flags & P_ADDITIVE;
+               m.index[0] = 0;
+               m.index[1] = 1;
+               m.index[2] = 2;
+               m.index[3] = 0;
+               m.index[4] = 2;
+               m.index[5] = 3;
                if (orientation == PARTICLE_BILLBOARD)
                {
                        VectorScale(vright, p->scalex, right);
@@ -1245,6 +1220,11 @@ void R_DrawParticles (void)
                }
                else if (orientation == PARTICLE_UPRIGHT_FACING)
                {
+                       v[0] = r_origin[0] - org[0];
+                       v[1] = r_origin[1] - org[1];
+                       v[2] = 0;
+                       VectorNormalizeFast(v);
+                       VectorVectors(v, right2, up2);
                        VectorScale(right2, p->scalex, right);
                        VectorScale(up2, p->scaley, up);
                }
@@ -1263,137 +1243,79 @@ void R_DrawParticles (void)
                }
                else
                        Host_Error("R_DrawParticles: unknown particle orientation %i\n", orientation);
-
-               m.cr = p->color[0] * (1.0f / 255.0f);
-               m.cg = p->color[1] * (1.0f / 255.0f);
-               m.cb = p->color[2] * (1.0f / 255.0f);
-               m.ca = p->alpha * (1.0f / 255.0f);
-               if (lighting >= 1 && (dynlight || lighting >= 2))
-               {
-                       R_CompleteLightPoint(v, org, true, leaf);
-                       m.cr *= v[0];
-                       m.cg *= v[1];
-                       m.cb *= v[2];
-               }
-
-               tex = &particletexture[texnum][0];
-
-               tvxyz[0][0] = org[0] - right[0] - up[0];
-               tvxyz[0][1] = org[1] - right[1] - up[1];
-               tvxyz[0][2] = org[2] - right[2] - up[2];
-               tvxyz[1][0] = org[0] - right[0] + up[0];
-               tvxyz[1][1] = org[1] - right[1] + up[1];
-               tvxyz[1][2] = org[2] - right[2] + up[2];
-               tvxyz[2][0] = org[0] + right[0] + up[0];
-               tvxyz[2][1] = org[1] + right[1] + up[1];
-               tvxyz[2][2] = org[2] + right[2] + up[2];
-               tvxyz[3][0] = org[0] + right[0] - up[0];
-               tvxyz[3][1] = org[1] + right[1] - up[1];
-               tvxyz[3][2] = org[2] + right[2] - up[2];
-               tvst[0][0] = tex->s1;
-               tvst[0][1] = tex->t1;
-               tvst[1][0] = tex->s1;
-               tvst[1][1] = tex->t2;
-               tvst[2][0] = tex->s2;
-               tvst[2][1] = tex->t2;
-               tvst[3][0] = tex->s2;
-               tvst[3][1] = tex->t1;
-
-               if (additive)
-               {
-                       m.blendfunc2 = GL_ONE;
-                       fog = 0;
-                       if (fogenabled)
-                       {
-                               texfog = &particletexture[texnum][1];
-                               VectorSubtract(org, r_origin, fogvec);
-                               ifog = 1 - exp(fogdensity/DotProduct(fogvec,fogvec));
-                               if (ifog < (1.0f - (1.0f / 64.0f)))
-                               {
-                                       if (ifog >= (1.0f / 64.0f))
-                                       {
-                                               // partially fogged, darken it
-                                               m.cr *= ifog;
-                                               m.cg *= ifog;
-                                               m.cb *= ifog;
-                                               R_Mesh_Draw(&m);
-                                       }
-                               }
-                               else
-                                       R_Mesh_Draw(&m);
-                       }
-                       else
-                               R_Mesh_Draw(&m);
-               }
-               else
+               m.vertex[0] = org[0] - right[0] - up[0];
+               m.vertex[1] = org[1] - right[1] - up[1];
+               m.vertex[2] = org[2] - right[2] - up[2];
+               m.vertex[4] = org[0] - right[0] + up[0];
+               m.vertex[5] = org[1] - right[1] + up[1];
+               m.vertex[6] = org[2] - right[2] + up[2];
+               m.vertex[8] = org[0] + right[0] + up[0];
+               m.vertex[9] = org[1] + right[1] + up[1];
+               m.vertex[10] = org[2] + right[2] + up[2];
+               m.vertex[12] = org[0] + right[0] - up[0];
+               m.vertex[13] = org[1] + right[1] - up[1];
+               m.vertex[14] = org[2] + right[2] - up[2];
+               tex = &particletexture[texnum];
+               m.texcoords[0][0] = tex->s1;
+               m.texcoords[0][1] = tex->t1;
+               m.texcoords[0][2] = tex->s1;
+               m.texcoords[0][3] = tex->t2;
+               m.texcoords[0][4] = tex->s2;
+               m.texcoords[0][5] = tex->t2;
+               m.texcoords[0][6] = tex->s2;
+               m.texcoords[0][7] = tex->t1;
+               cr = p->color[0] * (1.0f / 255.0f);
+               cg = p->color[1] * (1.0f / 255.0f);
+               cb = p->color[2] * (1.0f / 255.0f);
+               ca = p->alpha * (1.0f / 255.0f);
+               if (fogenabled)
                {
-                       m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
-                       fog = 0;
-                       if (fogenabled)
+                       VectorSubtract(org, r_origin, fogvec);
+                       fog = exp(fogdensity/DotProduct(fogvec,fogvec));
+                       ifog = 1 - fog;
+                       cr = cr * ifog;
+                       cg = cg * ifog;
+                       cb = cb * ifog;
+                       if (!additive)
                        {
-                               texfog = &particletexture[texnum][1];
-                               VectorSubtract(org, r_origin, fogvec);
-                               fog = exp(fogdensity/DotProduct(fogvec,fogvec));
-                               if (fog >= (1.0f / 64.0f))
-                               {
-                                       if (fog >= (1.0f - (1.0f / 64.0f)))
-                                       {
-                                               // fully fogged, just use the fog texture and render as alpha
-                                               m.cr = fogcolor[0];
-                                               m.cg = fogcolor[1];
-                                               m.cb = fogcolor[2];
-                                               tvst[0][0] = texfog->s1;
-                                               tvst[0][1] = texfog->t1;
-                                               tvst[1][0] = texfog->s1;
-                                               tvst[1][1] = texfog->t2;
-                                               tvst[2][0] = texfog->s2;
-                                               tvst[2][1] = texfog->t2;
-                                               tvst[3][0] = texfog->s2;
-                                               tvst[3][1] = texfog->t1;
-                                               R_Mesh_Draw(&m);
-                                       }
-                                       else
-                                       {
-                                               // partially fogged, darken the first pass
-                                               ifog = 1 - fog;
-                                               m.cr *= ifog;
-                                               m.cg *= ifog;
-                                               m.cb *= ifog;
-                                               if (tex->s1 == texfog->s1 && tex->t1 == texfog->t1)
-                                               {
-                                                       // fog texture is the same as the base, just change the color
-                                                       m.cr += fogcolor[0] * fog;
-                                                       m.cg += fogcolor[1] * fog;
-                                                       m.cb += fogcolor[2] * fog;
-                                                       R_Mesh_Draw(&m);
-                                               }
-                                               else
-                                               {
-                                                       // render the first pass (alpha), then do additive fog
-                                                       R_Mesh_Draw(&m);
-
-                                                       m.blendfunc2 = GL_ONE;
-                                                       m.cr = fogcolor[0] * fog;
-                                                       m.cg = fogcolor[1] * fog;
-                                                       m.cb = fogcolor[2] * fog;
-                                                       tvst[0][0] = texfog->s1;
-                                                       tvst[0][1] = texfog->t1;
-                                                       tvst[1][0] = texfog->s1;
-                                                       tvst[1][1] = texfog->t2;
-                                                       tvst[2][0] = texfog->s2;
-                                                       tvst[2][1] = texfog->t2;
-                                                       tvst[3][0] = texfog->s2;
-                                                       tvst[3][1] = texfog->t1;
-                                                       R_Mesh_Draw(&m);
-                                               }
-                                       }
-                               }
-                               else
-                                       R_Mesh_Draw(&m);
+                               cr += fogcolor[0] * fog;
+                               cg += fogcolor[1] * fog;
+                               cb += fogcolor[2] * fog;
                        }
-                       else
-                               R_Mesh_Draw(&m);
                }
+               m.color[0] = m.color[4] = m.color[8] = m.color[12] = cr * m.colorscale;
+               m.color[1] = m.color[5] = m.color[9] = m.color[13] = cg * m.colorscale;
+               m.color[2] = m.color[6] = m.color[10] = m.color[14] = cb * m.colorscale;
+               m.color[3] = m.color[7] = m.color[11] = m.color[15] = ca;
+               R_Mesh_Render();
+       }
+}
+
+void R_DrawParticles (void)
+{
+       int i;
+       float minparticledist;
+       particle_t *p;
+
+       // LordHavoc: early out conditions
+       if ((!cl_numparticles) || (!r_drawparticles.integer))
+               return;
+
+       c_particles += cl_numparticles;
+
+       minparticledist = DotProduct(r_origin, vpn) + 16.0f;
+
+       for (i = 0, p = particles;i < cl_numparticles;i++, p++)
+       {
+               // LordHavoc: only render if not too close
+               if (DotProduct(p->org, vpn) < minparticledist)
+                       continue;
+
+               // LordHavoc: check if it's in a visible leaf
+               //if (Mod_PointInLeaf(p->org, cl.worldmodel)->visframe != r_framecount)
+               //      continue;
+
+               R_MeshQueue_AddTransparent(p->org, R_DrawParticleCallback, p, 0);
        }
 }