C:\Documents and Settings\keith\Desktop\flare.c


#define VectorLerp(v1,lerp,v2,c) ((c)[0] = (v1)[0] + (lerp) * ((v2)[0] - (v1)[0]), (c)[1] = (v1)[1] + (lerp) * ((v2)[1] - (v1)[1]), (c)[2] = (v1)[2] + (lerp) * ((v2)[2] - (v1)[2]))

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, 1.50, .55, 0},
    {4, 1.15, .02, 0},
    {4, 1.10, .27, 1},
    {3, 1.60, 0.5, 1},
    {3, 1.11, 1.1, 1},
    {5, 0.49, 1.9, 1},
    {1, 0.70, 0.8, 1},
    {6, 0.60, .43, 1},
    {7, 1.22, 1.8, 1},
    {8, 1.80, 2.9, 1}
    
};
vec4_t flarearray2[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}
    
};

static float vdot(float a[3], float b[3])
{
  return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}

static void vcross(float cross[3], const float v1[3], const float v2[3])
{
  float tmp[3];

  tmp[0] = (v1[1] * v2[2]) - (v1[2] * v2[1]);
  tmp[1] = (v1[2] * v2[0]) - (v1[0] * v2[2]);
  tmp[2] = (v1[0] * v2[1]) - (v1[1] * v2[0]);
  VectorCopy(tmp, cross);
}

void Flare_ProjectSource ( vec3_t point, vec3_t distance, vec3_t forward, vec3_t right, vec3_t result )
{
    result[0] = point[0] + forward[0] * distance[0] + right[0] * distance[1];
    result[1] = point[1] + forward[1] * distance[0] + right[1] * distance[1];
    result[2] = point[2] + forward[2] * distance[0] + right[2] * distance[1] + distance[2];
}

// debugging tool
void printorigins (vec3_t origin, vec3_t origin2, vec3_t origin3 )
{
    if (origin)
        Com_Printf("DEBUG: %f %f %f\n",origin[0],origin[1],origin[2]);
    if (origin2)
        Com_Printf("DEBUG: ^1%f %f %f\n",origin2[0],origin2[1],origin2[2]);
    if (origin3)
        Com_Printf("DEBUG: ^2%f %f %f\n",origin3[0],origin3[1],origin3[2]);
}

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);
        VectorCopy(tr.endpos,cg.bigflare.origin);
        *x1 = (int)x;
        *y1 = (int)y;
        if (cg.bigflare.s_scale < 1)
            cg.bigflare.s_scale +=(.05*cg.normalfps);
        return trace;
    }
    else if (cg.bigflare.s_scale > 0)
    {
        qboolean trace = CG_origintoCoord(tr.endpos,&x,&y);
        VectorCopy(tr.endpos,cg.bigflare.origin);
        *x1 = (int)x;
        *y1 = (int)y;
        if (cg.bigflare.s_scale >0)
            cg.bigflare.s_scale -=(.05*cg.normalfps);
        cg.bigflare.scale = 0;
        return trace;
    }

    return qfalse;
}

void SCR_DrawFlareArray2 (vec3_t origin, vec3_t flaredir, vec4_t color)
{
    vec3_t flareorigin, vieworigin;
    vec3_t forward,forward2,end;
    int i;
    int xo, yo;
    int t;
    float s;
    vec3_t viewdir,temp,center,light,lightdir,axis,dx,dy;
//  vec3_t ,sx,sy;
    float nearclip = 1.0;
    float dot;

    // at = vieworigin
    // from = straight ahead! = end
    // light=flareorigin

    VectorCopy(cg.refdef.vieworg, vieworigin);
    VectorCopy(origin,flareorigin); 

    AngleVectors(cg.refdef.viewangles,forward,NULL,NULL);
    VectorMA(vieworigin,256,forward,end);

    AngleVectors(flaredir,forward2,NULL,NULL);
    VectorMA(vieworigin,256,forward2,flareorigin);
    
//  VectorLerp(vieworigin,1,flareorigin,light);

    VectorSubtract(vieworigin,end,viewdir);
    VectorNormalize(viewdir);

    VectorScale(viewdir,nearclip,temp);
    VectorAdd(temp,end,center);

    VectorSubtract(flareorigin,end,lightdir);
    VectorNormalize(lightdir);

    dot = vdot(lightdir,viewdir);
    VectorScale(lightdir,nearclip/dot,temp);
    VectorAdd(end,lightdir,light);

    VectorSubtract(light,center,axis);
    VectorCopy(axis,dx);
    VectorNormalize(dx);
    CrossProduct(dx,viewdir,dy);

    for (i=0; i<14; i++)
    {
        t = flarearray[i][0];
//      VectorScale(dx,flarearray[i][1],sx);
//      VectorScale(dy,flarearray[i][1],sy);

//      VectorSubtract(sy,sx,flareorigin);

        CG_origintoCoord(axis,&xo,&yo);

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

        xo-= s/2;
        yo-= s/2;

//      printorigins (NULL,origin,NULL);

//      Com_Printf("DEBUG: %i %i %f\n",xo,yo,s);

        trap_R_DrawStretchPic (xo,yo, s, s,0, 0, 1, 1, color, CG_MediaShader (cgs.media.shaderSunFlares[t]));

    }
}
void SCR_DrawFlareArray4 (vec3_t origin, vec3_t flaredir, vec4_t color)
{
    vec3_t flareorigin, vieworigin,viewdir;
    vec3_t forward;
    vec3_t average;
    int i;
    int xo, yo;
    int t;
    float s,t1;
//  int j;
    float t2=testvar_x->value;

    VectorCopy(cg.refdef.vieworg, vieworigin);
    VectorCopy(origin,flareorigin); 
    VectorCopy(cg.refdef.viewangles,viewdir);

//  VectorNormalize(vecdif);

    for (i=0; i<14; i++)
    {
        t = flarearray[i][0];
        t1= flarearray[i][1];

//      VectorScale(average,t1*testvar_x->value,average);

//      VectorAvg(viewdir,flaredir,average);

        VectorLerp(viewdir,t2,flaredir,average);

//      for (j=0; j<3; j++)
//          average[j]=((1-t2) *viewdir[j])+(t2 * flaredir[j]);


//      average[0]=(viewdir[0]+flaredir[0])*(0.5f*testvar_x->value);
//      average[1]=(viewdir[1]+flaredir[1])*(0.5f*testvar_x->value);
//      average[2]=(viewdir[2]+flaredir[2])*(0.5f*testvar_x->value);

//      ((c)[0]=((a)[0]+(b)[0])*0.5f,(c)[1]=((a)[1]+(b)[1])*0.5f, (c)[2]=((a)[2]+(b)[2])*0.5f)

//      if (flarearray[i][3] > 0)
//          VectorAdd(vecdif,flaredir,flaredir);
//      else
//          VectorSubtract(vecdif,flaredir,flaredir);

//      VectorAdd(vecdif,flaredir,flaredir);

        AngleVectors(average,forward,NULL,NULL);

        VectorMA(vieworigin,256,forward,flareorigin);

        CG_origintoCoord(flareorigin,&xo,&yo);

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

        xo-= s/2;
        yo-= s/2;

        trap_R_DrawStretchPic (xo,yo, s, s,0, 0, 1, 1, color, CG_MediaShader (cgs.media.shaderSunFlares[t]));

    }
}