]> git.xonotic.org Git - xonotic/darkplaces.git/blob - r_sprites.c
fix a bunch of pointer type warnings introduced by the change to const parameters...
[xonotic/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 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                 //VectorVectors(matrix3[0], matrix3[1], matrix3[2]);
74                 //VectorNegate(matrix3[1], matrix3[1]);
75                 matrix3[1][0] = matrix3[0][1];
76                 matrix3[1][1] = -matrix3[0][0];
77                 matrix3[1][2] = 0;
78                 matrix3[2][0] = 0;
79                 matrix3[2][1] = 0;
80                 matrix3[2][2] = 1;
81                 break;
82         case SPR_FACING_UPRIGHT:
83                 // flames and such
84                 // vertical beam sprite, faces viewer's origin (not the view plane)
85                 VectorSubtract(currentrenderentity->origin, r_origin, matrix3[0]);
86                 matrix3[0][2] = 0;
87                 VectorNormalizeFast(matrix3[0]);
88                 //VectorVectors(matrix3[0], matrix3[1], matrix3[2]);
89                 matrix3[1][0] = matrix3[0][1];
90                 matrix3[1][1] = -matrix3[0][0];
91                 matrix3[1][2] = 0;
92                 matrix3[2][0] = 0;
93                 matrix3[2][1] = 0;
94                 matrix3[2][2] = 1;
95                 break;
96         default:
97                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", type);
98                 // fall through to normal sprite
99         case SPR_VP_PARALLEL:
100                 // normal sprite
101                 // faces view plane
102                 VectorCopy(vpn, matrix3[0]);
103                 VectorNegate(vright, matrix3[1]);
104                 VectorCopy(vup, matrix3[2]);
105                 break;
106         case SPR_ORIENTED:
107                 // bullet marks on walls
108                 // ignores viewer entirely
109                 AngleVectorsFLU (currentrenderentity->angles, matrix3[0], matrix3[1], matrix3[2]);
110                 // nudge it toward the view, so it will be infront of the wall
111                 VectorSubtract(org, vpn, org);
112                 break;
113         case SPR_VP_PARALLEL_ORIENTED:
114                 // I have no idea what people would use this for
115                 // oriented relative to view space
116                 // FIXME: test this and make sure it mimicks software
117                 AngleVectorsFLU (currentrenderentity->angles, matrix1[0], matrix1[1], matrix1[2]);
118                 VectorCopy(vpn, matrix2[0]);
119                 VectorNegate(vright, matrix2[1]);
120                 VectorCopy(vup, matrix2[2]);
121                 R_ConcatRotations (matrix1[0], matrix2[0], matrix3[0]);
122                 break;
123         }
124
125         // don't draw if view origin is behind it
126         //if (DotProduct(org, matrix3[0]) < (DotProduct(r_origin, matrix3[0]) - 1.0f))
127         //      return true;
128
129         if (currentrenderentity->scale != 1)
130         {
131                 VectorScale(matrix3[1], currentrenderentity->scale, left);
132                 VectorScale(matrix3[2], currentrenderentity->scale, up);
133         }
134         else
135         {
136                 VectorCopy(matrix3[1], left);
137                 VectorCopy(matrix3[2], up);
138         }
139         return false;
140 }
141
142 void R_ClipSprite (void)
143 {
144         vec3_t org, left, up;
145
146         if (currentrenderentity->frameblend[0].frame < 0)
147                 return;
148
149         if (R_SpriteSetup(currentrenderentity->model->sprnum_type, org, left, up))
150                 return;
151
152         // LordHavoc: interpolated sprite rendering
153         R_ClipSpriteImage(org, left, up);
154 }
155
156 int spritepolyindex[6] = {0, 1, 2, 0, 2, 3};
157
158 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)
159 {
160         rmeshinfo_t m;
161         float v[4][4], st[4][2];
162         memset(&m, 0, sizeof(m));
163         m.transparent = true;
164         m.blendfunc1 = GL_SRC_ALPHA;
165         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
166         if ((currentrenderentity->effects & EF_ADDITIVE)
167          || (currentrenderentity->model->flags & EF_ADDITIVE)
168          || fog)
169                 m.blendfunc2 = GL_ONE;
170         m.numtriangles = 2;
171         m.index = spritepolyindex;
172         m.numverts = 4;
173         m.vertex = &v[0][0];
174         m.vertexstep = sizeof(float[4]);
175         m.cr = red;
176         m.cg = green;
177         m.cb = blue;
178         m.ca = alpha;
179         m.tex[0] = texture;
180         m.texcoords[0] = &st[0][0];
181         m.texcoordstep[0] = sizeof(float[2]);
182
183         // FIXME: negate left and right in loader
184         v[0][0] = origin[0] + frame->down * up[0] - frame->left  * left[0];
185         v[0][1] = origin[1] + frame->down * up[1] - frame->left  * left[1];
186         v[0][2] = origin[2] + frame->down * up[2] - frame->left  * left[2];
187         v[1][0] = origin[0] + frame->up   * up[0] - frame->left  * left[0];
188         v[1][1] = origin[1] + frame->up   * up[1] - frame->left  * left[1];
189         v[1][2] = origin[2] + frame->up   * up[2] - frame->left  * left[2];
190         v[2][0] = origin[0] + frame->up   * up[0] - frame->right * left[0];
191         v[2][1] = origin[1] + frame->up   * up[1] - frame->right * left[1];
192         v[2][2] = origin[2] + frame->up   * up[2] - frame->right * left[2];
193         v[3][0] = origin[0] + frame->down * up[0] - frame->right * left[0];
194         v[3][1] = origin[1] + frame->down * up[1] - frame->right * left[1];
195         v[3][2] = origin[2] + frame->down * up[2] - frame->right * left[2];
196         st[0][0] = 0;
197         st[0][1] = 1;
198         st[1][0] = 0;
199         st[1][1] = 0;
200         st[2][0] = 1;
201         st[2][1] = 0;
202         st[3][0] = 1;
203         st[3][1] = 1;
204
205         R_Mesh_Draw(&m);
206 }
207
208 /*
209 =================
210 R_DrawSpriteModel
211 =================
212 */
213 void R_DrawSpriteModel ()
214 {
215         int                     i;
216         vec3_t          left, up, org, color;
217         mspriteframe_t *frame;
218         vec3_t diff;
219         float           fog, ifog;
220
221         if (currentrenderentity->frameblend[0].frame < 0)
222                 return;
223
224         if (R_SpriteSetup(currentrenderentity->model->sprnum_type, org, left, up))
225                 return;
226
227         c_sprites++;
228
229         if ((currentrenderentity->model->flags & EF_FULLBRIGHT) || (currentrenderentity->effects & EF_FULLBRIGHT))
230                 color[0] = color[1] = color[2] = 1;
231         else
232                 R_CompleteLightPoint(color, currentrenderentity->origin, true, NULL);
233
234         if (fogenabled)
235         {
236                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
237                 fog = exp(fogdensity/DotProduct(diff,diff));
238                 if (fog > 1)
239                         fog = 1;
240         }
241         else
242                 fog = 0;
243         ifog = 1 - fog;
244
245 #ifdef LERPSPRITES
246         // LordHavoc: interpolated sprite rendering
247         for (i = 0;i < 4;i++)
248         {
249                 if (currentrenderentity->frameblend[i].lerp >= 0.01f)
250                 {
251                         frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[i].frame;
252                         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);
253                         if (fog * currentrenderentity->frameblend[i].lerp >= 0.01f)
254                                 GL_DrawSpriteImage(true, frame, R_GetTexture(frame->fogtexture), org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * currentrenderentity->alpha * currentrenderentity->frameblend[i].lerp);
255                 }
256         }
257 #else
258         // LordHavoc: no interpolation
259         frame = NULL;
260         for (i = 0;i < 4 && currentrenderentity->frameblend[i].lerp;i++)
261                 frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[i].frame;
262
263         GL_DrawSpriteImage(false, frame, R_GetTexture(frame->texture), org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, currentrenderentity->alpha);
264         if (fog * currentrenderentity->frameblend[i].lerp >= 0.01f)
265                 GL_DrawSpriteImage(true, frame, R_GetTexture(frame->fogtexture), org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * currentrenderentity->alpha);
266 #endif
267 }