vrshoot

diff libs/assimp/IFCMaterial.cpp @ 0:b2f14e535253

initial commit
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 01 Feb 2014 19:58:19 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/libs/assimp/IFCMaterial.cpp	Sat Feb 01 19:58:19 2014 +0200
     1.3 @@ -0,0 +1,179 @@
     1.4 +/*
     1.5 +Open Asset Import Library (assimp)
     1.6 +----------------------------------------------------------------------
     1.7 +
     1.8 +Copyright (c) 2006-2012, assimp team
     1.9 +All rights reserved.
    1.10 +
    1.11 +Redistribution and use of this software in source and binary forms, 
    1.12 +with or without modification, are permitted provided that the 
    1.13 +following conditions are met:
    1.14 +
    1.15 +* Redistributions of source code must retain the above
    1.16 +  copyright notice, this list of conditions and the
    1.17 +  following disclaimer.
    1.18 +
    1.19 +* Redistributions in binary form must reproduce the above
    1.20 +  copyright notice, this list of conditions and the
    1.21 +  following disclaimer in the documentation and/or other
    1.22 +  materials provided with the distribution.
    1.23 +
    1.24 +* Neither the name of the assimp team, nor the names of its
    1.25 +  contributors may be used to endorse or promote products
    1.26 +  derived from this software without specific prior
    1.27 +  written permission of the assimp team.
    1.28 +
    1.29 +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
    1.30 +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
    1.31 +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    1.32 +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
    1.33 +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    1.34 +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
    1.35 +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    1.36 +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
    1.37 +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
    1.38 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
    1.39 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    1.40 +
    1.41 +----------------------------------------------------------------------
    1.42 +*/
    1.43 +
    1.44 +/** @file  IFCMaterial.cpp
    1.45 + *  @brief Implementation of conversion routines to convert IFC materials to aiMaterial
    1.46 + */
    1.47 +
    1.48 +#include "AssimpPCH.h"
    1.49 +
    1.50 +#ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
    1.51 +#include "IFCUtil.h"
    1.52 +
    1.53 +namespace Assimp {
    1.54 +	namespace IFC {
    1.55 +
    1.56 +// ------------------------------------------------------------------------------------------------
    1.57 +int ConvertShadingMode(const std::string& name)
    1.58 +{
    1.59 +	if (name == "BLINN") {
    1.60 +		return aiShadingMode_Blinn;
    1.61 +	}
    1.62 +	else if (name == "FLAT" || name == "NOTDEFINED") {
    1.63 +		return aiShadingMode_NoShading;
    1.64 +	}
    1.65 +	else if (name == "PHONG") {
    1.66 +		return aiShadingMode_Phong;
    1.67 +	}
    1.68 +	IFCImporter::LogWarn("shading mode "+name+" not recognized by Assimp, using Phong instead");
    1.69 +	return aiShadingMode_Phong;
    1.70 +}
    1.71 +
    1.72 +// ------------------------------------------------------------------------------------------------
    1.73 +void FillMaterial(aiMaterial* mat,const IFC::IfcSurfaceStyle* surf,ConversionData& conv) 
    1.74 +{
    1.75 +	aiString name;
    1.76 +	name.Set((surf->Name? surf->Name.Get() : "IfcSurfaceStyle_Unnamed"));
    1.77 +	mat->AddProperty(&name,AI_MATKEY_NAME);
    1.78 +
    1.79 +	// now see which kinds of surface information are present
    1.80 +	BOOST_FOREACH(boost::shared_ptr< const IFC::IfcSurfaceStyleElementSelect > sel2, surf->Styles) {
    1.81 +		if (const IFC::IfcSurfaceStyleShading* shade = sel2->ResolveSelectPtr<IFC::IfcSurfaceStyleShading>(conv.db)) {
    1.82 +			aiColor4D col_base,col;
    1.83 +
    1.84 +			ConvertColor(col_base, shade->SurfaceColour);
    1.85 +			mat->AddProperty(&col_base,1, AI_MATKEY_COLOR_DIFFUSE);
    1.86 +
    1.87 +			if (const IFC::IfcSurfaceStyleRendering* ren = shade->ToPtr<IFC::IfcSurfaceStyleRendering>()) {
    1.88 +
    1.89 +				if (ren->Transparency) {
    1.90 +					const float t = 1.f-static_cast<float>(ren->Transparency.Get());
    1.91 +					mat->AddProperty(&t,1, AI_MATKEY_OPACITY);
    1.92 +				}
    1.93 +
    1.94 +				if (ren->DiffuseColour) {
    1.95 +					ConvertColor(col, *ren->DiffuseColour.Get(),conv,&col_base);
    1.96 +					mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE);
    1.97 +				}
    1.98 +
    1.99 +				if (ren->SpecularColour) {
   1.100 +					ConvertColor(col, *ren->SpecularColour.Get(),conv,&col_base);
   1.101 +					mat->AddProperty(&col,1, AI_MATKEY_COLOR_SPECULAR);
   1.102 +				}
   1.103 +
   1.104 +				if (ren->TransmissionColour) {
   1.105 +					ConvertColor(col, *ren->TransmissionColour.Get(),conv,&col_base);
   1.106 +					mat->AddProperty(&col,1, AI_MATKEY_COLOR_TRANSPARENT);
   1.107 +				}
   1.108 +
   1.109 +				if (ren->ReflectionColour) {
   1.110 +					ConvertColor(col, *ren->ReflectionColour.Get(),conv,&col_base);
   1.111 +					mat->AddProperty(&col,1, AI_MATKEY_COLOR_REFLECTIVE);
   1.112 +				}
   1.113 +
   1.114 +				const int shading = (ren->SpecularHighlight && ren->SpecularColour)?ConvertShadingMode(ren->ReflectanceMethod):static_cast<int>(aiShadingMode_Gouraud);
   1.115 +				mat->AddProperty(&shading,1, AI_MATKEY_SHADING_MODEL);
   1.116 +
   1.117 +				if (ren->SpecularHighlight) {
   1.118 +					if(const EXPRESS::REAL* rt = ren->SpecularHighlight.Get()->ToPtr<EXPRESS::REAL>()) {
   1.119 +						// at this point we don't distinguish between the two distinct ways of
   1.120 +						// specifying highlight intensities. leave this to the user.
   1.121 +						const float e = static_cast<float>(*rt);
   1.122 +						mat->AddProperty(&e,1,AI_MATKEY_SHININESS);
   1.123 +					}
   1.124 +					else {
   1.125 +						IFCImporter::LogWarn("unexpected type error, SpecularHighlight should be a REAL");
   1.126 +					}
   1.127 +				}
   1.128 +			}
   1.129 +		} /*
   1.130 +		else if (const IFC::IfcSurfaceStyleWithTextures* tex = sel2->ResolveSelectPtr<IFC::IfcSurfaceStyleWithTextures>(conv.db)) {
   1.131 +			// XXX
   1.132 +		} */
   1.133 +	}
   1.134 +
   1.135 +}
   1.136 +
   1.137 +// ------------------------------------------------------------------------------------------------
   1.138 +unsigned int ProcessMaterials(const IFC::IfcRepresentationItem& item, ConversionData& conv)
   1.139 +{
   1.140 +	if (conv.materials.empty()) {
   1.141 +		aiString name;
   1.142 +		std::auto_ptr<aiMaterial> mat(new aiMaterial());
   1.143 +
   1.144 +		name.Set("<IFCDefault>");
   1.145 +		mat->AddProperty(&name,AI_MATKEY_NAME);
   1.146 +
   1.147 +		const aiColor4D col = aiColor4D(0.6f,0.6f,0.6f,1.0f);
   1.148 +		mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE);
   1.149 +
   1.150 +		conv.materials.push_back(mat.release());
   1.151 +	}
   1.152 +
   1.153 +	STEP::DB::RefMapRange range = conv.db.GetRefs().equal_range(item.GetID());
   1.154 +	for(;range.first != range.second; ++range.first) {
   1.155 +		if(const IFC::IfcStyledItem* const styled = conv.db.GetObject((*range.first).second)->ToPtr<IFC::IfcStyledItem>()) {
   1.156 +			BOOST_FOREACH(const IFC::IfcPresentationStyleAssignment& as, styled->Styles) {
   1.157 +				BOOST_FOREACH(boost::shared_ptr<const IFC::IfcPresentationStyleSelect> sel, as.Styles) {
   1.158 +
   1.159 +					if (const IFC::IfcSurfaceStyle* const surf =  sel->ResolveSelectPtr<IFC::IfcSurfaceStyle>(conv.db)) {
   1.160 +						const std::string side = static_cast<std::string>(surf->Side);
   1.161 +						if (side != "BOTH") {
   1.162 +							IFCImporter::LogWarn("ignoring surface side marker on IFC::IfcSurfaceStyle: " + side);
   1.163 +						}
   1.164 +
   1.165 +						std::auto_ptr<aiMaterial> mat(new aiMaterial());
   1.166 +
   1.167 +						FillMaterial(mat.get(),surf,conv);
   1.168 +
   1.169 +						conv.materials.push_back(mat.release());
   1.170 +						return conv.materials.size()-1;
   1.171 +					}
   1.172 +				}
   1.173 +			}
   1.174 +		}
   1.175 +	}
   1.176 +	return 0;
   1.177 +}
   1.178 +
   1.179 +} // ! IFC
   1.180 +} // ! Assimp
   1.181 +
   1.182 +#endif