static crosshair is now drawn during sbar stage (where it should be), fixes the bug...
[xonotic/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         varray_color[0] = varray_color[4] = varray_color[8] = varray_color[12] = cr * r_colorscale;
45         varray_color[1] = varray_color[5] = varray_color[9] = varray_color[13] = cg * r_colorscale;
46         varray_color[2] = varray_color[6] = varray_color[10] = varray_color[14] = cb * r_colorscale;
47         varray_color[3] = varray_color[7] = varray_color[11] = varray_color[15] = ca;
48         varray_texcoord[0][0] = 0;
49         varray_texcoord[0][1] = 0;
50         varray_texcoord[0][2] = 0;
51         varray_texcoord[0][3] = 1;
52         varray_texcoord[0][4] = 1;
53         varray_texcoord[0][5] = 1;
54         varray_texcoord[0][6] = 1;
55         varray_texcoord[0][7] = 0;
56         varray_vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
57         varray_vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
58         varray_vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
59         varray_vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
60         varray_vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
61         varray_vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
62         varray_vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
63         varray_vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
64         varray_vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
65         varray_vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
66         varray_vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
67         varray_vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
68         R_Mesh_Draw(4, 2, polygonelements);
69 }
70
71 void R_GetCrosshairColor(float *out)
72 {
73         int i;
74         qbyte *color;
75         float scale, base;
76         if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
77         {
78                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
79                 if (i >= 208 && i < 224) // blue
80                         i += 8;
81                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
82                         i += 15;
83         }
84         else
85                 i = 15;
86         color = (qbyte *) &d_8to24table[i];
87         if (crosshair_flashspeed.value >= 0.01f)
88                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
89         else
90                 base = 0.0f;
91         scale = crosshair_brightness.value * (1.0f / 255.0f);
92         out[0] = color[0] * scale + base;
93         out[1] = color[1] * scale + base;
94         out[2] = color[2] * scale + base;
95         out[3] = crosshair_alpha.value;
96
97         // clamp the colors and alpha
98         out[0] = bound(0, out[0], 1);
99         out[1] = bound(0, out[1], 1);
100         out[2] = bound(0, out[2], 1);
101         out[3] = bound(0, out[3], 1.0f);
102 }
103
104 void R_DrawWorldCrosshair(void)
105 {
106         int num;
107         cachepic_t *pic;
108         vec3_t v1, v2, spriteorigin;
109         vec_t spritescale;
110         vec4_t color;
111         if (crosshair_static.integer)
112                 return;
113         num = crosshair.integer;
114         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
115                 return;
116         if (!cl.viewentity || !cl_entities[cl.viewentity].state_current.active)
117                 return;
118         pic = Draw_CachePic(va("gfx/crosshair%i.tga", num + 1));
119         if (!pic)
120                 return;
121         R_GetCrosshairColor(color);
122         
123         // trace the shot path up to a certain distance
124         VectorCopy(cl_entities[cl.viewentity].render.origin, v1);
125         v1[2] += 16; // HACK: this depends on the QC
126         
127         // get the forward vector for the gun (not the view)
128         AngleVectors(cl.viewangles, v2, NULL, NULL);
129         //VectorCopy(r_origin, v1);
130         VectorMA(v1, 8192, v2, v2);
131         spritescale = CL_TraceLine(v1, v2, spriteorigin, NULL, 0, true) * (8192.0f / 40.0f) * crosshair_size.value;
132
133         // draw the sprite
134         R_DrawCrosshairSprite(pic->tex, spriteorigin, spritescale, color[0], color[1], color[2], color[3]);
135 }
136
137 void R_Draw2DCrosshair(void)
138 {
139         int num;
140         cachepic_t *pic;
141         vec4_t color;
142         if (!crosshair_static.integer)
143                 return;
144         num = crosshair.integer;
145         if (num < 1 || num > NUMCROSSHAIRS || cl.intermission)
146                 return;
147         R_GetCrosshairColor(color);
148         pic = Draw_CachePic(va("gfx/crosshair%i.tga", num + 1));
149         if (pic)
150                 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);
151 }
152
153
154
155