void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement<double>* o) {

    std::string tmp = o->name().empty() ? o->guid() : o->name();

    std::replace( tmp.begin(), tmp.end(), ' ', '_');
    const std::string name = tmp;
    obj_stream << "g " << name << "\n";
    obj_stream << "s 1" << "\n";

    const IfcGeom::Representation::Triangulation<double>& mesh = o->geometry();

    const int vcount = mesh.verts().size() / 3;
    for ( std::vector<double>::const_iterator it = mesh.verts().begin(); it != mesh.verts().end(); ) {
        const double x = *(it++);
        const double y = *(it++);
        const double z = *(it++);
        obj_stream << "v " << x << " " << y << " " << z << "\n";
    }
    for ( std::vector<double>::const_iterator it = mesh.normals().begin(); it != mesh.normals().end(); ) {
        const double x = *(it++);
        const double y = *(it++);
        const double z = *(it++);
        obj_stream << "vn " << x << " " << y << " " << z << "\n";
    }

    int previous_material_id = -2;
    std::vector<int>::const_iterator material_it = mesh.material_ids().begin();

    for ( std::vector<int>::const_iterator it = mesh.faces().begin(); it != mesh.faces().end(); ) {

        const int material_id = *(material_it++);
        if (material_id != previous_material_id) {
            const IfcGeom::Material& material = mesh.materials()[material_id];
            const std::string material_name = material.name();
            obj_stream << "usemtl " << material_name << "\n";
            if (materials.find(material_name) == materials.end()) {
                writeMaterial(material);
                materials.insert(material_name);
            }
            previous_material_id = material_id;
        }

        const int v1 = *(it++)+vcount_total;
        const int v2 = *(it++)+vcount_total;
        const int v3 = *(it++)+vcount_total;
        obj_stream << "f " << v1 << "//" << v1 << " " << v2 << "//" << v2 << " " << v3 << "//" << v3 << "\n";
    }
    vcount_total += vcount;
}
Ejemplo n.º 2
0
void GLC_WorldTo3dxml::writeAllMaterialRelatedFilesIn3dxml()
{
	m_MaterialIdToMaterialName.clear();
	m_MaterialIdToMaterialId.clear();
	m_MaterialIdToTexture3dxmlName.clear();
	m_MaterialIdTo3dxmlImageId.clear();

	// Get the list of material
	QList<GLC_Material*> materialList= m_World.listOfMaterials();

	// Create the list of textured material and modified material list
	QList<GLC_Material*> texturedMaterial;
	const int size= materialList.size();
	for (int i= 0; i < size; ++i)
	{
		if (materialList.at(i)->hasTexture())
		{
			texturedMaterial.append(materialList.at(i));
		}
	}

	if (!texturedMaterial.isEmpty())
	{
		writeCatRepImageFile(texturedMaterial);
		writeImageFileIn3dxml(texturedMaterial);
	}


	// Write material 3DRep in the 3DXML
	for (int i= 0; i < size; ++i)
	{
		writeMaterial(materialList.at(i));
	}

	writeCatMaterialRef(materialList);



}
void CIrrMeshWriter::writeMeshBuffer(const scene::IMeshBuffer* buffer)
{
	Writer->writeElement(L"buffer", false);
	Writer->writeLineBreak();

	// write bounding box

	writeBoundingBox(buffer->getBoundingBox());
	Writer->writeLineBreak();

	// write material

	writeMaterial(buffer->getMaterial());

	// write vertices

	const core::stringw vertexTypeStr = video::sBuiltInVertexTypeNames[buffer->getVertexType()];

	Writer->writeElement(L"vertices", false,
		L"type", vertexTypeStr.c_str(),
		L"vertexCount", core::stringw(buffer->getVertexCount()).c_str());

	Writer->writeLineBreak();

	u32 vertexCount = buffer->getVertexCount();

	switch(buffer->getVertexType())
	{
	case video::EVT_STANDARD:
		{
			video::S3DVertex* vtx = (video::S3DVertex*)buffer->getVertices();
			for (u32 j=0; j<vertexCount; ++j)
			{
				core::stringw str = getVectorAsStringLine(vtx[j].Pos);
				str += L" ";
				str += getVectorAsStringLine(vtx[j].Normal);

				char tmp[12];
				sprintf(tmp, " %02x%02x%02x%02x ", vtx[j].Color.getAlpha(), vtx[j].Color.getRed(), vtx[j].Color.getGreen(), vtx[j].Color.getBlue());
				str += tmp;

				str += getVectorAsStringLine(vtx[j].TCoords);

				Writer->writeText(str.c_str());
				Writer->writeLineBreak();
			}
		}
		break;
	case video::EVT_2TCOORDS:
		{
			video::S3DVertex2TCoords* vtx = (video::S3DVertex2TCoords*)buffer->getVertices();
			for (u32 j=0; j<vertexCount; ++j)
			{
				core::stringw str = getVectorAsStringLine(vtx[j].Pos);
				str += L" ";
				str += getVectorAsStringLine(vtx[j].Normal);

				char tmp[12];
				sprintf(tmp, " %02x%02x%02x%02x ", vtx[j].Color.getAlpha(), vtx[j].Color.getRed(), vtx[j].Color.getGreen(), vtx[j].Color.getBlue());
				str += tmp;

				str += getVectorAsStringLine(vtx[j].TCoords);
				str += L" ";
				str += getVectorAsStringLine(vtx[j].TCoords2);

				Writer->writeText(str.c_str());
				Writer->writeLineBreak();
			}
		}
		break;
	case video::EVT_TANGENTS:
		{
			video::S3DVertexTangents* vtx = (video::S3DVertexTangents*)buffer->getVertices();
			for (u32 j=0; j<vertexCount; ++j)
			{
				core::stringw str = getVectorAsStringLine(vtx[j].Pos);
				str += L" ";
				str += getVectorAsStringLine(vtx[j].Normal);

				char tmp[12];
				sprintf(tmp, " %02x%02x%02x%02x ", vtx[j].Color.getAlpha(), vtx[j].Color.getRed(), vtx[j].Color.getGreen(), vtx[j].Color.getBlue());
				str += tmp;

				str += getVectorAsStringLine(vtx[j].TCoords);
				str += L" ";
				str += getVectorAsStringLine(vtx[j].Tangent);
				str += L" ";
				str += getVectorAsStringLine(vtx[j].Binormal);

				Writer->writeText(str.c_str());
				Writer->writeLineBreak();
			}
		}
		break;
	}

	Writer->writeClosingTag(L"vertices");
	Writer->writeLineBreak();

	// write indices

	Writer->writeElement(L"indices", false,
		L"indexCount", core::stringw(buffer->getIndexCount()).c_str());

	Writer->writeLineBreak();

	int indexCount = (int)buffer->getIndexCount();

	video::E_INDEX_TYPE iType = buffer->getIndexType();

	const u16* idx16 = buffer->getIndices();
	const u32* idx32 = (u32*) buffer->getIndices();
	const int maxIndicesPerLine = 25;

	for (int i=0; i<indexCount; ++i)
	{
		if(iType == video::EIT_16BIT)
		{
			core::stringw str((int)idx16[i]);
			Writer->writeText(str.c_str());
		}
		else
		{
			core::stringw str((int)idx32[i]);
			Writer->writeText(str.c_str());
		}

		if (i % maxIndicesPerLine != maxIndicesPerLine)
		{
			if (i % maxIndicesPerLine == maxIndicesPerLine-1)
				Writer->writeLineBreak();
			else
				Writer->writeText(L" ");
		}
	}

	if ((indexCount-1) % maxIndicesPerLine != maxIndicesPerLine-1)
		Writer->writeLineBreak();

	Writer->writeClosingTag(L"indices");
	Writer->writeLineBreak();

	// close buffer tag

	Writer->writeClosingTag(L"buffer");
}
void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement<double>* o) {

	obj_stream << "g " << o->unique_id() << "\n";
	obj_stream << "s 1" << "\n";

	const IfcGeom::Representation::Triangulation<double>& mesh = o->geometry();
	
	const int vcount = (int)mesh.verts().size() / 3;
	for ( std::vector<double>::const_iterator it = mesh.verts().begin(); it != mesh.verts().end(); ) {
		const double x = *(it++);
		const double y = *(it++);
		const double z = *(it++);
		obj_stream << "v " << x << " " << y << " " << z << "\n";
	}

	for ( std::vector<double>::const_iterator it = mesh.normals().begin(); it != mesh.normals().end(); ) {
		const double x = *(it++);
		const double y = *(it++);
		const double z = *(it++);
		obj_stream << "vn " << x << " " << y << " " << z << "\n";
	}

	int previous_material_id = -2;
	std::vector<int>::const_iterator material_it = mesh.material_ids().begin();

	for ( std::vector<int>::const_iterator it = mesh.faces().begin(); it != mesh.faces().end(); ) {
		
		const int material_id = *(material_it++);
		if (material_id != previous_material_id) {
			const IfcGeom::Material& material = mesh.materials()[material_id];
			const std::string material_name = material.name();
			obj_stream << "usemtl " << material_name << "\n";
			if (materials.find(material_name) == materials.end()) {
				writeMaterial(material);
				materials.insert(material_name);
			}
			previous_material_id = material_id;
		}

		const int v1 = *(it++)+vcount_total;
		const int v2 = *(it++)+vcount_total;
		const int v3 = *(it++)+vcount_total;
		obj_stream << "f " << v1 << "//" << v1 << " " << v2 << "//" << v2 << " " << v3 << "//" << v3 << "\n";

	}

	std::set<int> faces_set (mesh.faces().begin(), mesh.faces().end());
	const std::vector<int>& edges = mesh.edges();

	for ( std::vector<int>::const_iterator it = edges.begin(); it != edges.end(); ) {
		const int i1 = *(it++);
		const int i2 = *(it++);

		if (faces_set.find(i1) != faces_set.end() || faces_set.find(i2) != faces_set.end()) {
			continue;
		}

		const int material_id = *(material_it++);

		if (material_id != previous_material_id) {
			const IfcGeom::Material& material = mesh.materials()[material_id];
			const std::string material_name = material.name();
			obj_stream << "usemtl " << material_name << "\n";
			if (materials.find(material_name) == materials.end()) {
				writeMaterial(material);
				materials.insert(material_name);
			}
			previous_material_id = material_id;
		}

		const int v1 = i1 + vcount_total;
		const int v2 = i2 + vcount_total;

		obj_stream << "l " << v1 << " " << v2 << "\n";
	}

	vcount_total += vcount;
}
void WaveFrontOBJSerializer::write(const IfcGeom::TriangulationElement<real_t>* o)
{
    const std::string name = (settings().get(IfcGeom::IteratorSettings::USE_ELEMENT_GUIDS)
        ? o->guid() : (settings().get(IfcGeom::IteratorSettings::USE_ELEMENT_NAMES)
            ? o->name() : o->unique_id()));
    obj_stream << "g " << name << "\n";
	obj_stream << "s 1" << "\n";

    const IfcGeom::Representation::Triangulation<real_t>& mesh = o->geometry();
	
	const int vcount = (int)mesh.verts().size() / 3;
    for ( std::vector<real_t>::const_iterator it = mesh.verts().begin(); it != mesh.verts().end(); ) {
        const real_t x = *(it++) + (real_t)settings().offset[0];
        const real_t y = *(it++) + (real_t)settings().offset[1];
        const real_t z = *(it++) + (real_t)settings().offset[2];
		obj_stream << "v " << x << " " << y << " " << z << "\n";
	}

    for ( std::vector<real_t>::const_iterator it = mesh.normals().begin(); it != mesh.normals().end(); ) {
        const real_t x = *(it++);
        const real_t y = *(it++);
        const real_t z = *(it++);
		obj_stream << "vn " << x << " " << y << " " << z << "\n";
	}

    for (std::vector<real_t>::const_iterator it = mesh.uvs().begin(); it != mesh.uvs().end();) {
        const real_t u = *it++;
        const real_t v = *it++;
        obj_stream << "vt " << u << " " << v << "\n";
    }

	int previous_material_id = -2;
	std::vector<int>::const_iterator material_it = mesh.material_ids().begin();

    const bool has_uvs = !mesh.uvs().empty();
	for ( std::vector<int>::const_iterator it = mesh.faces().begin(); it != mesh.faces().end(); ) {
		
		const int material_id = *(material_it++);
		if (material_id != previous_material_id) {
			const IfcGeom::Material& material = mesh.materials()[material_id];
            std::string material_name = (settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES)
                ? material.original_name() : material.name());
            IfcUtil::sanitate_material_name(material_name);
			obj_stream << "usemtl " << material_name << "\n";
			if (materials.find(material_name) == materials.end()) {
				writeMaterial(material);
				materials.insert(material_name);
			}
			previous_material_id = material_id;
		}

		const int v1 = *(it++)+vcount_total;
		const int v2 = *(it++)+vcount_total;
		const int v3 = *(it++)+vcount_total;
        obj_stream << "f " << v1 << "/" << (has_uvs ? boost::lexical_cast<std::string>(v1) : "") << "/" << v1 << " "
            << v2 << "/" << (has_uvs ? boost::lexical_cast<std::string>(v2) : "") << "/" << v2 << " "
            << v3 << "/" << (has_uvs ? boost::lexical_cast<std::string>(v3) : "") << "/" << v3 << "\n";

	}

	std::set<int> faces_set (mesh.faces().begin(), mesh.faces().end());
	const std::vector<int>& edges = mesh.edges();

	for ( std::vector<int>::const_iterator it = edges.begin(); it != edges.end(); ) {
		const int i1 = *(it++);
		const int i2 = *(it++);

		if (faces_set.find(i1) != faces_set.end() || faces_set.find(i2) != faces_set.end()) {
			continue;
		}

		const int material_id = *(material_it++);

		if (material_id != previous_material_id) {
			const IfcGeom::Material& material = mesh.materials()[material_id];
            std::string material_name = (settings().get(IfcGeom::IteratorSettings::USE_MATERIAL_NAMES)
                ? material.original_name() : material.name());
            IfcUtil::sanitate_material_name(material_name);
			obj_stream << "usemtl " << material_name << "\n";
			if (materials.find(material_name) == materials.end()) {
				writeMaterial(material);
				materials.insert(material_name);
			}
			previous_material_id = material_id;
		}

		const int v1 = i1 + vcount_total;
		const int v2 = i2 + vcount_total;

		obj_stream << "l " << v1 << " " << v2 << "\n";
	}

	vcount_total += vcount;
}
Ejemplo n.º 6
0
	void G3djWriter::exportG3dj(G3djFile *file, const char* filePath){
		writer = new JSONWriter(filePath);

		writer->openObject();

		writer->writeStringPair("version", G3DJ_VERSION);
		writer->nextValue(true);

		// Write Meshes
		writer->openArray("meshes");

		// meshes
		for (unsigned int i=0; i<file->getMeshCount(); i++)
		{
			Mesh *mesh = file->getMesh(i);
			if(i>0)
				writer->nextValue(true);

			writer->openObject();

			// write ID out first
			writer->writeStringPair("id", mesh->getId().c_str());
			writer->nextValue(true);

			// then the attributes
			writer->openArray("attributes");
				writeAttributes(mesh);
			writer->closeArray();
			writer->nextValue(true);

			// then the interleaved vertices
			writer->openArray("vertices");
				writeVertices(mesh);
			writer->closeArray();
			writer->nextValue(true);

			// then the mesh parts (optional)
			if(mesh->parts.size() > 0){
				writer->openArray("parts");
					writeMeshParts(mesh);
				writer->closeArray();
			}
			writer->closeObject();
		}

		writer->closeArray();
		writer->nextValue(true);

		// Write Materials
		if(file->getMaterialCount() > 0){
			writer->openArray("materials");

			for(unsigned int i=0; i<file->getMaterialCount(); i++){
				G3djMaterial* material = file->getMaterial(i);
			
				if(i>0)
					writer->nextValue(true);

				writeMaterial(material);
			}

			writer->closeArray();
			writer->nextValue(true);
		}

		// Write Nodes
		writer->openArray("nodes");

		for(unsigned int i=0; i<file->getNodeCount(); i++){
			G3djNode* node = file->getNode(i);

			if(i>0)
				writer->nextValue(true);

			writeNodeRecursive(node);
		}

		writer->closeArray();
		writer->nextValue(true);

		// Write Animations
		writer->openArray("animations");

		for(unsigned int i=0; i<file->getAnimationClipCount(); i++){
			AnimationClip* animationClip = file->getAnimationClip(i);

			if(i>0)
				writer->nextValue(true);

			writeAnimationClip(animationClip);
		}

		writer->closeArray();

		// Main object closing
		writer->closeObject();
	}