#include "BrushTool.h"


BrushTool::BrushTool(void)
{
}


BrushTool::~BrushTool(void)
{
}

void BrushTool::Setup(double l_radius, LONG l_segments)
{
	radius=radiusstart=l_radius; 
	segments = l_segments;
	constructCircle(0,0);
}
void BrushTool::SetRadius(double l_radius)
{
	radius = l_radius < 0.0001 ? 0.0001 : l_radius > kBrushRadiusMax ? kBrushRadiusMax : l_radius;
	constructCircle(0,0);
}
void BrushTool::Draw(ToolContext &in_ctxt,double scale,double px, double py, double pz,double angle, CVector3 axis)
{
	angle = RadiansToDegrees(angle);
	
	glEnable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);
	glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
	glLineWidth(.5f);
	glEnable(GL_LINE_SMOOTH);	
	glPushMatrix();
	
	CTransformation t;

	t.SetScalingFromValues(radius,radius,radius);
	t.SetPosX(px);
	t.SetPosY(py);
	t.SetPosZ(pz);
	t.SetRotationFromAxisAngle(axis,angle);
	
	glTranslated(px,py,pz);
	glRotated(angle, axis.GetX(),axis.GetY(),axis.GetZ() );
	//draw brush gradient
	glBegin(GL_TRIANGLE_FAN);	
		glColor4f(0.1f,0.1f,0.2f,0.0f);	
		CVector3 ptc(0.0,0.0, 0.0);
		glVertex3d( ptc[0],ptc[1],ptc[2] );
		glColor4f(0.1f,0.1f,0.2f,0.2f);	
		for (int i=0;i< circlepts.GetCount();i+=2){	
			CVector3 pt( circlepts[i]*scale, circlepts[i+1]*scale, 0.0);
			glVertex3d( pt[0],pt[1],pt[2] );
		}
	
		CVector3 pt( circlepts[0]*scale, circlepts[1]*scale, 0.0);
		glVertex3d( pt[0],pt[1],pt[2] );
	glEnd();
	
	//draw brush circle
	glColor4f(0.1f,0.0f,0.2f,0.5f);
	
	glBegin(GL_LINE_LOOP);

		for (int i=0;i <  circlepts.GetCount();i+=2)	{	
			CVector3 pt( circlepts[i]*scale, circlepts[i+1]*scale, 0.0);			
			glVertex3d( pt[0],pt[1],pt[2] );
		}
	glEnd();
	glPopMatrix();
	glDisable(GL_LINE_SMOOTH);
	glEnable(GL_DEPTH_TEST);
	glDisable(GL_BLEND);

}
void BrushTool::constructCircle ( double in_centerx, double in_centery) 
{
	circlepts.Clear();
	int i = 0;
	const double kRadianIncr = 6.28f/segments;
	for (i = 0; i < segments; i++)
	{
		double angleRadians = kRadianIncr*i;
		circlepts.Add( radius * cos(angleRadians) + in_centerx  );
		circlepts.Add( radius * sin(angleRadians) + in_centery  );
	}
}