]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - r_crosshairs.c
commented out bit-hacking 1/sqrt code
[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 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         rmeshbufferinfo_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.depthdisable = true;
172         m.numtriangles = 2;
173         m.numverts = 4;
174         m.tex[0] = R_GetTexture(texture);
175         Matrix4x4_CreateIdentity(&m.matrix);
176         if (R_Mesh_Draw_GetBuffer(&m, false))
177         {
178                 m.index[0] = 0;
179                 m.index[1] = 1;
180                 m.index[2] = 2;
181                 m.index[3] = 0;
182                 m.index[4] = 2;
183                 m.index[5] = 3;
184                 m.color[0] = m.color[4] = m.color[8] = m.color[12] = cr * m.colorscale;
185                 m.color[1] = m.color[5] = m.color[9] = m.color[13] = cg * m.colorscale;
186                 m.color[2] = m.color[6] = m.color[10] = m.color[14] = cb * m.colorscale;
187                 m.color[3] = m.color[7] = m.color[11] = m.color[15] = ca;
188                 m.texcoords[0][0] = 0;
189                 m.texcoords[0][1] = 0;
190                 m.texcoords[0][2] = 0;
191                 m.texcoords[0][3] = 1;
192                 m.texcoords[0][4] = 1;
193                 m.texcoords[0][5] = 1;
194                 m.texcoords[0][6] = 1;
195                 m.texcoords[0][7] = 0;
196                 m.vertex[0] = origin[0] - vright[0] * scale - vup[0] * scale;
197                 m.vertex[1] = origin[1] - vright[1] * scale - vup[1] * scale;
198                 m.vertex[2] = origin[2] - vright[2] * scale - vup[2] * scale;
199                 m.vertex[4] = origin[0] - vright[0] * scale + vup[0] * scale;
200                 m.vertex[5] = origin[1] - vright[1] * scale + vup[1] * scale;
201                 m.vertex[6] = origin[2] - vright[2] * scale + vup[2] * scale;
202                 m.vertex[8] = origin[0] + vright[0] * scale + vup[0] * scale;
203                 m.vertex[9] = origin[1] + vright[1] * scale + vup[1] * scale;
204                 m.vertex[10] = origin[2] + vright[2] * scale + vup[2] * scale;
205                 m.vertex[12] = origin[0] + vright[0] * scale - vup[0] * scale;
206                 m.vertex[13] = origin[1] + vright[1] * scale - vup[1] * scale;
207                 m.vertex[14] = origin[2] + vright[2] * scale - vup[2] * scale;
208                 R_Mesh_Render();
209         }
210 }
211
212 void R_DrawCrosshair(void)
213 {
214         int i, num;
215         qbyte *color;
216         float scale, base;
217         vec3_t v1, v2, spriteorigin;
218         vec_t spritescale;
219         float cr, cg, cb, ca;
220         num = crosshair.integer - 1;
221         if (num < 0 || cl.intermission)
222                 return;
223         if (num >= NUMCROSSHAIRS)
224                 num = 0;
225         if (cl.viewentity >= 1 && cl.viewentity <= cl.maxclients)
226         {
227                 i = (cl.scores[cl.viewentity-1].colors & 0xF) << 4;
228                 if (i >= 208 && i < 224) // blue
229                         i += 8;
230                 else if (i < 128 || i >= 224) // 128-224 are backwards ranges (bright to dark, rather than dark to bright)
231                         i += 15;
232         }
233         else
234                 i = 15;
235         color = (qbyte *) &d_8to24table[i];
236         if (crosshair_flashspeed.value >= 0.01f)
237                 base = (sin(realtime * crosshair_flashspeed.value * (M_PI*2.0f)) * crosshair_flashrange.value);
238         else
239                 base = 0.0f;
240         scale = crosshair_brightness.value * (1.0f / 255.0f);
241
242         if (crosshair_static.integer)
243         {
244                 char *picname;
245                 cachepic_t *pic;
246
247                 picname = va("gfx/crosshair%i.tga", num + 1);
248                 pic = Draw_CachePic(picname);
249                 if (pic)
250                         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);
251         }
252         else
253         {
254                 // trace the shot path up to a certain distance
255                 VectorCopy(cl_entities[cl.viewentity].render.origin, v1);
256                 v1[2] += 16; // HACK: this depends on the QC
257                 // get the forward vector for the gun (not the view)
258                 AngleVectors(cl.viewangles, v2, NULL, NULL);
259                 //VectorCopy(r_origin, v1);
260                 VectorMA(v1, 8192, v2, v2);
261                 spritescale = 4.0f + (CL_TraceLine(v1, v2, spriteorigin, NULL, 0, true) * 8192.0f) * (1.0f / 48.0f);
262                 spritescale = bound(0.0f, spritescale, 32.0f);
263                 //VectorMA(spriteorigin, -4, vpn, spriteorigin);
264
265                 cr = color[0] * scale + base;
266                 cg = color[1] * scale + base;
267                 cb = color[2] * scale + base;
268                 ca = crosshair_alpha.value;
269
270                 // clamp the colors so they don't go negative
271                 cr = max(0, cr);
272                 cg = max(0, cg);
273                 cb = max(0, cb);
274                 // might as well clamp the alpha
275                 ca = bound(0, ca, 1.0f);
276
277                 // finally draw the sprite
278                 R_DrawCrosshairSprite(crosshairtexture[num], spriteorigin, spritescale, cr, cg, cb, ca);
279         }
280 }
281
282