D:\cygwin\wf_fusion_src_r8\CVSROOT\cgame\cg_screen.c

/*
==============================================================================
The Weapons Factory - 
  
Modified code by Keith Pase

Copyright (C) 1997-2003 Weapons Factory Software
Copyright (C) 1997-2001 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

  $Id: cg_screen.c,v 1.13 2004/03/09 10:32:32 keith Exp $

  $Log: cg_screen.c,v $
  Revision 1.13  2004/03/09 10:32:32  keith
  Sun Flares (start)


  master status bar, crosshairs, hud, etc

==============================================================================
*/


//=======================================================
qboolean CG_origintoCoordFloat(vec3_t origin, float *x, float *y)
{
    int x1 = 320;
    int y1 = 240;
    vec3_t  v, t;
    vec3_t  forward, right, up;
    float x3,y3;

    AngleVectors (cg.refdef.viewangles, forward, right, up);

    VectorSubtract (origin, cg.refdef.vieworg, v);

    t[0] = DotProduct(v,right);
    t[1] = DotProduct(v,up);
    t[2] = DotProduct(v,forward);       

    if(t[2] < 0.01f)
        return qfalse;

    x3 = (float)(x1) / t[2] * (96.0f/cg.refdef.fov_x);
    y3 = (float)(y1) / t[2] * (102.0f/cg.refdef.fov_y);

    *x = (float)(x1) + x3 * t[0];
    *y = (float)(y1) - y3 * t[1];

    return qtrue;
}

qboolean CG_origintoCoord( vec3_t origin, int *x, int *y )
{
    float   x1, y1;
    qboolean xy = CG_origintoCoordFloat( origin, &x1, &y1 );
    *x = (int)x1;
    *y = (int)y1;

    return xy;
}

qboolean CG_TraceFlare (vec3_t start, vec3_t axis, int *x1, int *y1)
{
    trace_t     tr;
    vec3_t      end;
    vec3_t      empty = {0,0,0};
    int         x,y;

    VectorMA ( start, 8192, axis, end );

    CG_VisualTrace ( &tr, start, empty, empty, end, 0, MASK_SHOT, SURF_GLASS );

    if (tr.surfFlags & SURF_SKY)
    {
        qboolean trace = CG_origintoCoord(tr.endpos,&x,&y);
        *x1 = (int)x;
        *y1 = (int)y;
        if (cg.bigflare.s_scale < 1)
            cg.bigflare.s_scale +=.05;
        return trace;
    }
    else if (cg.bigflare.s_scale > 0)
    {
        qboolean trace = CG_origintoCoord(tr.endpos,&x,&y);
        *x1 = (int)x;
        *y1 = (int)y;
        if (cg.bigflare.s_scale >0)
            cg.bigflare.s_scale -=.05;
        cg.bigflare.scale = 0;
        return trace;
    }

    return qfalse;
}

vec4_t flarearray[14] =
{   
    {0, 0.30, .7 , 0},
    {1, 0.52, .45, 0},
    {1, 0.57, .78, 0},
    {2, 1.00, 1.5, 0},
    {3, 4.00, .55, 0},
    {4,10.00, .02, 0},
    {4, 7.00, .27, 1},
    {3, 3.60, 0.5, 1},
    {3, 4.00, 1.1, 1},
    {5, 3.49, 1.9, 1},
    {1, 2.70, 0.8, 1},
    {6, 2.60, .43, 1},
    {7, 2.10, 1.8, 1},
    {8, 1.90, 2.9, 1}
    
};
/*
=================
SCR_DrawFlares
=================
*/
void SCR_DrawFlares (void)
{
    int pitch,yaw;
    int x2,y2;
    vec3_t  flaredir = {0,0,0};
    vec3_t  forward;
    vec3_t  center = {0,0,0};

    pitch   =   (360 - cg.bigflare.pitch) * -1;
    yaw     =   (360 - cg.bigflare.yaw) * -1;
    flaredir[0] = pitch;
    flaredir[1] = yaw;

    AngleVectors(flaredir,forward,NULL,NULL);

    if (CG_TraceFlare (cg.refdef.vieworg,forward,&x2,&y2))
    {
        int y3 = cg.refdef.height;
        int x3 = cg.refdef.width;
        int s,c;
        int fsize = 256;
        float falloff = .98;
        float xscale = 1;
        vec4_t color = {1,1,1,1};

        if (x2 > (float)(x3 * falloff))
        {
            float x4 = x3*falloff;

            xscale = 1-((x2-x4)/(x3-x4));
            if (xscale < 0)
                xscale = 0;
        }

        if (x2 < x3-(x3*falloff))
        {
            float x4 = x3-(x3*falloff);

            xscale = (x2/x4);
            if (xscale < 0)
                xscale = 0;
        }
        if (y2 > (float)(y3 * falloff))
        {
            float y4 = y3*falloff;

            xscale = 1-((y2-y4)/(y3-y4));
            if (xscale < 0)
                xscale = 0;
        }

        if (y2 < y3-(y3*falloff))
        {
            float y4 = y3-(y3*falloff);

            xscale = (y2/y4);
            if (xscale < 0)
                xscale = 0;
        }

        if (cg.bigflare.scale < xscale)
            cg.bigflare.scale +=.05;
        if (cg.bigflare.scale > xscale)
            cg.bigflare.scale -=.05;

        if (cg.bigflare.s_scale)
        {
            s= (int)(fsize * cg.bigflare.s_scale);
            for (c=0; c<4; c++)
                color[c] = cg.bigflare.s_scale;
        }
        else
        {
            s= (int)(fsize * cg.bigflare.scale); //todo fsize
            for (c=0; c<4; c++)
                color[c] = cg.bigflare.s_scale;
        }
        
        if (cg.bigflare.type > 1)
        {
            int i;
            int x4, y4;
            int s2,t;

            for (i=0; i<15; i++)
            {
                t = flarearray[i][0];

                // NEGATE AND MOVE FACTOR HELPME!
                if (flarearray[i][3])
                {
                    x4 = x2 * (flarearray[i][1] *-1);
                    y4 = y2 * (flarearray[i][1] *-1);
                }
                else
                {
                    x4 = x2 * flarearray[i][1];
                    y4 = y2 * flarearray[i][1];
                }

                s2 = 100 * flarearray[i][2];

                trap_R_DrawStretchPic (x4,y4, s2, s2,0, 0, 1, 1, color, CG_MediaShader (cgs.media.shaderSunFlares[t]));
            }
        }

        x2 -= s/2;
        y2 -= s/2;

        trap_R_DrawStretchPic (x2,y2, s, s,0, 0, 1, 1, color, CG_MediaShader (cgs.media.shaderBigSun));

    }

}