Do not build VBOs on a dedicated server.
[xonotic/darkplaces.git] / r_sprites.c
index b41041b..5c621da 100644 (file)
@@ -8,6 +8,10 @@ extern cvar_t r_track_sprites;
 extern cvar_t r_track_sprites_flags;
 extern cvar_t r_track_sprites_scalew;
 extern cvar_t r_track_sprites_scaleh;
+extern cvar_t r_overheadsprites_perspective;
+extern cvar_t r_overheadsprites_pushback;
+extern cvar_t r_overheadsprites_scalex;
+extern cvar_t r_overheadsprites_scaley;
 
 #define TSF_ROTATE 1
 #define TSF_ROTATE_CONTINOUSLY 2
@@ -31,23 +35,23 @@ extern cvar_t r_track_sprites_scaleh;
 #define SIDE_BOTTOM 3
 #define SIDE_RIGHT 4
 
-void R_TrackSprite(const entity_render_t *ent, vec3_t origin, vec3_t left, vec3_t up, int *edge, float *dir_angle)
+static void R_TrackSprite(const entity_render_t *ent, vec3_t origin, vec3_t left, vec3_t up, int *edge, float *dir_angle)
 {
        float distance;
        vec3_t bCoord; // body coordinates of object
        unsigned int i;
 
        // temporarily abuse bCoord as the vector player->sprite-origin
-       VectorSubtract(origin, r_view.origin, bCoord);
+       VectorSubtract(origin, r_refdef.view.origin, bCoord);
        distance = VectorLength(bCoord);
 
        // Now get the bCoords :)
-       Matrix4x4_Transform(&r_view.inverse_matrix, origin, bCoord);
+       Matrix4x4_Transform(&r_refdef.view.inverse_matrix, origin, bCoord);
 
        *edge = 0; // FIXME::should assume edge == 0, which is correct currently
        for(i = 0; i < 4; ++i)
        {
-               if(PlaneDiff(origin, &r_view.frustum[i]) < -EPSILON)
+               if(PlaneDiff(origin, &r_refdef.view.frustum[i]) < -EPSILON)
                        break;
        }
 
@@ -58,8 +62,8 @@ void R_TrackSprite(const entity_render_t *ent, vec3_t origin, vec3_t left, vec3_
                float ax, ay;  // absolute coords, used for division
                // I divide x and y by the greater absolute value to get ranges -1.0 to +1.0
                
-               bCoord[2] *= r_view.frustum_x;
-               bCoord[1] *= r_view.frustum_y;
+               bCoord[2] *= r_refdef.view.frustum_x;
+               bCoord[1] *= r_refdef.view.frustum_y;
 
                //Con_Printf("%f %f %f\n", bCoord[0], bCoord[1], bCoord[2]);
                
@@ -107,16 +111,16 @@ void R_TrackSprite(const entity_render_t *ent, vec3_t origin, vec3_t left, vec3_
                // d = sqrt(r*r / (1 + fxa*fxa + gyb*gyb))
                // thus:
                distance = sqrt((distance*distance) / (1.0 +
-                                       r_view.frustum_x*r_view.frustum_x * x*x * ax*ax +
-                                       r_view.frustum_y*r_view.frustum_y * y*y * ay*ay));
+                                       r_refdef.view.frustum_x*r_refdef.view.frustum_x * x*x * ax*ax +
+                                       r_refdef.view.frustum_y*r_refdef.view.frustum_y * y*y * ay*ay));
                // ^ the one we want        ^ the one we have       ^ our factors
                
                // Place the sprite a few units ahead of the player
-               VectorCopy(r_view.origin, origin);
-               VectorMA(origin, distance, r_view.forward, origin);
+               VectorCopy(r_refdef.view.origin, origin);
+               VectorMA(origin, distance, r_refdef.view.forward, origin);
                // Move the sprite left / up the screeen height
-               VectorMA(origin, distance * r_view.frustum_x * x * ax, left, origin);
-               VectorMA(origin, distance * r_view.frustum_y * y * ay, up, origin);
+               VectorMA(origin, distance * r_refdef.view.frustum_x * x * ax, left, origin);
+               VectorMA(origin, distance * r_refdef.view.frustum_y * y * ay, up, origin);
 
                if(r_track_sprites_flags.integer & TSF_ROTATE_CONTINOUSLY)
                {
@@ -137,7 +141,7 @@ void R_TrackSprite(const entity_render_t *ent, vec3_t origin, vec3_t left, vec3_
        }
 }
 
-void R_RotateSprite(const mspriteframe_t *frame, vec3_t origin, vec3_t left, vec3_t up, int edge, float dir_angle)
+static void R_RotateSprite(const mspriteframe_t *frame, vec3_t origin, vec3_t left, vec3_t up, int edge, float dir_angle)
 {
        if(!(r_track_sprites_flags.integer & TSF_ROTATE))
        {
@@ -186,7 +190,7 @@ void R_RotateSprite(const mspriteframe_t *frame, vec3_t origin, vec3_t left, vec
                        angle += 180.0f;
 
                // Rotate around rotation_angle - frame_angle
-               // The axis SHOULD equal r_view.forward, but let's generalize this:
+               // The axis SHOULD equal r_refdef.view.forward, but let's generalize this:
                CrossProduct(up, left, axis);
                if(r_track_sprites_flags.integer & TSF_ROTATE_CONTINOUSLY)
                        Matrix4x4_CreateRotate(&rotm, dir_angle - angle, axis[0], axis[1], axis[2]);
@@ -199,26 +203,29 @@ void R_RotateSprite(const mspriteframe_t *frame, vec3_t origin, vec3_t left, vec
        }
 }
 
-void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
+static float spritetexcoord2f[4*2] = {0, 1, 0, 0, 1, 0, 1, 1};
+
+static void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
 {
        int i;
-       model_t *model = ent->model;
+       dp_model_t *model = ent->model;
        vec3_t left, up, org, mforward, mleft, mup, middle;
        float scale, dx, dy, hud_vs_screen;
        int edge = 0;
        float dir_angle = 0.0f;
+       float vertex3f[12];
 
        // nudge it toward the view to make sure it isn't in a wall
        Matrix4x4_ToVectors(&ent->matrix, mforward, mleft, mup, org);
-       VectorSubtract(org, r_view.forward, org);
+       VectorSubtract(org, r_refdef.view.forward, org);
        switch(model->sprite.sprnum_type)
        {
        case SPR_VP_PARALLEL_UPRIGHT:
                // flames and such
                // vertical beam sprite, faces view plane
-               scale = ent->scale / sqrt(r_view.forward[0]*r_view.forward[0]+r_view.forward[1]*r_view.forward[1]);
-               left[0] = -r_view.forward[1] * scale;
-               left[1] = r_view.forward[0] * scale;
+               scale = ent->scale / sqrt(r_refdef.view.forward[0]*r_refdef.view.forward[0]+r_refdef.view.forward[1]*r_refdef.view.forward[1]);
+               left[0] = -r_refdef.view.forward[1] * scale;
+               left[1] = r_refdef.view.forward[0] * scale;
                left[2] = 0;
                up[0] = 0;
                up[1] = 0;
@@ -227,9 +234,9 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
        case SPR_FACING_UPRIGHT:
                // flames and such
                // vertical beam sprite, faces viewer's origin (not the view plane)
-               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]));
-               left[0] = (org[1] - r_view.origin[1]) * scale;
-               left[1] = -(org[0] - r_view.origin[0]) * scale;
+               scale = ent->scale / sqrt((org[0] - r_refdef.view.origin[0])*(org[0] - r_refdef.view.origin[0])+(org[1] - r_refdef.view.origin[1])*(org[1] - r_refdef.view.origin[1]));
+               left[0] = (org[1] - r_refdef.view.origin[1]) * scale;
+               left[1] = -(org[0] - r_refdef.view.origin[0]) * scale;
                left[2] = 0;
                up[0] = 0;
                up[1] = 0;
@@ -241,8 +248,8 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
        case SPR_VP_PARALLEL:
                // normal sprite
                // faces view plane
-               VectorScale(r_view.left, ent->scale, left);
-               VectorScale(r_view.up, ent->scale, up);
+               VectorScale(r_refdef.view.left, ent->scale, left);
+               VectorScale(r_refdef.view.up, ent->scale, up);
                break;
        case SPR_LABEL_SCALE:
                // normal sprite
@@ -250,17 +257,20 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                // fixed HUD pixel size specified in sprite
                // honors scale
                // honors a global label scaling cvar
+       
+               if(r_fb.water.renderingscene) // labels are considered HUD items, and don't appear in reflections
+                       return;
 
                // See the R_TrackSprite definition for a reason for this copying
-               VectorCopy(r_view.left, left);
-               VectorCopy(r_view.up, up);
+               VectorCopy(r_refdef.view.left, left);
+               VectorCopy(r_refdef.view.up, up);
                // It has to be done before the calculations, because it moves the origin.
                if(r_track_sprites.integer)
                        R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
                
-               scale = 2 * ent->scale * (DotProduct(r_view.forward, org) - DotProduct(r_view.forward, r_view.origin)) * r_labelsprites_scale.value;
-               VectorScale(left, scale * r_view.frustum_x / vid_conwidth.integer, left); // 1px
-               VectorScale(up, scale * r_view.frustum_y / vid_conheight.integer, up); // 1px
+               scale = 2 * ent->scale * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin)) * r_labelsprites_scale.value;
+               VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer, left); // 1px
+               VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer, up); // 1px
                break;
        case SPR_LABEL:
                // normal sprite
@@ -271,20 +281,23 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                // honors a global label scaling cvar before the rounding
                // FIXME assumes that 1qu is 1 pixel in the sprite like in SPR32 format. Should not do that, but instead query the source image! This bug only applies to the roundtopixels case, though.
 
+               if(r_fb.water.renderingscene) // labels are considered HUD items, and don't appear in reflections
+                       return;
+
                // See the R_TrackSprite definition for a reason for this copying
-               VectorCopy(r_view.left, left);
-               VectorCopy(r_view.up, up);
+               VectorCopy(r_refdef.view.left, left);
+               VectorCopy(r_refdef.view.up, up);
                // It has to be done before the calculations, because it moves the origin.
                if(r_track_sprites.integer)
                        R_TrackSprite(ent, org, left, up, &edge, &dir_angle);
                
-               scale = 2 * (DotProduct(r_view.forward, org) - DotProduct(r_view.forward, r_view.origin));
+               scale = 2 * (DotProduct(r_refdef.view.forward, org) - DotProduct(r_refdef.view.forward, r_refdef.view.origin));
 
                if(r_labelsprites_roundtopixels.integer)
                {
                        hud_vs_screen = max(
-                               vid_conwidth.integer / (float) r_view.width,
-                               vid_conheight.integer / (float) r_view.height
+                               vid_conwidth.integer / (float) r_refdef.view.width,
+                               vid_conheight.integer / (float) r_refdef.view.height
                        ) / max(0.125, r_labelsprites_scale.value);
 
                        // snap to "good sizes"
@@ -302,29 +315,29 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                        if(hud_vs_screen)
                        {
                                // use screen pixels
-                               VectorScale(left, scale * r_view.frustum_x / (r_view.width * hud_vs_screen), left); // 1px
-                               VectorScale(up, scale * r_view.frustum_y / (r_view.height * hud_vs_screen), up); // 1px
+                               VectorScale(left, scale * r_refdef.view.frustum_x / (r_refdef.view.width * hud_vs_screen), left); // 1px
+                               VectorScale(up, scale * r_refdef.view.frustum_y / (r_refdef.view.height * hud_vs_screen), up); // 1px
                        }
                        else
                        {
                                // use HUD pixels
-                               VectorScale(left, scale * r_view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
-                               VectorScale(up, scale * r_view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
+                               VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
+                               VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
                        }
 
                        if(hud_vs_screen == 1)
                        {
-                               VectorMA(r_view.origin, scale, r_view.forward, middle); // center of screen in distance scale
-                               dx = 0.5 - fmod(r_view.width * 0.5 + (DotProduct(org, left) - DotProduct(middle, left)) / DotProduct(left, left) + 0.5, 1.0);
-                               dy = 0.5 - fmod(r_view.height * 0.5 + (DotProduct(org, up) - DotProduct(middle, up)) / DotProduct(up, up) + 0.5, 1.0);
+                               VectorMA(r_refdef.view.origin, scale, r_refdef.view.forward, middle); // center of screen in distance scale
+                               dx = 0.5 - fmod(r_refdef.view.width * 0.5 + (DotProduct(org, left) - DotProduct(middle, left)) / DotProduct(left, left) + 0.5, 1.0);
+                               dy = 0.5 - fmod(r_refdef.view.height * 0.5 + (DotProduct(org, up) - DotProduct(middle, up)) / DotProduct(up, up) + 0.5, 1.0);
                                VectorMAMAM(1, org, dx, left, dy, up, org);
                        }
                }
                else
                {
                        // use HUD pixels
-                       VectorScale(left, scale * r_view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
-                       VectorScale(up, scale * r_view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
+                       VectorScale(left, scale * r_refdef.view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
+                       VectorScale(up, scale * r_refdef.view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
                }
                break;
        case SPR_ORIENTED:
@@ -337,28 +350,57 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                // I have no idea what people would use this for...
                // oriented relative to view space
                // FIXME: test this and make sure it mimicks software
-               left[0] = mleft[0] * r_view.forward[0] + mleft[1] * r_view.left[0] + mleft[2] * r_view.up[0];
-               left[1] = mleft[0] * r_view.forward[1] + mleft[1] * r_view.left[1] + mleft[2] * r_view.up[1];
-               left[2] = mleft[0] * r_view.forward[2] + mleft[1] * r_view.left[2] + mleft[2] * r_view.up[2];
-               up[0] = mup[0] * r_view.forward[0] + mup[1] * r_view.left[0] + mup[2] * r_view.up[0];
-               up[1] = mup[0] * r_view.forward[1] + mup[1] * r_view.left[1] + mup[2] * r_view.up[1];
-               up[2] = mup[0] * r_view.forward[2] + mup[1] * r_view.left[2] + mup[2] * r_view.up[2];
+               left[0] = mleft[0] * r_refdef.view.forward[0] + mleft[1] * r_refdef.view.left[0] + mleft[2] * r_refdef.view.up[0];
+               left[1] = mleft[0] * r_refdef.view.forward[1] + mleft[1] * r_refdef.view.left[1] + mleft[2] * r_refdef.view.up[1];
+               left[2] = mleft[0] * r_refdef.view.forward[2] + mleft[1] * r_refdef.view.left[2] + mleft[2] * r_refdef.view.up[2];
+               up[0] = mup[0] * r_refdef.view.forward[0] + mup[1] * r_refdef.view.left[0] + mup[2] * r_refdef.view.up[0];
+               up[1] = mup[0] * r_refdef.view.forward[1] + mup[1] * r_refdef.view.left[1] + mup[2] * r_refdef.view.up[1];
+               up[2] = mup[0] * r_refdef.view.forward[2] + mup[1] * r_refdef.view.left[2] + mup[2] * r_refdef.view.up[2];
+               break;
+       case SPR_OVERHEAD:
+               // Overhead games sprites, have some special hacks to look good
+               VectorScale(r_refdef.view.left, ent->scale * r_overheadsprites_scalex.value, left);
+               VectorScale(r_refdef.view.up, ent->scale * r_overheadsprites_scaley.value, up);
+               VectorSubtract(org, r_refdef.view.origin, middle);
+               VectorNormalize(middle);
+               // offset and rotate
+               dir_angle = r_overheadsprites_perspective.value * (1 - fabs(DotProduct(middle, r_refdef.view.forward)));
+               up[2] = up[2] + dir_angle;
+               VectorNormalize(up);
+               VectorScale(up, ent->scale * r_overheadsprites_scaley.value, up);
+               // offset (move nearer to player, yz is camera plane)
+               org[0] = org[0] - middle[0]*r_overheadsprites_pushback.value;
+               org[1] = org[1] - middle[1]*r_overheadsprites_pushback.value;
+               org[2] = org[2] - middle[2]*r_overheadsprites_pushback.value;
+               // little perspective effect
+               up[2] = up[2] + dir_angle * 0.3;
+               // a bit of counter-camera rotation
+               up[0] = up[0] + r_refdef.view.forward[0] * 0.07;
+               up[1] = up[1] + r_refdef.view.forward[1] * 0.07;
+               up[2] = up[2] + r_refdef.view.forward[2] * 0.07;
                break;
        }
 
-       R_Mesh_Matrix(&identitymatrix);
-
        // LordHavoc: interpolated sprite rendering
-       for (i = 0;i < 4;i++)
+       for (i = 0;i < MAX_FRAMEBLENDS;i++)
        {
                if (ent->frameblend[i].lerp >= 0.01f)
                {
-                       mspriteframe_t *frame = model->sprite.sprdata_frames + ent->frameblend[i].frame;
-                       texture_t *texture = model->data_textures + ent->frameblend[i].frame;
-#if 0
-                       vec3_t o, l, u;
-#endif
-                       R_UpdateTextureInfo(ent, texture);
+                       mspriteframe_t *frame;
+                       texture_t *texture;
+                       RSurf_ActiveCustomEntity(&identitymatrix, &identitymatrix, ent->flags, 0, ent->colormod[0], ent->colormod[1], ent->colormod[2], ent->alpha * ent->frameblend[i].lerp, 4, vertex3f, spritetexcoord2f, NULL, NULL, NULL, NULL, 2, polygonelement3i, polygonelement3s, false, false);
+                       frame = model->sprite.sprdata_frames + ent->frameblend[i].subframe;
+                       texture = R_GetCurrentTexture(model->data_textures + ent->frameblend[i].subframe);
+               
+                       // sprites are fullbright by default, but if this one is not fullbright we
+                       // need to combine the lighting into ambient as sprite lighting is not
+                       // directional
+                       if (!(texture->currentmaterialflags & MATERIALFLAG_FULLBRIGHT))
+                       {
+                               VectorMAM(1.0f, texture->render_modellight_ambient, 0.25f, texture->render_modellight_diffuse, texture->render_modellight_ambient);
+                               VectorClear(texture->render_modellight_diffuse);
+                               VectorClear(texture->render_modellight_specular);
+                       }
 
                        // SPR_LABEL should not use depth test AT ALL
                        if(model->sprite.sprnum_type == SPR_LABEL || model->sprite.sprnum_type == SPR_LABEL_SCALE)
@@ -368,37 +410,30 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                        if(edge)
                        {
                                // FIXME:: save vectors/origin and re-rotate? necessary if the hotspot can change per frame
-#if 0
-                               VectorCopy(org, o);
-                               VectorCopy(left, l);
-                               VectorCopy(up, u);
-#endif
                                R_RotateSprite(frame, org, left, up, edge, dir_angle);
-#if 1
                                edge = 0;
-#endif
-                       }
-                       // FIXME: negate left and right in loader
-                       R_DrawSprite(texture->currentlayers[0].blendfunc1, texture->currentlayers[0].blendfunc2, texture->basetexture, 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);
-                       if(edge)
-                       {
-#if 0
-                               VectorCopy(o, org);
-                               VectorCopy(l, left);
-                               VectorCopy(u, up);
-#endif
                        }
+
+                       R_CalcSprite_Vertex3f(vertex3f, org, left, up, frame->left, frame->right, frame->down, frame->up);
+
+                       if (r_showspriteedges.integer)
+                               for (i = 0; i < 4; i++)
+                                       R_DebugLine(vertex3f + i * 3, vertex3f + ((i + 1) % 4) * 3);
+
+                       R_DrawCustomSurface_Texture(texture, &identitymatrix, texture->currentmaterialflags, 0, 4, 0, 2, false, false);
                }
        }
+
+       rsurface.entity = NULL;
 }
 
 void R_Model_Sprite_Draw(entity_render_t *ent)
 {
        vec3_t org;
-       if (ent->frameblend[0].frame < 0)
+       if (ent->frameblend[0].subframe < 0)
                return;
 
        Matrix4x4_OriginFromMatrix(&ent->matrix, org);
-       R_MeshQueue_AddTransparent(ent->effects & EF_NODEPTHTEST ? r_view.origin : org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);
+       R_MeshQueue_AddTransparent((ent->flags & RENDER_WORLDOBJECT) ? TRANSPARENTSORT_SKY : (ent->flags & RENDER_NODEPTHTEST) ? TRANSPARENTSORT_HUD : TRANSPARENTSORT_DISTANCE, org, R_Model_Sprite_Draw_TransparentCallback, ent, 0, rsurface.rtlight);
 }