Exemple #1
0
/*! Get the tesselated shape as arrays of vertices and indices
	The geometry function extracts the triangle information from 
	the tesselated contours.  It fills an array of vertices and an 
	array of indices with the tesselation data, which can be used 
	with a Mesh to draw it to screen.
	
	<luacode>
	local vertex = Array()
	local index = Array()

	tess:geometry(vertex, index)

	local mesh = Mesh(ctx)
	mesh:vertex(vertex)
	mesh:index(index)
	mesh.primitive = GL.TRIANGLES
	</luacode>
	
	@param vertices The vertex array
	@param indices The indices array
	
	@name Tesselator:geometry
	
	@see Array
	@see opengl.Mesh
*/
int lua_tess_geometry(lua_State *L) {
	Tesselator *s = Glue<Tesselator>::checkto(L, 1);
	AlloArray *vertex = lua_array_checkto(L, 2);
	AlloArray *index = lua_array_checkto(L, 3);
	s->geometry(vertex, index);
	return 0;
}
Exemple #2
0
static void createGraphicsBase(const std::vector<std::vector<Point2f> >& polygons, GraphicsBase& result, bool evenodd, bool texcoords = false, float stx = 0, float sty = 0, const Matrix* matrix = NULL)
{
	Tesselator tes;
	tes.tesselate(polygons, evenodd);

	int start = result.vertices.size();
	int ntriangles = tes.triangles.size() / 2;

	for (std::size_t i = 0; i < ntriangles; ++i)
	{
		float x = tes.triangles[i * 2 + 0];
		float y = tes.triangles[i * 2 + 1];

		result.vertices.push_back(Point2f(x, y));
		if (texcoords)
		{
			float tx, ty;
			matrix->transformPoint(x, y, &tx, &ty);
			result.texcoords.push_back(Point2f(tx * stx, ty * sty));
		}

		result.indices.push_back(start + i);
	}
	result.vertices.Update();
	result.indices.Update();
	if (texcoords)
		result.texcoords.Update();
}
Exemple #3
0
/*! Set a contour vertex
	@param pos The position
	@name Tesselator:Vertex
*/
int lua_tess_Vertex(lua_State *L) {
	Tesselator *s = Glue<Tesselator>::checkto(L, 1);
	vec2 v;
	if(lua::to_vec<double>(L, 2, 2, &(v.x))) {
		s->Vertex(v);
	}
	else {
		luaL_error(L, "Tesselator.vertex: invalid arguments\n");
	}
	return 0;
}
	void Polygon::tesselate( AppearanceManager &appearanceManager, const TVec3d& normal )
	{
		_indices.clear();

		if ( !_exteriorRing || _exteriorRing->size() < 3 )
		{ 
			mergeRings( appearanceManager );
			return;
		}

		TexCoords texCoords;
		bool t = appearanceManager.getTexCoords( _exteriorRing->getId(), texCoords );
		_exteriorRing->finish( t ? &texCoords : &_texCoords );
		if ( t ) std::copy( texCoords.begin(), texCoords.end(), std::back_inserter( _texCoords ) );

		for ( unsigned int i = 0; i < _interiorRings.size(); i++ ) {
			TexCoords texCoords;
			bool t = appearanceManager.getTexCoords( _interiorRings[i]->getId(), texCoords );
			_interiorRings[i]->finish( t ? &texCoords : &_texCoords );
			if ( t ) std::copy( texCoords.begin(), texCoords.end(), std::back_inserter( _texCoords ) );
		}

		// Compute the total number of vertices
		unsigned int vsize = _exteriorRing->size();
		for ( unsigned int i = 0; i < _interiorRings.size(); i++ )
			vsize += _interiorRings[i]->size();

		Tesselator* tess = appearanceManager.getTesselator();
		tess->init( vsize, normal );

		tess->addContour( _exteriorRing->getVertices(), texCoords );

		for ( unsigned int i = 0; i < _interiorRings.size(); i++ )
			tess->addContour( _interiorRings[i]->getVertices(), texCoords ); 

		tess->compute();
		_vertices.reserve( tess->getVertices().size() );
		std::copy( tess->getVertices().begin(), tess->getVertices().end(), std::back_inserter( _vertices ) );

		unsigned int indicesSize = tess->getIndices().size();
		if ( indicesSize > 0 ) 
		{
			_indices.resize( indicesSize );
			memcpy( &_indices[0], &tess->getIndices()[0], indicesSize * sizeof(unsigned int) );
		}
		clearRings();
	}
Exemple #5
0
void  Output(double* OutputCoords[3], int* OutputIndices[3])
{
	if (tess.getIndices().size() == 0 ||OutputIndices == NULL || OutputIndices == NULL)
	{
		return;
	}
	std::vector<TVec3d>::iterator itr = tess.getVertices().begin();
	for (int i = 0;itr != tess.getVertices().end(); itr++, i++)
	{
		OutputCoords[i][0] = itr->x;
		OutputCoords[i][1] = itr->y;
		OutputCoords[i][2] = itr->z;
#ifdef _DEBUG
		//test
		std::cout<<OutputCoords[i][0]<<OutputCoords[i][1]<<OutputCoords[i][2]<<std::endl;
#endif
	}
#ifdef _DEBUG
	//test
	std::cout<<"good5"<<std::endl;
#endif
	std::vector<unsigned int>::iterator itr2 = tess.getIndices().begin();
	for (int i = 0; itr2 != tess.getIndices().end(); itr2 += 3, i++)
	{
		OutputIndices[i][0] = *itr2;
		OutputIndices[i][1] = *(itr2 + 1);
		OutputIndices[i][2] = *(itr2 + 2);
#ifdef _DEBUG	
		//test
		std::cout<<OutputIndices[i][0]<<OutputIndices[i][1]<<OutputIndices[i][2]<<std::endl;
#endif
	}
	//very important
	tess.getIndices().clear();
	tess.getVertices().clear();
	tess.getTexCoords().clear();
	std::vector<Vec3*>::iterator itr3 = IntRingCoords.begin();
	for (;itr3 != IntRingCoords.end(); itr3++)
	{
		delete *itr3;
		*itr3 = NULL;
	}
	IntRingCoords.clear();
	IntRingCoordsNum.clear();
#ifdef _DEBUG
	//test
	std::cout<<"good6"<<std::endl;	
#endif
}
Exemple #6
0
	static void CombineCallback_s(GLdouble coords[3], GLdouble *data[4], GLfloat weight[4], GLdouble **dataOut, void* polygon_data)   
	{   
		Tesselator* that = static_cast<Tesselator*>(polygon_data);
		that->CombineCallback(coords, data, weight, dataOut);
	}   
Exemple #7
0
	static void VertexCallback_s(GLvoid *vertex, void* polygon_data)   
	{   
		Tesselator* that = static_cast<Tesselator*>(polygon_data);
		that->VertexCallback(vertex);
	} 
Exemple #8
0
	static void EndCallback_s(void* polygon_data)   
	{   
		Tesselator* that = static_cast<Tesselator*>(polygon_data);
		that->EndCallback();
	}
Exemple #9
0
	static void BeginCallback_s(GLenum type, void* polygon_data)   
	{   
		Tesselator* that = static_cast<Tesselator*>(polygon_data);
		that->BeginCallback(type);
	} 
Exemple #10
0
bool  Tessellation(int& OutputVertNum, int& OutputIndiceNum)
{
	if (ExtRingCoords == NULL)
	{
		return false;
	}
#ifdef _DEBUG
	//test
	std::cout<<"EXT"<<std::endl;
	std::cout<<ExtRingVertNum<<std::endl;
	for (int i = 0; i < ExtRingVertNum; i++)
	{
		std::cout<<ExtRingCoords[i][0]<<ExtRingCoords[i][1]<<ExtRingCoords[i][2]<<std::endl;
	}
	std::cout<<"INT"<<std::endl;
	std::cout<<IntRingCoordsNum.size()<<std::endl;
	std::vector<int>::iterator itr = IntRingCoordsNum.begin();
	std::vector<Vec3*>::iterator itr2 = IntRingCoords.begin();
	for (;itr != IntRingCoordsNum.end(); itr++, itr2++)
	{
		std::cout<<*itr<<std::endl;
		for (int i = 0 ; i <*itr; i ++ )
		{
			std::cout<<(*itr2)[i][0]<<(*itr2)[i][1]<<(*itr2)[i][2]<<std::endl;
		}
	}
	std::cout<<"good1"<<std::endl;
#endif

	int IntRingNum = IntRingCoords.size();
	//calculate the normal
	Vec3 pNorm;
	pNorm[0] = 0.0;
	pNorm[1] = 0.0;
	pNorm[2] = 0.0;

	Vec3 lVert;
	lVert[0] = ExtRingCoords[ExtRingVertNum - 1][0];
	lVert[1] = ExtRingCoords[ExtRingVertNum - 1][1];
	lVert[2] = ExtRingCoords[ExtRingVertNum - 1][2];

	for (int i = 0 ; i < ExtRingVertNum; i++)
	{
		Vec3 pStep;
		pStep[0] = (lVert[2] + ExtRingCoords[i][2]) * (lVert[1] - ExtRingCoords[i][1]);
		pStep[1] = (lVert[0] + ExtRingCoords[i][0]) * (lVert[2] - ExtRingCoords[i][2]);
		pStep[2] = (lVert[1] + ExtRingCoords[i][1]) * (lVert[0] - ExtRingCoords[i][0]);

		pNorm[0] = pNorm[0] + pStep[0];
		pNorm[1] = pNorm[1] + pStep[1];
		pNorm[2] = pNorm[2] + pStep[2];

		lVert[0] = ExtRingCoords[i][0];
		lVert[1] = ExtRingCoords[i][1];
		lVert[2] = ExtRingCoords[i][2];
	}
#ifdef _DEBUG
	//test
	std::cout<<"good2"<<std::endl;
#endif
	//do tessellation
	//verts number
	int VertNum = ExtRingVertNum;
	for (int i = 0; i < IntRingCoordsNum.size(); i++)
		VertNum += IntRingCoordsNum[i];
	//
#ifdef _DEBUG
	//test
	std::cout<<"good2.1"<<std::endl;
	std::cout<<"VertNum: "<<VertNum<<std::endl;
	std::cout<<pNorm[0]<<pNorm[1]<<pNorm[2]<<std::endl;
#endif
	tess.init(VertNum, TVec3d(pNorm[0], pNorm[1], pNorm[2]));
	std::vector<TVec3d> ring;
	std::vector<TVec2f> tag;//no use
	//add ExtierRing
	for (int i = 0; i < ExtRingVertNum; i++)
	{
		TVec3d vert(ExtRingCoords[i][0], ExtRingCoords[i][1], ExtRingCoords[i][2]);
		ring.push_back(vert);
	}
#ifdef _DEBUG
	//test
	std::cout<<"good2.2"<<std::endl;
	std::cout<<ring.size()<<std::endl;
#endif
	tess.addContour(ring, tag);
	ring.clear();
	//add InteriorRings
	for (int i = 0; i < IntRingNum; i++)
	{	
		for (int j = 0 ; j < IntRingCoordsNum[i]; j++)
		{
			TVec3d vert(IntRingCoords[i][j][0], IntRingCoords[i][j][1], IntRingCoords[i][j][2]);
			ring.push_back(vert);
		}
		tess.addContour(ring, tag);
		ring.clear();
	}
#ifdef _DEBUG
	//test
	std::cout<<"good2.3"<<std::endl;
#endif
	tess.compute();
	//return results
	if (tess.getIndices().size() == 0)
	{
		return false;
	}
#ifdef _DEBUG
	//test
	std::cout<<"good3"<<std::endl;
#endif
	//return coordsSAT
	OutputVertNum = tess.getVertices().size();
	OutputIndiceNum = tess.getIndices().size()/3;

	//
	{
		delete[] ExtRingCoords;
		ExtRingCoords = NULL;
		std::vector<Vec3*>::iterator itr = IntRingCoords.begin();
		for (; itr != IntRingCoords.end(); itr++)
		{
			delete[] *itr;
			*itr = NULL;
		}
	}
#ifdef _DEBUG
	//test
	std::cout<<"good4"<<std::endl;
#endif
	return true;
}
Exemple #11
0
/*! End a contour within a polygon
	
	@name Tesselator:EndContour
*/
int lua_tess_EndContour(lua_State *L) {
	Tesselator *s = Glue<Tesselator>::checkto(L, 1);
	s->EndContour();
	return 0;
}
Exemple #12
0
/*! End a polygon
	
	@name Tesselator:EndPolygon
*/
int lua_tess_EndPolygon(lua_State *L) {
	Tesselator *s = Glue<Tesselator>::checkto(L, 1);
	s->EndPolygon();
	return 0;
}
TriangleSetPtr py_triangulation( const GeometryPtr& obj) {
	if (!obj)throw PythonExc_ValueError("Cannot tesselate empty object.");
	Tesselator t;
	if (!obj->apply(t))throw PythonExc_ValueError("Error in tesselation.");
	else return t.getTriangulation();
}