vrshoot

view libs/assimp/IFCMaterial.cpp @ 1:e7ca128b8713

looks nice :)
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 02 Feb 2014 00:35:22 +0200
parents
children
line source
1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
5 Copyright (c) 2006-2012, assimp team
6 All rights reserved.
8 Redistribution and use of this software in source and binary forms,
9 with or without modification, are permitted provided that the
10 following conditions are met:
12 * Redistributions of source code must retain the above
13 copyright notice, this list of conditions and the
14 following disclaimer.
16 * Redistributions in binary form must reproduce the above
17 copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
21 * Neither the name of the assimp team, nor the names of its
22 contributors may be used to endorse or promote products
23 derived from this software without specific prior
24 written permission of the assimp team.
26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
30 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
31 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 ----------------------------------------------------------------------
39 */
41 /** @file IFCMaterial.cpp
42 * @brief Implementation of conversion routines to convert IFC materials to aiMaterial
43 */
45 #include "AssimpPCH.h"
47 #ifndef ASSIMP_BUILD_NO_IFC_IMPORTER
48 #include "IFCUtil.h"
50 namespace Assimp {
51 namespace IFC {
53 // ------------------------------------------------------------------------------------------------
54 int ConvertShadingMode(const std::string& name)
55 {
56 if (name == "BLINN") {
57 return aiShadingMode_Blinn;
58 }
59 else if (name == "FLAT" || name == "NOTDEFINED") {
60 return aiShadingMode_NoShading;
61 }
62 else if (name == "PHONG") {
63 return aiShadingMode_Phong;
64 }
65 IFCImporter::LogWarn("shading mode "+name+" not recognized by Assimp, using Phong instead");
66 return aiShadingMode_Phong;
67 }
69 // ------------------------------------------------------------------------------------------------
70 void FillMaterial(aiMaterial* mat,const IFC::IfcSurfaceStyle* surf,ConversionData& conv)
71 {
72 aiString name;
73 name.Set((surf->Name? surf->Name.Get() : "IfcSurfaceStyle_Unnamed"));
74 mat->AddProperty(&name,AI_MATKEY_NAME);
76 // now see which kinds of surface information are present
77 BOOST_FOREACH(boost::shared_ptr< const IFC::IfcSurfaceStyleElementSelect > sel2, surf->Styles) {
78 if (const IFC::IfcSurfaceStyleShading* shade = sel2->ResolveSelectPtr<IFC::IfcSurfaceStyleShading>(conv.db)) {
79 aiColor4D col_base,col;
81 ConvertColor(col_base, shade->SurfaceColour);
82 mat->AddProperty(&col_base,1, AI_MATKEY_COLOR_DIFFUSE);
84 if (const IFC::IfcSurfaceStyleRendering* ren = shade->ToPtr<IFC::IfcSurfaceStyleRendering>()) {
86 if (ren->Transparency) {
87 const float t = 1.f-static_cast<float>(ren->Transparency.Get());
88 mat->AddProperty(&t,1, AI_MATKEY_OPACITY);
89 }
91 if (ren->DiffuseColour) {
92 ConvertColor(col, *ren->DiffuseColour.Get(),conv,&col_base);
93 mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE);
94 }
96 if (ren->SpecularColour) {
97 ConvertColor(col, *ren->SpecularColour.Get(),conv,&col_base);
98 mat->AddProperty(&col,1, AI_MATKEY_COLOR_SPECULAR);
99 }
101 if (ren->TransmissionColour) {
102 ConvertColor(col, *ren->TransmissionColour.Get(),conv,&col_base);
103 mat->AddProperty(&col,1, AI_MATKEY_COLOR_TRANSPARENT);
104 }
106 if (ren->ReflectionColour) {
107 ConvertColor(col, *ren->ReflectionColour.Get(),conv,&col_base);
108 mat->AddProperty(&col,1, AI_MATKEY_COLOR_REFLECTIVE);
109 }
111 const int shading = (ren->SpecularHighlight && ren->SpecularColour)?ConvertShadingMode(ren->ReflectanceMethod):static_cast<int>(aiShadingMode_Gouraud);
112 mat->AddProperty(&shading,1, AI_MATKEY_SHADING_MODEL);
114 if (ren->SpecularHighlight) {
115 if(const EXPRESS::REAL* rt = ren->SpecularHighlight.Get()->ToPtr<EXPRESS::REAL>()) {
116 // at this point we don't distinguish between the two distinct ways of
117 // specifying highlight intensities. leave this to the user.
118 const float e = static_cast<float>(*rt);
119 mat->AddProperty(&e,1,AI_MATKEY_SHININESS);
120 }
121 else {
122 IFCImporter::LogWarn("unexpected type error, SpecularHighlight should be a REAL");
123 }
124 }
125 }
126 } /*
127 else if (const IFC::IfcSurfaceStyleWithTextures* tex = sel2->ResolveSelectPtr<IFC::IfcSurfaceStyleWithTextures>(conv.db)) {
128 // XXX
129 } */
130 }
132 }
134 // ------------------------------------------------------------------------------------------------
135 unsigned int ProcessMaterials(const IFC::IfcRepresentationItem& item, ConversionData& conv)
136 {
137 if (conv.materials.empty()) {
138 aiString name;
139 std::auto_ptr<aiMaterial> mat(new aiMaterial());
141 name.Set("<IFCDefault>");
142 mat->AddProperty(&name,AI_MATKEY_NAME);
144 const aiColor4D col = aiColor4D(0.6f,0.6f,0.6f,1.0f);
145 mat->AddProperty(&col,1, AI_MATKEY_COLOR_DIFFUSE);
147 conv.materials.push_back(mat.release());
148 }
150 STEP::DB::RefMapRange range = conv.db.GetRefs().equal_range(item.GetID());
151 for(;range.first != range.second; ++range.first) {
152 if(const IFC::IfcStyledItem* const styled = conv.db.GetObject((*range.first).second)->ToPtr<IFC::IfcStyledItem>()) {
153 BOOST_FOREACH(const IFC::IfcPresentationStyleAssignment& as, styled->Styles) {
154 BOOST_FOREACH(boost::shared_ptr<const IFC::IfcPresentationStyleSelect> sel, as.Styles) {
156 if (const IFC::IfcSurfaceStyle* const surf = sel->ResolveSelectPtr<IFC::IfcSurfaceStyle>(conv.db)) {
157 const std::string side = static_cast<std::string>(surf->Side);
158 if (side != "BOTH") {
159 IFCImporter::LogWarn("ignoring surface side marker on IFC::IfcSurfaceStyle: " + side);
160 }
162 std::auto_ptr<aiMaterial> mat(new aiMaterial());
164 FillMaterial(mat.get(),surf,conv);
166 conv.materials.push_back(mat.release());
167 return conv.materials.size()-1;
168 }
169 }
170 }
171 }
172 }
173 return 0;
174 }
176 } // ! IFC
177 } // ! Assimp
179 #endif