2013-03-24 10:48:39 +00:00
|
|
|
/********************************************************************************
|
|
|
|
|
* *
|
|
|
|
|
* This file is part of IfcOpenShell. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell is free software: you can redistribute it and/or modify *
|
|
|
|
|
* it under the terms of the Lesser GNU General Public License as published by *
|
|
|
|
|
* the Free Software Foundation, either version 3.0 of the License, or *
|
|
|
|
|
* (at your option) any later version. *
|
|
|
|
|
* *
|
|
|
|
|
* IfcOpenShell is distributed in the hope that it will be useful, *
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
|
* Lesser GNU General Public License for more details. *
|
|
|
|
|
* *
|
|
|
|
|
* You should have received a copy of the Lesser GNU General Public License *
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
|
* *
|
|
|
|
|
********************************************************************************/
|
|
|
|
|
|
2013-05-05 08:49:25 +00:00
|
|
|
#include "../ifcgeom/IfcGeomRenderStyles.h"
|
2013-03-24 10:48:39 +00:00
|
|
|
|
|
|
|
|
#include "WavefrontOBJSerializer.h"
|
|
|
|
|
|
|
|
|
|
bool WaveFrontOBJSerializer::ready() {
|
|
|
|
|
return obj_stream.is_open() && mtl_stream.is_open();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WaveFrontOBJSerializer::writeHeader() {
|
|
|
|
|
obj_stream << "# File generated by IfcOpenShell " << IFCOPENSHELL_VERSION << std::endl;
|
|
|
|
|
#ifdef WIN32
|
|
|
|
|
const char dir_separator = '\\';
|
|
|
|
|
#else
|
|
|
|
|
const char dir_separator = '/';
|
|
|
|
|
#endif
|
|
|
|
|
std::string mtl_basename = mtl_filename;
|
|
|
|
|
std::string::size_type slash = mtl_basename.find_last_of(dir_separator);
|
|
|
|
|
if (slash != std::string::npos) {
|
|
|
|
|
mtl_basename = mtl_basename.substr(slash+1);
|
|
|
|
|
}
|
|
|
|
|
obj_stream << "mtllib " << mtl_basename << std::endl;
|
|
|
|
|
mtl_stream << "# File generated by IfcOpenShell " << IFCOPENSHELL_VERSION << std::endl;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-05 08:49:25 +00:00
|
|
|
void WaveFrontOBJSerializer::writeMaterial(const IfcGeom::SurfaceStyle& style) {
|
|
|
|
|
mtl_stream << "newmtl " << style.Name() << std::endl;
|
|
|
|
|
if (style.Diffuse()) {
|
|
|
|
|
const IfcGeom::SurfaceStyle::ColorComponent& diffuse = *style.Diffuse();
|
|
|
|
|
mtl_stream << "Kd " << diffuse.R() << " " << diffuse.G() << " " << diffuse.B() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
if (style.Specular()) {
|
|
|
|
|
const IfcGeom::SurfaceStyle::ColorComponent& specular = *style.Specular();
|
|
|
|
|
mtl_stream << "Ks " << specular.R() << " " << specular.G() << " " << specular.B() << std::endl;
|
|
|
|
|
}
|
|
|
|
|
if (style.Specularity()) {
|
|
|
|
|
mtl_stream << "Ns " << (*style.Specularity()) << std::endl;
|
|
|
|
|
}
|
|
|
|
|
if (style.Transparency()) {
|
|
|
|
|
const double transparency = 1.0 - *style.Transparency();
|
|
|
|
|
if (transparency < 1) {
|
|
|
|
|
mtl_stream << "Tr " << transparency << std::endl;
|
|
|
|
|
mtl_stream << "d " << transparency << std::endl;
|
|
|
|
|
mtl_stream << "D " << transparency << std::endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-03-24 10:48:39 +00:00
|
|
|
}
|
|
|
|
|
void WaveFrontOBJSerializer::writeTesselated(const IfcGeomObjects::IfcGeomObject* o) {
|
|
|
|
|
const std::string name = o->name.empty() ? o->guid : o->name;
|
|
|
|
|
obj_stream << "g " << name << std::endl;
|
|
|
|
|
obj_stream << "s 1" << std::endl;
|
2013-05-05 08:49:25 +00:00
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
const int vcount = o->mesh->verts.size() / 3;
|
2013-05-05 08:49:25 +00:00
|
|
|
for ( std::vector<float>::const_iterator it = o->mesh->verts.begin(); it != o->mesh->verts.end(); ) {
|
2013-03-24 10:48:39 +00:00
|
|
|
const double x = *(it++);
|
|
|
|
|
const double y = *(it++);
|
|
|
|
|
const double z = *(it++);
|
|
|
|
|
obj_stream << "v " << x << " " << y << " " << z << std::endl;
|
|
|
|
|
}
|
2013-05-05 08:49:25 +00:00
|
|
|
for ( std::vector<float>::const_iterator it = o->mesh->normals.begin(); it != o->mesh->normals.end(); ) {
|
2013-03-24 10:48:39 +00:00
|
|
|
const double x = *(it++);
|
|
|
|
|
const double y = *(it++);
|
|
|
|
|
const double z = *(it++);
|
|
|
|
|
obj_stream << "vn " << x << " " << y << " " << z << std::endl;
|
|
|
|
|
}
|
2013-05-05 08:49:25 +00:00
|
|
|
|
|
|
|
|
int previous_material_id = -2;
|
|
|
|
|
std::vector<int>::const_iterator material_it = o->mesh->materials.begin();
|
|
|
|
|
|
|
|
|
|
for ( std::vector<int>::const_iterator it = o->mesh->faces.begin(); it != o->mesh->faces.end(); ) {
|
|
|
|
|
|
|
|
|
|
const int material_id = *(material_it++);
|
|
|
|
|
if (material_id != previous_material_id) {
|
|
|
|
|
const IfcGeom::SurfaceStyle* material_style = 0;
|
|
|
|
|
if (material_id >= 0) {
|
|
|
|
|
material_style = o->mesh->surface_styles[material_id];
|
|
|
|
|
} else {
|
|
|
|
|
material_style = IfcGeom::get_default_style(o->type);
|
|
|
|
|
}
|
|
|
|
|
const std::string material_name = material_style->Name();
|
|
|
|
|
obj_stream << "usemtl " << material_name << std::endl;
|
|
|
|
|
if (materials.find(material_name) == materials.end()) {
|
|
|
|
|
writeMaterial(*material_style);
|
|
|
|
|
materials.insert(material_name);
|
|
|
|
|
}
|
|
|
|
|
previous_material_id = material_id;
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-24 10:48:39 +00:00
|
|
|
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 << std::endl;
|
|
|
|
|
}
|
|
|
|
|
vcount_total += vcount;
|
|
|
|
|
}
|