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