ccc9bb0d54d6e631875611b106fc05c6231b9d11
[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, int depthdisable, 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, depthdisable, 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 = (entity_render_t *)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->flags & RENDER_LIGHT))
95                 color[0] = color[1] = color[2] = 1;
96         else
97         {
98                 R_CompleteLightPoint(color, diffusecolor, diffusenormal, ent->origin, true);
99                 VectorMA(color, 0.5f, diffusecolor, color);
100         }
101         color[0] *= ent->colormod[0];
102         color[1] *= ent->colormod[1];
103         color[2] *= ent->colormod[2];
104
105         if (fogenabled)
106         {
107                 VectorSubtract(ent->origin, r_vieworigin, diff);
108                 fog = exp(fogdensity/DotProduct(diff,diff));
109                 if (fog > 1)
110                         fog = 1;
111         }
112         else
113                 fog = 0;
114         ifog = 1 - fog;
115
116         if (r_lerpsprites.integer)
117         {
118                 // LordHavoc: interpolated sprite rendering
119                 for (i = 0;i < 4;i++)
120                 {
121                         if (ent->frameblend[i].lerp >= 0.01f)
122                         {
123                                 frame = ent->model->sprite.sprdata_frames + ent->frameblend[i].frame;
124                                 R_DrawSpriteImage((ent->effects & EF_ADDITIVE), (ent->effects & EF_NODEPTHTEST), frame, frame->texture, org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, ent->alpha * ent->frameblend[i].lerp);
125                                 if (fog * ent->frameblend[i].lerp >= 0.01f)
126                                         R_DrawSpriteImage(true, (ent->effects & EF_NODEPTHTEST), frame, frame->fogtexture, org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * ent->alpha * ent->frameblend[i].lerp);
127                         }
128                 }
129         }
130         else
131         {
132                 // LordHavoc: no interpolation
133                 frame = NULL;
134                 for (i = 0;i < 4 && ent->frameblend[i].lerp;i++)
135                         frame = ent->model->sprite.sprdata_frames + ent->frameblend[i].frame;
136
137                 if (frame)
138                 {
139                         R_DrawSpriteImage((ent->effects & EF_ADDITIVE), (ent->effects & EF_NODEPTHTEST), frame, frame->texture, org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, ent->alpha);
140                         if (fog * ent->frameblend[i].lerp >= 0.01f)
141                                 R_DrawSpriteImage(true, (ent->effects & EF_NODEPTHTEST), frame, frame->fogtexture, org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * ent->alpha);
142                 }
143         }
144 }
145
146 void R_Model_Sprite_Draw(entity_render_t *ent)
147 {
148         if (ent->frameblend[0].frame < 0)
149                 return;
150
151         c_sprites++;
152
153         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_vieworigin : ent->origin, R_DrawSpriteModelCallback, ent, 0);
154 }
155