cleaned up almost all direct indexing of matrix4x4_t structures to go through proper...
[divverent/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;
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_OriginFromMatrix(&ent->matrix, 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                 left[0] = r_view.left[0] * ent->scale;
47                 left[1] = r_view.left[1] * ent->scale;
48                 left[2] = r_view.left[2] * ent->scale;
49                 up[0] = r_view.up[0] * ent->scale;
50                 up[1] = r_view.up[1] * ent->scale;
51                 up[2] = r_view.up[2] * ent->scale;
52                 break;
53         case SPR_ORIENTED:
54                 // bullet marks on walls
55                 // ignores viewer entirely
56                 left[0] = ent->matrix.m[0][1];
57                 left[1] = ent->matrix.m[1][1];
58                 left[2] = ent->matrix.m[2][1];
59                 up[0] = ent->matrix.m[0][2];
60                 up[1] = ent->matrix.m[1][2];
61                 up[2] = ent->matrix.m[2][2];
62                 break;
63         case SPR_VP_PARALLEL_ORIENTED:
64                 // I have no idea what people would use this for...
65                 // oriented relative to view space
66                 // FIXME: test this and make sure it mimicks software
67                 left[0] = ent->matrix.m[0][1] * r_view.forward[0] + ent->matrix.m[1][1] * r_view.left[0] + ent->matrix.m[2][1] * r_view.up[0];
68                 left[1] = ent->matrix.m[0][1] * r_view.forward[1] + ent->matrix.m[1][1] * r_view.left[1] + ent->matrix.m[2][1] * r_view.up[1];
69                 left[2] = ent->matrix.m[0][1] * r_view.forward[2] + ent->matrix.m[1][1] * r_view.left[2] + ent->matrix.m[2][1] * r_view.up[2];
70                 up[0] = ent->matrix.m[0][2] * r_view.forward[0] + ent->matrix.m[1][2] * r_view.left[0] + ent->matrix.m[2][2] * r_view.up[0];
71                 up[1] = ent->matrix.m[0][2] * r_view.forward[1] + ent->matrix.m[1][2] * r_view.left[1] + ent->matrix.m[2][2] * r_view.up[1];
72                 up[2] = ent->matrix.m[0][2] * r_view.forward[2] + ent->matrix.m[1][2] * r_view.left[2] + ent->matrix.m[2][2] * r_view.up[2];
73                 break;
74         }
75
76         R_Mesh_Matrix(&identitymatrix);
77
78         if (!(ent->flags & RENDER_LIGHT))
79                 color[0] = color[1] = color[2] = 1;
80         else
81         {
82                 R_CompleteLightPoint(color, diffusecolor, diffusenormal, ent->origin, true);
83                 VectorMA(color, 0.5f, diffusecolor, color);
84         }
85         color[0] *= ent->colormod[0];
86         color[1] *= ent->colormod[1];
87         color[2] *= ent->colormod[2];
88
89         // LordHavoc: interpolated sprite rendering
90         for (i = 0;i < 4;i++)
91         {
92                 if (ent->frameblend[i].lerp >= 0.01f)
93                 {
94                         frame = model->sprite.sprdata_frames + ent->frameblend[i].frame;
95                         // FIXME: negate left and right in loader
96                         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);
97                 }
98         }
99 }
100
101 void R_Model_Sprite_Draw(entity_render_t *ent)
102 {
103         if (ent->frameblend[0].frame < 0)
104                 return;
105
106         R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_view.origin : ent->origin, R_Model_Sprite_Draw_TransparentCallback, ent, 0, r_shadow_rtlight);
107 }
108