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