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