void FullColorBrushTool::leftButtonDown(const TPointD &pos, const TMouseEvent &e)
{
	m_brushPos = m_mousePos = pos;

	Viewer *viewer = getViewer();
	if (!viewer)
		return;

	TRasterImageP ri = (TRasterImageP)getImage(true);
	if (!ri)
		ri = (TRasterImageP)touchImage();

	if (!ri)
		return;

	TRasterP ras = ri->getRaster();
	TDimension dim = ras->getSize();

	if (!(m_workRaster && m_backUpRas))
		setWorkAndBackupImages();

	m_workRaster->lock();

	double maxThick = m_thickness.getValue().second;
	double thickness = m_pressure.getValue() ? computeThickness(e.m_pressure, m_thickness) : maxThick;
	double opacity = (m_pressure.getValue() ? computeThickness(e.m_pressure, m_opacity) : m_opacity.getValue().second) * 0.01;
	TPointD rasCenter = TPointD(dim.lx * 0.5, dim.ly * 0.5);
	TThickPoint point(pos + rasCenter, thickness);
	TPointD halfThick(maxThick * 0.5, maxThick * 0.5);
	TRectD invalidateRect(pos - halfThick, pos + halfThick);

	m_points.clear();
	m_points.push_back(point);

	m_tileSet = new TTileSetFullColor(ras->getSize());
	m_tileSaver = new TTileSaverFullColor(ras, m_tileSet);
	double hardness = m_hardness.getValue() * 0.01;

	m_brush = new BluredBrush(m_workRaster, maxThick, m_brushPad, hardness == 1.0);
	m_strokeRect = m_brush->getBoundFromPoints(m_points);
	updateWorkAndBackupRasters(m_strokeRect);
	m_tileSaver->save(m_strokeRect);
	m_brush->addPoint(point, opacity);
	m_brush->updateDrawing(ras, m_backUpRas, m_currentColor, m_strokeRect, m_opacity.getValue().second * 0.01);
	m_oldOpacity = opacity;
	m_lastRect = m_strokeRect;

	invalidate(invalidateRect.enlarge(2));
}
void Astrolabe::buildTheMesh()
{
	if(profile.size() > 1)
	{
		//create our mesh
		numVertices = 16 * profile.size() + 8;//top, bottom, front, back, lofts & caps
		vertices.resize( numVertices );
		normals.resize( numVertices );
		
		//add the vertices
		ofVec3f halfThick(0,thickness * .5 , 0);
		
		for(int i=0; i<profile.size()-1; i++)
		{
			ofVec3f v0 = profile[i] * innerRadius;
			ofVec3f v1 = profile[i] * outerRadius;
			ofVec3f v2 = profile[i+1] * outerRadius;
			ofVec3f v3 = profile[i+1] * innerRadius;
			
			vertices[i*16 + 0] =  v0 + halfThick;
			vertices[i*16 + 1] =  v1 + halfThick;
			vertices[i*16 + 2] =  v2 + halfThick;
			vertices[i*16 + 3] =  v3 + halfThick;
			
			vertices[i*16 + 4] = v0 - halfThick;
			vertices[i*16 + 5] = v3 - halfThick;
			vertices[i*16 + 6] = v2 - halfThick;
			vertices[i*16 + 7] = v1 - halfThick;
			
			vertices[i*16 + 8] = v1 + halfThick;
			vertices[i*16 + 9] = v1 - halfThick;
			vertices[i*16 + 10] = v2 - halfThick;
			vertices[i*16 + 11] = v2 + halfThick;
			
			vertices[i*16 + 12] = v3 + halfThick;
			vertices[i*16 + 13] = v3 - halfThick;
			vertices[i*16 + 14] = v0 - halfThick;
			vertices[i*16 + 15] = v0 + halfThick;
			
			
			//normals
			fill (normals.begin()+(i*16), normals.begin()+(i*16+3), ofVec3f(0,1,0));
			fill (normals.begin()+(i*16+4), normals.begin()+(i*16+7), ofVec3f(0,-1,0));
			
			normals[i*16 + 8] = profile[i];
			normals[i*16 + 9] = profile[i];
			normals[i*16 + 10] = profile[i+1];
			normals[i*16 + 11] = profile[i+1];
			
			normals[i*16 + 12] = -profile[i+1];
			normals[i*16 + 13] = -profile[i+1];
			normals[i*16 + 14] = -profile[i];
			normals[i*16 + 15] = -profile[i];
			
		}
		
		//cap the ends
		//front
		ofVec3f v0 = profile.front() * innerRadius + halfThick;
		ofVec3f v1 = profile.front() * outerRadius + halfThick;
		ofVec3f v2 = profile.front() * outerRadius - halfThick;
		ofVec3f v3 = profile.front() * innerRadius - halfThick;
		
		*(vertices.end()-8) = v0;
		*(vertices.end()-7) = v3;
		*(vertices.end()-6) = v2;
		*(vertices.end()-5) = v1;
		
		fill (normals.end()-8, normals.end()-5, normalFromThreePoints(v0,v2,v1) );
		
		//back
		v0 = profile.back() * innerRadius + halfThick;
		v1 = profile.back() * outerRadius + halfThick;
		v2 = profile.back() * outerRadius - halfThick;
		v3 = profile.back() * innerRadius - halfThick;
		
		*(vertices.end()-4) = v0;
		*(vertices.end()-3) = v1;
		*(vertices.end()-2) = v2;
		*(vertices.end()-1) = v3;
		
		fill (normals.end()-4, normals.end()-1, normalFromThreePoints(v0,v1,v2) );
		
		//build the vbo
		vbo.clear();
		vbo.setVertexData( &vertices[0], vertices.size(), GL_DYNAMIC_DRAW );
		vbo.setNormalData( &normals[0], normals.size(), GL_DYNAMIC_DRAW );
		
		vertices.clear();
		normals.clear();
	}
}