// gl_effects.c

#include "gl_local.h"

/*
=============
V_AddBlend
=============
*/
void V_AddBlend (float r, float g, float b, float a, float *v_blend)
{
	float	a2, a3;

	if (a <= 0)
		return;
	a2 = v_blend[3] + (1-v_blend[3])*a;	// new total alpha
	a3 = v_blend[3]/a2;		// fraction of color from old

	v_blend[0] = v_blend[0]*a3 + r*(1-a3);
	v_blend[1] = v_blend[1]*a3 + g*(1-a3);
	v_blend[2] = v_blend[2]*a3 + b*(1-a3);
	v_blend[3] = a2;
}

/*
=============
R_EnableFlareParms
=============
*/
void R_EnableFlareParms (void)
{
	qglDisable (GL_FOG);

	qglDepthMask (0);
	qglDisable (GL_TEXTURE_2D);
	qglShadeModel (GL_SMOOTH);
	qglEnable (GL_BLEND);
	qglBlendFunc (GL_ONE, GL_ONE);
}

/*
=============
R_DisableFlareParms
=============
*/
void R_DisableFlareParms (void)
{
	qglColor3f (1,1,1);
	qglDisable (GL_BLEND);
	qglEnable (GL_TEXTURE_2D);
	qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	qglDepthMask (1);

	if (r_newrefdef.foginfo.active)
		qglEnable (GL_FOG);
}

/*
=============
R_DrawFlare
=============
*/
void R_DrawFlare (flare_t *flare)
{
	int		i, j;
	float	a;
	vec3_t	v;
	float	rad;
	vec3_t	v_blend;
	vec3_t	forward, right, up;

	AngleVectors( flare->angles, forward, right, up );

	rad = flare->intensity * 0.2;

	VectorSubtract (flare->origin, r_origin, v);

	// FIXME?
	if (VectorLength (v) < rad)
	{	// view is inside the dlight
		V_AddBlend (flare->color[0], flare->color[1], flare->color[2], flare->intensity * 0.0003, v_blend);
		return;
	}

	qglBegin (GL_TRIANGLE_FAN);
	qglColor4f (1, 1, 1, 1);					// fade from white
	for (i=0 ; i<3 ; i++)
		v[i] = flare->origin[i] - vpn[i];//*rad;
	qglVertex3fv (v);

	for (i = 32; i>=0 ; i--)
	{
		a = i/32.0 * M_PI*2;
		for (j=0 ; j<3 ; j++)
		{

			qglColor4f ((float) (10 * 0.004), 
						(float) (0 * 0.004), 
						(float) (100 * 0.004), 0);	// fade to color
			v[j] = flare->origin[j] + vright[j]*cos(a)*rad + vup[j]*sin(a)*rad;
		}

		qglVertex3fv (v);
	}
	qglEnd ();
}

/*
=============
R_RenderFlares
=============
*/
void R_RenderFlares (void)
{
	int		i;
	flare_t	*f;

	R_EnableFlareParms ();

	f = r_newrefdef.flares;
	for (i=0 ; i<r_newrefdef.num_flares ; i++, f++)
		R_DrawFlare (f);

	R_DisableFlareParms ();
}

/*
=============
R_DrawAlphaBeam
=============
*/
void R_DrawAlphaBeam (void)
{
}

