+
+#define MAX_PARTICLETEXTURES 64
+// particletexture_t is a rectangle in the particlefonttexture
+typedef struct
+{
+ rtexture_t *texture;
+ float s1, t1, s2, t2;
+}
+particletexture_t;
+
+#if WORKINGLQUAKE
+static int particlefonttexture;
+#else
+static rtexturepool_t *particletexturepool;
+static rtexture_t *particlefonttexture;
+#endif
+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 texnum, qbyte *data, qbyte *particletexturedata)
+{
+ int basex, basey, y;
+ basex = ((texnum >> 0) & 7) * 32;
+ basey = ((texnum >> 3) & 7) * 32;
+ particletexture[texnum].s1 = (basex + 1) / 256.0f;
+ particletexture[texnum].t1 = (basey + 1) / 256.0f;
+ particletexture[texnum].s2 = (basex + 31) / 256.0f;
+ particletexture[texnum].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], data2[64][16][4];
+ vec3_t light;
+ qbyte particletexturedata[256*256*4];
+
+ memset(particletexturedata, 255, sizeof(particletexturedata));
+
+ // the second setuptex parameter must match the tex_ numbers
+ // 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(tex_smoke[i], &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(tex_rainsplash[i], &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(tex_particle, &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(tex_raindrop, &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(tex_bubble, &data[0][0][0], particletexturedata);
+
+#if WORKINGLQUAKE
+ glBindTexture(GL_TEXTURE_2D, (particlefonttexture = gl_extension_number++));
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+#else
+ particlefonttexture = R_LoadTexture2D(particletexturepool, "particlefont", 256, 256, particletexturedata, TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE, NULL);
+ for (i = 0;i < MAX_PARTICLETEXTURES;i++)
+ particletexture[i].texture = particlefonttexture;
+
+ // beam
+ fractalnoise(&noise1[0][0], 64, 4);
+ m = 0;
+ for (y = 0;y < 64;y++)
+ {
+ for (x = 0;x < 16;x++)
+ {
+ if (x < 8)
+ d = x;
+ else
+ d = (15 - x);
+ d = d * d * noise1[y][x] / (7 * 7);
+ data2[y][x][0] = data2[y][x][1] = data2[y][x][2] = (qbyte) bound(0, d, 255);
+ data2[y][x][3] = 255;
+ }
+ }
+
+ particletexture[tex_beam].texture = R_LoadTexture2D(particletexturepool, "beam", 16, 64, &data2[0][0][0], TEXTYPE_RGBA, TEXF_PRECACHE, NULL);
+ particletexture[tex_beam].s1 = 0;
+ particletexture[tex_beam].t1 = 0;
+ particletexture[tex_beam].s2 = 1;
+ particletexture[tex_beam].t2 = 1;
+#endif
+}
+
+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)
+{
+ cl_numparticles = 0;
+}
+
+void R_Particles_Init (void)
+{
+ Cvar_RegisterVariable(&r_drawparticles);
+#ifdef WORKINGLQUAKE
+ r_part_start();
+#else
+ R_RegisterModule("R_Particles", r_part_start, r_part_shutdown, r_part_newmap);
+#endif
+}
+
+#ifdef WORKINGLQUAKE
+void R_InitParticles(void)
+{
+ CL_Particles_Init();
+ R_Particles_Init();
+}
+
+float varray_vertex[16];
+#endif
+
+void R_DrawParticleCallback(const void *calldata1, int calldata2)
+{
+ float org[3], up2[3], v[3], right[3], up[3], fog, ifog, fogvec[3], cr, cg, cb, ca;
+ particletexture_t *tex;
+#ifndef WORKINGLQUAKE
+ rmeshstate_t m;
+#endif
+ const particle_t *p = calldata1;
+
+ VectorCopy(p->org, org);
+
+ if (p->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 (p->orientation == PARTICLE_SPARK)
+ {
+ 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 (p->orientation == PARTICLE_BEAM)
+ R_CalcBeamVerts(varray_vertex, p->org, p->vel2, p->scalex);
+ else if (p->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", p->orientation);
+
+ tex = &particletexture[p->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 WORKINGLQUAKE
+ if (p->additive)
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE);
+ else
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+ glBegin(GL_QUADS);
+ glColor4f(cr, cg, cb, ca);
+ glTexCoord2f(tex->s2, tex->t1);glVertex3f(varray_vertex[ 0], varray_vertex[ 1], varray_vertex[ 2]);
+ glTexCoord2f(tex->s1, tex->t1);glVertex3f(varray_vertex[ 4], varray_vertex[ 5], varray_vertex[ 6]);
+ glTexCoord2f(tex->s1, tex->t2);glVertex3f(varray_vertex[ 8], varray_vertex[ 9], varray_vertex[10]);
+ glTexCoord2f(tex->s2, tex->t2);glVertex3f(varray_vertex[12], varray_vertex[13], varray_vertex[14]);
+ glEnd();
+#else
+ memset(&m, 0, sizeof(m));
+ m.blendfunc1 = GL_SRC_ALPHA;
+ if (p->additive)
+ m.blendfunc2 = GL_ONE;
+ else
+ m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+ m.tex[0] = R_GetTexture(tex->texture);
+ R_Mesh_Matrix(&r_identitymatrix);
+ R_Mesh_State(&m);
+
+ 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 (!p->additive)
+ {
+ cr += fogcolor[0] * fog;
+ cg += fogcolor[1] * fog;
+ cb += fogcolor[2] * fog;
+ }
+ }
+ cr *= r_colorscale;
+ cg *= r_colorscale;
+ cb *= r_colorscale;
+
+ if (p->orientation == PARTICLE_BEAM)
+ {
+ VectorSubtract(p->vel2, p->org, up);
+ VectorNormalizeFast(up);
+ v[0] = DotProduct(p->org, up) * (1.0f / 64.0f) - cl.time * 0.25;
+ v[1] = DotProduct(p->vel2, up) * (1.0f / 64.0f) - cl.time * 0.25;
+ varray_texcoord[0][0] = 1;varray_texcoord[0][1] = v[0];
+ varray_texcoord[0][4] = 0;varray_texcoord[0][5] = v[0];
+ varray_texcoord[0][8] = 0;varray_texcoord[0][9] = v[1];
+ varray_texcoord[0][12] = 1;varray_texcoord[0][13] = v[1];
+ }
+ else
+ {
+ varray_texcoord[0][0] = tex->s2;varray_texcoord[0][1] = tex->t1;
+ varray_texcoord[0][4] = tex->s1;varray_texcoord[0][5] = tex->t1;
+ varray_texcoord[0][8] = tex->s1;varray_texcoord[0][9] = tex->t2;
+ varray_texcoord[0][12] = tex->s2;varray_texcoord[0][13] = tex->t2;
+ }
+
+ GL_Color(cr, cg, cb, ca);
+ R_Mesh_Draw(4, 2, polygonelements);
+#endif
+}
+
+void R_DrawParticles (void)
+{
+ int i;
+ float minparticledist;
+ particle_t *p;
+
+#ifdef WORKINGLQUAKE
+ CL_MoveParticles();
+#endif
+
+ // LordHavoc: early out conditions
+ if ((!cl_numparticles) || (!r_drawparticles.integer))
+ return;
+
+ minparticledist = DotProduct(r_origin, vpn) + 16.0f;
+
+#ifdef WORKINGLQUAKE
+ glBindTexture(GL_TEXTURE_2D, particlefonttexture);
+ glEnable(GL_BLEND);
+ glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
+ glDepthMask(0);
+ // LordHavoc: only render if not too close
+ for (i = 0, p = particles;i < cl_numparticles;i++, p++)
+ if (DotProduct(p->org, vpn) >= minparticledist)
+ R_DrawParticleCallback(p, 0);
+ glDepthMask(1);
+ glDisable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+#else
+ // LordHavoc: only render if not too close
+ c_particles += cl_numparticles;
+ for (i = 0, p = particles;i < cl_numparticles;i++, p++)
+ if (DotProduct(p->org, vpn) >= minparticledist || p->orientation == PARTICLE_BEAM)
+ R_MeshQueue_AddTransparent(p->org, R_DrawParticleCallback, p, 0);
+#endif
+}
+