added R_ScrollTexCoord2f (copies while texture coordinates with an offset)
[divverent/darkplaces.git] / r_crosshairs.c
1
2 #include "quakedef.h"
3 #include "cl_collision.h"
4
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"};
10 cvar_t crosshair_static = {CVAR_SAVE, "crosshair_static", "0"};
11
12 // must match NUMCROSSHAIRS in gl_draw.c
13 #define NUMCROSSHAIRS 5
14
15 void R_Crosshairs_Init(void)
16 {
17         Cvar_RegisterVariable(&crosshair_brightness);
18         Cvar_RegisterVariable(&crosshair_alpha);
19         Cvar_RegisterVariable(&crosshair_flashspeed);
20         Cvar_RegisterVariable(&crosshair_flashrange);
21         Cvar_RegisterVariable(&crosshair_size);
22         Cvar_RegisterVariable(&crosshair_static);
23 }
24
25 void R_DrawCrosshairSprite(rtexture_t *texture, vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
26 {
27         rmeshstate_t m;
28         float diff[3];
29
30         if (fogenabled)
31         {
32                 VectorSubtract(origin, r_origin, diff);
33                 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
34         }
35
36         memset(&m, 0, sizeof(m));
37         m.blendfunc1 = GL_SRC_ALPHA;
38         m.blendfunc2 = GL_ONE;
39         m.depthdisable = true;
40         m.tex[0] = R_GetTexture(texture);
41         R_Mesh_Matrix(&r_identitymatrix);
42         R_Mesh_State(&m);
43
44         GL_Color(cr * r_colorscale, cg * r_colorscale, cb * r_colorscale, ca);
45         R_DrawSpriteMesh(origin, vright, vup, scale, -scale, -scale, scale);
46 }
47
48 void R_GetCrosshairColor(float *out)
49 {
50         int i;
51         qbyte *color;
52         float scale, base;
53         if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
54         {
55                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
56                 if (i >= 208 && i < 224) // blue
57                         i += 8;
58                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
59                         i += 15;
60         }
61         else
62                 i = 15;
63         color = (qbyte *) &palette_complete[i];
64         if (crosshair_flashspeed.value >= 0.01f)
65                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
66         else
67                 base = 0.0f;
68         scale = crosshair_brightness.value * (1.0f / 255.0f);
69         out[0] = color[0] * scale + base;
70         out[1] = color[1] * scale + base;
71         out[2] = color[2] * scale + base;
72         out[3] = crosshair_alpha.value;
73
74         // clamp the colors and alpha
75         out[0] = bound(0, out[0], 1);
76         out[1] = bound(0, out[1], 1);
77         out[2] = bound(0, out[2], 1);
78         out[3] = bound(0, out[3], 1.0f);
79 }
80
81 void R_DrawWorldCrosshair(void)
82 {
83         int num;
84         cachepic_t *pic;
85         vec3_t v1, v2, spriteorigin;
86         vec_t spritescale;
87         vec4_t color;
88         if (crosshair_static.integer)
89                 return;
90         num = crosshair.integer;
91         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
92                 return;
93         if (!cl.viewentity || !cl_entities[cl.viewentity].state_current.active)
94                 return;
95         pic = Draw_CachePic(va("gfx/crosshair%i.tga", num));
96         if (!pic)
97                 return;
98         R_GetCrosshairColor(color);
99
100         // trace the shot path up to a certain distance
101         VectorCopy(cl_entities[cl.viewentity].render.origin, v1);
102         v1[2] += 16; // HACK: this depends on the QC
103
104         // get the forward vector for the gun (not the view)
105         AngleVectors(cl.viewangles, v2, NULL, NULL);
106         //VectorCopy(r_origin, v1);
107         VectorMA(v1, 8192, v2, v2);
108         spritescale = CL_TraceLine(v1, v2, spriteorigin, NULL, 0, true, NULL) * (8192.0f / 40.0f) * crosshair_size.value;
109
110         // draw the sprite
111         R_DrawCrosshairSprite(pic->tex, spriteorigin, spritescale, color[0], color[1], color[2], color[3]);
112 }
113
114 void R_Draw2DCrosshair(void)
115 {
116         int num;
117         cachepic_t *pic;
118         vec4_t color;
119         if (!crosshair_static.integer)
120                 return;
121         num = crosshair.integer;
122         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
123                 return;
124         if (!cl.viewentity || !cl_entities[cl.viewentity].state_current.active)
125                 return;
126         pic = Draw_CachePic(va("gfx/crosshair%i.tga", num));
127         if (pic)
128         {
129                 R_GetCrosshairColor(color);
130                 DrawQ_Pic((vid.conwidth - pic->width * crosshair_size.value) * 0.5f, (vid.conheight - pic->height * crosshair_size.value) * 0.5f, pic->name, pic->width * crosshair_size.value, pic->height * crosshair_size.value, color[0], color[1], color[2], color[3], 0);
131         }
132 }
133
134
135
136