/*
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_sprite.c -- sprite rendering
// moved from r_main.c

#include "r_local.h"

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

	SPRITE MODELS

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

/*
=================
R_DrawSpriteModel
=================
*/
void R_DrawSpriteModel (entity_t *e)
{
	float alpha = 1.0F;
	vec3_t	point;
	dsprframe_t	*frame;
	float		*up, *right;
	dsprite_t		*psprite;

	// don't even bother culling, because it's just a single
	// polygon without a surface cache

	psprite = (dsprite_t *)currentmodel->extradata;

	e->frame %= psprite->numframes;

	frame = &psprite->frames[e->frame];

	if (!frame) return;

	// normal sprite
	up = vup;
	right = vright;

	if ( e->flags & RF_TRANSLUCENT )
		alpha = e->alpha;
		
	if (!currentmodel->skins[0][e->frame])
		return;

	SetVertexOverbrights(true); // added

	// Knightmare added- Psychospaz's additive transparency
	if ((currententity->flags & RF_TRANS_ADDITIVE) && (alpha != 1.0F))
	{ 
		qglEnable( GL_BLEND );
		GL_TexEnv( GL_MODULATE );
		qglDisable( GL_ALPHA_TEST );
		qglDepthMask (false);
		qglBlendFunc   (GL_SRC_ALPHA, GL_ONE);	

		GL_Bind(currentmodel->skins[0][e->frame]->texnum);

		qglColor4ub(255, 255, 255, (byte)(alpha*254));
	}
	else
	{
		GL_TexEnv( GL_MODULATE );
		if ( alpha == 1.0F )
		{
			qglEnable (GL_ALPHA_TEST);
			qglDepthMask (true);
		}
		else {
			GL_BlendFunction (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
			qglDepthMask (false);
			qglEnable( GL_BLEND );
			qglDisable( GL_ALPHA_TEST );
		}
		GL_Bind(currentmodel->skins[0][e->frame]->texnum);
		qglColor4f( 1, 1, 1, alpha );
	}
	// end Knightmare

	qglBegin (GL_QUADS);

	qglTexCoord2f (0, 1);
	VectorMA (e->origin, -frame->origin_y, up, point);
	VectorMA (point, -frame->origin_x, right, point);
	qglVertex3fv (point);

	qglTexCoord2f (0, 0);
	VectorMA (e->origin, frame->height - frame->origin_y, up, point);
	VectorMA (point, -frame->origin_x, right, point);
	qglVertex3fv (point);

	qglTexCoord2f (1, 0);
	VectorMA (e->origin, frame->height - frame->origin_y, up, point);
	VectorMA (point, frame->width - frame->origin_x, right, point);
	qglVertex3fv (point);

	qglTexCoord2f (1, 1);
	VectorMA (e->origin, -frame->origin_y, up, point);
	VectorMA (point, frame->width - frame->origin_x, right, point);
	qglVertex3fv (point);
	
	qglEnd ();

	qglBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	GL_TexEnv( GL_REPLACE );
	qglDepthMask (true);
	qglDisable (GL_ALPHA_TEST);
	qglDisable ( GL_BLEND );
	qglColor4f   (1,1,1,1);

	SetVertexOverbrights (false); // added
}
