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