+
+#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;
+static particletexture_t particletexture[MAX_PARTICLETEXTURES];
+
+static cvar_t r_drawparticles = {0, "r_drawparticles", "1"};
+
+static qbyte shadebubble(float dx, float dy, vec3_t light)
+{
+ float dz, f, dot;
+ vec3_t normal;
+ dz = 1 - (dx*dx+dy*dy);
+ if (dz > 0) // it does hit the sphere
+ {
+ f = 0;
+ // back side
+ normal[0] = dx;normal[1] = dy;normal[2] = dz;
+ VectorNormalize(normal);
+ dot = DotProduct(normal, light);
+ if (dot > 0.5) // interior reflection
+ f += ((dot * 2) - 1);
+ else if (dot < -0.5) // exterior reflection
+ f += ((dot * -2) - 1);
+ // front side
+ normal[0] = dx;normal[1] = dy;normal[2] = -dz;
+ VectorNormalize(normal);
+ dot = DotProduct(normal, light);
+ if (dot > 0.5) // interior reflection
+ f += ((dot * 2) - 1);
+ else if (dot < -0.5) // exterior reflection
+ f += ((dot * -2) - 1);
+ f *= 128;
+ f += 16; // just to give it a haze so you can see the outline
+ f = bound(0, f, 255);
+ return (qbyte) f;
+ }
+ else
+ return 0;
+}
+
+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].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);
+}
+
+static void R_InitParticleTexture (void)
+{
+ int x,y,d,i,m;
+ float dx, dy, radius, f, f2;
+ qbyte data[32][32][4], noise1[64][64], noise2[64][64];
+ vec3_t light;
+ qbyte particletexturedata[256*256*4];
+
+ memset(particletexturedata, 255, sizeof(particletexturedata));
+
+ // the particletexture[][] array numbers must match the cl_part.c textures
+ // smoke/blood
+ for (i = 0;i < 8;i++)
+ {
+ do
+ {
+ fractalnoise(&noise1[0][0], 64, 4);
+ fractalnoise(&noise2[0][0], 64, 8);
+ m = 0;
+ for (y = 0;y < 32;y++)
+ {
+ dy = y - 16;
+ for (x = 0;x < 32;x++)
+ {
+ data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
+ dx = x - 16;
+ d = (noise2[y][x] - 128) * 3 + 192;
+ if (d > 0)
+ d = (d * (256 - (int) (dx*dx+dy*dy))) >> 8;
+ d = (d * noise1[y][x]) >> 7;
+ d = bound(0, d, 255);
+ data[y][x][3] = (qbyte) d;
+ if (m < d)
+ m = d;
+ }
+ }
+ }
+ while (m < 224);
+
+ setuptex(i + 0, i + 0, &data[0][0][0], particletexturedata);
+ }
+
+ // rain splash
+ for (i = 0;i < 16;i++)
+ {
+ radius = i * 3.0f / 16.0f;
+ f2 = 255.0f * ((15.0f - i) / 15.0f);
+ for (y = 0;y < 32;y++)
+ {
+ dy = (y - 16) * 0.25f;
+ for (x = 0;x < 32;x++)
+ {
+ dx = (x - 16) * 0.25f;
+ data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
+ f = (1.0 - fabs(radius - sqrt(dx*dx+dy*dy))) * f2;
+ f = bound(0.0f, f, 255.0f);
+ data[y][x][3] = (int) f;
+ }
+ }
+ setuptex(i + 8, i + 16, &data[0][0][0], particletexturedata);
+ }
+
+ // normal particle
+ for (y = 0;y < 32;y++)
+ {
+ dy = y - 16;
+ for (x = 0;x < 32;x++)
+ {
+ data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
+ dx = x - 16;
+ d = (256 - (dx*dx+dy*dy));
+ d = bound(0, d, 255);
+ data[y][x][3] = (qbyte) d;
+ }
+ }
+ setuptex(24, 32, &data[0][0][0], particletexturedata);
+
+ // rain
+ light[0] = 1;light[1] = 1;light[2] = 1;
+ VectorNormalize(light);
+ for (y = 0;y < 32;y++)
+ {
+ for (x = 0;x < 32;x++)
+ {
+ data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
+ 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, 33, &data[0][0][0], particletexturedata);
+
+ // bubble
+ light[0] = 1;light[1] = 1;light[2] = 1;
+ VectorNormalize(light);
+ for (y = 0;y < 32;y++)
+ {
+ for (x = 0;x < 32;x++)
+ {
+ data[y][x][0] = data[y][x][1] = data[y][x][2] = 255;
+ data[y][x][3] = shadebubble((x - 16) * (1.0 / 16.0), (y - 16) * (1.0 / 16.0), light);
+ }
+ }
+ setuptex(26, 34, &data[0][0][0], particletexturedata);
+
+ particlefonttexture = R_LoadTexture (particletexturepool, "particlefont", 256, 256, particletexturedata, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
+}
+
+static void r_part_start(void)
+{
+ particletexturepool = R_AllocTexturePool();
+ R_InitParticleTexture ();
+}
+
+static void r_part_shutdown(void)
+{
+ R_FreeTexturePool(&particletexturepool);
+}
+
+static void r_part_newmap(void)
+{
+}
+
+void R_Particles_Init (void)
+{
+ Cvar_RegisterVariable(&r_drawparticles);
+ R_RegisterModule("R_Particles", r_part_start, r_part_shutdown, r_part_newmap);
+}
+
+void R_DrawParticleCallback(const void *calldata1, int calldata2)
+{
+ int additive, texnum, orientation;
+ float org[3], up2[3], v[3], right[3], up[3], fog, ifog, fogvec[3], cr, cg, cb, ca;
+ particletexture_t *tex;
+ rmeshstate_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;
+
+ memset(&m, 0, sizeof(m));
+ m.blendfunc1 = GL_SRC_ALPHA;
+ if (additive)
+ m.blendfunc2 = GL_ONE;
+ else
+ m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+ m.tex[0] = R_GetTexture(particlefonttexture);
+ R_Mesh_Matrix(&r_identitymatrix);
+ R_Mesh_State(&m);
+
+ tex = &particletexture[texnum];
+ 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)
+ {
+ 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)
+ {
+ cr += fogcolor[0] * fog;
+ cg += fogcolor[1] * fog;
+ cb += fogcolor[2] * fog;
+ }
+ }
+ cr *= r_colorscale;
+ cg *= r_colorscale;
+ cb *= r_colorscale;
+
+ varray_color[ 0] = varray_color[ 4] = varray_color[ 8] = varray_color[12] = cr;
+ varray_color[ 1] = varray_color[ 5] = varray_color[ 9] = varray_color[13] = cg;
+ varray_color[ 2] = varray_color[ 6] = varray_color[10] = varray_color[14] = cb;
+ varray_color[ 3] = varray_color[ 7] = varray_color[11] = varray_color[15] = ca;
+ varray_texcoord[0][0] = tex->s2;varray_texcoord[0][1] = tex->t1;
+ varray_texcoord[0][2] = tex->s1;varray_texcoord[0][3] = tex->t1;
+ varray_texcoord[0][4] = tex->s1;varray_texcoord[0][5] = tex->t2;
+ varray_texcoord[0][6] = tex->s2;varray_texcoord[0][7] = tex->t2;
+
+ if (orientation == PARTICLE_BEAM)
+ {
+ VectorMA(p->org, -p->scaley, p->vel, v);
+ VectorMA(p->org, p->scaley, p->vel, up2);
+ R_CalcBeamVerts(varray_vertex, v, up2, p->scalex);
+ }
+ else if (orientation == PARTICLE_BILLBOARD)
+ {
+ VectorScale(vright, p->scalex, right);
+ VectorScale(vup, p->scaley, up);
+ varray_vertex[ 0] = org[0] + right[0] - up[0];
+ varray_vertex[ 1] = org[1] + right[1] - up[1];
+ varray_vertex[ 2] = org[2] + right[2] - up[2];
+ varray_vertex[ 4] = org[0] - right[0] - up[0];
+ varray_vertex[ 5] = org[1] - right[1] - up[1];
+ varray_vertex[ 6] = org[2] - right[2] - up[2];
+ varray_vertex[ 8] = org[0] - right[0] + up[0];
+ varray_vertex[ 9] = org[1] - right[1] + up[1];
+ varray_vertex[10] = org[2] - right[2] + up[2];
+ varray_vertex[12] = org[0] + right[0] + up[0];
+ varray_vertex[13] = org[1] + right[1] + up[1];
+ varray_vertex[14] = org[2] + right[2] + up[2];
+ }
+ else if (orientation == PARTICLE_ORIENTED_DOUBLESIDED)
+ {
+ // double-sided
+ if (DotProduct(p->vel2, r_origin) > DotProduct(p->vel2, org))
+ {
+ VectorNegate(p->vel2, v);
+ VectorVectors(v, right, up);
+ }
+ else
+ VectorVectors(p->vel2, right, up);
+ VectorScale(right, p->scalex, right);
+ VectorScale(up, p->scaley, up);
+ varray_vertex[ 0] = org[0] + right[0] - up[0];
+ varray_vertex[ 1] = org[1] + right[1] - up[1];
+ varray_vertex[ 2] = org[2] + right[2] - up[2];
+ varray_vertex[ 4] = org[0] - right[0] - up[0];
+ varray_vertex[ 5] = org[1] - right[1] - up[1];
+ varray_vertex[ 6] = org[2] - right[2] - up[2];
+ varray_vertex[ 8] = org[0] - right[0] + up[0];
+ varray_vertex[ 9] = org[1] - right[1] + up[1];
+ varray_vertex[10] = org[2] - right[2] + up[2];
+ varray_vertex[12] = org[0] + right[0] + up[0];
+ varray_vertex[13] = org[1] + right[1] + up[1];
+ varray_vertex[14] = org[2] + right[2] + up[2];
+ }
+ else
+ Host_Error("R_DrawParticles: unknown particle orientation %i\n", orientation);
+ R_Mesh_Draw(4, 2, polygonelements);
+}
+
+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;
+
+ // LordHavoc: only render if not too close
+ for (i = 0, p = particles;i < cl_numparticles;i++, p++)
+ if (DotProduct(p->org, vpn) >= minparticledist)
+ R_MeshQueue_AddTransparent(p->org, R_DrawParticleCallback, p, 0);
+}
+