]> git.xonotic.org Git - xonotic/darkplaces.git/blob - r_sprites.c
support slope-dependent shadowmap biasing with polygon offset
[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         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         // R_GetCurrentTexture needs this
212         rsurface.entity = (entity_render_t *)ent;
213
214         // nudge it toward the view to make sure it isn't in a wall
215         Matrix4x4_ToVectors(&ent->matrix, mforward, mleft, mup, org);
216         VectorSubtract(org, r_refdef.view.forward, org);
217         switch(model->sprite.sprnum_type)
218         {
219         case SPR_VP_PARALLEL_UPRIGHT:
220                 // flames and such
221                 // vertical beam sprite, faces view plane
222                 scale = ent->scale / sqrt(r_refdef.view.forward[0]*r_refdef.view.forward[0]+r_refdef.view.forward[1]*r_refdef.view.forward[1]);
223                 left[0] = -r_refdef.view.forward[1] * scale;
224                 left[1] = r_refdef.view.forward[0] * scale;
225                 left[2] = 0;
226                 up[0] = 0;
227                 up[1] = 0;
228                 up[2] = ent->scale;
229                 break;
230         case SPR_FACING_UPRIGHT:
231                 // flames and such
232                 // vertical beam sprite, faces viewer's origin (not the view plane)
233                 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]));
234                 left[0] = (org[1] - r_refdef.view.origin[1]) * scale;
235                 left[1] = -(org[0] - r_refdef.view.origin[0]) * scale;
236                 left[2] = 0;
237                 up[0] = 0;
238                 up[1] = 0;
239                 up[2] = ent->scale;
240                 break;
241         default:
242                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", model->sprite.sprnum_type);
243                 // fall through to normal sprite
244         case SPR_VP_PARALLEL:
245                 // normal sprite
246                 // faces view plane
247                 VectorScale(r_refdef.view.left, ent->scale, left);
248                 VectorScale(r_refdef.view.up, ent->scale, up);
249                 break;
250         case SPR_LABEL_SCALE:
251                 // normal sprite
252                 // faces view plane
253                 // fixed HUD pixel size specified in sprite
254                 // honors scale
255                 // honors a global label scaling cvar
256         
257                 if(r_waterstate.renderingscene) // labels are considered HUD items, and don't appear in reflections
258                         return;
259
260                 // See the R_TrackSprite definition for a reason for this copying
261                 VectorCopy(r_refdef.view.left, left);
262                 VectorCopy(r_refdef.view.up, up);
263                 // It has to be done before the calculations, because it moves the origin.
264                 if(r_track_sprites.integer)
265                         R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
266                 
267                 scale = 2 * ent->scale * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin)) * r_labelsprites_scale.value;
268                 VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer, left); // 1px
269                 VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer, up); // 1px
270                 break;
271         case SPR_LABEL:
272                 // normal sprite
273                 // faces view plane
274                 // fixed pixel size specified in sprite
275                 // tries to get the right size in HUD units, if possible
276                 // ignores scale
277                 // honors a global label scaling cvar before the rounding
278                 // 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.
279
280                 if(r_waterstate.renderingscene) // labels are considered HUD items, and don't appear in reflections
281                         return;
282
283                 // See the R_TrackSprite definition for a reason for this copying
284                 VectorCopy(r_refdef.view.left, left);
285                 VectorCopy(r_refdef.view.up, up);
286                 // It has to be done before the calculations, because it moves the origin.
287                 if(r_track_sprites.integer)
288                         R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
289                 
290                 scale = 2 * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin));
291
292                 if(r_labelsprites_roundtopixels.integer)
293                 {
294                         hud_vs_screen = max(
295                                 vid_conwidth.integer / (float) r_refdef.view.width,
296                                 vid_conheight.integer / (float) r_refdef.view.height
297                         ) / max(0.125, r_labelsprites_scale.value);
298
299                         // snap to "good sizes"
300                         // 1     for (0.6, 1.41]
301                         // 2     for (1.8, 3.33]
302                         if(hud_vs_screen <= 0.6)
303                                 hud_vs_screen = 0; // don't, use real HUD pixels
304                         else if(hud_vs_screen <= 1.41)
305                                 hud_vs_screen = 1;
306                         else if(hud_vs_screen <= 3.33)
307                                 hud_vs_screen = 2;
308                         else
309                                 hud_vs_screen = 0; // don't, use real HUD pixels
310
311                         if(hud_vs_screen)
312                         {
313                                 // use screen pixels
314                                 VectorScale(left, scale * r_refdef.view.frustum_x / (r_refdef.view.width * hud_vs_screen), left); // 1px
315                                 VectorScale(up, scale * r_refdef.view.frustum_y / (r_refdef.view.height * hud_vs_screen), up); // 1px
316                         }
317                         else
318                         {
319                                 // use HUD pixels
320                                 VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
321                                 VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
322                         }
323
324                         if(hud_vs_screen == 1)
325                         {
326                                 VectorMA(r_refdef.view.origin, scale, r_refdef.view.forward, middle); // center of screen in distance scale
327                                 dx = 0.5 - fmod(r_refdef.view.width * 0.5 + (DotProduct(org, left) - DotProduct(middle, left)) / DotProduct(left, left) + 0.5, 1.0);
328                                 dy = 0.5 - fmod(r_refdef.view.height * 0.5 + (DotProduct(org, up) - DotProduct(middle, up)) / DotProduct(up, up) + 0.5, 1.0);
329                                 VectorMAMAM(1, org, dx, left, dy, up, org);
330                         }
331                 }
332                 else
333                 {
334                         // use HUD pixels
335                         VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
336                         VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
337                 }
338                 break;
339         case SPR_ORIENTED:
340                 // bullet marks on walls
341                 // ignores viewer entirely
342                 VectorCopy(mleft, left);
343                 VectorCopy(mup, up);
344                 break;
345         case SPR_VP_PARALLEL_ORIENTED:
346                 // I have no idea what people would use this for...
347                 // oriented relative to view space
348                 // FIXME: test this and make sure it mimicks software
349                 left[0] = mleft[0] * r_refdef.view.forward[0] + mleft[1] * r_refdef.view.left[0] + mleft[2] * r_refdef.view.up[0];
350                 left[1] = mleft[0] * r_refdef.view.forward[1] + mleft[1] * r_refdef.view.left[1] + mleft[2] * r_refdef.view.up[1];
351                 left[2] = mleft[0] * r_refdef.view.forward[2] + mleft[1] * r_refdef.view.left[2] + mleft[2] * r_refdef.view.up[2];
352                 up[0] = mup[0] * r_refdef.view.forward[0] + mup[1] * r_refdef.view.left[0] + mup[2] * r_refdef.view.up[0];
353                 up[1] = mup[0] * r_refdef.view.forward[1] + mup[1] * r_refdef.view.left[1] + mup[2] * r_refdef.view.up[1];
354                 up[2] = mup[0] * r_refdef.view.forward[2] + mup[1] * r_refdef.view.left[2] + mup[2] * r_refdef.view.up[2];
355                 break;
356         }
357
358         R_Mesh_Matrix(&identitymatrix);
359
360         // LordHavoc: interpolated sprite rendering
361         for (i = 0;i < MAX_FRAMEBLENDS;i++)
362         {
363                 if (ent->frameblend[i].lerp >= 0.01f)
364                 {
365                         mspriteframe_t *frame = model->sprite.sprdata_frames + ent->frameblend[i].subframe;
366                         texture_t *texture = R_GetCurrentTexture(model->data_textures + ent->frameblend[i].subframe);
367 #if 0
368                         vec3_t o, l, u;
369 #endif
370
371                         // SPR_LABEL should not use depth test AT ALL
372                         if(model->sprite.sprnum_type == SPR_LABEL || model->sprite.sprnum_type == SPR_LABEL_SCALE)
373                                 if(texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE)
374                                         texture->currentmaterialflags = (texture->currentmaterialflags & ~MATERIALFLAG_SHORTDEPTHRANGE) | MATERIALFLAG_NODEPTHTEST;
375
376                         if(edge)
377                         {
378                                 // FIXME:: save vectors/origin and re-rotate? necessary if the hotspot can change per frame
379 #if 0
380                                 VectorCopy(org, o);
381                                 VectorCopy(left, l);
382                                 VectorCopy(up, u);
383 #endif
384                                 R_RotateSprite(frame, org, left, up, edge, dir_angle);
385 #if 1
386                                 edge = 0;
387 #endif
388                         }
389                         // FIXME: negate left and right in loader
390                         R_DrawSprite(texture->currentlayers[0].blendfunc1, texture->currentlayers[0].blendfunc2, texture->basetexture, texture->currentskinframe->fog, (texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST) != 0, (texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE) != 0, 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);
391                         if(edge)
392                         {
393 #if 0
394                                 VectorCopy(o, org);
395                                 VectorCopy(l, left);
396                                 VectorCopy(u, up);
397 #endif
398                         }
399                 }
400         }
401
402         rsurface.entity = NULL;
403 }
404
405 void R_Model_Sprite_Draw(entity_render_t *ent)
406 {
407         vec3_t org;
408         if (ent->frameblend[0].subframe < 0)
409                 return;
410
411         Matrix4x4_OriginFromMatrix(&ent->matrix, org);
412         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_refdef.view.origin : org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);
413 }
414