fix label sprites if r_waterstate.renderingscene
[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_refdef.view.origin, bCoord);
42         distance = VectorLength(bCoord);
43
44         // Now get the bCoords :)
45         Matrix4x4_Transform(&r_refdef.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_refdef.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_refdef.view.frustum_x;
62                 bCoord[1] *= r_refdef.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_refdef.view.frustum_x*r_refdef.view.frustum_x * x*x * ax*ax +
111                                         r_refdef.view.frustum_y*r_refdef.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_refdef.view.origin, origin);
116                 VectorMA(origin, distance, r_refdef.view.forward, origin);
117                 // Move the sprite left / up the screeen height
118                 VectorMA(origin, distance * r_refdef.view.frustum_x * x * ax, left, origin);
119                 VectorMA(origin, distance * r_refdef.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_refdef.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         dp_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_refdef.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_refdef.view.forward[0]*r_refdef.view.forward[0]+r_refdef.view.forward[1]*r_refdef.view.forward[1]);
220                 left[0] = -r_refdef.view.forward[1] * scale;
221                 left[1] = r_refdef.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_refdef.view.origin[0])*(org[0] - r_refdef.view.origin[0])+(org[1] - r_refdef.view.origin[1])*(org[1] - r_refdef.view.origin[1]));
231                 left[0] = (org[1] - r_refdef.view.origin[1]) * scale;
232                 left[1] = -(org[0] - r_refdef.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_refdef.view.left, ent->scale, left);
245                 VectorScale(r_refdef.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                 if(r_waterstate.renderingscene) // labels are considered HUD items, and don't appear in reflections
255                         return;
256
257                 // See the R_TrackSprite definition for a reason for this copying
258                 VectorCopy(r_refdef.view.left, left);
259                 VectorCopy(r_refdef.view.up, up);
260                 // It has to be done before the calculations, because it moves the origin.
261                 if(r_track_sprites.integer)
262                         R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
263                 
264                 scale = 2 * ent->scale * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin)) * r_labelsprites_scale.value;
265                 VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer, left); // 1px
266                 VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer, up); // 1px
267                 break;
268         case SPR_LABEL:
269                 // normal sprite
270                 // faces view plane
271                 // fixed pixel size specified in sprite
272                 // tries to get the right size in HUD units, if possible
273                 // ignores scale
274                 // honors a global label scaling cvar before the rounding
275                 // 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.
276
277                 if(r_waterstate.renderingscene) // labels are considered HUD items, and don't appear in reflections
278                         return;
279
280                 // See the R_TrackSprite definition for a reason for this copying
281                 VectorCopy(r_refdef.view.left, left);
282                 VectorCopy(r_refdef.view.up, up);
283                 // It has to be done before the calculations, because it moves the origin.
284                 if(r_track_sprites.integer)
285                         R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
286                 
287                 scale = 2 * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin));
288
289                 if(r_labelsprites_roundtopixels.integer)
290                 {
291                         hud_vs_screen = max(
292                                 vid_conwidth.integer / (float) r_refdef.view.width,
293                                 vid_conheight.integer / (float) r_refdef.view.height
294                         ) / max(0.125, r_labelsprites_scale.value);
295
296                         // snap to "good sizes"
297                         // 1     for (0.6, 1.41]
298                         // 2     for (1.8, 3.33]
299                         if(hud_vs_screen <= 0.6)
300                                 hud_vs_screen = 0; // don't, use real HUD pixels
301                         else if(hud_vs_screen <= 1.41)
302                                 hud_vs_screen = 1;
303                         else if(hud_vs_screen <= 3.33)
304                                 hud_vs_screen = 2;
305                         else
306                                 hud_vs_screen = 0; // don't, use real HUD pixels
307
308                         if(hud_vs_screen)
309                         {
310                                 // use screen pixels
311                                 VectorScale(left, scale * r_refdef.view.frustum_x / (r_refdef.view.width * hud_vs_screen), left); // 1px
312                                 VectorScale(up, scale * r_refdef.view.frustum_y / (r_refdef.view.height * hud_vs_screen), up); // 1px
313                         }
314                         else
315                         {
316                                 // use HUD pixels
317                                 VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
318                                 VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
319                         }
320
321                         if(hud_vs_screen == 1)
322                         {
323                                 VectorMA(r_refdef.view.origin, scale, r_refdef.view.forward, middle); // center of screen in distance scale
324                                 dx = 0.5 - fmod(r_refdef.view.width * 0.5 + (DotProduct(org, left) - DotProduct(middle, left)) / DotProduct(left, left) + 0.5, 1.0);
325                                 dy = 0.5 - fmod(r_refdef.view.height * 0.5 + (DotProduct(org, up) - DotProduct(middle, up)) / DotProduct(up, up) + 0.5, 1.0);
326                                 VectorMAMAM(1, org, dx, left, dy, up, org);
327                         }
328                 }
329                 else
330                 {
331                         // use HUD pixels
332                         VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
333                         VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
334                 }
335                 break;
336         case SPR_ORIENTED:
337                 // bullet marks on walls
338                 // ignores viewer entirely
339                 VectorCopy(mleft, left);
340                 VectorCopy(mup, up);
341                 break;
342         case SPR_VP_PARALLEL_ORIENTED:
343                 // I have no idea what people would use this for...
344                 // oriented relative to view space
345                 // FIXME: test this and make sure it mimicks software
346                 left[0] = mleft[0] * r_refdef.view.forward[0] + mleft[1] * r_refdef.view.left[0] + mleft[2] * r_refdef.view.up[0];
347                 left[1] = mleft[0] * r_refdef.view.forward[1] + mleft[1] * r_refdef.view.left[1] + mleft[2] * r_refdef.view.up[1];
348                 left[2] = mleft[0] * r_refdef.view.forward[2] + mleft[1] * r_refdef.view.left[2] + mleft[2] * r_refdef.view.up[2];
349                 up[0] = mup[0] * r_refdef.view.forward[0] + mup[1] * r_refdef.view.left[0] + mup[2] * r_refdef.view.up[0];
350                 up[1] = mup[0] * r_refdef.view.forward[1] + mup[1] * r_refdef.view.left[1] + mup[2] * r_refdef.view.up[1];
351                 up[2] = mup[0] * r_refdef.view.forward[2] + mup[1] * r_refdef.view.left[2] + mup[2] * r_refdef.view.up[2];
352                 break;
353         }
354
355         R_Mesh_Matrix(&identitymatrix);
356
357         // LordHavoc: interpolated sprite rendering
358         for (i = 0;i < 4;i++)
359         {
360                 if (ent->frameblend[i].lerp >= 0.01f)
361                 {
362                         mspriteframe_t *frame = model->sprite.sprdata_frames + ent->frameblend[i].frame;
363                         texture_t *texture = model->data_textures + ent->frameblend[i].frame;
364 #if 0
365                         vec3_t o, l, u;
366 #endif
367                         R_UpdateTextureInfo(ent, texture);
368
369                         // SPR_LABEL should not use depth test AT ALL
370                         if(model->sprite.sprnum_type == SPR_LABEL || model->sprite.sprnum_type == SPR_LABEL_SCALE)
371                                 if(texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE)
372                                         texture->currentmaterialflags = (texture->currentmaterialflags & ~MATERIALFLAG_SHORTDEPTHRANGE) | MATERIALFLAG_NODEPTHTEST;
373
374                         if(edge)
375                         {
376                                 // FIXME:: save vectors/origin and re-rotate? necessary if the hotspot can change per frame
377 #if 0
378                                 VectorCopy(org, o);
379                                 VectorCopy(left, l);
380                                 VectorCopy(up, u);
381 #endif
382                                 R_RotateSprite(frame, org, left, up, edge, dir_angle);
383 #if 1
384                                 edge = 0;
385 #endif
386                         }
387                         // FIXME: negate left and right in loader
388                         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);
389                         if(edge)
390                         {
391 #if 0
392                                 VectorCopy(o, org);
393                                 VectorCopy(l, left);
394                                 VectorCopy(u, up);
395 #endif
396                         }
397                 }
398         }
399 }
400
401 void R_Model_Sprite_Draw(entity_render_t *ent)
402 {
403         vec3_t org;
404         if (ent->frameblend[0].frame < 0)
405                 return;
406
407         Matrix4x4_OriginFromMatrix(&ent->matrix, org);
408         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_refdef.view.origin : org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);
409 }
410