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 extern cvar_t r_overheadsprites_scalex;
14 extern cvar_t r_overheadsprites_scaley;
15 
16 #define TSF_ROTATE 1
17 #define TSF_ROTATE_CONTINOUSLY 2
18 
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 :)
21 #ifndef EPSILON
22 # define EPSILON (1.0f / 32.0f)
23 # define MIN_EPSILON 0.0001f
24 #endif
25 
26 /* R_Track_Sprite
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
29    the hotspot.
30 
31    --blub
32  */
33 #define SIDE_TOP 1
34 #define SIDE_LEFT 2
35 #define SIDE_BOTTOM 3
36 #define SIDE_RIGHT 4
37 
R_TrackSprite(const entity_render_t * ent,vec3_t origin,vec3_t left,vec3_t up,int * edge,float * dir_angle)38 static void R_TrackSprite(const entity_render_t *ent, vec3_t origin, vec3_t left, vec3_t up, int *edge, float *dir_angle)
39 {
40 	float distance;
41 	vec3_t bCoord; // body coordinates of object
42 	unsigned int i;
43 
44 	// temporarily abuse bCoord as the vector player->sprite-origin
45 	VectorSubtract(origin, r_refdef.view.origin, bCoord);
46 	distance = VectorLength(bCoord);
47 
48 	// Now get the bCoords :)
49 	Matrix4x4_Transform(&r_refdef.view.inverse_matrix, origin, bCoord);
50 
51 	*edge = 0; // FIXME::should assume edge == 0, which is correct currently
52 	for(i = 0; i < 4; ++i)
53 	{
54 		if(PlaneDiff(origin, &r_refdef.view.frustum[i]) < -EPSILON)
55 			break;
56 	}
57 
58 	// If it wasn't outside a plane, no tracking needed
59 	if(i < 4)
60 	{
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
64 
65 		bCoord[2] *= r_refdef.view.frustum_x;
66 		bCoord[1] *= r_refdef.view.frustum_y;
67 
68 		//Con_Printf("%f %f %f\n", bCoord[0], bCoord[1], bCoord[2]);
69 
70 		ax = fabs(bCoord[1]);
71 		ay = fabs(bCoord[2]);
72 		// get the greater value and determine the screen edge it's on
73 		if(ax < ay)
74 		{
75 			ax = ay;
76 			// 180 or 0 degrees
77 			if(bCoord[2] < 0.0f)
78 				*edge = SIDE_BOTTOM;
79 			else
80 				*edge = SIDE_TOP;
81 		} else {
82 			if(bCoord[1] < 0.0f)
83 				*edge = SIDE_RIGHT;
84 			else
85 				*edge = SIDE_LEFT;
86 		}
87 
88 		// umm...
89 		if(ax < MIN_EPSILON) // this was == 0.0f before --blub
90 			ax = MIN_EPSILON;
91 		// get the -1 to +1 range
92 		x = bCoord[1] / ax;
93 		y = bCoord[2] / ax;
94 
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
100 		// f is frustum X
101 		// x is x
102 		// a is ax
103 		// g is frustum Y
104 		// y is y
105 		// b is ay
106 
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))
112 		// thus:
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
117 
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);
124 
125 		if(r_track_sprites_flags.integer & TSF_ROTATE_CONTINOUSLY)
126 		{
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
130 			if(x < 0.0f)
131 				*dir_angle += 180.0f;
132 		}
133 
134 		left[0] *= r_track_sprites_scalew.value;
135 		left[1] *= r_track_sprites_scalew.value;
136 		left[2] *= r_track_sprites_scalew.value;
137 
138 		up[0] *= r_track_sprites_scaleh.value;
139 		up[1] *= r_track_sprites_scaleh.value;
140 		up[2] *= r_track_sprites_scaleh.value;
141 	}
142 }
143 
R_RotateSprite(const mspriteframe_t * frame,vec3_t origin,vec3_t left,vec3_t up,int edge,float dir_angle)144 static void R_RotateSprite(const mspriteframe_t *frame, vec3_t origin, vec3_t left, vec3_t up, int edge, float dir_angle)
145 {
146 	if(!(r_track_sprites_flags.integer & TSF_ROTATE))
147 	{
148 		// move down by its size if on top, otherwise it's invisible
149 		if(edge == SIDE_TOP)
150 			VectorMA(origin, -(fabs(frame->up)+fabs(frame->down)), up, origin);
151 	} else {
152 		static float rotation_angles[5] =
153 		{
154 			0, // no edge
155 			-90.0f,	//top
156 			0.0f,	// left
157 			90.0f,	// bottom
158 			180.0f,	// right
159 		};
160 
161 		// rotate around the hotspot according to which edge it's on
162 		// since the hotspot == the origin, only rotate the vectors
163 		matrix4x4_t rotm;
164 		vec3_t axis;
165 		vec3_t temp;
166 		vec2_t dir;
167 		float angle;
168 
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
171 
172 		dir[0] = frame->right + frame->left;
173 		dir[1] = frame->down + frame->up;
174 
175 		// only rotate when the hotspot isn't the center though.
176 		if(dir[0] < MIN_EPSILON && dir[1] < MIN_EPSILON)
177 		{
178 			return;
179 		}
180 
181 		// Now that we've kicked center-hotspotted sprites, rotate using the appropriate matrix :)
182 
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 :)
185 
186 		angle = atan(dir[1] / dir[0]) * 180.0f/M_PI;
187 
188 		// we need the real, full angle
189 		if(dir[0] < 0.0f)
190 			angle += 180.0f;
191 
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]);
197 		else
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);
203 	}
204 }
205 
206 static float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
207 
R_Model_Sprite_Draw_TransparentCallback(const entity_render_t * ent,const rtlight_t * rtlight,int numsurfaces,int * surfacelist)208 static void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
209 {
210 	int i;
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;
214 	int edge = 0;
215 	float dir_angle = 0.0f;
216 	float vertex3f[12];
217 
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)
222 	{
223 	case SPR_VP_PARALLEL_UPRIGHT:
224 		// flames and such
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;
229 		left[2] = 0;
230 		up[0] = 0;
231 		up[1] = 0;
232 		up[2] = ent->scale;
233 		break;
234 	case SPR_FACING_UPRIGHT:
235 		// flames and such
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;
240 		left[2] = 0;
241 		up[0] = 0;
242 		up[1] = 0;
243 		up[2] = ent->scale;
244 		break;
245 	default:
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:
249 		// normal sprite
250 		// faces view plane
251 		VectorScale(r_refdef.view.left, ent->scale, left);
252 		VectorScale(r_refdef.view.up, ent->scale, up);
253 		break;
254 	case SPR_LABEL_SCALE:
255 		// normal sprite
256 		// faces view plane
257 		// fixed HUD pixel size specified in sprite
258 		// honors scale
259 		// honors a global label scaling cvar
260 
261 		if(r_fb.water.renderingscene) // labels are considered HUD items, and don't appear in reflections
262 			return;
263 
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);
270 
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
274 		break;
275 	case SPR_LABEL:
276 		// normal sprite
277 		// faces view plane
278 		// fixed pixel size specified in sprite
279 		// tries to get the right size in HUD units, if possible
280 		// ignores scale
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.
283 
284 		if(r_fb.water.renderingscene) // labels are considered HUD items, and don't appear in reflections
285 			return;
286 
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);
293 
294 		scale = 2 * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin));
295 
296 		if(r_labelsprites_roundtopixels.integer)
297 		{
298 			hud_vs_screen = max(
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);
302 
303 			// snap to "good sizes"
304 			// 1     for (0.6, 1.41]
305 			// 2     for (1.8, 3.33]
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)
309 				hud_vs_screen = 1;
310 			else if(hud_vs_screen <= 3.33)
311 				hud_vs_screen = 2;
312 			else
313 				hud_vs_screen = 0; // don't, use real HUD pixels
314 
315 			if(hud_vs_screen)
316 			{
317 				// use screen 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
320 			}
321 			else
322 			{
323 				// use HUD pixels
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
326 			}
327 
328 			if(hud_vs_screen == 1)
329 			{
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);
334 			}
335 		}
336 		else
337 		{
338 			// use HUD pixels
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
341 		}
342 		break;
343 	case SPR_ORIENTED:
344 		// bullet marks on walls
345 		// ignores viewer entirely
346 		VectorCopy(mleft, left);
347 		VectorCopy(mup, up);
348 		break;
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];
359 		break;
360 	case SPR_OVERHEAD:
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);
366 		// offset and rotate
367 		dir_angle = r_overheadsprites_perspective.value * (1 - fabs(DotProduct(middle, r_refdef.view.forward)));
368 		up[2] = up[2] + dir_angle;
369 		VectorNormalize(up);
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;
381 		break;
382 	}
383 
384 	// LordHavoc: interpolated sprite rendering
385 	for (i = 0;i < MAX_FRAMEBLENDS;i++)
386 	{
387 		if (ent->frameblend[i].lerp >= 0.01f)
388 		{
389 			mspriteframe_t *frame;
390 			texture_t *texture;
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);
394 
395 			// sprites are fullbright by default, but if this one is not fullbright we
396 			// need to combine the lighting into ambient as sprite lighting is not
397 			// directional
398 			if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
399 			{
400 				VectorMAM(1.0f, texture->render_modellight_ambient, 0.25f, texture->render_modellight_diffuse, texture->render_modellight_ambient);
401 				VectorClear(texture->render_modellight_diffuse);
402 				VectorClear(texture->render_modellight_specular);
403 			}
404 
405 			// SPR_LABEL should not use depth test AT ALL
406 			if(model->sprite.sprnum_type == SPR_LABEL || model->sprite.sprnum_type == SPR_LABEL_SCALE)
407 				if(texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE)
408 					texture->currentmaterialflags = (texture->currentmaterialflags & ~MATERIALFLAG_SHORTDEPTHRANGE) | MATERIALFLAG_NODEPTHTEST;
409 
410 			if(edge)
411 			{
412 				// FIXME:: save vectors/origin and re-rotate? necessary if the hotspot can change per frame
413 				R_RotateSprite(frame, org, left, up, edge, dir_angle);
414 				edge = 0;
415 			}
416 
417 			R_CalcSprite_Vertex3f(vertex3f, org, left, up, frame->left, frame->right, frame->down, frame->up);
418 
419 			R_DrawCustomSurface_Texture(texture, &identitymatrix, texture->currentmaterialflags, 0, 4, 0, 2, false, false);
420 		}
421 	}
422 
423 	rsurface.entity = NULL;
424 }
425 
R_Model_Sprite_Draw(entity_render_t * ent)426 void R_Model_Sprite_Draw(entity_render_t *ent)
427 {
428 	vec3_t org;
429 	if (ent->frameblend[0].subframe < 0)
430 		return;
431 
432 	Matrix4x4_OriginFromMatrix(&ent->matrix, org);
433 	R_MeshQueue_AddTransparent((ent->flags & RENDER_WORLDOBJECT) ? TRANSPARENTSORT_SKY : (ent->flags & RENDER_NODEPTHTEST) ? TRANSPARENTSORT_HUD : TRANSPARENTSORT_DISTANCE, org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);
434 }
435 
436