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