/*
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.

*/

// r_beam.c -- beam rendering
// moved from r_main.c

#include "r_local.h"


/*
=======================
R_RenderBeam
=======================
*/
void R_RenderBeam (vec3_t start, vec3_t end, float size, float red, float green, float blue, float alpha)
{
	float	len;
//	int		oldrender=0, rendertype = 0;
		
	vec3_t		up		= {vup[0]    * 0.75f, vup[1]    * 0.75f, vup[2]    * 0.75f};
	vec3_t		right	= {vright[0] * 0.75f, vright[1] * 0.75f, vright[2] * 0.75f};
	vec3_t		coord[4]; //, shadelight;
	
	vec3_t ang_up, ang_right, vdelta; //, origin;

	VectorAdd      (up, right, coord[0]);
	VectorSubtract (right, up, coord[1]);
	VectorNegate   (coord[0], coord[2]);
	VectorNegate   (coord[1], coord[3]);

//	qglEnable( GL_TEXTURE_2D ); //Knightmare added
	GL_TexEnv( GL_MODULATE );
	qglDepthMask   (false);
//	qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
//	qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	qglBlendFunc   (GL_SRC_ALPHA, GL_ONE); //Knightmare- this fixes the black background
	qglEnable( GL_BLEND );
	GL_ShadeModel (GL_SMOOTH);
	GL_Bind(r_particlebeam->texnum);
	qglColor4ub( (byte)red, (byte)green, (byte)blue, (byte)((alpha>=254)?254:alpha) );

	VectorSubtract(start, end, ang_up);
	len = VectorNormalize(ang_up);

	VectorSubtract(r_newrefdef.vieworg, start, vdelta);
	CrossProduct(ang_up, vdelta, ang_right);
	if(!VectorCompare(ang_right, vec3_origin))
		VectorNormalize(ang_right);

	VectorScale (ang_right, size*2, ang_right); //Knightmare- a little narrower, please

	VectorAdd(start, ang_right, coord[0]);
	VectorAdd(end, ang_right, coord[1]);
	VectorSubtract(end, ang_right, coord[2]);
	VectorSubtract(start, ang_right, coord[3]);

	qglPushMatrix();
	{
		qglBegin (GL_QUADS);
		{
			qglTexCoord2f (0, 1);
			qglVertex3fv  (coord[0]);
			qglTexCoord2f (0, 0);
			qglVertex3fv  (coord[1]);
			qglTexCoord2f (1, 0); 
			qglVertex3fv  (coord[2]);
			qglTexCoord2f (1, 1);
			qglVertex3fv  (coord[3]);
		}
		qglEnd ();
	}
	qglPopMatrix ();


	qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	GL_TexEnv( GL_MODULATE );
	qglDepthMask (true);
	qglDisable ( GL_BLEND );
	qglColor4f   (1,1,1,1);
}


/*
=======================
R_DrawBeam
=======================
*/
void R_DrawBeam( entity_t *e )
{
	qboolean fog_on = false;

	// Knightmare- no fog on lasers
	if (qglIsEnabled(GL_FOG)) // check if fog is enabled
	{
		fog_on = true;
		qglDisable(GL_FOG); // if so, disable it
	}

	R_RenderBeam( e->origin, e->oldorigin, e->frame, 
		( d_8to24table[e->skinnum & 0xFF] ) & 0xFF,
		( d_8to24table[e->skinnum & 0xFF] >> 8 ) & 0xFF,
		( d_8to24table[e->skinnum & 0xFF] >> 16 ) & 0xFF,
		e->alpha*255 );

	// re-enable fog if it was on
	if (fog_on)
		qglEnable(GL_FOG);

	return;
}
