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

*/
#include "sw_local.h"

vec3_t r_pright, r_pup, r_ppn;

void R_DrawParticle( particle_t *pparticle ) {
	vec3_t	local, transformed;
	float	zi;
	byte	*pdest;
	short	*pz, *dpz;
	color_t      color;
	int		i, izi, pix, count, u, v;
	byte	*dst;
	byte *oneMinusAlphaTable, alphabyte;

	/*
	** transform the particle
	*/
	VectorSubtract (pparticle->origin, r_origin, local);

	transformed[0] = DotProduct(local, r_pright);
	transformed[1] = DotProduct(local, r_pup);
	transformed[2] = DotProduct(local, r_ppn);		

	if (transformed[2] < PARTICLE_Z_CLIP)
		return;

	/*
	** project the point
	*/
	// FIXME: preadjust xcenter and ycenter
	zi = 1.0 / transformed[2];
	u = (int)(xcenter + zi * transformed[0] + 0.5);
	v = (int)(ycenter - zi * transformed[1] + 0.5);

	if ((v > d_vrectbottom_particle) || 
		(u > d_vrectright_particle) ||
		(v < d_vrecty) ||
		(u < d_vrectx))
	{
		return;
	}

	/*
	** compute addresses of zbuffer, framebuffer, and 
	** compute the Z-buffer reference value.
	*/
	pz = d_pzbuffer + (d_zwidth * v) + u;
	pdest = d_viewbuffer + d_scantable[v] + ( u << VID_SHIFT );
	izi = (int)(zi * 0x8000);

	/*
	** determine the screen area covered by the particle,
	** which also means clamping to a min and max
	*/
	pix = izi >> d_pix_shift;
	if (pix < d_pix_min)
		pix = d_pix_min;
	else if (pix > d_pix_max)
		pix = d_pix_max;

	if( pparticle->color != 255 ) {
		*( uint32 * )color = d_8to24table[pparticle->color & 255];
	} else {
		*( uint32 * )color = *( uint32 * )pparticle->rgb;
	}
	VectorScale( color, pparticle->alpha, color );
	alphabyte = 255 * ( 1.0f - pparticle->alpha );
	oneMinusAlphaTable = r_alphaTable[alphabyte];

	/*
	** render the appropriate pixels
	*/
	i = pix;
	do {
		count = pix;
		dst = pdest;
		dpz = pz;
		do {
			if( *dpz <= izi ) {
			//	*dpz = izi;
				dst[0] = oneMinusAlphaTable[dst[0]] + color[2];
				dst[1] = oneMinusAlphaTable[dst[1]] + color[1];
				dst[2] = oneMinusAlphaTable[dst[2]] + color[0];
			}
			dst += 4; dpz++;
		} while( --count );
		pz += d_zwidth;
		pdest += r_screenwidth;
	} while( --i );

}


/*
** R_DrawParticles
**
** Responsible for drawing all of the particles in the particle list
** throughout the world.  Doesn't care if we're using the C path or
** if we're using the asm path, it simply assigns a function pointer
** and goes.
*/
void R_DrawParticles( void ) {
	particle_t *p;
	int         i;

	VectorScale( vright, xscaleshrink, r_pright );
	VectorScale( vup, yscaleshrink, r_pup );
	VectorCopy( vpn, r_ppn );

	for( i = 0, p = r_newrefdef.particles; i < r_newrefdef.num_particles; i++, p++ ) {
		R_DrawParticle( p );
	}

}

