Major update, been neglecting CVS for some time...
[divverent/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 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                 vec3_t forward;
43                 AngleVectors (e->render.angles, forward, right, up);
44                 // nudge it toward the view, so it will be infront of the wall
45                 VectorSubtract(e->render.origin, vpn, org);
46                 // don't draw if it's a backface
47                 if (DotProduct(r_origin, forward) < (DotProduct(org, forward) + 1.0f))
48                         return;
49         }
50         else
51         {
52                 // normal sprite
53                 VectorCopy(vup, up);
54                 VectorCopy(vright, right);
55                 VectorCopy(e->render.origin, org);
56                 // don't draw if it's a backface
57                 if (DotProduct(org, vpn) < (DotProduct(r_origin, vpn) + 1.0f))
58                         return;
59         }
60
61
62         if (e->render.scale != 1)
63         {
64                 VectorScale(up, e->render.scale, up);
65                 VectorScale(right, e->render.scale, right);
66         }
67
68         // LordHavoc: interpolated sprite rendering
69         R_ClipSpriteImage(e, psprite, blend, org, up, right);
70 }
71
72 void GL_DrawSpriteImage (mspriteframe_t *frame, vec3_t origin, vec3_t up, vec3_t right, byte red, byte green, byte blue, int alpha)
73 {
74         byte alphaub;
75         alphaub = bound(0, alpha, 255);
76         transpolybegin(R_GetTexture(frame->texture), 0, R_GetTexture(frame->fogtexture), currententity->render.effects & EF_ADDITIVE ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
77         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);
78         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);
79         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);
80         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);
81         transpolyend();
82 }
83
84 /*
85 =================
86 R_DrawSpriteModel
87 =================
88 */
89 void R_DrawSpriteModel (entity_t *e, frameblend_t *blend)
90 {
91         vec3_t          right, up, org, color, mins, maxs;
92         byte            colorub[4];
93         msprite_t       *psprite;
94
95         if (!blend[0].lerp)
96                 return;
97
98         VectorAdd (e->render.origin, e->render.model->mins, mins);
99         VectorAdd (e->render.origin, e->render.model->maxs, maxs);
100
101 //      if (R_CullBox (mins, maxs))
102 //              return;
103
104         c_sprites++;
105
106         psprite = Mod_Extradata(e->render.model);
107         //psprite = e->model->cache.data;
108
109         if (psprite->type == SPR_ORIENTED)
110         {       // bullet marks on walls
111                 vec3_t forward;
112                 AngleVectors (e->render.angles, forward, right, up);
113                 VectorSubtract(e->render.origin, vpn, org);
114                 if (DotProduct(r_origin, forward) < DotProduct(e->render.origin, forward))
115                         return; // backface
116         }
117         else
118         {       // normal sprite
119                 VectorCopy(vup, up);
120                 VectorCopy(vright, right);
121                 VectorCopy(e->render.origin, org);
122         }
123
124         if (e->render.scale != 1)
125         {
126                 VectorScale(up, e->render.scale, up);
127                 VectorScale(right, e->render.scale, right);
128         }
129
130         if (e->render.model->flags & EF_FULLBRIGHT || e->render.effects & EF_FULLBRIGHT)
131         {
132                 color[0] = e->render.colormod[0] * 255;
133                 color[1] = e->render.colormod[1] * 255;
134                 color[2] = e->render.colormod[2] * 255;
135         }
136         else
137                 R_CompleteLightPoint(color, e->render.origin, true);
138
139         colorub[0] = bound(0, color[0], 255);
140         colorub[1] = bound(0, color[1], 255);
141         colorub[2] = bound(0, color[2], 255);
142
143         // LordHavoc: interpolated sprite rendering
144         if (blend[0].lerp)
145                 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);
146         if (blend[1].lerp)
147                 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);
148         if (blend[2].lerp)
149                 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);
150         if (blend[3].lerp)
151                 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);
152 }
153