Serious-Sam style crosshair (an actual sprite in the world, with slight variation...
[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
11 // must match NUMCROSSHAIRS in gl_draw.c
12 #define NUMCROSSHAIRS 5
13
14 static qbyte *crosshairtexdata[NUMCROSSHAIRS] =
15 {
16         "................"
17         "................"
18         "................"
19         "...33......33..."
20         "...355....553..."
21         "....577..775...."
22         ".....77..77....."
23         "................"
24         "................"
25         ".....77..77....."
26         "....577..775...."
27         "...355....553..."
28         "...33......33..."
29         "................"
30         "................"
31         "................"
32         ,
33         "................"
34         "................"
35         "................"
36         "...3........3..."
37         "....5......5...."
38         ".....7....7....."
39         "......7..7......"
40         "................"
41         "................"
42         "......7..7......"
43         ".....7....7....."
44         "....5......5...."
45         "...3........3..."
46         "................"
47         "................"
48         "................"
49         ,
50         "................"
51         ".......77......."
52         ".......77......."
53         "................"
54         "................"
55         ".......44......."
56         ".......44......."
57         ".77..44..44..77."
58         ".77..44..44..77."
59         ".......44......."
60         ".......44......."
61         "................"
62         ".......77......."
63         ".......77......."
64         "................"
65         "................"
66         ,
67         "................"
68         "................"
69         "................"
70         "................"
71         "................"
72         "................"
73         "................"
74         "................"
75         "........7777777."
76         "........752....."
77         "........72......"
78         "........7......."
79         "........7......."
80         "........7......."
81         "................"
82         "................"
83         ,
84         "................"
85         "................"
86         "................"
87         "................"
88         "................"
89         "........7......."
90         "................"
91         "........4......."
92         ".....7.4.4.7...."
93         "........4......."
94         "................"
95         "........7......."
96         "................"
97         "................"
98         "................"
99         "................"
100 };
101
102 rtexturepool_t *crosshairtexturepool;
103 rtexture_t *crosshairtexture[NUMCROSSHAIRS];
104
105 void r_crosshairs_start(void)
106 {
107         int num;
108         int i;
109         char *in;
110         qbyte data[16*16][4];
111         crosshairtexturepool = R_AllocTexturePool();
112         for (num = 0;num < 5;num++)
113         {
114                 in = crosshairtexdata[num];
115                 for (i = 0;i < 16*16;i++)
116                 {
117                         if (in[i] == '.')
118                         {
119                                 data[i][0] = 255;
120                                 data[i][1] = 255;
121                                 data[i][2] = 255;
122                                 data[i][3] = 0;
123                         }
124                         else
125                         {
126                                 data[i][0] = 255;
127                                 data[i][1] = 255;
128                                 data[i][2] = 255;
129                                 data[i][3] = (qbyte) ((int) (in[i] - '0') * 255 / 7);
130                         }
131                 }
132                 crosshairtexture[num] = R_LoadTexture(crosshairtexturepool, va("crosshair%i", num), 16, 16, &data[0][0], TEXTYPE_RGBA, TEXF_ALPHA | TEXF_PRECACHE);
133         }
134 }
135
136 void r_crosshairs_shutdown(void)
137 {
138         R_FreeTexturePool(&crosshairtexturepool);
139 }
140
141 void r_crosshairs_newmap(void)
142 {
143 }
144
145 void R_Crosshairs_Init(void)
146 {
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);
153 }
154
155 /*
156 void DrawCrosshair(int num)
157 {
158         int i;
159         qbyte *color;
160         float scale, base;
161         char *picname;
162         cachepic_t *pic;
163         if (num < 0 || num >= NUMCROSSHAIRS)
164                 num = 0;
165         if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
166         {
167                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
168                 if (i >= 208 && i < 224) // blue
169                         i += 8;
170                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
171                         i += 15;
172         }
173         else
174                 i = 15;
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);
178         else
179                 base = 0.0f;
180         scale = crosshair_brightness.value * (1.0f / 255.0f);
181         picname = va("gfx/crosshair%i.tga", num + 1);
182         pic = Draw_CachePic(picname);
183         if (pic)
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);
185 }
186 */
187
188 int crosshairpolyindex[6] = {0, 1, 2, 0, 2, 3};
189
190 void R_DrawCrosshairSprite(rtexture_t *texture, vec3_t origin, vec_t scale, float cr, float cg, float cb, float ca)
191 {
192         rmeshinfo_t m;
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;
199         m.numtriangles = 2;
200         m.numverts = 4;
201         m.cr = cr;
202         m.cg = cg;
203         m.cb = cb;
204         m.ca = ca;
205         if (fogenabled)
206         {
207                 VectorSubtract(origin, r_origin, diff);
208                 m.ca *= 1 - exp(fogdensity/DotProduct(diff,diff));
209         }
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]);
216         tvst[0][0] = 0;
217         tvst[0][1] = 0;
218         tvst[1][0] = 0;
219         tvst[1][1] = 1;
220         tvst[2][0] = 1;
221         tvst[2][1] = 1;
222         tvst[3][0] = 1;
223         tvst[3][1] = 0;
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;
236         R_Mesh_Draw(&m);
237 }
238
239 void R_DrawCrosshair(void)
240 {
241         int i, num;
242         qbyte *color;
243         float scale, base;
244         vec3_t v1, v2, spriteorigin;
245         vec_t spritescale;
246         num = crosshair.integer - 1;
247         if (num < 0)
248                 return;
249         if (num >= NUMCROSSHAIRS)
250                 num = 0;
251         if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
252         {
253                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
254                 if (i >= 208 && i < 224) // blue
255                         i += 8;
256                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
257                         i += 15;
258         }
259         else
260                 i = 15;
261         color = (qbyte *) &d_8to24table[i];
262         if (crosshair_flashspeed.value >= 0.01f)
263                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
264         else
265                 base = 0.0f;
266         scale = crosshair_brightness.value * (1.0f / 255.0f);
267         //picname = va("gfx/crosshair%i.tga", num + 1);
268         //pic = Draw_CachePic(picname);
269         //if (pic)
270         //      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);
271         {
272                 // trace the shot path up to a certain distance
273                 VectorCopy(cl_entities[cl.viewentity].render.origin, v1);
274                 v1[2] += 16; // HACK: this depends on the QC
275                 // get the forward vector for the gun (not the view)
276                 AngleVectors(cl.viewangles, v2, NULL, NULL);
277                 //VectorCopy(r_origin, v1);
278                 VectorMA(v1, 8192, v2, v2);
279                 spritescale = 4.0f + (CL_TraceLine(v1, v2, spriteorigin, NULL, 0, true) * 8192.0f) * (1.0f / 48.0f);
280                 spritescale = bound(0.0f, spritescale, 32.0f);
281                 //VectorMA(spriteorigin, -4, vpn, spriteorigin);
282                 // put the sprite there
283                 R_DrawCrosshairSprite(crosshairtexture[num], spriteorigin, spritescale, color[0] * scale + base, color[1] * scale + base, color[2] * scale + base, crosshair_alpha.value);
284         }
285 }
286
287