vertex arrays renamed to varray_ and exposed to rest of engine
[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 static qbyte *crosshairtexdata[NUMCROSSHAIRS] =
16 {
17         "................"
18         "................"
19         "................"
20         "...33......33..."
21         "...355....553..."
22         "....577..775...."
23         ".....77..77....."
24         "................"
25         "................"
26         ".....77..77....."
27         "....577..775...."
28         "...355....553..."
29         "...33......33..."
30         "................"
31         "................"
32         "................"
33         ,
34         "................"
35         "................"
36         "................"
37         "...3........3..."
38         "....5......5...."
39         ".....7....7....."
40         "......7..7......"
41         "................"
42         "................"
43         "......7..7......"
44         ".....7....7....."
45         "....5......5...."
46         "...3........3..."
47         "................"
48         "................"
49         "................"
50         ,
51         "................"
52         ".......77......."
53         ".......77......."
54         "................"
55         "................"
56         ".......44......."
57         ".......44......."
58         ".77..44..44..77."
59         ".77..44..44..77."
60         ".......44......."
61         ".......44......."
62         "................"
63         ".......77......."
64         ".......77......."
65         "................"
66         "................"
67         ,
68         "................"
69         "................"
70         "................"
71         "................"
72         "................"
73         "................"
74         "................"
75         "................"
76         "........7777777."
77         "........752....."
78         "........72......"
79         "........7......."
80         "........7......."
81         "........7......."
82         "................"
83         "................"
84         ,
85         "................"
86         "................"
87         "................"
88         "................"
89         "................"
90         "........7......."
91         "................"
92         "........4......."
93         ".....7.4.4.7...."
94         "........4......."
95         "................"
96         "........7......."
97         "................"
98         "................"
99         "................"
100         "................"
101 };
102
103 rtexturepool_t *crosshairtexturepool;
104 rtexture_t *crosshairtexture[NUMCROSSHAIRS];
105
106 void r_crosshairs_start(void)
107 {
108         int num;
109         int i;
110         char *in;
111         qbyte data[16*16][4];
112         crosshairtexturepool = R_AllocTexturePool();
113         for (num = 0;num < 5;num++)
114         {
115                 in = crosshairtexdata[num];
116                 for (i = 0;i < 16*16;i++)
117                 {
118                         if (in[i] == '.')
119                         {
120                                 data[i][0] = 255;
121                                 data[i][1] = 255;
122                                 data[i][2] = 255;
123                                 data[i][3] = 0;
124                         }
125                         else
126                         {
127                                 data[i][0] = 255;
128                                 data[i][1] = 255;
129                                 data[i][2] = 255;
130                                 data[i][3] = (qbyte) ((int) (in[i] - '0') * 255 / 7);
131                         }
132                 }
133                 crosshairtexture[num] = R_LoadTexture(crosshairtexturepool, va("crosshair%i", num), 16, 16, &data[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
134         }
135 }
136
137 void r_crosshairs_shutdown(void)
138 {
139         R_FreeTexturePool(&crosshairtexturepool);
140 }
141
142 void r_crosshairs_newmap(void)
143 {
144 }
145
146 void R_Crosshairs_Init(void)
147 {
148         Cvar_RegisterVariable(&crosshair_brightness);
149         Cvar_RegisterVariable(&crosshair_alpha);
150         Cvar_RegisterVariable(&crosshair_flashspeed);
151         Cvar_RegisterVariable(&crosshair_flashrange);
152         Cvar_RegisterVariable(&crosshair_size);
153         Cvar_RegisterVariable(&crosshair_static);
154         R_RegisterModule("R_Crosshair", r_crosshairs_start, r_crosshairs_shutdown, r_crosshairs_newmap);
155 }
156
157 void R_DrawCrosshairSprite(rtexture_t *texture, vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
158 {
159         rmeshstate_t m;
160         float diff[3];
161
162         if (fogenabled)
163         {
164                 VectorSubtract(origin, r_origin, diff);
165                 ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
166         }
167
168         memset(&m, 0, sizeof(m));
169         m.blendfunc1 = GL_SRC_ALPHA;
170         m.blendfunc2 = GL_ONE;
171         m.wantoverbright = false;
172         m.depthdisable = true;
173         m.tex[0] = R_GetTexture(texture);
174         Matrix4x4_CreateIdentity(&m.matrix);
175         R_Mesh_State(&m);
176
177         varray_element[0] = 0;
178         varray_element[1] = 1;
179         varray_element[2] = 2;
180         varray_element[3] = 0;
181         varray_element[4] = 2;
182         varray_element[5] = 3;
183         varray_color[0] = varray_color[4] = varray_color[8] = varray_color[12] = cr * mesh_colorscale;
184         varray_color[1] = varray_color[5] = varray_color[9] = varray_color[13] = cg * mesh_colorscale;
185         varray_color[2] = varray_color[6] = varray_color[10] = varray_color[14] = cb * mesh_colorscale;
186         varray_color[3] = varray_color[7] = varray_color[11] = varray_color[15] = ca;
187         varray_texcoord[0][0] = 0;
188         varray_texcoord[0][1] = 0;
189         varray_texcoord[0][2] = 0;
190         varray_texcoord[0][3] = 1;
191         varray_texcoord[0][4] = 1;
192         varray_texcoord[0][5] = 1;
193         varray_texcoord[0][6] = 1;
194         varray_texcoord[0][7] = 0;
195         varray_vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
196         varray_vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
197         varray_vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
198         varray_vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
199         varray_vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
200         varray_vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
201         varray_vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
202         varray_vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
203         varray_vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
204         varray_vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
205         varray_vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
206         varray_vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
207         R_Mesh_Draw(4, 2);
208 }
209
210 void R_DrawCrosshair(void)
211 {
212         int i, num;
213         qbyte *color;
214         float scale, base;
215         vec3_t v1, v2, spriteorigin;
216         vec_t spritescale;
217         float cr, cg, cb, ca;
218         num = crosshair.integer - 1;
219         if (num < 0 || cl.intermission)
220                 return;
221         if (num >= NUMCROSSHAIRS)
222                 num = 0;
223         if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
224         {
225                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
226                 if (i >= 208 && i < 224) // blue
227                         i += 8;
228                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
229                         i += 15;
230         }
231         else
232                 i = 15;
233         color = (qbyte *) &d_8to24table[i];
234         if (crosshair_flashspeed.value >= 0.01f)
235                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
236         else
237                 base = 0.0f;
238         scale = crosshair_brightness.value * (1.0f / 255.0f);
239
240         if (crosshair_static.integer)
241         {
242                 char *picname;
243                 cachepic_t *pic;
244
245                 picname = va("gfx/crosshair%i.tga", num + 1);
246                 pic = Draw_CachePic(picname);
247                 if (pic)
248                         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);
249         }
250         else
251         {
252                 // trace the shot path up to a certain distance
253                 VectorCopy(cl_entities[cl.viewentity].render.origin, v1);
254                 v1[2] += 16; // HACK: this depends on the QC
255                 // get the forward vector for the gun (not the view)
256                 AngleVectors(cl.viewangles, v2, NULL, NULL);
257                 //VectorCopy(r_origin, v1);
258                 VectorMA(v1, 8192, v2, v2);
259                 spritescale = 4.0f + (CL_TraceLine(v1, v2, spriteorigin, NULL, 0, true) * 8192.0f) * (1.0f / 48.0f);
260                 spritescale = bound(0.0f, spritescale, 32.0f);
261                 //VectorMA(spriteorigin, -4, vpn, spriteorigin);
262
263                 cr = color[0] * scale + base;
264                 cg = color[1] * scale + base;
265                 cb = color[2] * scale + base;
266                 ca = crosshair_alpha.value;
267
268                 // clamp the colors so they don't go negative
269                 cr = max(0, cr);
270                 cg = max(0, cg);
271                 cb = max(0, cb);
272                 // might as well clamp the alpha
273                 ca = bound(0, ca, 1.0f);
274
275                 // finally draw the sprite
276                 R_DrawCrosshairSprite(crosshairtexture[num], spriteorigin, spritescale, cr, cg, cb, ca);
277         }
278 }
279
280