new sprite types SPR_LABEL and SPR_LABEL_SCALE
[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
8 void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
9 {
10         int i;
11         model_t *model = ent->model;
12         vec3_t left, up, org, mforward, mleft, mup, middle;
13         float scale, dx, dy, hud_vs_screen;
14
15         // nudge it toward the view to make sure it isn't in a wall
16         Matrix4x4_ToVectors(&ent->matrix, mforward, mleft, mup, org);
17         VectorSubtract(org, r_view.forward, org);
18         switch(model->sprite.sprnum_type)
19         {
20         case SPR_VP_PARALLEL_UPRIGHT:
21                 // flames and such
22                 // vertical beam sprite, faces view plane
23                 scale = ent->scale / sqrt(r_view.forward[0]*r_view.forward[0]+r_view.forward[1]*r_view.forward[1]);
24                 left[0] = -r_view.forward[1] * scale;
25                 left[1] = r_view.forward[0] * scale;
26                 left[2] = 0;
27                 up[0] = 0;
28                 up[1] = 0;
29                 up[2] = ent->scale;
30                 break;
31         case SPR_FACING_UPRIGHT:
32                 // flames and such
33                 // vertical beam sprite, faces viewer's origin (not the view plane)
34                 scale = ent->scale / sqrt((org[0] - r_view.origin[0])*(org[0] - r_view.origin[0])+(org[1] - r_view.origin[1])*(org[1] - r_view.origin[1]));
35                 left[0] = (org[1] - r_view.origin[1]) * scale;
36                 left[1] = -(org[0] - r_view.origin[0]) * scale;
37                 left[2] = 0;
38                 up[0] = 0;
39                 up[1] = 0;
40                 up[2] = ent->scale;
41                 break;
42         default:
43                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", model->sprite.sprnum_type);
44                 // fall through to normal sprite
45         case SPR_VP_PARALLEL:
46                 // normal sprite
47                 // faces view plane
48                 VectorScale(r_view.left, ent->scale, left);
49                 VectorScale(r_view.up, ent->scale, up);
50                 break;
51         case SPR_LABEL_SCALE:
52                 // normal sprite
53                 // faces view plane
54                 // fixed HUD pixel size specified in sprite
55                 // honors scale
56                 // honors a global label scaling cvar
57
58                 scale = 2 * ent->scale * (DotProduct(r_view.forward, org) - DotProduct(r_view.forward, r_view.origin)) * r_labelsprites_scale.value;
59                 VectorScale(r_view.left, scale * r_view.frustum_x / vid_conwidth.integer, left); // 1px
60                 VectorScale(r_view.up, scale * r_view.frustum_y / vid_conheight.integer, up); // 1px
61                 break;
62         case SPR_LABEL:
63                 // normal sprite
64                 // faces view plane
65                 // fixed pixel size specified in sprite
66                 // tries to get the right size in HUD units, if possible
67                 // ignores scale
68                 // honors a global label scaling cvar before the rounding
69                 // 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.
70
71                 scale = 2 * (DotProduct(r_view.forward, org) - DotProduct(r_view.forward, r_view.origin));
72
73                 if(r_labelsprites_roundtopixels.integer)
74                 {
75                         hud_vs_screen = max(
76                                 vid_conwidth.integer / (float) r_view.width,
77                                 vid_conheight.integer / (float) r_view.height
78                         ) / max(0.125, r_labelsprites_scale.value);
79
80                         // snap to "good sizes"
81                         // 1     for (0.6, 1.41]
82                         // 2     for (1.8, 3.33]
83                         if(hud_vs_screen <= 0.6)
84                                 hud_vs_screen = 0; // don't, use real HUD pixels
85                         else if(hud_vs_screen <= 1.41)
86                                 hud_vs_screen = 1;
87                         else if(hud_vs_screen <= 3.33)
88                                 hud_vs_screen = 2;
89                         else
90                                 hud_vs_screen = 0; // don't, use real HUD pixels
91
92                         if(hud_vs_screen)
93                         {
94                                 // use screen pixels
95                                 VectorScale(r_view.left, scale * r_view.frustum_x / (r_view.width * hud_vs_screen), left); // 1px
96                                 VectorScale(r_view.up, scale * r_view.frustum_y / (r_view.height * hud_vs_screen), up); // 1px
97                         }
98                         else
99                         {
100                                 // use HUD pixels
101                                 VectorScale(r_view.left, scale * r_view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
102                                 VectorScale(r_view.up, scale * r_view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
103                         }
104
105                         if(hud_vs_screen == 1)
106                         {
107                                 VectorMA(r_view.origin, scale, r_view.forward, middle); // center of screen in distance scale
108                                 dx = 0.5 - fmod(r_view.width * 0.5 + (DotProduct(org, left) - DotProduct(middle, left)) / DotProduct(left, left) + 0.5, 1.0);
109                                 dy = 0.5 - fmod(r_view.height * 0.5 + (DotProduct(org, up) - DotProduct(middle, up)) / DotProduct(up, up) + 0.5, 1.0);
110                                 VectorMAMAM(1, org, dx, left, dy, up, org);
111                         }
112                 }
113                 else
114                 {
115                         // use HUD pixels
116                         VectorScale(r_view.left, scale * r_view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
117                         VectorScale(r_view.up, scale * r_view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
118                 }
119                 break;
120         case SPR_ORIENTED:
121                 // bullet marks on walls
122                 // ignores viewer entirely
123                 VectorCopy(mleft, left);
124                 VectorCopy(mup, up);
125                 break;
126         case SPR_VP_PARALLEL_ORIENTED:
127                 // I have no idea what people would use this for...
128                 // oriented relative to view space
129                 // FIXME: test this and make sure it mimicks software
130                 left[0] = mleft[0] * r_view.forward[0] + mleft[1] * r_view.left[0] + mleft[2] * r_view.up[0];
131                 left[1] = mleft[0] * r_view.forward[1] + mleft[1] * r_view.left[1] + mleft[2] * r_view.up[1];
132                 left[2] = mleft[0] * r_view.forward[2] + mleft[1] * r_view.left[2] + mleft[2] * r_view.up[2];
133                 up[0] = mup[0] * r_view.forward[0] + mup[1] * r_view.left[0] + mup[2] * r_view.up[0];
134                 up[1] = mup[0] * r_view.forward[1] + mup[1] * r_view.left[1] + mup[2] * r_view.up[1];
135                 up[2] = mup[0] * r_view.forward[2] + mup[1] * r_view.left[2] + mup[2] * r_view.up[2];
136                 break;
137         }
138
139         R_Mesh_Matrix(&identitymatrix);
140
141         // LordHavoc: interpolated sprite rendering
142         for (i = 0;i < 4;i++)
143         {
144                 if (ent->frameblend[i].lerp >= 0.01f)
145                 {
146                         mspriteframe_t *frame = model->sprite.sprdata_frames + ent->frameblend[i].frame;
147                         texture_t *texture = model->data_textures + ent->frameblend[i].frame;
148                         R_UpdateTextureInfo(ent, texture);
149                         // FIXME: negate left and right in loader
150                         R_DrawSprite(texture->currentlayers[0].blendfunc1, texture->currentlayers[0].blendfunc2, texture->basetexture, texture->currentskinframe->fog, (texture->currentmaterialflags & MATERIALFLAG_NODEPTHTEST), (texture->currentmaterialflags & MATERIALFLAG_SHORTDEPTHRANGE), 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);
151                 }
152         }
153 }
154
155 void R_Model_Sprite_Draw(entity_render_t *ent)
156 {
157         vec3_t org;
158         if (ent->frameblend[0].frame < 0)
159                 return;
160
161         Matrix4x4_OriginFromMatrix(&ent->matrix, org);
162         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_view.origin : org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);
163 }
164