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;
13 #define TSF_ROTATE_CONTINOUSLY 2
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 :)
18 # define EPSILON (1.0f / 32.0f)
19 # define MIN_EPSILON 0.0001f
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
34 void R_TrackSprite(const entity_render_t *ent, vec3_t origin, vec3_t left, vec3_t up, int *edge, float *dir_angle)
37 vec3_t bCoord; // body coordinates of object
40 // temporarily abuse bCoord as the vector player->sprite-origin
41 VectorSubtract(origin, r_refdef.view.origin, bCoord);
42 distance = VectorLength(bCoord);
44 // Now get the bCoords :)
45 Matrix4x4_Transform(&r_refdef.view.inverse_matrix, origin, bCoord);
47 *edge = 0; // FIXME::should assume edge == 0, which is correct currently
48 for(i = 0; i < 4; ++i)
50 if(PlaneDiff(origin, &r_refdef.view.frustum[i]) < -EPSILON)
54 // If it wasn't outside a plane, no tracking needed
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
61 bCoord[2] *= r_refdef.view.frustum_x;
62 bCoord[1] *= r_refdef.view.frustum_y;
64 //Con_Printf("%f %f %f\n", bCoord[0], bCoord[1], bCoord[2]);
68 // get the greater value and determine the screen edge it's on
85 if(ax < MIN_EPSILON) // this was == 0.0f before --blub
87 // get the -1 to +1 range
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
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))
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
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);
121 if(r_track_sprites_flags.integer & TSF_ROTATE_CONTINOUSLY)
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
127 *dir_angle += 180.0f;
130 left[0] *= r_track_sprites_scalew.value;
131 left[1] *= r_track_sprites_scalew.value;
132 left[2] *= r_track_sprites_scalew.value;
134 up[0] *= r_track_sprites_scaleh.value;
135 up[1] *= r_track_sprites_scaleh.value;
136 up[2] *= r_track_sprites_scaleh.value;
140 void R_RotateSprite(const mspriteframe_t *frame, vec3_t origin, vec3_t left, vec3_t up, int edge, float dir_angle)
142 if(!(r_track_sprites_flags.integer & TSF_ROTATE))
144 // move down by its size if on top, otherwise it's invisible
146 VectorMA(origin, -(fabs(frame->up)+fabs(frame->down)), up, origin);
148 static float rotation_angles[5] =
157 // rotate around the hotspot according to which edge it's on
158 // since the hotspot == the origin, only rotate the vectors
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
168 dir[0] = frame->right + frame->left;
169 dir[1] = frame->down + frame->up;
171 // only rotate when the hotspot isn't the center though.
172 if(dir[0] < MIN_EPSILON && dir[1] < MIN_EPSILON)
177 // Now that we've kicked center-hotspotted sprites, rotate using the appropriate matrix :)
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 :)
182 angle = atan(dir[1] / dir[0]) * 180.0f/M_PI;
184 // we need the real, full angle
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]);
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);
202 void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
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;
209 float dir_angle = 0.0f;
211 // R_GetCurrentTexture needs this
212 rsurface.entity = (entity_render_t *)ent;
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)
219 case SPR_VP_PARALLEL_UPRIGHT:
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;
230 case SPR_FACING_UPRIGHT:
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;
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:
247 VectorScale(r_refdef.view.left, ent->scale, left);
248 VectorScale(r_refdef.view.up, ent->scale, up);
250 case SPR_LABEL_SCALE:
253 // fixed HUD pixel size specified in sprite
255 // honors a global label scaling cvar
257 if(r_waterstate.renderingscene) // labels are considered HUD items, and don't appear in reflections
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);
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
274 // fixed pixel size specified in sprite
275 // tries to get the right size in HUD units, if possible
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.
280 if(r_waterstate.renderingscene) // labels are considered HUD items, and don't appear in reflections
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);
290 scale = 2 * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin));
292 if(r_labelsprites_roundtopixels.integer)
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);
299 // snap to "good sizes"
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)
306 else if(hud_vs_screen <= 3.33)
309 hud_vs_screen = 0; // don't, use real HUD 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
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
324 if(hud_vs_screen == 1)
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);
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
340 // bullet marks on walls
341 // ignores viewer entirely
342 VectorCopy(mleft, left);
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];
358 R_Mesh_Matrix(&identitymatrix);
360 // LordHavoc: interpolated sprite rendering
361 for (i = 0;i < MAX_FRAMEBLENDS;i++)
363 if (ent->frameblend[i].lerp >= 0.01f)
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);
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;
378 // FIXME:: save vectors/origin and re-rotate? necessary if the hotspot can change per frame
384 R_RotateSprite(frame, org, left, up, edge, dir_angle);
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);
402 rsurface.entity = NULL;
405 void R_Model_Sprite_Draw(entity_render_t *ent)
408 if (ent->frameblend[0].subframe < 0)
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);