more cleaning of matrix4x4_t struct access
[xonotic/darkplaces.git] / r_sprites.c
1
2 #include "quakedef.h"
3 #include "r_shadow.h"
4
5 void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
6 {
7         int i;
8         model_t *model = ent->model;
9         vec3_t left, up, org, color, diffusecolor, diffusenormal, mforward, mleft, mup;
10         mspriteframe_t *frame;
11         float scale;
12
13         // nudge it toward the view to make sure it isn't in a wall
14         Matrix4x4_ToVectors(&ent->matrix, mforward, mleft, mup, org);
15         VectorSubtract(org, r_view.forward, org);
16         switch(model->sprite.sprnum_type)
17         {
18         case SPR_VP_PARALLEL_UPRIGHT:
19                 // flames and such
20                 // vertical beam sprite, faces view plane
21                 scale = ent->scale / sqrt(r_view.forward[0]*r_view.forward[0]+r_view.forward[1]*r_view.forward[1]);
22                 left[0] = -r_view.forward[1] * scale;
23                 left[1] = r_view.forward[0] * scale;
24                 left[2] = 0;
25                 up[0] = 0;
26                 up[1] = 0;
27                 up[2] = ent->scale;
28                 break;
29         case SPR_FACING_UPRIGHT:
30                 // flames and such
31                 // vertical beam sprite, faces viewer's origin (not the view plane)
32                 scale = ent->scale / sqrt((org[0] - r_view.origin[0])*(org[0] - r_view.origin[0])+(org[1] - r_view.origin[1])*(org[1] - r_view.origin[1]));
33                 left[0] = (org[1] - r_view.origin[1]) * scale;
34                 left[1] = -(org[0] - r_view.origin[0]) * scale;
35                 left[2] = 0;
36                 up[0] = 0;
37                 up[1] = 0;
38                 up[2] = ent->scale;
39                 break;
40         default:
41                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", model->sprite.sprnum_type);
42                 // fall through to normal sprite
43         case SPR_VP_PARALLEL:
44                 // normal sprite
45                 // faces view plane
46                 VectorScale(r_view.left, ent->scale, left);
47                 VectorScale(r_view.up, ent->scale, up);
48                 break;
49         case SPR_ORIENTED:
50                 // bullet marks on walls
51                 // ignores viewer entirely
52                 VectorCopy(mleft, left);
53                 VectorCopy(mup, up);
54                 break;
55         case SPR_VP_PARALLEL_ORIENTED:
56                 // I have no idea what people would use this for...
57                 // oriented relative to view space
58                 // FIXME: test this and make sure it mimicks software
59                 left[0] = mleft[0] * r_view.forward[0] + mleft[1] * r_view.left[0] + mleft[2] * r_view.up[0];
60                 left[1] = mleft[0] * r_view.forward[1] + mleft[1] * r_view.left[1] + mleft[2] * r_view.up[1];
61                 left[2] = mleft[0] * r_view.forward[2] + mleft[1] * r_view.left[2] + mleft[2] * r_view.up[2];
62                 up[0] = mup[0] * r_view.forward[0] + mup[1] * r_view.left[0] + mup[2] * r_view.up[0];
63                 up[1] = mup[0] * r_view.forward[1] + mup[1] * r_view.left[1] + mup[2] * r_view.up[1];
64                 up[2] = mup[0] * r_view.forward[2] + mup[1] * r_view.left[2] + mup[2] * r_view.up[2];
65                 break;
66         }
67
68         R_Mesh_Matrix(&identitymatrix);
69
70         if (!(ent->flags & RENDER_LIGHT))
71                 color[0] = color[1] = color[2] = 1;
72         else
73         {
74                 R_CompleteLightPoint(color, diffusecolor, diffusenormal, ent->origin, true);
75                 VectorMA(color, 0.5f, diffusecolor, color);
76         }
77         color[0] *= ent->colormod[0];
78         color[1] *= ent->colormod[1];
79         color[2] *= ent->colormod[2];
80
81         // LordHavoc: interpolated sprite rendering
82         for (i = 0;i < 4;i++)
83         {
84                 if (ent->frameblend[i].lerp >= 0.01f)
85                 {
86                         frame = model->sprite.sprdata_frames + ent->frameblend[i].frame;
87                         // FIXME: negate left and right in loader
88                         R_DrawSprite(GL_SRC_ALPHA, (ent->effects & EF_ADDITIVE) ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, frame->skin.base, frame->skin.fog, (ent->effects & EF_NODEPTHTEST), org, left, up, frame->left, frame->right, frame->down, frame->up, color[0], color[1], color[2], ent->alpha * ent->frameblend[i].lerp);
89                 }
90         }
91 }
92
93 void R_Model_Sprite_Draw(entity_render_t *ent)
94 {
95         if (ent->frameblend[0].frame < 0)
96                 return;
97
98         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_view.origin : ent->origin, R_Model_Sprite_Draw_TransparentCallback, ent, 0, r_shadow_rtlight);
99 }
100