7b5b8e55a604237aebe7ba5bc8700afd2578e30e
[divverent/darkplaces.git] / r_sprites.c
1
2 #include "quakedef.h"
3
4 static int R_SpriteSetup (const entity_render_t *ent, int type, float org[3], float left[3], float up[3])
5 {
6         float matrix1[3][3], matrix2[3][3], matrix3[3][3];
7
8         VectorCopy(ent->origin, org);
9         switch(type)
10         {
11         case SPR_VP_PARALLEL_UPRIGHT:
12                 // flames and such
13                 // vertical beam sprite, faces view plane
14                 VectorNegate(vpn, matrix3[0]);
15                 matrix3[0][2] = 0;
16                 VectorNormalizeFast(matrix3[0]);
17                 matrix3[1][0] = matrix3[0][1];
18                 matrix3[1][1] = -matrix3[0][0];
19                 matrix3[1][2] = 0;
20                 matrix3[2][0] = 0;
21                 matrix3[2][1] = 0;
22                 matrix3[2][2] = 1;
23                 break;
24         case SPR_FACING_UPRIGHT:
25                 // flames and such
26                 // vertical beam sprite, faces viewer's origin (not the view plane)
27                 VectorSubtract(ent->origin, r_origin, matrix3[0]);
28                 matrix3[0][2] = 0;
29                 VectorNormalizeFast(matrix3[0]);
30                 matrix3[1][0] = matrix3[0][1];
31                 matrix3[1][1] = -matrix3[0][0];
32                 matrix3[1][2] = 0;
33                 matrix3[2][0] = 0;
34                 matrix3[2][1] = 0;
35                 matrix3[2][2] = 1;
36                 break;
37         default:
38                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", type);
39                 // fall through to normal sprite
40         case SPR_VP_PARALLEL:
41                 // normal sprite
42                 // faces view plane
43                 VectorCopy(vpn, matrix3[0]);
44                 VectorNegate(vright, matrix3[1]);
45                 VectorCopy(vup, matrix3[2]);
46                 break;
47         case SPR_ORIENTED:
48                 // bullet marks on walls
49                 // ignores viewer entirely
50                 AngleVectorsFLU (ent->angles, matrix3[0], matrix3[1], matrix3[2]);
51                 // nudge it toward the view, so it will be infront of the wall
52                 VectorSubtract(org, vpn, org);
53                 break;
54         case SPR_VP_PARALLEL_ORIENTED:
55                 // I have no idea what people would use this for
56                 // oriented relative to view space
57                 // FIXME: test this and make sure it mimicks software
58                 AngleVectorsFLU (ent->angles, matrix1[0], matrix1[1], matrix1[2]);
59                 VectorCopy(vpn, matrix2[0]);
60                 VectorNegate(vright, matrix2[1]);
61                 VectorCopy(vup, matrix2[2]);
62                 R_ConcatRotations (matrix1[0], matrix2[0], matrix3[0]);
63                 break;
64         }
65
66         if (ent->scale != 1)
67         {
68                 VectorScale(matrix3[1], ent->scale, left);
69                 VectorScale(matrix3[2], ent->scale, up);
70         }
71         else
72         {
73                 VectorCopy(matrix3[1], left);
74                 VectorCopy(matrix3[2], up);
75         }
76         return false;
77 }
78
79 static void R_DrawSpriteImage (int additive, mspriteframe_t *frame, rtexture_t *texture, vec3_t origin, vec3_t up, vec3_t left, float red, float green, float blue, float alpha)
80 {
81         // FIXME: negate left and right in loader
82         R_DrawSprite(GL_SRC_ALPHA, additive ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, texture, false, origin, left, up, frame->left, frame->right, frame->down, frame->up, red, green, blue, alpha);
83 }
84
85 void R_DrawSpriteModelCallback(const void *calldata1, int calldata2)
86 {
87         const entity_render_t *ent = calldata1;
88         int i;
89         vec3_t left, up, org, color;
90         mspriteframe_t *frame;
91         vec3_t diff;
92         float fog, ifog;
93
94         if (R_SpriteSetup(ent, ent->model->sprnum_type, org, left, up))
95                 return;
96
97         R_Mesh_Matrix(&r_identitymatrix);
98
99         if ((ent->model->flags & EF_FULLBRIGHT) || (ent->effects & EF_FULLBRIGHT))
100                 color[0] = color[1] = color[2] = 1;
101         else
102                 R_CompleteLightPoint(color, ent->origin, true, NULL);
103
104         if (fogenabled)
105         {
106                 VectorSubtract(ent->origin, r_origin, diff);
107                 fog = exp(fogdensity/DotProduct(diff,diff));
108                 if (fog > 1)
109                         fog = 1;
110         }
111         else
112                 fog = 0;
113         ifog = 1 - fog;
114
115         if (r_lerpsprites.integer)
116         {
117                 // LordHavoc: interpolated sprite rendering
118                 for (i = 0;i < 4;i++)
119                 {
120                         if (ent->frameblend[i].lerp >= 0.01f)
121                         {
122                                 frame = ent->model->sprdata_frames + ent->frameblend[i].frame;
123                                 R_DrawSpriteImage((ent->effects & EF_ADDITIVE) || (ent->model->flags & EF_ADDITIVE), frame, frame->texture, org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, ent->alpha * ent->frameblend[i].lerp);
124                                 if (fog * ent->frameblend[i].lerp >= 0.01f)
125                                         R_DrawSpriteImage(true, frame, frame->fogtexture, org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * ent->alpha * ent->frameblend[i].lerp);
126                         }
127                 }
128         }
129         else
130         {
131                 // LordHavoc: no interpolation
132                 frame = NULL;
133                 for (i = 0;i < 4 && ent->frameblend[i].lerp;i++)
134                         frame = ent->model->sprdata_frames + ent->frameblend[i].frame;
135
136                 R_DrawSpriteImage((ent->effects & EF_ADDITIVE) || (ent->model->flags & EF_ADDITIVE), frame, frame->texture, org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, ent->alpha);
137                 if (fog * ent->frameblend[i].lerp >= 0.01f)
138                         R_DrawSpriteImage(true, frame, frame->fogtexture, org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * ent->alpha);
139         }
140 }
141
142 void R_Model_Sprite_Draw(entity_render_t *ent)
143 {
144         if (ent->frameblend[0].frame < 0)
145                 return;
146
147         c_sprites++;
148
149         R_MeshQueue_AddTransparent(ent->origin, R_DrawSpriteModelCallback, ent, 0);
150 }
151