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