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