removed support for r_ser (also known as R_Clip or "Hidden Surface Removal") as it...
[xonotic/darkplaces.git] / r_sprites.c
1
2 #include "quakedef.h"
3
4 #define LERPSPRITES
5
6 static int R_SpriteSetup (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(currentrenderentity->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(currentrenderentity->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 (currentrenderentity->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 (currentrenderentity->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 (currentrenderentity->scale != 1)
69         {
70                 VectorScale(matrix3[1], currentrenderentity->scale, left);
71                 VectorScale(matrix3[2], currentrenderentity->scale, up);
72         }
73         else
74         {
75                 VectorCopy(matrix3[1], left);
76                 VectorCopy(matrix3[2], up);
77         }
78         return false;
79 }
80
81 static int spritepolyindex[6] = {0, 1, 2, 0, 2, 3};
82
83 static 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)
84 {
85         rmeshinfo_t m;
86         float v[4][4], st[4][2];
87         memset(&m, 0, sizeof(m));
88         m.transparent = true;
89         m.blendfunc1 = GL_SRC_ALPHA;
90         m.blendfunc2 = GL_ONE_MINUS_SRC_ALPHA;
91         if ((currentrenderentity->effects & EF_ADDITIVE)
92          || (currentrenderentity->model->flags & EF_ADDITIVE)
93          || fog)
94                 m.blendfunc2 = GL_ONE;
95         m.numtriangles = 2;
96         m.index = spritepolyindex;
97         m.numverts = 4;
98         m.vertex = &v[0][0];
99         m.vertexstep = sizeof(float[4]);
100         m.cr = red;
101         m.cg = green;
102         m.cb = blue;
103         m.ca = alpha;
104         m.tex[0] = texture;
105         m.texcoords[0] = &st[0][0];
106         m.texcoordstep[0] = sizeof(float[2]);
107
108         // FIXME: negate left and right in loader
109         v[0][0] = origin[0] + frame->down * up[0] - frame->left  * left[0];
110         v[0][1] = origin[1] + frame->down * up[1] - frame->left  * left[1];
111         v[0][2] = origin[2] + frame->down * up[2] - frame->left  * left[2];
112         v[1][0] = origin[0] + frame->up   * up[0] - frame->left  * left[0];
113         v[1][1] = origin[1] + frame->up   * up[1] - frame->left  * left[1];
114         v[1][2] = origin[2] + frame->up   * up[2] - frame->left  * left[2];
115         v[2][0] = origin[0] + frame->up   * up[0] - frame->right * left[0];
116         v[2][1] = origin[1] + frame->up   * up[1] - frame->right * left[1];
117         v[2][2] = origin[2] + frame->up   * up[2] - frame->right * left[2];
118         v[3][0] = origin[0] + frame->down * up[0] - frame->right * left[0];
119         v[3][1] = origin[1] + frame->down * up[1] - frame->right * left[1];
120         v[3][2] = origin[2] + frame->down * up[2] - frame->right * left[2];
121         st[0][0] = 0;
122         st[0][1] = 1;
123         st[1][0] = 0;
124         st[1][1] = 0;
125         st[2][0] = 1;
126         st[2][1] = 0;
127         st[3][0] = 1;
128         st[3][1] = 1;
129
130         R_Mesh_Draw(&m);
131 }
132
133 /*
134 =================
135 R_DrawSpriteModel
136 =================
137 */
138 void R_DrawSpriteModel ()
139 {
140         int                     i;
141         vec3_t          left, up, org, color;
142         mspriteframe_t *frame;
143         vec3_t diff;
144         float           fog, ifog;
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         c_sprites++;
153
154         if ((currentrenderentity->model->flags & EF_FULLBRIGHT) || (currentrenderentity->effects & EF_FULLBRIGHT))
155                 color[0] = color[1] = color[2] = 1;
156         else
157                 R_CompleteLightPoint(color, currentrenderentity->origin, true, NULL);
158
159         if (fogenabled)
160         {
161                 VectorSubtract(currentrenderentity->origin, r_origin, diff);
162                 fog = exp(fogdensity/DotProduct(diff,diff));
163                 if (fog > 1)
164                         fog = 1;
165         }
166         else
167                 fog = 0;
168         ifog = 1 - fog;
169
170 #ifdef LERPSPRITES
171         // LordHavoc: interpolated sprite rendering
172         for (i = 0;i < 4;i++)
173         {
174                 if (currentrenderentity->frameblend[i].lerp >= 0.01f)
175                 {
176                         frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[i].frame;
177                         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);
178                         if (fog * currentrenderentity->frameblend[i].lerp >= 0.01f)
179                                 GL_DrawSpriteImage(true, frame, R_GetTexture(frame->fogtexture), org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * currentrenderentity->alpha * currentrenderentity->frameblend[i].lerp);
180                 }
181         }
182 #else
183         // LordHavoc: no interpolation
184         frame = NULL;
185         for (i = 0;i < 4 && currentrenderentity->frameblend[i].lerp;i++)
186                 frame = currentrenderentity->model->sprdata_frames + currentrenderentity->frameblend[i].frame;
187
188         GL_DrawSpriteImage(false, frame, R_GetTexture(frame->texture), org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, currentrenderentity->alpha);
189         if (fog * currentrenderentity->frameblend[i].lerp >= 0.01f)
190                 GL_DrawSpriteImage(true, frame, R_GetTexture(frame->fogtexture), org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * currentrenderentity->alpha);
191 #endif
192 }
193