2011-06-14 15:58:22 +00:00
|
|
|
/*
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2011-06-14 15:58:22 +00:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2015-06-28 19:48:25 +00:00
|
|
|
Copyright (c) 2006-2015, assimp team
|
2011-06-14 15:58:22 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2011-06-14 15:58:22 +00:00
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
2012-02-03 03:38:30 +00:00
|
|
|
written permission of the assimp team.
|
2011-06-14 15:58:22 +00:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file IFCMaterial.cpp
|
|
|
|
* @brief Implementation of conversion routines to convert IFC materials to aiMaterial
|
|
|
|
*/
|
|
|
|
|
2015-04-15 23:00:17 +00:00
|
|
|
|
2011-06-14 15:58:22 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
|
|
|
|
#include "IFCUtil.h"
|
2015-02-28 12:40:10 +00:00
|
|
|
#include <limits>
|
2015-06-28 19:48:25 +00:00
|
|
|
#include "../include/assimp/material.h"
|
2011-06-14 15:58:22 +00:00
|
|
|
|
|
|
|
namespace Assimp {
|
|
|
|
namespace IFC {
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
int ConvertShadingMode(const std::string& name)
|
|
|
|
{
|
|
|
|
if (name == "BLINN") {
|
|
|
|
return aiShadingMode_Blinn;
|
|
|
|
}
|
|
|
|
else if (name == "FLAT" || name == "NOTDEFINED") {
|
|
|
|
return aiShadingMode_NoShading;
|
|
|
|
}
|
|
|
|
else if (name == "PHONG") {
|
|
|
|
return aiShadingMode_Phong;
|
|
|
|
}
|
|
|
|
IFCImporter::LogWarn("shading mode "+name+" not recognized by Assimp, using Phong instead");
|
|
|
|
return aiShadingMode_Phong;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2011-08-22 20:22:51 +00:00
|
|
|
void FillMaterial(aiMaterial* mat,const IFC::IfcSurfaceStyle* surf,ConversionData& conv)
|
2011-06-14 15:58:22 +00:00
|
|
|
{
|
|
|
|
aiString name;
|
|
|
|
name.Set((surf->Name? surf->Name.Get() : "IfcSurfaceStyle_Unnamed"));
|
|
|
|
mat->AddProperty(&name,AI_MATKEY_NAME);
|
|
|
|
|
|
|
|
// now see which kinds of surface information are present
|
|
|
|
BOOST_FOREACH(boost::shared_ptr< const IFC::IfcSurfaceStyleElementSelect > sel2, surf->Styles) {
|
|
|
|
if (const IFC::IfcSurfaceStyleShading* shade = sel2->ResolveSelectPtr<IFC::IfcSurfaceStyleShading>(conv.db)) {
|
2012-02-02 22:41:23 +00:00
|
|
|
aiColor4D col_base,col;
|
2011-06-14 15:58:22 +00:00
|
|
|
|
|
|
|
ConvertColor(col_base, shade->SurfaceColour);
|
|
|
|
mat->AddProperty(&col_base,1, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
|
|
|
|
if (const IFC::IfcSurfaceStyleRendering* ren = shade->ToPtr<IFC::IfcSurfaceStyleRendering>()) {
|
|
|
|
|
|
|
|
if (ren->Transparency) {
|
2012-02-02 22:41:23 +00:00
|
|
|
const float t = 1.f-static_cast<float>(ren->Transparency.Get());
|
2011-06-14 15:58:22 +00:00
|
|
|
mat->AddProperty(&t,1, AI_MATKEY_OPACITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ren->DiffuseColour) {
|
|
|
|
ConvertColor(col, *ren->DiffuseColour.Get(),conv,&col_base);
|
|
|
|
mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ren->SpecularColour) {
|
|
|
|
ConvertColor(col, *ren->SpecularColour.Get(),conv,&col_base);
|
|
|
|
mat->AddProperty(&col,1, AI_MATKEY_COLOR_SPECULAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ren->TransmissionColour) {
|
|
|
|
ConvertColor(col, *ren->TransmissionColour.Get(),conv,&col_base);
|
|
|
|
mat->AddProperty(&col,1, AI_MATKEY_COLOR_TRANSPARENT);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ren->ReflectionColour) {
|
|
|
|
ConvertColor(col, *ren->ReflectionColour.Get(),conv,&col_base);
|
|
|
|
mat->AddProperty(&col,1, AI_MATKEY_COLOR_REFLECTIVE);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int shading = (ren->SpecularHighlight && ren->SpecularColour)?ConvertShadingMode(ren->ReflectanceMethod):static_cast<int>(aiShadingMode_Gouraud);
|
|
|
|
mat->AddProperty(&shading,1, AI_MATKEY_SHADING_MODEL);
|
|
|
|
|
|
|
|
if (ren->SpecularHighlight) {
|
|
|
|
if(const EXPRESS::REAL* rt = ren->SpecularHighlight.Get()->ToPtr<EXPRESS::REAL>()) {
|
|
|
|
// at this point we don't distinguish between the two distinct ways of
|
|
|
|
// specifying highlight intensities. leave this to the user.
|
2012-02-02 22:41:23 +00:00
|
|
|
const float e = static_cast<float>(*rt);
|
2011-06-14 15:58:22 +00:00
|
|
|
mat->AddProperty(&e,1,AI_MATKEY_SHININESS);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
IFCImporter::LogWarn("unexpected type error, SpecularHighlight should be a REAL");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-08 01:28:14 +00:00
|
|
|
} /*
|
2011-06-14 15:58:22 +00:00
|
|
|
else if (const IFC::IfcSurfaceStyleWithTextures* tex = sel2->ResolveSelectPtr<IFC::IfcSurfaceStyleWithTextures>(conv.db)) {
|
|
|
|
// XXX
|
2012-07-08 01:28:14 +00:00
|
|
|
} */
|
2011-06-14 15:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-02-23 13:23:28 +00:00
|
|
|
unsigned int ProcessMaterials(uint64_t id, unsigned int prevMatId, ConversionData& conv, bool forceDefaultMat)
|
2011-06-14 15:58:22 +00:00
|
|
|
{
|
2015-02-23 13:23:28 +00:00
|
|
|
STEP::DB::RefMapRange range = conv.db.GetRefs().equal_range(id);
|
2011-06-14 15:58:22 +00:00
|
|
|
for(;range.first != range.second; ++range.first) {
|
|
|
|
if(const IFC::IfcStyledItem* const styled = conv.db.GetObject((*range.first).second)->ToPtr<IFC::IfcStyledItem>()) {
|
|
|
|
BOOST_FOREACH(const IFC::IfcPresentationStyleAssignment& as, styled->Styles) {
|
|
|
|
BOOST_FOREACH(boost::shared_ptr<const IFC::IfcPresentationStyleSelect> sel, as.Styles) {
|
|
|
|
|
2015-02-23 13:23:28 +00:00
|
|
|
if( const IFC::IfcSurfaceStyle* const surf = sel->ResolveSelectPtr<IFC::IfcSurfaceStyle>(conv.db) ) {
|
|
|
|
// try to satisfy from cache
|
|
|
|
ConversionData::MaterialCache::iterator mit = conv.cached_materials.find(surf);
|
|
|
|
if( mit != conv.cached_materials.end() )
|
|
|
|
return mit->second;
|
|
|
|
|
|
|
|
// not found, create new material
|
2011-06-14 15:58:22 +00:00
|
|
|
const std::string side = static_cast<std::string>(surf->Side);
|
2015-02-23 13:23:28 +00:00
|
|
|
if( side != "BOTH" ) {
|
2011-06-14 15:58:22 +00:00
|
|
|
IFCImporter::LogWarn("ignoring surface side marker on IFC::IfcSurfaceStyle: " + side);
|
|
|
|
}
|
|
|
|
|
2011-08-22 20:22:51 +00:00
|
|
|
std::auto_ptr<aiMaterial> mat(new aiMaterial());
|
2011-06-14 15:58:22 +00:00
|
|
|
|
2015-02-23 13:23:28 +00:00
|
|
|
FillMaterial(mat.get(), surf, conv);
|
2011-06-14 15:58:22 +00:00
|
|
|
|
|
|
|
conv.materials.push_back(mat.release());
|
2015-02-23 13:23:28 +00:00
|
|
|
unsigned int matindex = conv.materials.size() - 1;
|
|
|
|
conv.cached_materials[surf] = matindex;
|
|
|
|
return matindex;
|
2011-06-14 15:58:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-02-23 13:23:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// no local material defined. If there's global one, use that instead
|
2015-02-28 12:40:10 +00:00
|
|
|
if( prevMatId != std::numeric_limits<uint32_t>::max() )
|
2015-02-23 13:23:28 +00:00
|
|
|
return prevMatId;
|
|
|
|
|
|
|
|
// we're still here - create an default material if required, or simply fail otherwise
|
|
|
|
if( !forceDefaultMat )
|
2015-02-28 12:40:10 +00:00
|
|
|
return std::numeric_limits<uint32_t>::max();
|
2015-02-23 13:23:28 +00:00
|
|
|
|
|
|
|
aiString name;
|
|
|
|
name.Set("<IFCDefault>");
|
|
|
|
// ConvertColorToString( color, name);
|
|
|
|
|
|
|
|
// look if there's already a default material with this base color
|
|
|
|
for( size_t a = 0; a < conv.materials.size(); ++a )
|
|
|
|
{
|
|
|
|
aiString mname;
|
|
|
|
conv.materials[a]->Get(AI_MATKEY_NAME, mname);
|
|
|
|
if( name == mname )
|
|
|
|
return (unsigned int)a;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we're here, yet - no default material with suitable color available. Generate one
|
|
|
|
std::auto_ptr<aiMaterial> mat(new aiMaterial());
|
|
|
|
mat->AddProperty(&name,AI_MATKEY_NAME);
|
|
|
|
|
|
|
|
const aiColor4D col = aiColor4D( 0.6f, 0.6f, 0.6f, 1.0f); // aiColor4D( color.r, color.g, color.b, 1.0f);
|
|
|
|
mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE);
|
|
|
|
|
|
|
|
conv.materials.push_back(mat.release());
|
|
|
|
return (unsigned int) conv.materials.size() - 1;
|
2011-06-14 15:58:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // ! IFC
|
|
|
|
} // ! Assimp
|
|
|
|
|
|
|
|
#endif
|