rewrote RecursiveHullCheck, no longer gets stuck on angle changes, and is generally...
[divverent/darkplaces.git] / r_sprites.c
1 #include "quakedef.h"
2
3 void R_ClipSpriteImage (msprite_t *psprite, vec3_t origin, vec3_t right, vec3_t up)
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)) + currentrenderentity->frameblend[0].frame;
10         fleft  = frame->left;
11         fdown  = frame->down;
12         fright = frame->right;
13         fup    = frame->up;
14         for (i = 1;i < 4 && currentrenderentity->frameblend[i].lerp;i++)
15         {
16                 frame = ((mspriteframe_t *)(psprite->ofs_frames + (int) psprite)) + currentrenderentity->frameblend[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, currentrenderentity, NULL, NULL);
27 }
28
29 int R_SpriteSetup (int type, float org[3], float right[3], float up[3])
30 {
31         float matrix1[3][3], matrix2[3][3], matrix3[3][3];
32
33         VectorCopy(currentrenderentity->origin, org);
34         switch(type)
35         {
36         case SPR_VP_PARALLEL_UPRIGHT:
37                 // flames and such
38                 // vertical beam sprite, faces view plane
39                 VectorNegate(vpn, matrix3[0]);
40                 matrix3[0][2] = 0;
41                 VectorNormalizeFast(matrix3[0]);
42                 VectorVectors(matrix3[0], matrix3[1], matrix3[2]);
43                 break;
44         case SPR_FACING_UPRIGHT:
45                 // flames and such
46                 // vertical beam sprite, faces viewer's origin (not the view plane)
47                 VectorSubtract(r_origin, currentrenderentity->origin, matrix3[0]);
48                 matrix3[0][2] = 0;
49                 VectorNormalizeFast(matrix3[0]);
50                 VectorVectors(matrix3[0], matrix3[1], matrix3[2]);
51                 break;
52         default:
53                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", type);
54                 // fall through to normal sprite
55         case SPR_VP_PARALLEL:
56                 // normal sprite
57                 // faces view plane
58                 VectorCopy(vpn, matrix3[0]);
59                 VectorCopy(vright, matrix3[1]);
60                 VectorCopy(vup, matrix3[2]);
61                 break;
62         case SPR_ORIENTED:
63                 // bullet marks on walls
64                 // ignores viewer entirely
65                 AngleVectors (currentrenderentity->angles, matrix3[0], matrix3[1], matrix3[2]);
66                 // nudge it toward the view, so it will be infront of the wall
67                 VectorSubtract(org, vpn, org);
68                 break;
69         case SPR_VP_PARALLEL_ORIENTED:
70                 // I have no idea what people would use this for
71                 // oriented relative to view space
72                 // FIXME: test this and make sure it mimicks software
73                 AngleVectors (currentrenderentity->angles, matrix1[0], matrix1[1], matrix1[2]);
74                 VectorCopy(vpn, matrix2[0]);
75                 VectorCopy(vright, matrix2[1]);
76                 VectorCopy(vup, matrix2[2]);
77                 R_ConcatRotations (matrix1, matrix2, matrix3);
78                 break;
79         }
80
81         // don't draw if view origin is behind it
82         if (DotProduct(org, matrix3[0]) < (DotProduct(r_origin, matrix3[0]) - 1.0f))
83                 return true;
84
85         if (currentrenderentity->scale != 1)
86         {
87                 VectorScale(matrix3[1], currentrenderentity->scale, matrix3[1]);
88                 VectorScale(matrix3[2], currentrenderentity->scale, matrix3[2]);
89         }
90
91         VectorCopy(matrix3[1], right);
92         VectorCopy(matrix3[2], up);
93         return false;
94 }
95
96 void R_ClipSprite (void)
97 {
98         vec3_t org, right, up;
99         msprite_t *psprite;
100
101         if (currentrenderentity->frameblend[0].frame < 0)
102                 return;
103
104         psprite = Mod_Extradata(currentrenderentity->model);
105         if (R_SpriteSetup(psprite->type, org, right, up))
106                 return;
107
108         // LordHavoc: interpolated sprite rendering
109         R_ClipSpriteImage(psprite, org, right, up);
110 }
111
112 void GL_DrawSpriteImage (mspriteframe_t *frame, vec3_t origin, vec3_t up, vec3_t right, byte red, byte green, byte blue, int alpha)
113 {
114         byte alphaub;
115         alphaub = bound(0, alpha, 255);
116         transpolybegin(R_GetTexture(frame->texture), 0, R_GetTexture(frame->fogtexture), ((currentrenderentity->effects & EF_ADDITIVE) || (currentrenderentity->model->flags & EF_ADDITIVE)) ? TPOLYTYPE_ADD : TPOLYTYPE_ALPHA);
117         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);
118         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);
119         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);
120         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);
121         transpolyend();
122 }
123
124 /*
125 =================
126 R_DrawSpriteModel
127 =================
128 */
129 void R_DrawSpriteModel ()
130 {
131         int                     i;
132         vec3_t          right, up, org, color;
133         byte            colorub[4];
134         msprite_t       *psprite;
135
136         if (currentrenderentity->frameblend[0].frame < 0)
137                 return;
138
139         psprite = Mod_Extradata(currentrenderentity->model);
140         if (R_SpriteSetup(psprite->type, org, right, up))
141                 return;
142
143         c_sprites++;
144
145         if ((currentrenderentity->model->flags & EF_FULLBRIGHT) || (currentrenderentity->effects & EF_FULLBRIGHT))
146         {
147                 color[0] = currentrenderentity->colormod[0] * 255;
148                 color[1] = currentrenderentity->colormod[1] * 255;
149                 color[2] = currentrenderentity->colormod[2] * 255;
150         }
151         else
152                 R_CompleteLightPoint(color, currentrenderentity->origin, true, NULL);
153
154         colorub[0] = bound(0, color[0], 255);
155         colorub[1] = bound(0, color[1], 255);
156         colorub[2] = bound(0, color[2], 255);
157
158         // LordHavoc: interpolated sprite rendering
159         for (i = 0;i < 4;i++)
160                 if (currentrenderentity->frameblend[i].lerp)
161                         GL_DrawSpriteImage(((mspriteframe_t *)(psprite->ofs_frames + (int) psprite)) + currentrenderentity->frameblend[i].frame, org, up, right, colorub[0],colorub[1],colorub[2], currentrenderentity->alpha*255*currentrenderentity->frameblend[i].lerp);
162 }
163