fixed oriented sprite problems (no longer uses entity angles for them) by rewriting...
[xonotic/darkplaces.git] / r_sprites.c
1
2 #include "quakedef.h"
3
4 static int R_SpriteSetup (const entity_render_t *ent, int type, float org[3], float left[3], float up[3])
5 {
6         float scale;
7
8         // nudge it toward the view to make sure it isn't in a wall
9         org[0] = ent->matrix.m[0][3] - r_viewforward[0];
10         org[1] = ent->matrix.m[1][3] - r_viewforward[1];
11         org[2] = ent->matrix.m[2][3] - r_viewforward[2];
12         switch(type)
13         {
14         case SPR_VP_PARALLEL_UPRIGHT:
15                 // flames and such
16                 // vertical beam sprite, faces view plane
17                 scale = ent->scale / sqrt(r_viewforward[0]*r_viewforward[0]+r_viewforward[1]*r_viewforward[1]);
18                 left[0] = -r_viewforward[1] * scale;
19                 left[1] = r_viewforward[0] * scale;
20                 left[2] = 0;
21                 up[0] = 0;
22                 up[1] = 0;
23                 up[2] = ent->scale;
24                 break;
25         case SPR_FACING_UPRIGHT:
26                 // flames and such
27                 // vertical beam sprite, faces viewer's origin (not the view plane)
28                 scale = ent->scale / sqrt((org[0] - r_vieworigin[0])*(org[0] - r_vieworigin[0])+(org[1] - r_vieworigin[1])*(org[1] - r_vieworigin[1]));
29                 left[0] = (org[1] - r_vieworigin[1]) * scale;
30                 left[1] = -(org[0] - r_vieworigin[0]) * scale;
31                 left[2] = 0;
32                 up[0] = 0;
33                 up[1] = 0;
34                 up[2] = ent->scale;
35                 break;
36         default:
37                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", type);
38                 // fall through to normal sprite
39         case SPR_VP_PARALLEL:
40                 // normal sprite
41                 // faces view plane
42                 left[0] = r_viewleft[0] * ent->scale;
43                 left[1] = r_viewleft[1] * ent->scale;
44                 left[2] = r_viewleft[2] * ent->scale;
45                 up[0] = r_viewup[0] * ent->scale;
46                 up[1] = r_viewup[1] * ent->scale;
47                 up[2] = r_viewup[2] * ent->scale;
48                 break;
49         case SPR_ORIENTED:
50                 // bullet marks on walls
51                 // ignores viewer entirely
52                 left[0] = ent->matrix.m[0][1];
53                 left[1] = ent->matrix.m[1][1];
54                 left[2] = ent->matrix.m[2][1];
55                 up[0] = ent->matrix.m[0][2];
56                 up[1] = ent->matrix.m[1][2];
57                 up[2] = ent->matrix.m[2][2];
58                 break;
59         case SPR_VP_PARALLEL_ORIENTED:
60                 // I have no idea what people would use this for...
61                 // oriented relative to view space
62                 // FIXME: test this and make sure it mimicks software
63                 left[0] = ent->matrix.m[0][1] * r_viewforward[0] + ent->matrix.m[1][1] * r_viewleft[0] + ent->matrix.m[2][1] * r_viewup[0];
64                 left[1] = ent->matrix.m[0][1] * r_viewforward[1] + ent->matrix.m[1][1] * r_viewleft[1] + ent->matrix.m[2][1] * r_viewup[1];
65                 left[2] = ent->matrix.m[0][1] * r_viewforward[2] + ent->matrix.m[1][1] * r_viewleft[2] + ent->matrix.m[2][1] * r_viewup[2];
66                 up[0] = ent->matrix.m[0][2] * r_viewforward[0] + ent->matrix.m[1][2] * r_viewleft[0] + ent->matrix.m[2][2] * r_viewup[0];
67                 up[1] = ent->matrix.m[0][2] * r_viewforward[1] + ent->matrix.m[1][2] * r_viewleft[1] + ent->matrix.m[2][2] * r_viewup[1];
68                 up[2] = ent->matrix.m[0][2] * r_viewforward[2] + ent->matrix.m[1][2] * r_viewleft[2] + ent->matrix.m[2][2] * r_viewup[2];
69                 break;
70         }
71         return false;
72 }
73
74 static void R_DrawSpriteImage (int additive, mspriteframe_t *frame, rtexture_t *texture, vec3_t origin, vec3_t up, vec3_t left, float red, float green, float blue, float alpha)
75 {
76         // FIXME: negate left and right in loader
77         R_DrawSprite(GL_SRC_ALPHA, additive ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, texture, false, origin, left, up, frame->left, frame->right, frame->down, frame->up, red, green, blue, alpha);
78 }
79
80 void R_DrawSpriteModelCallback(const void *calldata1, int calldata2)
81 {
82         const entity_render_t *ent = calldata1;
83         int i;
84         vec3_t left, up, org, color, diffusecolor, diffusenormal;
85         mspriteframe_t *frame;
86         vec3_t diff;
87         float fog, ifog;
88
89         if (R_SpriteSetup(ent, ent->model->sprite.sprnum_type, org, left, up))
90                 return;
91
92         R_Mesh_Matrix(&r_identitymatrix);
93
94         if ((ent->model->flags & EF_FULLBRIGHT) || (ent->effects & EF_FULLBRIGHT))
95                 color[0] = color[1] = color[2] = 1;
96         else
97         {
98                 R_CompleteLightPoint(color, diffusecolor, diffusenormal, ent->origin, true, NULL);
99                 VectorMA(color, 0.5f, diffusecolor, color);
100         }
101
102         if (fogenabled)
103         {
104                 VectorSubtract(ent->origin, r_vieworigin, diff);
105                 fog = exp(fogdensity/DotProduct(diff,diff));
106                 if (fog > 1)
107                         fog = 1;
108         }
109         else
110                 fog = 0;
111         ifog = 1 - fog;
112
113         if (r_lerpsprites.integer)
114         {
115                 // LordHavoc: interpolated sprite rendering
116                 for (i = 0;i < 4;i++)
117                 {
118                         if (ent->frameblend[i].lerp >= 0.01f)
119                         {
120                                 frame = ent->model->sprite.sprdata_frames + ent->frameblend[i].frame;
121                                 R_DrawSpriteImage((ent->effects & EF_ADDITIVE) || (ent->model->flags & EF_ADDITIVE), frame, frame->texture, org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, ent->alpha * ent->frameblend[i].lerp);
122                                 if (fog * ent->frameblend[i].lerp >= 0.01f)
123                                         R_DrawSpriteImage(true, frame, frame->fogtexture, org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * ent->alpha * ent->frameblend[i].lerp);
124                         }
125                 }
126         }
127         else
128         {
129                 // LordHavoc: no interpolation
130                 frame = NULL;
131                 for (i = 0;i < 4 && ent->frameblend[i].lerp;i++)
132                         frame = ent->model->sprite.sprdata_frames + ent->frameblend[i].frame;
133
134                 R_DrawSpriteImage((ent->effects & EF_ADDITIVE) || (ent->model->flags & EF_ADDITIVE), frame, frame->texture, org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, ent->alpha);
135                 if (fog * ent->frameblend[i].lerp >= 0.01f)
136                         R_DrawSpriteImage(true, frame, frame->fogtexture, org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * ent->alpha);
137         }
138 }
139
140 void R_Model_Sprite_Draw(entity_render_t *ent)
141 {
142         if (ent->frameblend[0].frame < 0)
143                 return;
144
145         c_sprites++;
146
147         R_MeshQueue_AddTransparent(ent->origin, R_DrawSpriteModelCallback, ent, 0);
148 }
149