]> icculus.org git repositories - divverent/darkplaces.git/blob - r_sprites.c
Tomaz's new water effect has been added and looks very nice
[divverent/darkplaces.git] / r_sprites.c
1
2 #include "quakedef.h"
3
4 static int R_SpriteSetup (const entity_render_t *ent, int type, float org[3], float left[3], float up[3])
5 {
6         float matrix1[3][3], matrix2[3][3], matrix3[3][3];
7
8         VectorCopy(ent->origin, org);
9         switch(type)
10         {
11         case SPR_VP_PARALLEL_UPRIGHT:
12                 // flames and such
13                 // vertical beam sprite, faces view plane
14                 VectorNegate(vpn, matrix3[0]);
15                 matrix3[0][2] = 0;
16                 VectorNormalizeFast(matrix3[0]);
17                 matrix3[1][0] = matrix3[0][1];
18                 matrix3[1][1] = -matrix3[0][0];
19                 matrix3[1][2] = 0;
20                 matrix3[2][0] = 0;
21                 matrix3[2][1] = 0;
22                 matrix3[2][2] = 1;
23                 break;
24         case SPR_FACING_UPRIGHT:
25                 // flames and such
26                 // vertical beam sprite, faces viewer's origin (not the view plane)
27                 VectorSubtract(ent->origin, r_origin, matrix3[0]);
28                 matrix3[0][2] = 0;
29                 VectorNormalizeFast(matrix3[0]);
30                 matrix3[1][0] = matrix3[0][1];
31                 matrix3[1][1] = -matrix3[0][0];
32                 matrix3[1][2] = 0;
33                 matrix3[2][0] = 0;
34                 matrix3[2][1] = 0;
35                 matrix3[2][2] = 1;
36                 break;
37         default:
38                 Con_Printf("R_SpriteSetup: unknown sprite type %i\n", type);
39                 // fall through to normal sprite
40         case SPR_VP_PARALLEL:
41                 // normal sprite
42                 // faces view plane
43                 VectorCopy(vpn, matrix3[0]);
44                 VectorNegate(vright, matrix3[1]);
45                 VectorCopy(vup, matrix3[2]);
46                 break;
47         case SPR_ORIENTED:
48                 // bullet marks on walls
49                 // ignores viewer entirely
50                 AngleVectorsFLU (ent->angles, matrix3[0], matrix3[1], matrix3[2]);
51                 // nudge it toward the view, so it will be infront of the wall
52                 VectorSubtract(org, vpn, org);
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                 AngleVectorsFLU (ent->angles, matrix1[0], matrix1[1], matrix1[2]);
59                 VectorCopy(vpn, matrix2[0]);
60                 VectorNegate(vright, matrix2[1]);
61                 VectorCopy(vup, matrix2[2]);
62                 R_ConcatRotations (matrix1[0], matrix2[0], matrix3[0]);
63                 break;
64         }
65
66         if (ent->scale != 1)
67         {
68                 VectorScale(matrix3[1], ent->scale, left);
69                 VectorScale(matrix3[2], ent->scale, up);
70         }
71         else
72         {
73                 VectorCopy(matrix3[1], left);
74                 VectorCopy(matrix3[2], up);
75         }
76         return false;
77 }
78
79 static void R_DrawSpriteImage (int additive, mspriteframe_t *frame, rtexture_t *texture, vec3_t origin, vec3_t up, vec3_t left, float red, float green, float blue, float alpha)
80 {
81         // FIXME: negate left and right in loader
82         R_DrawSprite(GL_SRC_ALPHA, additive ? GL_ONE : GL_ONE_MINUS_SRC_ALPHA, texture, false, origin, left, up, frame->left, frame->right, frame->down, frame->up, red, green, blue, alpha);
83 }
84
85 void R_DrawSpriteModelCallback(const void *calldata1, int calldata2)
86 {
87         const entity_render_t *ent = calldata1;
88         int i;
89         vec3_t left, up, org, color, diffusecolor, diffusenormal;
90         mspriteframe_t *frame;
91         vec3_t diff;
92         float fog, ifog;
93
94         if (R_SpriteSetup(ent, ent->model->sprite.sprnum_type, org, left, up))
95                 return;
96
97         R_Mesh_Matrix(&r_identitymatrix);
98
99         if ((ent->model->flags & EF_FULLBRIGHT) || (ent->effects & EF_FULLBRIGHT))
100                 color[0] = color[1] = color[2] = 1;
101         else
102         {
103                 R_CompleteLightPoint(color, diffusecolor, diffusenormal, ent->origin, true, NULL);
104                 VectorMA(color, 0.5f, diffusecolor, color);
105         }
106
107         if (fogenabled)
108         {
109                 VectorSubtract(ent->origin, r_origin, diff);
110                 fog = exp(fogdensity/DotProduct(diff,diff));
111                 if (fog > 1)
112                         fog = 1;
113         }
114         else
115                 fog = 0;
116         ifog = 1 - fog;
117
118         if (r_lerpsprites.integer)
119         {
120                 // LordHavoc: interpolated sprite rendering
121                 for (i = 0;i < 4;i++)
122                 {
123                         if (ent->frameblend[i].lerp >= 0.01f)
124                         {
125                                 frame = ent->model->sprite.sprdata_frames + ent->frameblend[i].frame;
126                                 R_DrawSpriteImage((ent->effects & EF_ADDITIVE) || (ent->model->flags & EF_ADDITIVE), frame, frame->texture, org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, ent->alpha * ent->frameblend[i].lerp);
127                                 if (fog * ent->frameblend[i].lerp >= 0.01f)
128                                         R_DrawSpriteImage(true, frame, frame->fogtexture, org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * ent->alpha * ent->frameblend[i].lerp);
129                         }
130                 }
131         }
132         else
133         {
134                 // LordHavoc: no interpolation
135                 frame = NULL;
136                 for (i = 0;i < 4 && ent->frameblend[i].lerp;i++)
137                         frame = ent->model->sprite.sprdata_frames + ent->frameblend[i].frame;
138
139                 R_DrawSpriteImage((ent->effects & EF_ADDITIVE) || (ent->model->flags & EF_ADDITIVE), frame, frame->texture, org, up, left, color[0] * ifog, color[1] * ifog, color[2] * ifog, ent->alpha);
140                 if (fog * ent->frameblend[i].lerp >= 0.01f)
141                         R_DrawSpriteImage(true, frame, frame->fogtexture, org, up, left, fogcolor[0],fogcolor[1],fogcolor[2], fog * ent->alpha);
142         }
143 }
144
145 void R_Model_Sprite_Draw(entity_render_t *ent)
146 {
147         if (ent->frameblend[0].frame < 0)
148                 return;
149
150         c_sprites++;
151
152         R_MeshQueue_AddTransparent(ent->origin, R_DrawSpriteModelCallback, ent, 0);
153 }
154