// create a mesh storing a pointer in a map so it can be retrieved later by geometry UID
bool MeshImporter::write_geometry(const COLLADAFW::Geometry *geom)
{

	if (geom->getType() != COLLADAFW::Geometry::GEO_TYPE_MESH) {
		// TODO: report warning
		fprintf(stderr, "Mesh type %s is not supported\n", bc_geomTypeToStr(geom->getType()));
		return true;
	}
	
	COLLADAFW::Mesh *mesh = (COLLADAFW::Mesh *)geom;
	
	if (!is_nice_mesh(mesh)) {
		fprintf(stderr, "Ignoring mesh %s\n", bc_get_dae_name(mesh).c_str());
		return true;
	}
	
	const std::string& str_geom_id = mesh->getName().size() ? mesh->getName() : mesh->getOriginalId();
	Mesh *me = BKE_mesh_add(G.main, (char *)str_geom_id.c_str());
	me->id.us--; // is already 1 here, but will be set later in BKE_mesh_assign_object

	// store the Mesh pointer to link it later with an Object
	// mesh_geom_map needed to map mesh to its geometry name (for shape key naming)
	this->uid_mesh_map[mesh->getUniqueId()] = me;
	this->mesh_geom_map[std::string(me->id.name)] = str_geom_id;
	
	read_vertices(mesh, me);
	read_polys(mesh, me);

	// must validate before calculating edges
	BKE_mesh_calc_normals(me);
	BKE_mesh_validate(me, false, false);
	// validation does this
	// BKE_mesh_calc_edges(me, false, false);

	// read_lines() must be called after the face edges have been generated.
	// Oterwise the loose edges will be silently deleted again.
	read_lines(mesh, me);

	return true;
}
Beispiel #2
0
void BlenderStrokeRenderer::RenderStrokeRepBasic(StrokeRep *iStrokeRep) const
{
	////////////////////
	//  Build up scene
	////////////////////

	vector<Strip*>& strips = iStrokeRep->getStrips();
	Strip::vertex_container::iterator v[3];
	StrokeVertexRep *svRep[3];
	/* Vec3r color[3]; */ /* UNUSED */
	unsigned int vertex_index, edge_index, loop_index;
	Vec2r p;

	for (vector<Strip*>::iterator s = strips.begin(), send = strips.end(); s != send; ++s) {
		Strip::vertex_container& strip_vertices = (*s)->vertices();
		int strip_vertex_count = (*s)->sizeStrip();
		int xl, xu, yl, yu, n, visible_faces, visible_segments;
		bool visible;

		// iterate over all vertices and count visible faces and strip segments
		// (note: a strip segment is a series of visible faces, while two strip
		// segments are separated by one or more invisible faces)
		v[0] = strip_vertices.begin();
		v[1] = v[0] + 1;
		v[2] = v[0] + 2;
		visible_faces = visible_segments = 0;
		visible = false;
		for (n = 2; n < strip_vertex_count; n++, v[0]++, v[1]++, v[2]++) {
			svRep[0] = *(v[0]);
			svRep[1] = *(v[1]);
			svRep[2] = *(v[2]);
			xl = xu = yl = yu = 0;
			for (int j = 0; j < 3; j++) {
				p = svRep[j]->point2d();
				if (p[0] < 0.0)
					xl++;
				else if (p[0] > _width)
					xu++;
				if (p[1] < 0.0)
					yl++;
				else if (p[1] > _height)
					yu++;
			}
			if (xl == 3 || xu == 3 || yl == 3 || yu == 3) {
				visible = false;
			}
			else {
				visible_faces++;
				if (!visible)
					visible_segments++;
				visible = true;
			}
		}
		if (visible_faces == 0)
			continue;

		//me = Mesh.New()
#if 0
		Object *object_mesh = BKE_object_add(freestyle_bmain, freestyle_scene, OB_MESH);
#else
		Object *object_mesh = NewMesh();
#endif
		Mesh *mesh = (Mesh *)object_mesh->data;
		mesh->mat = (Material **)MEM_mallocN(1 * sizeof(Material *), "MaterialList");
		mesh->mat[0] = material;
		mesh->totcol = 1;
		test_object_materials(freestyle_bmain, (ID *)mesh);

		// vertices allocation
		mesh->totvert = visible_faces + visible_segments * 2;
		mesh->mvert = (MVert *)CustomData_add_layer(&mesh->vdata, CD_MVERT, CD_CALLOC, NULL, mesh->totvert);

		// edges allocation
		mesh->totedge = visible_faces * 2 + visible_segments;
		mesh->medge = (MEdge *)CustomData_add_layer(&mesh->edata, CD_MEDGE, CD_CALLOC, NULL, mesh->totedge);

		// faces allocation
		mesh->totpoly = visible_faces;
		mesh->mpoly = (MPoly *)CustomData_add_layer(&mesh->pdata, CD_MPOLY, CD_CALLOC, NULL, mesh->totpoly);

		// loops allocation
		mesh->totloop = visible_faces * 3;
		mesh->mloop = (MLoop *)CustomData_add_layer(&mesh->ldata, CD_MLOOP, CD_CALLOC, NULL, mesh->totloop);

		// colors allocation
		mesh->mloopcol = (MLoopCol *)CustomData_add_layer(&mesh->ldata, CD_MLOOPCOL, CD_CALLOC, NULL, mesh->totloop);

		////////////////////
		//  Data copy
		////////////////////

		MVert *vertices = mesh->mvert;
		MEdge *edges = mesh->medge;
		MPoly *polys = mesh->mpoly;
		MLoop *loops = mesh->mloop;
		MLoopCol *colors = mesh->mloopcol;

		v[0] = strip_vertices.begin();
		v[1] = v[0] + 1;
		v[2] = v[0] + 2;

		vertex_index = edge_index = loop_index = 0;
		visible = false;

		// Note: Mesh generation in the following loop assumes stroke strips
		// to be triangle strips.
		for (n = 2; n < strip_vertex_count; n++, v[0]++, v[1]++, v[2]++) {
			svRep[0] = *(v[0]);
			svRep[1] = *(v[1]);
			svRep[2] = *(v[2]);
			xl = xu = yl = yu = 0;
			for (int j = 0; j < 3; j++) {
				p = svRep[j]->point2d();
				if (p[0] < 0.0)
					xl++;
				else if (p[0] > _width)
					xu++;
				if (p[1] < 0.0)
					yl++;
				else if (p[1] > _height)
					yu++;
			}
			if (xl == 3 || xu == 3 || yl == 3 || yu == 3) {
				visible = false;
			}
			else {
				if (!visible) {
					// first vertex
					vertices->co[0] = svRep[0]->point2d()[0];
					vertices->co[1] = svRep[0]->point2d()[1];
					vertices->co[2] = get_stroke_vertex_z();
					++vertices;
					++vertex_index;

					// second vertex
					vertices->co[0] = svRep[1]->point2d()[0];
					vertices->co[1] = svRep[1]->point2d()[1];
					vertices->co[2] = get_stroke_vertex_z();
					++vertices;
					++vertex_index;

					// first edge
					edges->v1 = vertex_index - 2;
					edges->v2 = vertex_index - 1;
					++edges;
					++edge_index;
				}
				visible = true;

				// vertex
				vertices->co[0] = svRep[2]->point2d()[0];
				vertices->co[1] = svRep[2]->point2d()[1];
				vertices->co[2] = get_stroke_vertex_z();
				++vertices;
				++vertex_index;

				// edges
				edges->v1 = vertex_index - 1;
				edges->v2 = vertex_index - 3;
				++edges;
				++edge_index;

				edges->v1 = vertex_index - 1;
				edges->v2 = vertex_index - 2;
				++edges;
				++edge_index;

				// poly
				polys->loopstart = loop_index;
				polys->totloop = 3;
				++polys;

				// loops
				if (n % 2 == 0) {
					loops[0].v = vertex_index - 1;
					loops[0].e = edge_index - 1;

					loops[1].v = vertex_index - 2;
					loops[1].e = edge_index - 3;

					loops[2].v = vertex_index - 3;
					loops[2].e = edge_index - 2;
				}
				else {
					loops[0].v = vertex_index - 1;
					loops[0].e = edge_index - 2;

					loops[1].v = vertex_index - 3;
					loops[1].e = edge_index - 3;

					loops[2].v = vertex_index - 2;
					loops[2].e = edge_index - 1;
				}
				loops += 3;
				loop_index += 3;

				// colors
				if (n % 2 == 0) {
					colors[0].r = (short)(255.0f * svRep[2]->color()[0]);
					colors[0].g = (short)(255.0f * svRep[2]->color()[1]);
					colors[0].b = (short)(255.0f * svRep[2]->color()[2]);
					colors[0].a = (short)(255.0f * svRep[2]->alpha());

					colors[1].r = (short)(255.0f * svRep[1]->color()[0]);
					colors[1].g = (short)(255.0f * svRep[1]->color()[1]);
					colors[1].b = (short)(255.0f * svRep[1]->color()[2]);
					colors[1].a = (short)(255.0f * svRep[1]->alpha());

					colors[2].r = (short)(255.0f * svRep[0]->color()[0]);
					colors[2].g = (short)(255.0f * svRep[0]->color()[1]);
					colors[2].b = (short)(255.0f * svRep[0]->color()[2]);
					colors[2].a = (short)(255.0f * svRep[0]->alpha());
				}
				else {
					colors[0].r = (short)(255.0f * svRep[2]->color()[0]);
					colors[0].g = (short)(255.0f * svRep[2]->color()[1]);
					colors[0].b = (short)(255.0f * svRep[2]->color()[2]);
					colors[0].a = (short)(255.0f * svRep[2]->alpha());

					colors[1].r = (short)(255.0f * svRep[0]->color()[0]);
					colors[1].g = (short)(255.0f * svRep[0]->color()[1]);
					colors[1].b = (short)(255.0f * svRep[0]->color()[2]);
					colors[1].a = (short)(255.0f * svRep[0]->alpha());

					colors[2].r = (short)(255.0f * svRep[1]->color()[0]);
					colors[2].g = (short)(255.0f * svRep[1]->color()[1]);
					colors[2].b = (short)(255.0f * svRep[1]->color()[2]);
					colors[2].a = (short)(255.0f * svRep[1]->alpha());
				}
				colors += 3;
			}
		} // loop over strip vertices
#if 0
		BKE_mesh_validate(mesh, TRUE);
#endif
	} // loop over strips
}