void ZoaDebugFunctions::calculateTriMeshNormals( ci::TriMesh &mesh )
{
	const std::vector<ci::Vec3f>& vertices = mesh.getVertices();
	const std::vector<uint32_t>& indices = mesh.getIndices();

	// remove all current normals
	std::vector<ci::Vec3f>& normals = mesh.getNormals();
	normals.reserve( mesh.getNumVertices() );
	normals.clear();

	// set the normal for each vertex to (0, 0, 0)
	for(size_t i=0; i < mesh.getNumVertices(); ++i)
		normals.push_back( ci::Vec3f::zero() );

	// Average out the normal for each vertex at an index
	for(size_t i=0; i< mesh.getNumTriangles(); ++i)
	{
		ci::Vec3f v0 = vertices[ indices[i * 3] ];
		ci::Vec3f v1 = vertices[ indices[i * 3 + 1] ];
		ci::Vec3f v2 = vertices[ indices[i * 3 + 2] ];

		// calculate normal and normalize it, so each of the normals equally contributes to the final result
		ci::Vec3f e0 = v2 - v0;
		ci::Vec3f e1 = v2 - v1;
		ci::Vec3f n = e0.cross(e1).normalized();

		// add the normal to the final result, so we get an average of the normals of each triangle
		normals[ indices[i * 3] ] += n;
		normals[ indices[i * 3 + 1] ] += n;
		normals[ indices[i * 3 + 2] ] += n;
	}

	// the normals are probably not normalized by now, so make sure their lengths will be 1.0 as expected
	for(size_t i=0;i< normals.size();++i) {
		normals[i].normalize();
	}
}
void DelaunayMeshMaker::draw()
{
	gl::clear();

	gl::pushMatrices();

	gl::pushModelView();
		gl::color( ColorA::white() );

		if( mMesh.getNumTriangles() > 0 ){
			if( mApplyPhongShading )
				mPhongShader.bind();

			gl::enableDepthRead();
			gl::enableDepthWrite();

			gl::disableAlphaBlending();

			if( mDrawWireframe )
				gl::enableWireframe();

			gl::draw( mVboMesh );

			if( mDrawWireframe )
				gl::disableWireframe();

			if( mApplyPhongShading )
				mPhongShader.unbind();
		}

		if( mAlpha > 0.0f ){
			gl::disableDepthRead();
			gl::disableDepthWrite();

			gl::enableAlphaBlending();

			gl::color( ColorA( 1,1,1, mAlpha ) );
			gl::draw( mSurface );
		}
	gl::popModelView();

	gl::popMatrices();

	if( mTesselator->isRunning() ){
		gl::disableDepthRead();
		gl::disableDepthWrite();

		gl::enableAlphaBlending();

		gl::color( ColorA(0.0f, 0.0f, 0.0f, 0.5f ) );
		gl::drawSolidRect( Rectf( 0, 0, app::getWindowWidth(), app::getWindowHeight() ) );

		gl::pushModelView();
			gl::translate( app::getWindowSize()/2 );
			double time = getElapsedSeconds();
			double fraction = time - (int) time;
			int numFractions = 12;

			for(int i=0;i<numFractions;++i) {
				float a = (float) (fraction + i * (1.0f / (float)numFractions));
				a -= (int) a;

				gl::pushModelView();
				gl::rotate( i * ( -360.0f/(float)numFractions ) );
				gl::color( ColorA(1,1,1,1-a) );
				gl::drawSolidRect( Rectf(-6.0f, -44.0f, +6.0f, -31.0f) );
				gl::popModelView();
			}
		gl::popModelView();
	}

	mParams.draw();
}