+ VectorSubtract(org, r_origin, diff);
+ fog = exp(fogdensity/DotProduct(diff,diff));
+ if (fog >= 0.01f)
+ {
+ if (fog >= 0.99f)
+ {
+ // fully fogged, just use the fog texture and render as alpha
+ m.cr = fogcolor[0];
+ m.cg = fogcolor[1];
+ m.cb = fogcolor[2];
+ m.ca = r->color[3];
+ tv[0][3] = texfog->s1;
+ tv[0][4] = texfog->t1;
+ tv[1][3] = texfog->s1;
+ tv[1][4] = texfog->t2;
+ tv[2][3] = texfog->s2;
+ tv[2][4] = texfog->t2;
+ tv[3][3] = texfog->s2;
+ tv[3][4] = texfog->t1;
+ R_Mesh_Draw(&m);
+ }
+ else
+ {
+ // partially fogged, darken the first pass
+ m.cr *= 1 - fog;
+ m.cg *= 1 - fog;
+ m.cb *= 1 - fog;
+ 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];
+ m.cg = fogcolor[1];
+ m.cb = fogcolor[2];
+ m.ca = r->color[3] * fog;
+ tv[0][3] = texfog->s1;
+ tv[0][4] = texfog->t1;
+ tv[1][3] = texfog->s1;
+ tv[1][4] = texfog->t2;
+ tv[2][3] = texfog->s2;
+ tv[2][4] = texfog->t2;
+ tv[3][3] = texfog->s2;
+ tv[3][4] = texfog->t1;
+ R_Mesh_Draw(&m);
+ m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
+ }
+ }
+ }
+ else
+ R_Mesh_Draw(&m);