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 extern cvar_t r_overheadsprites_scalex;
14 extern cvar_t r_overheadsprites_scaley;
17 #define TSF_ROTATE_CONTINOUSLY 2
19 // use same epsilon as in sv_phys.c, it's not in any header, that's why i redefine it
20 // MIN_EPSILON is for accurateness' sake :)
22 # define EPSILON (1.0f / 32.0f)
23 # define MIN_EPSILON 0.0001f
27 If the sprite is out of view, track it.
28 `origin`, `left` and `up` are changed by this function to achive a rotation around
38 void R_TrackSprite(const entity_render_t *ent, vec3_t origin, vec3_t left, vec3_t up, int *edge, float *dir_angle)
41 vec3_t bCoord; // body coordinates of object
44 // temporarily abuse bCoord as the vector player->sprite-origin
45 VectorSubtract(origin, r_refdef.view.origin, bCoord);
46 distance = VectorLength(bCoord);
48 // Now get the bCoords :)
49 Matrix4x4_Transform(&r_refdef.view.inverse_matrix, origin, bCoord);
51 *edge = 0; // FIXME::should assume edge == 0, which is correct currently
52 for(i = 0; i < 4; ++i)
54 if(PlaneDiff(origin, &r_refdef.view.frustum[i]) < -EPSILON)
58 // If it wasn't outside a plane, no tracking needed
61 float x, y; // screen X and Y coordinates
62 float ax, ay; // absolute coords, used for division
63 // I divide x and y by the greater absolute value to get ranges -1.0 to +1.0
65 bCoord[2] *= r_refdef.view.frustum_x;
66 bCoord[1] *= r_refdef.view.frustum_y;
68 //Con_Printf("%f %f %f\n", bCoord[0], bCoord[1], bCoord[2]);
72 // get the greater value and determine the screen edge it's on
89 if(ax < MIN_EPSILON) // this was == 0.0f before --blub
91 // get the -1 to +1 range
95 ax = (1.0f / VectorLength(left));
96 ay = (1.0f / VectorLength(up));
97 // Using the placement below the distance of a sprite is
98 // real dist = sqrt(d*d + dfxa*dfxa + dgyb*dgyb)
99 // d is the distance we use
107 // real dist (r) shall be d, so
108 // r*r = d*d + dfxa*dfxa + dgyb*dgyb
109 // r*r = d*d * (1 + fxa*fxa + gyb*gyb)
110 // d*d = r*r / (1 + fxa*fxa + gyb*gyb)
111 // d = sqrt(r*r / (1 + fxa*fxa + gyb*gyb))
113 distance = sqrt((distance*distance) / (1.0 +
114 r_refdef.view.frustum_x*r_refdef.view.frustum_x * x*x * ax*ax +
115 r_refdef.view.frustum_y*r_refdef.view.frustum_y * y*y * ay*ay));
116 // ^ the one we want ^ the one we have ^ our factors
118 // Place the sprite a few units ahead of the player
119 VectorCopy(r_refdef.view.origin, origin);
120 VectorMA(origin, distance, r_refdef.view.forward, origin);
121 // Move the sprite left / up the screeen height
122 VectorMA(origin, distance * r_refdef.view.frustum_x * x * ax, left, origin);
123 VectorMA(origin, distance * r_refdef.view.frustum_y * y * ay, up, origin);
125 if(r_track_sprites_flags.integer & TSF_ROTATE_CONTINOUSLY)
127 // compute the rotation, negate y axis, we're pointing outwards
128 *dir_angle = atan(-y / x) * 180.0f/M_PI;
129 // we need the real, full angle
131 *dir_angle += 180.0f;
134 left[0] *= r_track_sprites_scalew.value;
135 left[1] *= r_track_sprites_scalew.value;
136 left[2] *= r_track_sprites_scalew.value;
138 up[0] *= r_track_sprites_scaleh.value;
139 up[1] *= r_track_sprites_scaleh.value;
140 up[2] *= r_track_sprites_scaleh.value;
144 void R_RotateSprite(const mspriteframe_t *frame, vec3_t origin, vec3_t left, vec3_t up, int edge, float dir_angle)
146 if(!(r_track_sprites_flags.integer & TSF_ROTATE))
148 // move down by its size if on top, otherwise it's invisible
150 VectorMA(origin, -(fabs(frame->up)+fabs(frame->down)), up, origin);
152 static float rotation_angles[5] =
161 // rotate around the hotspot according to which edge it's on
162 // since the hotspot == the origin, only rotate the vectors
169 if(edge < 1 || edge > 4)
170 return; // this usually means something went wrong somewhere, there's no way to get a wrong edge value currently
172 dir[0] = frame->right + frame->left;
173 dir[1] = frame->down + frame->up;
175 // only rotate when the hotspot isn't the center though.
176 if(dir[0] < MIN_EPSILON && dir[1] < MIN_EPSILON)
181 // Now that we've kicked center-hotspotted sprites, rotate using the appropriate matrix :)
183 // determine the angle of a sprite, we could only do that once though and
184 // add a `qboolean initialized' to the mspriteframe_t struct... let's get the direction vector of it :)
186 angle = atan(dir[1] / dir[0]) * 180.0f/M_PI;
188 // we need the real, full angle
192 // Rotate around rotation_angle - frame_angle
193 // The axis SHOULD equal r_refdef.view.forward, but let's generalize this:
194 CrossProduct(up, left, axis);
195 if(r_track_sprites_flags.integer & TSF_ROTATE_CONTINOUSLY)
196 Matrix4x4_CreateRotate(&rotm, dir_angle - angle, axis[0], axis[1], axis[2]);
198 Matrix4x4_CreateRotate(&rotm, rotation_angles[edge] - angle, axis[0], axis[1], axis[2]);
199 Matrix4x4_Transform(&rotm, up, temp);
200 VectorCopy(temp, up);
201 Matrix4x4_Transform(&rotm, left, temp);
202 VectorCopy(temp, left);
206 static float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
208 void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
211 dp_model_t *model = ent->model;
212 vec3_t left, up, org, mforward, mleft, mup, middle;
213 float scale, dx, dy, hud_vs_screen;
215 float dir_angle = 0.0f;
218 // nudge it toward the view to make sure it isn't in a wall
219 Matrix4x4_ToVectors(&ent->matrix, mforward, mleft, mup, org);
220 VectorSubtract(org, r_refdef.view.forward, org);
221 switch(model->sprite.sprnum_type)
223 case SPR_VP_PARALLEL_UPRIGHT:
225 // vertical beam sprite, faces view plane
226 scale = ent->scale / sqrt(r_refdef.view.forward[0]*r_refdef.view.forward[0]+r_refdef.view.forward[1]*r_refdef.view.forward[1]);
227 left[0] = -r_refdef.view.forward[1] * scale;
228 left[1] = r_refdef.view.forward[0] * scale;
234 case SPR_FACING_UPRIGHT:
236 // vertical beam sprite, faces viewer's origin (not the view plane)
237 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]));
238 left[0] = (org[1] - r_refdef.view.origin[1]) * scale;
239 left[1] = -(org[0] - r_refdef.view.origin[0]) * scale;
246 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", model->sprite.sprnum_type);
247 // fall through to normal sprite
248 case SPR_VP_PARALLEL:
251 VectorScale(r_refdef.view.left, ent->scale, left);
252 VectorScale(r_refdef.view.up, ent->scale, up);
254 case SPR_LABEL_SCALE:
257 // fixed HUD pixel size specified in sprite
259 // honors a global label scaling cvar
261 if(r_waterstate.renderingscene) // labels are considered HUD items, and don't appear in reflections
264 // See the R_TrackSprite definition for a reason for this copying
265 VectorCopy(r_refdef.view.left, left);
266 VectorCopy(r_refdef.view.up, up);
267 // It has to be done before the calculations, because it moves the origin.
268 if(r_track_sprites.integer)
269 R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
271 scale = 2 * ent->scale * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin)) * r_labelsprites_scale.value;
272 VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer, left); // 1px
273 VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer, up); // 1px
278 // fixed pixel size specified in sprite
279 // tries to get the right size in HUD units, if possible
281 // honors a global label scaling cvar before the rounding
282 // 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.
284 if(r_waterstate.renderingscene) // labels are considered HUD items, and don't appear in reflections
287 // See the R_TrackSprite definition for a reason for this copying
288 VectorCopy(r_refdef.view.left, left);
289 VectorCopy(r_refdef.view.up, up);
290 // It has to be done before the calculations, because it moves the origin.
291 if(r_track_sprites.integer)
292 R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
294 scale = 2 * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin));
296 if(r_labelsprites_roundtopixels.integer)
299 vid_conwidth.integer / (float) r_refdef.view.width,
300 vid_conheight.integer / (float) r_refdef.view.height
301 ) / max(0.125, r_labelsprites_scale.value);
303 // snap to "good sizes"
306 if(hud_vs_screen <= 0.6)
307 hud_vs_screen = 0; // don't, use real HUD pixels
308 else if(hud_vs_screen <= 1.41)
310 else if(hud_vs_screen <= 3.33)
313 hud_vs_screen = 0; // don't, use real HUD pixels
318 VectorScale(left, scale * r_refdef.view.frustum_x / (r_refdef.view.width * hud_vs_screen), left); // 1px
319 VectorScale(up, scale * r_refdef.view.frustum_y / (r_refdef.view.height * hud_vs_screen), up); // 1px
324 VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
325 VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
328 if(hud_vs_screen == 1)
330 VectorMA(r_refdef.view.origin, scale, r_refdef.view.forward, middle); // center of screen in distance scale
331 dx = 0.5 - fmod(r_refdef.view.width * 0.5 + (DotProduct(org, left) - DotProduct(middle, left)) / DotProduct(left, left) + 0.5, 1.0);
332 dy = 0.5 - fmod(r_refdef.view.height * 0.5 + (DotProduct(org, up) - DotProduct(middle, up)) / DotProduct(up, up) + 0.5, 1.0);
333 VectorMAMAM(1, org, dx, left, dy, up, org);
339 VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
340 VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
344 // bullet marks on walls
345 // ignores viewer entirely
346 VectorCopy(mleft, left);
349 case SPR_VP_PARALLEL_ORIENTED:
350 // I have no idea what people would use this for...
351 // oriented relative to view space
352 // FIXME: test this and make sure it mimicks software
353 left[0] = mleft[0] * r_refdef.view.forward[0] + mleft[1] * r_refdef.view.left[0] + mleft[2] * r_refdef.view.up[0];
354 left[1] = mleft[0] * r_refdef.view.forward[1] + mleft[1] * r_refdef.view.left[1] + mleft[2] * r_refdef.view.up[1];
355 left[2] = mleft[0] * r_refdef.view.forward[2] + mleft[1] * r_refdef.view.left[2] + mleft[2] * r_refdef.view.up[2];
356 up[0] = mup[0] * r_refdef.view.forward[0] + mup[1] * r_refdef.view.left[0] + mup[2] * r_refdef.view.up[0];
357 up[1] = mup[0] * r_refdef.view.forward[1] + mup[1] * r_refdef.view.left[1] + mup[2] * r_refdef.view.up[1];
358 up[2] = mup[0] * r_refdef.view.forward[2] + mup[1] * r_refdef.view.left[2] + mup[2] * r_refdef.view.up[2];
361 // Overhead games sprites, have some special hacks to look good
362 VectorScale(r_refdef.view.left, ent->scale * r_overheadsprites_scalex.value, left);
363 VectorScale(r_refdef.view.up, ent->scale * r_overheadsprites_scaley.value, up);
364 VectorSubtract(org, r_refdef.view.origin, middle);
365 VectorNormalize(middle);
367 dir_angle = r_overheadsprites_perspective.value * (1 - fabs(DotProduct(middle, r_refdef.view.forward)));
368 up[2] = up[2] + dir_angle;
370 VectorScale(up, ent->scale * r_overheadsprites_scaley.value, up);
371 // offset (move nearer to player, yz is camera plane)
372 org[0] = org[0] - middle[0]*r_overheadsprites_pushback.value;
373 org[1] = org[1] - middle[1]*r_overheadsprites_pushback.value;
374 org[2] = org[2] - middle[2]*r_overheadsprites_pushback.value;
375 // little perspective effect
376 up[2] = up[2] + dir_angle * 0.3;
377 // a bit of counter-camera rotation
378 up[0] = up[0] + r_refdef.view.forward[0] * 0.07;
379 up[1] = up[1] + r_refdef.view.forward[1] * 0.07;
380 up[2] = up[2] + r_refdef.view.forward[2] * 0.07;
384 // LordHavoc: interpolated sprite rendering
385 for (i = 0;i < MAX_FRAMEBLENDS;i++)
387 if (ent->frameblend[i].lerp >= 0.01f)
389 mspriteframe_t *frame;
391 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);
392 frame = model->sprite.sprdata_frames + ent->frameblend[i].subframe;
393 texture = R_GetCurrentTexture(model->data_textures + ent->frameblend[i].subframe);
395 // lit sprite by lightgrid if it is not fullbright, lit only ambient
396 if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
397 VectorAdd(ent->modellight_ambient, ent->modellight_diffuse, rsurface.modellight_ambient); // sprites dont use lightdirection
399 // SPR_LABEL should not use depth test AT ALL
400 if(model->sprite.sprnum_type == SPR_LABEL || model->sprite.sprnum_type == SPR_LABEL_SCALE)
401 if(texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE)
402 texture->currentmaterialflags = (texture->currentmaterialflags & ~MATERIALFLAG_SHORTDEPTHRANGE) | MATERIALFLAG_NODEPTHTEST;
406 // FIXME:: save vectors/origin and re-rotate? necessary if the hotspot can change per frame
407 R_RotateSprite(frame, org, left, up, edge, dir_angle);
411 R_CalcSprite_Vertex3f(vertex3f, org, left, up, frame->left, frame->right, frame->down, frame->up);
413 R_DrawCustomSurface_Texture(texture, &identitymatrix, texture->currentmaterialflags, 0, 4, 0, 2, false, false);
417 rsurface.entity = NULL;
420 void R_Model_Sprite_Draw(entity_render_t *ent)
423 if (ent->frameblend[0].subframe < 0)
426 Matrix4x4_OriginFromMatrix(&ent->matrix, org);
427 R_MeshQueue_AddTransparent(ent->flags & RENDER_NODEPTHTEST ? r_refdef.view.origin : org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);