3 #include "cl_collision.h"
5 cvar_t crosshair_brightness = {CVAR_SAVE, "crosshair_brightness", "1"};
6 cvar_t crosshair_alpha = {CVAR_SAVE, "crosshair_alpha", "1"};
7 cvar_t crosshair_flashspeed = {CVAR_SAVE, "crosshair_flashspeed", "2"};
8 cvar_t crosshair_flashrange = {CVAR_SAVE, "crosshair_flashrange", "0.1"};
9 cvar_t crosshair_size = {CVAR_SAVE, "crosshair_size", "1"};
11 // must match NUMCROSSHAIRS in gl_draw.c
12 #define NUMCROSSHAIRS 5
14 static qbyte *crosshairtexdata[NUMCROSSHAIRS] =
102 rtexturepool_t *crosshairtexturepool;
103 rtexture_t *crosshairtexture[NUMCROSSHAIRS];
105 void r_crosshairs_start(void)
110 qbyte data[16*16][4];
111 crosshairtexturepool = R_AllocTexturePool();
112 for (num = 0;num < 5;num++)
114 in = crosshairtexdata[num];
115 for (i = 0;i < 16*16;i++)
129 data[i][3] = (qbyte) ((int) (in[i] - '0') * 255 / 7);
132 crosshairtexture[num] = R_LoadTexture(crosshairtexturepool, va("crosshair%i", num), 16, 16, &data[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
136 void r_crosshairs_shutdown(void)
138 R_FreeTexturePool(&crosshairtexturepool);
141 void r_crosshairs_newmap(void)
145 void R_Crosshairs_Init(void)
147 Cvar_RegisterVariable(&crosshair_brightness);
148 Cvar_RegisterVariable(&crosshair_alpha);
149 Cvar_RegisterVariable(&crosshair_flashspeed);
150 Cvar_RegisterVariable(&crosshair_flashrange);
151 Cvar_RegisterVariable(&crosshair_size);
152 R_RegisterModule("R_Crosshair", r_crosshairs_start, r_crosshairs_shutdown, r_crosshairs_newmap);
156 void DrawCrosshair(int num)
163 if (num < 0 || num >= NUMCROSSHAIRS)
165 if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
167 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
168 if (i >= 208 && i < 224) // blue
170 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
175 color = (qbyte *) &d_8to24table[i];
176 if (crosshair_flashspeed.value >= 0.01f)
177 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
180 scale = crosshair_brightness.value * (1.0f / 255.0f);
181 picname = va("gfx/crosshair%i.tga", num + 1);
182 pic = Draw_CachePic(picname);
184 DrawQ_Pic((vid.conwidth - pic->width * crosshair_size.value) * 0.5f, (vid.conheight - pic->height * crosshair_size.value) * 0.5f, picname, pic->width * crosshair_size.value, pic->height * crosshair_size.value, color[0] * scale + base, color[1] * scale + base, color[2] * scale + base, crosshair_alpha.value, 0);
188 int crosshairpolyindex[6] = {0, 1, 2, 0, 2, 3};
190 void R_DrawCrosshairSprite(rtexture_t *texture, vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
193 float tvxyz[4][4], tvst[4][2], diff[3];
194 memset(&m, 0, sizeof(m));
195 m.transparent = false;
196 m.blendfunc1 = GL_SRC_ALPHA;
197 m.blendfunc2 = GL_ONE;
198 m.depthdisable = true;
207 VectorSubtract(origin, r_origin, diff);
208 m.ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
210 m.index = crosshairpolyindex;
211 m.vertex = &tvxyz[0][0];
212 m.vertexstep = sizeof(float[4]);
213 m.tex[0] = R_GetTexture(texture);
214 m.texcoords[0] = &tvst[0][0];
215 m.texcoordstep[0] = sizeof(float[2]);
224 tvxyz[0][0] = origin[0] - vright[0] * scale - vup[0] * scale;
225 tvxyz[0][1] = origin[1] - vright[1] * scale - vup[1] * scale;
226 tvxyz[0][2] = origin[2] - vright[2] * scale - vup[2] * scale;
227 tvxyz[1][0] = origin[0] - vright[0] * scale + vup[0] * scale;
228 tvxyz[1][1] = origin[1] - vright[1] * scale + vup[1] * scale;
229 tvxyz[1][2] = origin[2] - vright[2] * scale + vup[2] * scale;
230 tvxyz[2][0] = origin[0] + vright[0] * scale + vup[0] * scale;
231 tvxyz[2][1] = origin[1] + vright[1] * scale + vup[1] * scale;
232 tvxyz[2][2] = origin[2] + vright[2] * scale + vup[2] * scale;
233 tvxyz[3][0] = origin[0] + vright[0] * scale - vup[0] * scale;
234 tvxyz[3][1] = origin[1] + vright[1] * scale - vup[1] * scale;
235 tvxyz[3][2] = origin[2] + vright[2] * scale - vup[2] * scale;
239 void R_DrawCrosshair(void)
244 vec3_t v1, v2, spriteorigin;
246 float cr, cg, cb, ca;
247 num = crosshair.integer - 1;
250 if (num >= NUMCROSSHAIRS)
252 if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
254 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
255 if (i >= 208 && i < 224) // blue
257 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
262 color = (qbyte *) &d_8to24table[i];
263 if (crosshair_flashspeed.value >= 0.01f)
264 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
267 scale = crosshair_brightness.value * (1.0f / 255.0f);
268 //picname = va("gfx/crosshair%i.tga", num + 1);
269 //pic = Draw_CachePic(picname);
271 // DrawQ_Pic((vid.conwidth - pic->width * crosshair_size.value) * 0.5f, (vid.conheight - pic->height * crosshair_size.value) * 0.5f, picname, pic->width * crosshair_size.value, pic->height * crosshair_size.value, color[0] * scale + base, color[1] * scale + base, color[2] * scale + base, crosshair_alpha.value, 0);
273 // trace the shot path up to a certain distance
274 VectorCopy(cl_entities[cl.viewentity].render.origin, v1);
275 v1[2] += 16; // HACK: this depends on the QC
276 // get the forward vector for the gun (not the view)
277 AngleVectors(cl.viewangles, v2, NULL, NULL);
278 //VectorCopy(r_origin, v1);
279 VectorMA(v1, 8192, v2, v2);
280 spritescale = 4.0f + (CL_TraceLine(v1, v2, spriteorigin, NULL, 0, true) * 8192.0f) * (1.0f / 48.0f);
281 spritescale = bound(0.0f, spritescale, 32.0f);
282 //VectorMA(spriteorigin, -4, vpn, spriteorigin);
284 cr = color[0] * scale + base;
285 cg = color[1] * scale + base;
286 cb = color[2] * scale + base;
287 ca = crosshair_alpha.value;
289 // clamp the colors so they don't go negative
293 // might as well clamp the alpha
294 ca = bound(0, ca, 1.0f);
296 // finally draw the sprite
297 R_DrawCrosshairSprite(crosshairtexture[num], spriteorigin, spritescale, cr, cg, cb, ca);