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