OSG_USING_NAMESPACE

Vec4f cToV (Color4f & col)
{
    return Vec4f(col.red(), col.green(), col.blue(), col.alpha());
}
void Graphics3DExtrude::drawRect(const Pnt2f& TopLeft, const Pnt2f& BottomRight, const Color4f& Color, const Real32& Opacity) const
{
   Real32 Alpha(Color.alpha() * Opacity * getOpacity());
	if(Alpha < 1.0 || getEnablePolygonAntiAliasing())
	{
		//Setup the Blending equations properly
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glEnable(GL_BLEND);
	}

    glColor4f(Color.red(), Color.green(), Color.blue(), Alpha );
	glBegin(GL_QUADS);
	   //Front
	   glNormal3f(0.0,0.0,1.0);
	   glVertex3f(TopLeft.x(), TopLeft.y(), 0.0);
	   glVertex3f(TopLeft.x(), BottomRight.y(), 0.0);
	   glVertex3f(BottomRight.x(), BottomRight.y(), 0.0);
	   glVertex3f(BottomRight.x(), TopLeft.y(), 0.0);

	   //Back
	   glNormal3f(0.0,0.0,-1.0);
	   glVertex3f(TopLeft.x(), TopLeft.y(), getExtrudeLength());
	   glVertex3f(BottomRight.x(), TopLeft.y(), getExtrudeLength());
	   glVertex3f(BottomRight.x(), BottomRight.y(), getExtrudeLength());
	   glVertex3f(TopLeft.x(), BottomRight.y(), getExtrudeLength());
	   
	   //Top
	   glNormal3f(0.0,-1.0,0.0);
	   glVertex3f(TopLeft.x(), TopLeft.y(), 0.0);
	   glVertex3f(BottomRight.x(), TopLeft.y(), 0.0);
	   glVertex3f(BottomRight.x(), TopLeft.y(), getExtrudeLength());
	   glVertex3f(TopLeft.x(), TopLeft.y(), getExtrudeLength());

	   //Bottom
	   glNormal3f(0.0,1.0,0.0);
	   glVertex3f(TopLeft.x(), BottomRight.y(), 0.0);
	   glVertex3f(TopLeft.x(), BottomRight.y(), getExtrudeLength());
	   glVertex3f(BottomRight.x(), BottomRight.y(), getExtrudeLength());
	   glVertex3f(BottomRight.x(), BottomRight.y(), 0.0);
	   
	   //Right
	   glNormal3f(1.0,0.0,0.0);
	   glVertex3f(BottomRight.x(), TopLeft.y(), 0.0);
	   glVertex3f(BottomRight.x(), BottomRight.y(), 0.0);
	   glVertex3f(BottomRight.x(), BottomRight.y(), getExtrudeLength());
	   glVertex3f(BottomRight.x(), TopLeft.y(), getExtrudeLength());

	   //Left
	   glNormal3f(-1.0,0.0,0.0);
	   glVertex3f(TopLeft.x(), TopLeft.y(), 0.0);
	   glVertex3f(TopLeft.x(), TopLeft.y(), getExtrudeLength());
	   glVertex3f(TopLeft.x(), BottomRight.y(), getExtrudeLength());
	   glVertex3f(TopLeft.x(), BottomRight.y(), 0.0);

	glEnd();
	
	if(Alpha < 1.0 || getEnablePolygonAntiAliasing())
	{
		glDisable(GL_BLEND);
	}
}
Action::ResultE LineParticleSystemDrawer::draw(DrawEnv *pEnv,
                                               ParticleSystemUnrecPtr System,
                                               const MFUInt32& Sort)
{
 	UInt32 NumParticles(System->getNumParticles());

	bool areEndpointsFadeSame(getEndPointFading().x() == getEndPointFading().y());
	Color4f Color;

	if(NumParticles != 0)
	{

		bool SeparateColors(System->getNumColors() > 1);
		bool SeparateSizes(System->getNumSizes() > 1);
		bool SeparateNormals(System->getNumNormals() > 1);

		glBegin(GL_LINES);
			//Colors
			if(!SeparateColors && areEndpointsFadeSame)
			{
				Color = System->getColor(0);
				glColor4f(Color.red(), Color.green(), Color.blue(), Color.alpha() * getEndPointFading().x());
			}
			//Sizes
			if(!SeparateSizes)
			{
				//glColor4fv(System->getColor(0).getValuesRGBA());
			}
			//Normals
			if(!SeparateNormals)
			{
				glNormal3fv(System->getNormal(0).getValues());
			}
			for(UInt32 i(0) ; i<NumParticles ; ++i)
			{
				//Start Color
				if(SeparateColors)
				{
					Color = System->getColor(i);
					glColor4f(Color.red(), Color.green(), Color.blue(), Color.alpha() * getEndPointFading().x());
				}
				else if(!SeparateColors && !areEndpointsFadeSame)
				{
					Color = System->getColor(0);
					glColor4f(Color.red(), Color.green(), Color.blue(), Color.alpha() * getEndPointFading().x());
				}
				//Sizes
				if(SeparateSizes)
				{
					//glColor4fv(System->getColor(i).getValuesRGBA());
				}
				//Normals
				if(SeparateNormals)
				{
					glNormal3fv(System->getNormal(i).getValues());
				}
				//Positions
				glVertex3fv(System->getPosition(i).getValues());
				
				//End Color
				if(SeparateColors && !areEndpointsFadeSame)
				{
					Color = System->getColor(i);
					glColor4f(Color.red(), Color.green(), Color.blue(), Color.alpha() * getEndPointFading().y());
				}
				else if(!SeparateColors && !areEndpointsFadeSame)
				{
					Color = System->getColor(0);
					glColor4f(Color.red(), Color.green(), Color.blue(), Color.alpha() * getEndPointFading().y());
				}
				glVertex3fv(getLineEndpoint(System, i).getValues());
			}
		glEnd();
	}

    return Action::Continue;
}