fix for oriented sprites not appearing
[xonotic/darkplaces.git] / r_sprites.c
1 #include "quakedef.h"
2
3 void R_ClipSpriteImage (entity_t *e, msprite_t *psprite, frameblend_t *blend, vec3_t origin, vec3_t up, vec3_t right)
4 {
5         int i;
6         mspriteframe_t *frame;
7         vec3_t points[4];
8         float fleft, fright, fdown, fup;
9         frame = ((mspriteframe_t *)(psprite->ofs_frames + (int) psprite)) + blend[0].frame;
10         fleft  = frame->left;
11         fdown  = frame->down;
12         fright = frame->right;
13         fup    = frame->up;
14         for (i = 1;i < 4 && blend[i].lerp;i++)
15         {
16                 frame = ((mspriteframe_t *)(psprite->ofs_frames + (int) psprite)) + blend[i].frame;
17                 fleft  = min(fleft , frame->left );
18                 fdown  = min(fdown , frame->down );
19                 fright = max(fright, frame->right);
20                 fup    = max(fup   , frame->up   );
21         }
22         points[0][0] = origin[0] + fdown * up[0] + fleft  * right[0];points[0][1] = origin[1] + fdown * up[1] + fleft  * right[1];points[0][2] = origin[2] + fdown * up[2] + fleft  * right[2];
23         points[1][0] = origin[0] + fup   * up[0] + fleft  * right[0];points[1][1] = origin[1] + fup   * up[1] + fleft  * right[1];points[1][2] = origin[2] + fup   * up[2] + fleft  * right[2];
24         points[2][0] = origin[0] + fup   * up[0] + fright * right[0];points[2][1] = origin[1] + fup   * up[1] + fright * right[1];points[2][2] = origin[2] + fup   * up[2] + fright * right[2];
25         points[3][0] = origin[0] + fdown * up[0] + fright * right[0];points[3][1] = origin[1] + fdown * up[1] + fright * right[1];points[3][2] = origin[2] + fdown * up[2] + fright * right[2];
26         R_Clip_AddPolygon(&points[0][0], 4, sizeof(float[3]), false, R_Entity_Callback, e, NULL, NULL);
27 }
28
29 void R_ClipSprite (entity_t *e, frameblend_t *blend)
30 {
31         vec3_t forward, right, up, org;
32         msprite_t *psprite;
33
34         if (!blend[0].lerp)
35                 return;
36
37         psprite = Mod_Extradata(e->render.model);
38
39         if (psprite->type == SPR_ORIENTED)
40         {
41                 // bullet marks on walls
42                 AngleVectors (e->render.angles, forward, right, up);
43                 // nudge it toward the view, so it will be infront of the wall
44                 VectorSubtract(e->render.origin, vpn, org);
45                 // don't draw if it's a backface
46                 if (DotProduct(r_origin, forward) > (DotProduct(org, forward) - 1.0f))
47                         return;
48         }
49         else
50         {
51                 // normal sprite
52                 VectorCopy(vup, up);
53                 VectorCopy(vright, right);
54                 VectorCopy(e->render.origin, org);
55                 // don't draw if it's a backface
56                 if (DotProduct(org, vpn) < (DotProduct(r_origin, vpn) + 1.0f))
57                         return;
58         }
59
60
61         if (e->render.scale != 1)
62         {
63                 VectorScale(up, e->render.scale, up);
64                 VectorScale(right, e->render.scale, right);
65         }
66
67         // LordHavoc: interpolated sprite rendering
68         R_ClipSpriteImage(e, psprite, blend, org, up, right);
69 }
70
71 void GL_DrawSpriteImage (mspriteframe_t *frame, vec3_t origin, vec3_t up, vec3_t right, byte red, byte green, byte blue, int alpha)
72 {
73         byte alphaub;
74         alphaub = bound(0, alpha, 255);
75         transpolybegin(R_GetTexture(frame->texture), 0, R_GetTexture(frame->fogtexture), currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
76         transpolyvertub(origin[0] + frame->down * up[0] + frame->left  * right[0], origin[1] + frame->down * up[1] + frame->left  * right[1], origin[2] + frame->down * up[2] + frame->left  * right[2], 0, 1, red, green, blue, alphaub);
77         transpolyvertub(origin[0] + frame->up   * up[0] + frame->left  * right[0], origin[1] + frame->up   * up[1] + frame->left  * right[1], origin[2] + frame->up   * up[2] + frame->left  * right[2], 0, 0, red, green, blue, alphaub);
78         transpolyvertub(origin[0] + frame->up   * up[0] + frame->right * right[0], origin[1] + frame->up   * up[1] + frame->right * right[1], origin[2] + frame->up   * up[2] + frame->right * right[2], 1, 0, red, green, blue, alphaub);
79         transpolyvertub(origin[0] + frame->down * up[0] + frame->right * right[0], origin[1] + frame->down * up[1] + frame->right * right[1], origin[2] + frame->down * up[2] + frame->right * right[2], 1, 1, red, green, blue, alphaub);
80         transpolyend();
81 }
82
83 /*
84 =================
85 R_DrawSpriteModel
86 =================
87 */
88 void R_DrawSpriteModel (entity_t *e, frameblend_t *blend)
89 {
90         vec3_t          forward, right, up, org, color, mins, maxs;
91         byte            colorub[4];
92         msprite_t       *psprite;
93
94         if (!blend[0].lerp)
95                 return;
96
97         VectorAdd (e->render.origin, e->render.model->mins, mins);
98         VectorAdd (e->render.origin, e->render.model->maxs, maxs);
99
100 //      if (R_CullBox (mins, maxs))
101 //              return;
102
103         c_sprites++;
104
105         psprite = Mod_Extradata(e->render.model);
106         //psprite = e->model->cache.data;
107
108         if (psprite->type == SPR_ORIENTED)
109         {
110                 // bullet marks on walls
111                 AngleVectors (e->render.angles, forward, right, up);
112                 VectorSubtract(e->render.origin, vpn, org);
113         }
114         else
115         {
116                 // normal sprite
117                 VectorCopy(vup, up);
118                 VectorCopy(vright, right);
119                 VectorCopy(e->render.origin, org);
120         }
121
122         if (e->render.scale != 1)
123         {
124                 VectorScale(up, e->render.scale, up);
125                 VectorScale(right, e->render.scale, right);
126         }
127
128         if (e->render.model->flags & EF_FULLBRIGHT || e->render.effects & EF_FULLBRIGHT)
129         {
130                 color[0] = e->render.colormod[0] * 255;
131                 color[1] = e->render.colormod[1] * 255;
132                 color[2] = e->render.colormod[2] * 255;
133         }
134         else
135                 R_CompleteLightPoint(color, e->render.origin, true);
136
137         colorub[0] = bound(0, color[0], 255);
138         colorub[1] = bound(0, color[1], 255);
139         colorub[2] = bound(0, color[2], 255);
140
141         // LordHavoc: interpolated sprite rendering
142         if (blend[0].lerp)
143                 GL_DrawSpriteImage(((mspriteframe_t *)(psprite->ofs_frames + (int) psprite)) + blend[0].frame, org, up, right, colorub[0],colorub[1],colorub[2], e->render.alpha*255*blend[0].lerp);
144         if (blend[1].lerp)
145                 GL_DrawSpriteImage(((mspriteframe_t *)(psprite->ofs_frames + (int) psprite)) + blend[1].frame, org, up, right, colorub[0],colorub[1],colorub[2], e->render.alpha*255*blend[1].lerp);
146         if (blend[2].lerp)
147                 GL_DrawSpriteImage(((mspriteframe_t *)(psprite->ofs_frames + (int) psprite)) + blend[2].frame, org, up, right, colorub[0],colorub[1],colorub[2], e->render.alpha*255*blend[2].lerp);
148         if (blend[3].lerp)
149                 GL_DrawSpriteImage(((mspriteframe_t *)(psprite->ofs_frames + (int) psprite)) + blend[3].frame, org, up, right, colorub[0],colorub[1],colorub[2], e->render.alpha*255*blend[3].lerp);
150 }
151