added cl_screen.c/h (eventually most 2D stuff should be moved here)
[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 //int spritepolyindex[6] = {0, 1, 2, 0, 2, 3};
111
112 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)
113 {
114         rmeshinfo_t m;
115         float v[4][4], st[4][2];
116         // LordHavoc: this meshinfo must match up with R_Mesh_DrawDecal
117         // LordHavoc: the commented out lines are hardwired behavior in R_Mesh_DrawDecal
118         memset(&m, 0, sizeof(m));
119         m.transparent = true;
120         m.blendfunc1 = GL_SRC_ALPHA;
121         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
122         if ((currentrenderentity->effects & EF_ADDITIVE)
123          || (currentrenderentity->model->flags & EF_ADDITIVE)
124          || fog)
125                 m.blendfunc2 = GL_ONE;
126         //m.numtriangles = 2;
127         //m.index = spritepolyindex;
128         //m.numverts = 4;
129         m.vertex = &v[0][0];
130         //m.vertexstep = sizeof(float[4]);
131         m.cr = red;
132         m.cg = green;
133         m.cb = blue;
134         m.ca = alpha;
135         m.tex[0] = texture;
136         m.texcoords[0] = &st[0][0];
137         //m.texcoordstep[0] = sizeof(float[2]);
138
139         v[0][0] = origin[0] + frame->down * up[0] + frame->left  * right[0];
140         v[0][1] = origin[1] + frame->down * up[1] + frame->left  * right[1];
141         v[0][2] = origin[2] + frame->down * up[2] + frame->left  * right[2];
142         v[1][0] = origin[0] + frame->up   * up[0] + frame->left  * right[0];
143         v[1][1] = origin[1] + frame->up   * up[1] + frame->left  * right[1];
144         v[1][2] = origin[2] + frame->up   * up[2] + frame->left  * right[2];
145         v[2][0] = origin[0] + frame->up   * up[0] + frame->right * right[0];
146         v[2][1] = origin[1] + frame->up   * up[1] + frame->right * right[1];
147         v[2][2] = origin[2] + frame->up   * up[2] + frame->right * right[2];
148         v[3][0] = origin[0] + frame->down * up[0] + frame->right * right[0];
149         v[3][1] = origin[1] + frame->down * up[1] + frame->right * right[1];
150         v[3][2] = origin[2] + frame->down * up[2] + frame->right * right[2];
151         st[0][0] = 0;
152         st[0][1] = 1;
153         st[1][0] = 0;
154         st[1][1] = 0;
155         st[2][0] = 1;
156         st[2][1] = 0;
157         st[3][0] = 1;
158         st[3][1] = 1;
159
160         R_Mesh_DrawDecal(&m);
161 }
162
163 /*
164 =================
165 R_DrawSpriteModel
166 =================
167 */
168 void R_DrawSpriteModel ()
169 {
170         int                     i;
171         vec3_t          right, up, org, color;
172         mspriteframe_t *frame;
173         vec3_t diff;
174         float           fog, ifog;
175
176         if (currentrenderentity->frameblend[0].frame < 0)
177                 return;
178
179         if (R_SpriteSetup(currentrenderentity->model->sprnum_type, org, right, up))
180                 return;
181
182         c_sprites++;
183
184         if ((currentrenderentity->model->flags & EF_FULLBRIGHT) || (currentrenderentity->effects & EF_FULLBRIGHT))
185                 color[0] = color[1] = color[2] = 1;
186         else
187                 R_CompleteLightPoint(color, currentrenderentity->origin, true, NULL);
188
189         if (fogenabled)
190         {
191                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
192                 fog = exp(fogdensity/DotProduct(diff,diff));
193                 if (fog > 1)
194                         fog = 1;
195         }
196         else
197                 fog = 0;
198         ifog = 1 - fog;
199
200         // LordHavoc: interpolated sprite rendering
201         for (i = 0;i < 4;i++)
202         {
203                 if (currentrenderentity->frameblend[i].lerp >= 0.01f)
204                 {
205                         frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[i].frame;
206                         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);
207                         if (fog * currentrenderentity->frameblend[i].lerp >= 0.01f)
208                                 GL_DrawSpriteImage(true, frame, R_GetTexture(frame->fogtexture), org, up, right, fogcolor[0],fogcolor[1],fogcolor[2], fog * currentrenderentity->alpha * currentrenderentity->frameblend[i].lerp);
209                 }
210         }
211 }