made sprite interpolation optional with a #define (enabled currently, looks too bad...
[divverent/darkplaces.git] / r_sprites.c
1 #include "quakedef.h"
2
3 // LordHavoc: disabled lerping
4 #define LERPSPRITES
5
6 #ifdef LERPSPRITES
7 void R_ClipSpriteImage (vec3_t origin, vec3_t right, vec3_t up)
8 {
9         int i;
10         mspriteframe_t *frame;
11         vec3_t points[4];
12         float fleft, fright, fdown, fup;
13         frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[0].frame;
14         fleft  = frame->left;
15         fdown  = frame->down;
16         fright = frame->right;
17         fup    = frame->up;
18         for (i = 1;i < 4 && currentrenderentity->frameblend[i].lerp;i++)
19         {
20                 frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[i].frame;
21                 fleft  = min(fleft , frame->left );
22                 fdown  = min(fdown , frame->down );
23                 fright = max(fright, frame->right);
24                 fup    = max(fup   , frame->up   );
25         }
26         points[0][0] = origin[0] + fdown * up[0] + fleft  * right[0];points[0][1] = origin[1] + fdown * up[1] + fleft  * right[1];points[0][2] = origin[2] + fdown * up[2] + fleft  * right[2];
27         points[1][0] = origin[0] + fup   * up[0] + fleft  * right[0];points[1][1] = origin[1] + fup   * up[1] + fleft  * right[1];points[1][2] = origin[2] + fup   * up[2] + fleft  * right[2];
28         points[2][0] = origin[0] + fup   * up[0] + fright * right[0];points[2][1] = origin[1] + fup   * up[1] + fright * right[1];points[2][2] = origin[2] + fup   * up[2] + fright * right[2];
29         points[3][0] = origin[0] + fdown * up[0] + fright * right[0];points[3][1] = origin[1] + fdown * up[1] + fright * right[1];points[3][2] = origin[2] + fdown * up[2] + fright * right[2];
30         R_Clip_AddPolygon(&points[0][0], 4, sizeof(float[3]), false, R_Entity_Callback, currentrenderentity, NULL, NULL);
31 }
32 #else
33 void R_ClipSpriteImage (vec3_t origin, vec3_t right, vec3_t up)
34 {
35         int i;
36         mspriteframe_t *frame;
37         vec3_t points[4];
38         float fleft, fright, fdown, fup;
39         frame = NULL;
40         for (i = 0;i < 4 && currentrenderentity->frameblend[i].lerp;i++)
41                 frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[i].frame;
42         fleft  = frame->left;
43         fdown  = frame->down;
44         fright = frame->right;
45         fup    = frame->up;
46         points[0][0] = origin[0] + fdown * up[0] + fleft  * right[0];points[0][1] = origin[1] + fdown * up[1] + fleft  * right[1];points[0][2] = origin[2] + fdown * up[2] + fleft  * right[2];
47         points[1][0] = origin[0] + fup   * up[0] + fleft  * right[0];points[1][1] = origin[1] + fup   * up[1] + fleft  * right[1];points[1][2] = origin[2] + fup   * up[2] + fleft  * right[2];
48         points[2][0] = origin[0] + fup   * up[0] + fright * right[0];points[2][1] = origin[1] + fup   * up[1] + fright * right[1];points[2][2] = origin[2] + fup   * up[2] + fright * right[2];
49         points[3][0] = origin[0] + fdown * up[0] + fright * right[0];points[3][1] = origin[1] + fdown * up[1] + fright * right[1];points[3][2] = origin[2] + fdown * up[2] + fright * right[2];
50         R_Clip_AddPolygon(&points[0][0], 4, sizeof(float[3]), false, R_Entity_Callback, currentrenderentity, NULL, NULL);
51 }
52 #endif
53
54 int R_SpriteSetup (int type, float org[3], float right[3], float up[3])
55 {
56         float matrix1[3][3], matrix2[3][3], matrix3[3][3];
57
58         VectorCopy(currentrenderentity->origin, org);
59         switch(type)
60         {
61         case SPR_VP_PARALLEL_UPRIGHT:
62                 // flames and such
63                 // vertical beam sprite, faces view plane
64                 VectorNegate(vpn, matrix3[0]);
65                 matrix3[0][2] = 0;
66                 VectorNormalizeFast(matrix3[0]);
67                 VectorVectors(matrix3[0], matrix3[1], matrix3[2]);
68                 break;
69         case SPR_FACING_UPRIGHT:
70                 // flames and such
71                 // vertical beam sprite, faces viewer's origin (not the view plane)
72                 VectorSubtract(r_origin, currentrenderentity->origin, matrix3[0]);
73                 matrix3[0][2] = 0;
74                 VectorNormalizeFast(matrix3[0]);
75                 VectorVectors(matrix3[0], matrix3[1], matrix3[2]);
76                 break;
77         default:
78                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", type);
79                 // fall through to normal sprite
80         case SPR_VP_PARALLEL:
81                 // normal sprite
82                 // faces view plane
83                 VectorCopy(vpn, matrix3[0]);
84                 VectorCopy(vright, matrix3[1]);
85                 VectorCopy(vup, matrix3[2]);
86                 break;
87         case SPR_ORIENTED:
88                 // bullet marks on walls
89                 // ignores viewer entirely
90                 AngleVectors (currentrenderentity->angles, matrix3[0], matrix3[1], matrix3[2]);
91                 // nudge it toward the view, so it will be infront of the wall
92                 VectorSubtract(org, vpn, org);
93                 break;
94         case SPR_VP_PARALLEL_ORIENTED:
95                 // I have no idea what people would use this for
96                 // oriented relative to view space
97                 // FIXME: test this and make sure it mimicks software
98                 AngleVectors (currentrenderentity->angles, matrix1[0], matrix1[1], matrix1[2]);
99                 VectorCopy(vpn, matrix2[0]);
100                 VectorCopy(vright, matrix2[1]);
101                 VectorCopy(vup, matrix2[2]);
102                 R_ConcatRotations (matrix1, matrix2, matrix3);
103                 break;
104         }
105
106         // don't draw if view origin is behind it
107         if (DotProduct(org, matrix3[0]) < (DotProduct(r_origin, matrix3[0]) - 1.0f))
108                 return true;
109
110         if (currentrenderentity->scale != 1)
111         {
112                 VectorScale(matrix3[1], currentrenderentity->scale, matrix3[1]);
113                 VectorScale(matrix3[2], currentrenderentity->scale, matrix3[2]);
114         }
115
116         VectorCopy(matrix3[1], right);
117         VectorCopy(matrix3[2], up);
118         return false;
119 }
120
121 void R_ClipSprite (void)
122 {
123         vec3_t org, right, up;
124
125         if (currentrenderentity->frameblend[0].frame < 0)
126                 return;
127
128         if (R_SpriteSetup(currentrenderentity->model->sprnum_type, org, right, up))
129                 return;
130
131         // LordHavoc: interpolated sprite rendering
132         R_ClipSpriteImage(org, right, up);
133 }
134
135 int spritepolyindex[6] = {0, 1, 2, 0, 2, 3};
136
137 void GL_DrawSpriteImage (int fog, mspriteframe_t *frame, int texture, vec3_t origin, vec3_t up, vec3_t right, float red, float green, float blue, float alpha)
138 {
139         rmeshinfo_t m;
140         float v[4][4], st[4][2];
141         // LordHavoc: this meshinfo must match up with R_Mesh_DrawDecal
142         // LordHavoc: the commented out lines are hardwired behavior in R_Mesh_DrawDecal
143         memset(&m, 0, sizeof(m));
144         m.transparent = true;
145         m.blendfunc1 = GL_SRC_ALPHA;
146         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
147         if ((currentrenderentity->effects & EF_ADDITIVE)
148          || (currentrenderentity->model->flags & EF_ADDITIVE)
149          || fog)
150                 m.blendfunc2 = GL_ONE;
151         m.numtriangles = 2;
152         m.index = spritepolyindex;
153         m.numverts = 4;
154         m.vertex = &v[0][0];
155         m.vertexstep = sizeof(float[4]);
156         m.cr = red;
157         m.cg = green;
158         m.cb = blue;
159         m.ca = alpha;
160         m.tex[0] = texture;
161         m.texcoords[0] = &st[0][0];
162         m.texcoordstep[0] = sizeof(float[2]);
163
164         v[0][0] = origin[0] + frame->down * up[0] + frame->left  * right[0];
165         v[0][1] = origin[1] + frame->down * up[1] + frame->left  * right[1];
166         v[0][2] = origin[2] + frame->down * up[2] + frame->left  * right[2];
167         v[1][0] = origin[0] + frame->up   * up[0] + frame->left  * right[0];
168         v[1][1] = origin[1] + frame->up   * up[1] + frame->left  * right[1];
169         v[1][2] = origin[2] + frame->up   * up[2] + frame->left  * right[2];
170         v[2][0] = origin[0] + frame->up   * up[0] + frame->right * right[0];
171         v[2][1] = origin[1] + frame->up   * up[1] + frame->right * right[1];
172         v[2][2] = origin[2] + frame->up   * up[2] + frame->right * right[2];
173         v[3][0] = origin[0] + frame->down * up[0] + frame->right * right[0];
174         v[3][1] = origin[1] + frame->down * up[1] + frame->right * right[1];
175         v[3][2] = origin[2] + frame->down * up[2] + frame->right * right[2];
176         st[0][0] = 0;
177         st[0][1] = 1;
178         st[1][0] = 0;
179         st[1][1] = 0;
180         st[2][0] = 1;
181         st[2][1] = 0;
182         st[3][0] = 1;
183         st[3][1] = 1;
184
185         R_Mesh_Draw(&m);
186 }
187
188 /*
189 =================
190 R_DrawSpriteModel
191 =================
192 */
193 void R_DrawSpriteModel ()
194 {
195         int                     i;
196         vec3_t          right, up, org, color;
197         mspriteframe_t *frame;
198         vec3_t diff;
199         float           fog, ifog;
200
201         if (currentrenderentity->frameblend[0].frame < 0)
202                 return;
203
204         if (R_SpriteSetup(currentrenderentity->model->sprnum_type, org, right, up))
205                 return;
206
207         c_sprites++;
208
209         if ((currentrenderentity->model->flags & EF_FULLBRIGHT) || (currentrenderentity->effects & EF_FULLBRIGHT))
210                 color[0] = color[1] = color[2] = 1;
211         else
212                 R_CompleteLightPoint(color, currentrenderentity->origin, true, NULL);
213
214         if (fogenabled)
215         {
216                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
217                 fog = exp(fogdensity/DotProduct(diff,diff));
218                 if (fog > 1)
219                         fog = 1;
220         }
221         else
222                 fog = 0;
223         ifog = 1 - fog;
224
225 #ifdef LERPSPRITES
226         // LordHavoc: interpolated sprite rendering
227         for (i = 0;i < 4;i++)
228         {
229                 if (currentrenderentity->frameblend[i].lerp >= 0.01f)
230                 {
231                         frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[i].frame;
232                         GL_DrawSpriteImage(false, frame, R_GetTexture(frame->texture), org, up, right, color[0] * ifog, color[1] * ifog, color[2] * ifog, currentrenderentity->alpha * currentrenderentity->frameblend[i].lerp);
233                         if (fog * currentrenderentity->frameblend[i].lerp >= 0.01f)
234                                 GL_DrawSpriteImage(true, frame, R_GetTexture(frame->fogtexture), org, up, right, fogcolor[0],fogcolor[1],fogcolor[2], fog * currentrenderentity->alpha * currentrenderentity->frameblend[i].lerp);
235                 }
236         }
237 #else
238         // LordHavoc: no interpolation
239         frame = NULL;
240         for (i = 0;i < 4 && currentrenderentity->frameblend[i].lerp;i++)
241                 frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[i].frame;
242
243         GL_DrawSpriteImage(false, frame, R_GetTexture(frame->texture), org, up, right, color[0] * ifog, color[1] * ifog, color[2] * ifog, currentrenderentity->alpha);
244         if (fog * currentrenderentity->frameblend[i].lerp >= 0.01f)
245                 GL_DrawSpriteImage(true, frame, R_GetTexture(frame->fogtexture), org, up, right, fogcolor[0],fogcolor[1],fogcolor[2], fog * currentrenderentity->alpha);
246 #endif
247 }