add Blub's r_track_sprites for SPR_LABEL*
authordivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 9 Nov 2007 13:57:58 +0000 (13:57 +0000)
committerdivverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Fri, 9 Nov 2007 13:57:58 +0000 (13:57 +0000)
git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@7671 d7cf8633-e32d-0410-b094-e92efae38249

client.h
gl_rmain.c
r_sprites.c
view.c

index 9261605..830b933 100644 (file)
--- a/client.h
+++ b/client.h
@@ -1404,7 +1404,7 @@ typedef struct r_view_s
        // (as long as R_View_Update is called)
 
        // eye position information
-       matrix4x4_t matrix;
+       matrix4x4_t matrix, inverse_matrix;
        vec3_t origin;
        vec3_t forward;
        vec3_t left;
index 034e541..5976c1c 100644 (file)
@@ -112,6 +112,10 @@ cvar_t gl_lightmaps = {0, "gl_lightmaps", "0", "draws only lightmaps, no texture
 
 cvar_t r_test = {0, "r_test", "0", "internal development use only, leave it alone (usually does nothing anyway)"};
 cvar_t r_batchmode = {0, "r_batchmode", "1", "selects method of rendering multiple surfaces with one driver call (values are 0, 1, 2, etc...)"};
+cvar_t r_track_sprites = {CVAR_SAVE, "r_track_sprites", "1", "track SPR_LABEL* sprites by putting them as indicator at the screen border to rotate to"};
+cvar_t r_track_sprites_flags = {CVAR_SAVE, "r_track_sprites_flags", "1", "1: Rotate sprites accodringly, 2: Make it a continuous rotation"};
+cvar_t r_track_sprites_scalew = {CVAR_SAVE, "r_track_sprites_scalew", "1", "width scaling of tracked sprites"};
+cvar_t r_track_sprites_scaleh = {CVAR_SAVE, "r_track_sprites_scaleh", "1", "height scaling of tracked sprites"};
 
 extern qboolean v_flipped_state;
 
@@ -1901,6 +1905,11 @@ void GL_Main_Init(void)
        if (gamemode == GAME_NEHAHRA || gamemode == GAME_TENEBRAE)
                Cvar_SetValue("r_fullbrights", 0);
        R_RegisterModule("GL_Main", gl_main_start, gl_main_shutdown, gl_main_newmap);
+
+       Cvar_RegisterVariable(&r_track_sprites);
+       Cvar_RegisterVariable(&r_track_sprites_flags);
+       Cvar_RegisterVariable(&r_track_sprites_scalew);
+       Cvar_RegisterVariable(&r_track_sprites_scaleh);
 }
 
 extern void R_Textures_Init(void);
@@ -2104,7 +2113,8 @@ static void R_View_UpdateEntityVisible (void)
                for (i = 0;i < r_refdef.numentities;i++)
                {
                        ent = r_refdef.entities[i];
-                       r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && !R_CullBox(ent->mins, ent->maxs) && ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.worldmodel, r_viewcache.world_leafvisible, ent->mins, ent->maxs));
+                       r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs)) && ((ent->effects & EF_NODEPTHTEST) || (ent->flags & RENDER_VIEWMODEL) || r_refdef.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.worldmodel, r_viewcache.world_leafvisible, ent->mins, ent->maxs));
+
                }
                if(r_cullentities_trace.integer)
                {
@@ -2127,7 +2137,7 @@ static void R_View_UpdateEntityVisible (void)
                for (i = 0;i < r_refdef.numentities;i++)
                {
                        ent = r_refdef.entities[i];
-                       r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && !R_CullBox(ent->mins, ent->maxs);
+                       r_viewcache.entityvisible[i] = !(ent->flags & renderimask) && ((ent->model->type == mod_sprite && (ent->model->sprite.sprnum_type == SPR_LABEL || ent->model->sprite.sprnum_type == SPR_LABEL_SCALE)) || !R_CullBox(ent->mins, ent->maxs));
                }
        }
 
@@ -2309,8 +2319,6 @@ static void R_View_SetFrustum(void)
        r_view.frustum[5].dist = m[15] + m[14];
 #endif
 
-
-
        if (r_view.useperspective)
        {
                slopex = 1.0 / r_view.frustum_x;
@@ -2320,6 +2328,14 @@ static void R_View_SetFrustum(void)
                VectorMA(r_view.forward, -slopey, r_view.up  , r_view.frustum[2].normal);
                VectorMA(r_view.forward,  slopey, r_view.up  , r_view.frustum[3].normal);
                VectorCopy(r_view.forward, r_view.frustum[4].normal);
+               
+               // Leaving those out was a mistake, those were in the old code, and they
+               // fix a reproducable bug in this one: frustum culling got fucked up when viewmatrix was an identity matrix
+               // I couldn't reproduce it after adding those normalizations. --blub
+               VectorNormalize(r_view.frustum[0].normal);
+               VectorNormalize(r_view.frustum[1].normal);
+               VectorNormalize(r_view.frustum[2].normal);
+               VectorNormalize(r_view.frustum[3].normal);
 
                // calculate frustum corners, which are used to calculate deformed frustum planes for shadow caster culling
                VectorMAMAMAM(1, r_view.origin, 1024, r_view.forward, -1024 * slopex, r_view.left, -1024 * slopey, r_view.up, r_view.frustumcorner[0]);
index c514b81..6bfe473 100644 (file)
@@ -4,6 +4,171 @@
 
 extern cvar_t r_labelsprites_scale;
 extern cvar_t r_labelsprites_roundtopixels;
+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;
+
+#define TSF_ROTATE 1
+#define TSF_ROTATE_CONTINOUSLY 2
+
+// use same epsilon as in sv_phys.c, it's not in any header, that's why i redefine it
+// MIN_EPSILON is for accurateness' sake :)
+#ifndef EPSILON
+# define EPSILON (1.0f / 32.0f)
+# define MIN_EPSILON 0.0001f
+#endif
+
+/* R_Track_Sprite
+   If the sprite is out of view, track it.
+   `origin`, `left` and `up` are changed by this function to achive a rotation around
+   the hotspot.
+   
+   --blub
+ */
+#define SIDE_TOP 1
+#define SIDE_LEFT 2
+#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)
+{
+       vec3_t bCoord; // body coordinates of object
+       unsigned int i;
+       Matrix4x4_Transform(&r_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)
+                       break;
+       }
+
+       // If it wasn't outside a plane, no tracking needed
+       if(i < 4)
+       {
+               float x, y;    // screen X and Y coordinates
+               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;
+
+               //Con_Printf("%f %f %f\n", bCoord[0], bCoord[1], bCoord[2]);
+               
+               ax = fabs(bCoord[1]);
+               ay = fabs(bCoord[2]);
+               // get the greater value and determine the screen edge it's on
+               if(ax < ay)
+               {
+                       ax = ay;
+                       // 180 or 0 degrees
+                       if(bCoord[2] < 0.0f)
+                               *edge = SIDE_BOTTOM;
+                       else
+                               *edge = SIDE_TOP;
+               } else {
+                       if(bCoord[1] < 0.0f)
+                               *edge = SIDE_RIGHT;
+                       else
+                               *edge = SIDE_LEFT;
+               }
+               // should I add || blah == -0.0f? since -0.0f != 0.0f binary speaking, the FPU shouldn't care though?
+               if(ax == 0.0f)
+                       ax = MIN_EPSILON;
+               // get the -1 to +1 range
+               x = bCoord[1] / ax;
+               y = bCoord[2] / ax;
+               
+               // Place the sprite a few units ahead of the player
+               VectorCopy(r_view.origin, origin);
+               VectorMA(origin, 50.0f, r_view.forward, origin);
+               // Move the sprite left / up the screeen height
+               ax = (1.0f / VectorLength(left));
+               ay = (1.0f / VectorLength(up));
+               VectorMA(origin, 50.0f * r_view.frustum_x * x * ax, left, origin);
+               VectorMA(origin, 50.0f * r_view.frustum_y * y * ay, up, origin);
+
+               if(r_track_sprites_flags.integer & TSF_ROTATE_CONTINOUSLY)
+               {
+                       // compute the rotation, negate y axis, we're pointing outwards
+                       *dir_angle = atan(-y / x) * 180.0f/M_PI;
+                       // we need the real, full angle
+                       if(x < 0.0f)
+                               *dir_angle += 180.0f;
+               }
+
+               left[0] *= r_track_sprites_scalew.value;
+               left[1] *= r_track_sprites_scalew.value;
+               left[2] *= r_track_sprites_scalew.value;
+
+               up[0] *= r_track_sprites_scaleh.value;
+               up[1] *= r_track_sprites_scaleh.value;
+               up[2] *= r_track_sprites_scaleh.value;
+       }
+}
+
+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))
+       {
+               // move down by its size if on top, otherwise it's invisible
+               if(edge == SIDE_TOP)
+                       VectorMA(origin, -(fabs(frame->up)+fabs(frame->down)), up, origin);
+       } else {
+               static float rotation_angles[5] =
+               {
+                       0, // no edge
+                       -90.0f, //top
+                       0.0f,   // left
+                       90.0f,  // bottom
+                       180.0f, // right
+               };
+               
+               // rotate around the hotspot according to which edge it's on
+               // since the hotspot == the origin, only rotate the vectors
+               matrix4x4_t rotm;
+               vec3_t axis;
+               vec3_t temp;
+               vec2_t dir;
+               float angle;
+
+               if(edge < 1 || edge > 4)
+                       return; // this usually means something went wrong somewhere, there's no way to get a wrong edge value currently
+               
+               dir[0] = frame->right + frame->left;
+               dir[1] = frame->down + frame->up;
+
+               // only rotate when the hotspot isn't the center though.
+               if(dir[0] < MIN_EPSILON && dir[1] < MIN_EPSILON)
+               {
+                       return;
+               }
+
+               // Now that we've kicked center-hotspotted sprites, rotate using the appropriate matrix :)
+
+               // determine the angle of a sprite, we could only do that once though and
+               // add a `qboolean initialized' to the mspriteframe_t struct... let's get the direction vector of it :)
+
+               angle = atan(dir[1] / dir[0]) * 180.0f/M_PI;
+
+               // we need the real, full angle
+               if(dir[0] < 0.0f)
+                       angle += 180.0f;
+
+               // Rotate around rotation_angle - frame_angle
+               // The axis SHOULD equal r_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]);
+               else
+                       Matrix4x4_CreateRotate(&rotm, rotation_angles[edge] - angle, axis[0], axis[1], axis[2]);
+               Matrix4x4_Transform(&rotm, up, temp);
+               VectorCopy(temp, up);
+               Matrix4x4_Transform(&rotm, left, temp);
+               VectorCopy(temp, left);
+       }
+}
 
 void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const rtlight_t *rtlight, int numsurfaces, int *surfacelist)
 {
@@ -11,6 +176,8 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
        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;
 
        // nudge it toward the view to make sure it isn't in a wall
        Matrix4x4_ToVectors(&ent->matrix, mforward, mleft, mup, org);
@@ -55,9 +222,16 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                // honors scale
                // honors a global label scaling cvar
 
+               // See the R_TrackSprite definition for a reason for this copying
+               VectorCopy(r_view.left, left);
+               VectorCopy(r_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(r_view.left, scale * r_view.frustum_x / vid_conwidth.integer, left); // 1px
-               VectorScale(r_view.up, scale * r_view.frustum_y / vid_conheight.integer, up); // 1px
+               VectorScale(left, scale * r_view.frustum_x / vid_conwidth.integer, left); // 1px
+               VectorScale(up, scale * r_view.frustum_y / vid_conheight.integer, up); // 1px
                break;
        case SPR_LABEL:
                // normal sprite
@@ -68,6 +242,13 @@ 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.
 
+               // See the R_TrackSprite definition for a reason for this copying
+               VectorCopy(r_view.left, left);
+               VectorCopy(r_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));
 
                if(r_labelsprites_roundtopixels.integer)
@@ -92,14 +273,14 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                        if(hud_vs_screen)
                        {
                                // use screen pixels
-                               VectorScale(r_view.left, scale * r_view.frustum_x / (r_view.width * hud_vs_screen), left); // 1px
-                               VectorScale(r_view.up, scale * r_view.frustum_y / (r_view.height * hud_vs_screen), up); // 1px
+                               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
                        }
                        else
                        {
                                // use HUD pixels
-                               VectorScale(r_view.left, scale * r_view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
-                               VectorScale(r_view.up, scale * r_view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
+                               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
                        }
 
                        if(hud_vs_screen == 1)
@@ -113,8 +294,8 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                else
                {
                        // use HUD pixels
-                       VectorScale(r_view.left, scale * r_view.frustum_x / vid_conwidth.integer * r_labelsprites_scale.value, left); // 1px
-                       VectorScale(r_view.up, scale * r_view.frustum_y / vid_conheight.integer * r_labelsprites_scale.value, up); // 1px
+                       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
                }
                break;
        case SPR_ORIENTED:
@@ -145,9 +326,33 @@ void R_Model_Sprite_Draw_TransparentCallback(const entity_render_t *ent, const r
                {
                        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);
+                       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
+                       }
                }
        }
 }
diff --git a/view.c b/view.c
index da8370c..a7a5c38 100644 (file)
--- a/view.c
+++ b/view.c
@@ -549,6 +549,7 @@ void V_CalcRefdef (void)
                        Matrix4x4_CreateFromQuakeEntity(&viewmodelmatrix, gunorg[0], gunorg[1], gunorg[2], viewangles[0], viewangles[1], viewangles[2], cl_viewmodel_scale.value);
                        VectorCopy(vieworg, cl.csqc_origin);
                        VectorCopy(viewangles, cl.csqc_angles);
+                       Matrix4x4_Invert_Simple(&r_view.inverse_matrix, &r_view.matrix);
                }
        }
 }