mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-12 10:33:20 +00:00
[Work in progress] Start adding support for IfcSurfaceStyleShading and -Rendering
This commit is contained in:
@@ -17,7 +17,7 @@
|
||||
* *
|
||||
********************************************************************************/
|
||||
|
||||
#include "../ifcconvert/SurfaceStyle.h"
|
||||
#include "../ifcgeom/IfcGeomRenderStyles.h"
|
||||
|
||||
#include "WavefrontOBJSerializer.h"
|
||||
|
||||
@@ -41,40 +41,69 @@ void WaveFrontOBJSerializer::writeHeader() {
|
||||
mtl_stream << "# File generated by IfcOpenShell " << IFCOPENSHELL_VERSION << std::endl;
|
||||
}
|
||||
|
||||
void WaveFrontOBJSerializer::writeMaterial(const SurfaceStyle& style) {
|
||||
mtl_stream << "newmtl " << style.Name() << std::endl
|
||||
<< "Kd " << style.Diffuse().R() << " " << style.Diffuse().G() << " " << style.Diffuse().B() << std::endl
|
||||
<< "Ks " << style.Specular().R() << " " << style.Specular().G() << " " << style.Specular().B() << std::endl
|
||||
<< "Ka " << style.Ambient().R() << " " << style.Ambient().G() << " " << style.Ambient().B() << std::endl
|
||||
<< "Ns " << style.Specularity() << std::endl
|
||||
<< "Tr " << style.Transparency() << std::endl
|
||||
<< "d " << style.Transparency() << std::endl
|
||||
<< "D " << style.Transparency() << std::endl;
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
obj_stream << "usemtl " << o->type << std::endl;
|
||||
if (materials.find(o->type) == materials.end()) {
|
||||
writeMaterial(GetDefaultMaterial(o->type));
|
||||
materials.insert(o->type);
|
||||
}
|
||||
|
||||
const int vcount = o->mesh->verts.size() / 3;
|
||||
for ( IfcGeomObjects::FltIt it = o->mesh->verts.begin(); it != o->mesh->verts.end(); ) {
|
||||
for ( std::vector<float>::const_iterator it = o->mesh->verts.begin(); it != o->mesh->verts.end(); ) {
|
||||
const double x = *(it++);
|
||||
const double y = *(it++);
|
||||
const double z = *(it++);
|
||||
obj_stream << "v " << x << " " << y << " " << z << std::endl;
|
||||
}
|
||||
for ( IfcGeomObjects::FltIt it = o->mesh->normals.begin(); it != o->mesh->normals.end(); ) {
|
||||
for ( std::vector<float>::const_iterator it = o->mesh->normals.begin(); it != o->mesh->normals.end(); ) {
|
||||
const double x = *(it++);
|
||||
const double y = *(it++);
|
||||
const double z = *(it++);
|
||||
obj_stream << "vn " << x << " " << y << " " << z << std::endl;
|
||||
}
|
||||
for ( IfcGeomObjects::IntIt it = o->mesh->faces.begin(); it != o->mesh->faces.end(); ) {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
const int v1 = *(it++)+vcount_total;
|
||||
const int v2 = *(it++)+vcount_total;
|
||||
const int v3 = *(it++)+vcount_total;
|
||||
|
||||
Reference in New Issue
Block a user