add Blub's r_track_sprites for SPR_LABEL*
[xonotic/darkplaces.git] / r_sprites.c
1
2 #include "quakedef.h"
3 #include "r_shadow.h"
4
5 extern cvar_t r_labelsprites_scale;
6 extern cvar_t r_labelsprites_roundtopixels;
7 extern cvar_t r_track_sprites;
8 extern cvar_t r_track_sprites_flags;
9 extern cvar_t r_track_sprites_scalew;
10 extern cvar_t r_track_sprites_scaleh;
11
12 #define TSF_ROTATE 1
13 #define TSF_ROTATE_CONTINOUSLY 2
14
15 // use same epsilon as in sv_phys.c, it's not in any header, that's why i redefine it
16 // MIN_EPSILON is for accurateness' sake :)
17 #ifndef EPSILON
18 # define EPSILON (1.0f / 32.0f)
19 # define MIN_EPSILON 0.0001f
20 #endif
21
22 /* R_Track_Sprite
23    If the sprite is out of view, track it.
24    `origin`, `left` and `up` are changed by this function to achive a rotation around
25    the hotspot.
26    
27    --blub
28  */
29 #define SIDE_TOP 1
30 #define SIDE_LEFT 2
31 #define SIDE_BOTTOM 3
32 #define SIDE_RIGHT 4
33
34 void R_TrackSprite(const entity_render_t *ent, vec3_t origin, vec3_t left, vec3_t up, int *edge, float *dir_angle)
35 {
36         vec3_t bCoord; // body coordinates of object
37         unsigned int i;
38         Matrix4x4_Transform(&r_view.inverse_matrix, origin, bCoord);
39
40         *edge = 0; // FIXME::should assume edge == 0, which is correct currently
41         for(i = 0; i < 4; ++i)
42         {
43                 if(PlaneDiff(origin, &r_view.frustum[i]) < -EPSILON)
44                         break;
45         }
46
47         // If it wasn't outside a plane, no tracking needed
48         if(i < 4)
49         {
50                 float x, y;    // screen X and Y coordinates
51                 float ax, ay;  // absolute coords, used for division
52                 // I divide x and y by the greater absolute value to get ranges -1.0 to +1.0
53                 
54                 bCoord[2] *= r_view.frustum_x;
55                 bCoord[1] *= r_view.frustum_y;
56
57                 //Con_Printf("%f %f %f\n", bCoord[0], bCoord[1], bCoord[2]);
58                 
59                 ax = fabs(bCoord[1]);
60                 ay = fabs(bCoord[2]);
61                 // get the greater value and determine the screen edge it's on
62                 if(ax < ay)
63                 {
64                         ax = ay;
65                         // 180 or 0 degrees
66                         if(bCoord[2] < 0.0f)
67                                 *edge = SIDE_BOTTOM;
68                         else
69                                 *edge = SIDE_TOP;
70                 } else {
71                         if(bCoord[1] < 0.0f)
72                                 *edge = SIDE_RIGHT;
73                         else
74                                 *edge = SIDE_LEFT;
75                 }
76                 // should I add || blah == -0.0f? since -0.0f != 0.0f binary speaking, the FPU shouldn't care though?
77                 if(ax == 0.0f)
78                         ax = MIN_EPSILON;
79                 // get the -1 to +1 range
80                 x = bCoord[1] / ax;
81                 y = bCoord[2] / ax;
82                 
83                 // Place the sprite a few units ahead of the player
84                 VectorCopy(r_view.origin, origin);
85                 VectorMA(origin, 50.0f, r_view.forward, origin);
86                 // Move the sprite left / up the screeen height
87                 ax = (1.0f / VectorLength(left));
88                 ay = (1.0f / VectorLength(up));
89                 VectorMA(origin, 50.0f * r_view.frustum_x * x * ax, left, origin);
90                 VectorMA(origin, 50.0f * r_view.frustum_y * y * ay, up, origin);
91
92                 if(r_track_sprites_flags.integer & TSF_ROTATE_CONTINOUSLY)
93                 {
94                         // compute the rotation, negate y axis, we're pointing outwards
95                         *dir_angle = atan(-y / x) * 180.0f/M_PI;
96                         // we need the real, full angle
97                         if(x < 0.0f)
98                                 *dir_angle += 180.0f;
99                 }
100
101                 left[0] *= r_track_sprites_scalew.value;
102                 left[1] *= r_track_sprites_scalew.value;
103                 left[2] *= r_track_sprites_scalew.value;
104
105                 up[0] *= r_track_sprites_scaleh.value;
106                 up[1] *= r_track_sprites_scaleh.value;
107                 up[2] *= r_track_sprites_scaleh.value;
108         }
109 }
110
111 void R_RotateSprite(const mspriteframe_t *frame, vec3_t origin, vec3_t left, vec3_t up, int edge, float dir_angle)
112 {
113         if(!(r_track_sprites_flags.integer & TSF_ROTATE))
114         {
115                 // move down by its size if on top, otherwise it's invisible
116                 if(edge == SIDE_TOP)
117                         VectorMA(origin, -(fabs(frame->up)+fabs(frame->down)), up, origin);
118         } else {
119                 static float rotation_angles[5] =
120                 {
121                         0, // no edge
122                         -90.0f, //top
123                         0.0f,   // left
124                         90.0f,  // bottom
125                         180.0f, // right
126                 };
127                 
128                 // rotate around the hotspot according to which edge it's on
129                 // since the hotspot == the origin, only rotate the vectors
130                 matrix4x4_t rotm;
131                 vec3_t axis;
132                 vec3_t temp;
133                 vec2_t dir;
134                 float angle;
135
136                 if(edge < 1 || edge > 4)
137                         return; // this usually means something went wrong somewhere, there's no way to get a wrong edge value currently
138                 
139                 dir[0] = frame->right + frame->left;
140                 dir[1] = frame->down + frame->up;
141
142                 // only rotate when the hotspot isn't the center though.
143                 if(dir[0] < MIN_EPSILON && dir[1] < MIN_EPSILON)
144                 {
145                         return;
146                 }
147
148                 // Now that we've kicked center-hotspotted sprites, rotate using the appropriate matrix :)
149
150                 // determine the angle of a sprite, we could only do that once though and
151                 // add a `qboolean initialized' to the mspriteframe_t struct... let's get the direction vector of it :)
152
153                 angle = atan(dir[1] / dir[0]) * 180.0f/M_PI;
154
155                 // we need the real, full angle
156                 if(dir[0] < 0.0f)
157                         angle += 180.0f;
158
159                 // Rotate around rotation_angle - frame_angle
160                 // The axis SHOULD equal r_view.forward, but let's generalize this:
161                 CrossProduct(up, left, axis);
162                 if(r_track_sprites_flags.integer & TSF_ROTATE_CONTINOUSLY)
163                         Matrix4x4_CreateRotate(&rotm, dir_angle - angle, axis[0], axis[1], axis[2]);
164                 else
165                         Matrix4x4_CreateRotate(&rotm, rotation_angles[edge] - angle, axis[0], axis[1], axis[2]);
166                 Matrix4x4_Transform(&rotm, up, temp);
167                 VectorCopy(temp, up);
168                 Matrix4x4_Transform(&rotm, left, temp);
169                 VectorCopy(temp, left);
170         }
171 }
172
173 void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
174 {
175         int i;
176         model_t *model = ent->model;
177         vec3_t left, up, org, mforward, mleft, mup, middle;
178         float scale, dx, dy, hud_vs_screen;
179         int edge = 0;
180         float dir_angle = 0.0f;
181
182         // nudge it toward the view to make sure it isn't in a wall
183         Matrix4x4_ToVectors(&ent->matrix, mforward, mleft, mup, org);
184         VectorSubtract(org, r_view.forward, org);
185         switch(model->sprite.sprnum_type)
186         {
187         case SPR_VP_PARALLEL_UPRIGHT:
188                 // flames and such
189                 // vertical beam sprite, faces view plane
190                 scale = ent->scale / sqrt(r_view.forward[0]*r_view.forward[0]+r_view.forward[1]*r_view.forward[1]);
191                 left[0] = -r_view.forward[1] * scale;
192                 left[1] = r_view.forward[0] * scale;
193                 left[2] = 0;
194                 up[0] = 0;
195                 up[1] = 0;
196                 up[2] = ent->scale;
197                 break;
198         case SPR_FACING_UPRIGHT:
199                 // flames and such
200                 // vertical beam sprite, faces viewer's origin (not the view plane)
201                 scale = ent->scale / sqrt((org[0] - r_view.origin[0])*(org[0] - r_view.origin[0])+(org[1] - r_view.origin[1])*(org[1] - r_view.origin[1]));
202                 left[0] = (org[1] - r_view.origin[1]) * scale;
203                 left[1] = -(org[0] - r_view.origin[0]) * scale;
204                 left[2] = 0;
205                 up[0] = 0;
206                 up[1] = 0;
207                 up[2] = ent->scale;
208                 break;
209         default:
210                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", model->sprite.sprnum_type);
211                 // fall through to normal sprite
212         case SPR_VP_PARALLEL:
213                 // normal sprite
214                 // faces view plane
215                 VectorScale(r_view.left, ent->scale, left);
216                 VectorScale(r_view.up, ent->scale, up);
217                 break;
218         case SPR_LABEL_SCALE:
219                 // normal sprite
220                 // faces view plane
221                 // fixed HUD pixel size specified in sprite
222                 // honors scale
223                 // honors a global label scaling cvar
224
225                 // See the R_TrackSprite definition for a reason for this copying
226                 VectorCopy(r_view.left, left);
227                 VectorCopy(r_view.up, up);
228                 // It has to be done before the calculations, because it moves the origin.
229                 if(r_track_sprites.integer)
230                         R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
231                 
232                 scale = 2 * ent->scale * (DotProduct(r_view.forward, org) - DotProduct(r_view.forward, r_view.origin)) * r_labelsprites_scale.value;
233                 VectorScale(left, scale * r_view.frustum_x / vid_conwidth.integer, left); // 1px
234                 VectorScale(up, scale * r_view.frustum_y / vid_conheight.integer, up); // 1px
235                 break;
236         case SPR_LABEL:
237                 // normal sprite
238                 // faces view plane
239                 // fixed pixel size specified in sprite
240                 // tries to get the right size in HUD units, if possible
241                 // ignores scale
242                 // honors a global label scaling cvar before the rounding
243                 // FIXME assumes that 1qu is 1 pixel in the sprite like in SPR32 format. Should not do that, but instead query the source image! This bug only applies to the roundtopixels case, though.
244
245                 // See the R_TrackSprite definition for a reason for this copying
246                 VectorCopy(r_view.left, left);
247                 VectorCopy(r_view.up, up);
248                 // It has to be done before the calculations, because it moves the origin.
249                 if(r_track_sprites.integer)
250                         R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
251                 
252                 scale = 2 * (DotProduct(r_view.forward, org) - DotProduct(r_view.forward, r_view.origin));
253
254                 if(r_labelsprites_roundtopixels.integer)
255                 {
256                         hud_vs_screen = max(
257                                 vid_conwidth.integer / (float) r_view.width,
258                                 vid_conheight.integer / (float) r_view.height
259                         ) / max(0.125, r_labelsprites_scale.value);
260
261                         // snap to "good sizes"
262                         // 1     for (0.6, 1.41]
263                         // 2     for (1.8, 3.33]
264                         if(hud_vs_screen <= 0.6)
265                                 hud_vs_screen = 0; // don't, use real HUD pixels
266                         else if(hud_vs_screen <= 1.41)
267                                 hud_vs_screen = 1;
268                         else if(hud_vs_screen <= 3.33)
269                                 hud_vs_screen = 2;
270                         else
271                                 hud_vs_screen = 0; // don't, use real HUD pixels
272
273                         if(hud_vs_screen)
274                         {
275                                 // use screen pixels
276                                 VectorScale(left, scale * r_view.frustum_x / (r_view.width * hud_vs_screen), left); // 1px
277                                 VectorScale(up, scale * r_view.frustum_y / (r_view.height * hud_vs_screen), up); // 1px
278                         }
279                         else
280                         {
281                                 // use HUD pixels
282                                 VectorScale(left, scale * r_view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
283                                 VectorScale(up, scale * r_view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
284                         }
285
286                         if(hud_vs_screen == 1)
287                         {
288                                 VectorMA(r_view.origin, scale, r_view.forward, middle); // center of screen in distance scale
289                                 dx = 0.5 - fmod(r_view.width * 0.5 + (DotProduct(org, left) - DotProduct(middle, left)) / DotProduct(left, left) + 0.5, 1.0);
290                                 dy = 0.5 - fmod(r_view.height * 0.5 + (DotProduct(org, up) - DotProduct(middle, up)) / DotProduct(up, up) + 0.5, 1.0);
291                                 VectorMAMAM(1, org, dx, left, dy, up, org);
292                         }
293                 }
294                 else
295                 {
296                         // use HUD pixels
297                         VectorScale(left, scale * r_view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
298                         VectorScale(up, scale * r_view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
299                 }
300                 break;
301         case SPR_ORIENTED:
302                 // bullet marks on walls
303                 // ignores viewer entirely
304                 VectorCopy(mleft, left);
305                 VectorCopy(mup, up);
306                 break;
307         case SPR_VP_PARALLEL_ORIENTED:
308                 // I have no idea what people would use this for...
309                 // oriented relative to view space
310                 // FIXME: test this and make sure it mimicks software
311                 left[0] = mleft[0] * r_view.forward[0] + mleft[1] * r_view.left[0] + mleft[2] * r_view.up[0];
312                 left[1] = mleft[0] * r_view.forward[1] + mleft[1] * r_view.left[1] + mleft[2] * r_view.up[1];
313                 left[2] = mleft[0] * r_view.forward[2] + mleft[1] * r_view.left[2] + mleft[2] * r_view.up[2];
314                 up[0] = mup[0] * r_view.forward[0] + mup[1] * r_view.left[0] + mup[2] * r_view.up[0];
315                 up[1] = mup[0] * r_view.forward[1] + mup[1] * r_view.left[1] + mup[2] * r_view.up[1];
316                 up[2] = mup[0] * r_view.forward[2] + mup[1] * r_view.left[2] + mup[2] * r_view.up[2];
317                 break;
318         }
319
320         R_Mesh_Matrix(&identitymatrix);
321
322         // LordHavoc: interpolated sprite rendering
323         for (i = 0;i < 4;i++)
324         {
325                 if (ent->frameblend[i].lerp >= 0.01f)
326                 {
327                         mspriteframe_t *frame = model->sprite.sprdata_frames + ent->frameblend[i].frame;
328                         texture_t *texture = model->data_textures + ent->frameblend[i].frame;
329 #if 0
330                         vec3_t o, l, u;
331 #endif
332                         R_UpdateTextureInfo(ent, texture);
333                         if(edge)
334                         {
335                                 // FIXME:: save vectors/origin and re-rotate? necessary if the hotspot can change per frame
336 #if 0
337                                 VectorCopy(org, o);
338                                 VectorCopy(left, l);
339                                 VectorCopy(up, u);
340 #endif
341                                 R_RotateSprite(frame, org, left, up, edge, dir_angle);
342 #if 1
343                                 edge = 0;
344 #endif
345                         }
346                         // FIXME: negate left and right in loader
347                         R_DrawSprite(texture->currentlayers[0].blendfunc1, texture->currentlayers[0].blendfunc2, texture->basetexture, texture->currentskinframe->fog, (texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST), (texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE), org, left, up, frame->left, frame->right, frame->down, frame->up, texture->currentlayers[0].color[0], texture->currentlayers[0].color[1], texture->currentlayers[0].color[2], ent->alpha * ent->frameblend[i].lerp);
348                         if(edge)
349                         {
350 #if 0
351                                 VectorCopy(o, org);
352                                 VectorCopy(l, left);
353                                 VectorCopy(u, up);
354 #endif
355                         }
356                 }
357         }
358 }
359
360 void R_Model_Sprite_Draw(entity_render_t *ent)
361 {
362         vec3_t org;
363         if (ent->frameblend[0].frame < 0)
364                 return;
365
366         Matrix4x4_OriginFromMatrix(&ent->matrix, org);
367         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_view.origin : org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);
368 }
369