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