vrshoot
diff libs/assimp/OgreMaterial.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/OgreMaterial.cpp Sat Feb 01 19:58:19 2014 +0200 1.3 @@ -0,0 +1,453 @@ 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 +/** 1.45 +This file contains material related code. This is 1.46 +spilitted up from the main file OgreImporter.cpp 1.47 +to make it shorter easier to maintain. 1.48 +*/ 1.49 +#include "AssimpPCH.h" 1.50 + 1.51 +#ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER 1.52 + 1.53 +#include <vector> 1.54 +#include <sstream> 1.55 +using namespace std; 1.56 + 1.57 +#include "OgreImporter.hpp" 1.58 +#include "irrXMLWrapper.h" 1.59 +#include "TinyFormatter.h" 1.60 + 1.61 +namespace Assimp 1.62 +{ 1.63 +namespace Ogre 1.64 +{ 1.65 + 1.66 + 1.67 + 1.68 +aiMaterial* OgreImporter::LoadMaterial(const std::string MaterialName) const 1.69 +{ 1.70 + /*For better understanding of the material parser, here is a material example file: 1.71 + 1.72 + material Sarg 1.73 + { 1.74 + receive_shadows on 1.75 + technique 1.76 + { 1.77 + pass 1.78 + { 1.79 + ambient 0.500000 0.500000 0.500000 1.000000 1.80 + diffuse 0.640000 0.640000 0.640000 1.000000 1.81 + specular 0.500000 0.500000 0.500000 1.000000 12.500000 1.82 + emissive 0.000000 0.000000 0.000000 1.000000 1.83 + texture_unit 1.84 + { 1.85 + texture SargTextur.tga 1.86 + tex_address_mode wrap 1.87 + filtering linear linear none 1.88 + } 1.89 + } 1.90 + } 1.91 + } 1.92 + 1.93 + */ 1.94 + 1.95 + /*and here is another one: 1.96 + 1.97 + import * from abstract_base_passes_depth.material 1.98 + import * from abstract_base.material 1.99 + import * from mat_shadow_caster.material 1.100 + import * from mat_character_singlepass.material 1.101 + 1.102 + material hero/hair/caster : mat_shadow_caster_skin_areject 1.103 + { 1.104 + set $diffuse_map "hero_hair_alpha_c.dds" 1.105 + } 1.106 + 1.107 + material hero/hair_alpha : mat_char_cns_singlepass_areject_4weights 1.108 + { 1.109 + set $diffuse_map "hero_hair_alpha_c.dds" 1.110 + set $specular_map "hero_hair_alpha_s.dds" 1.111 + set $normal_map "hero_hair_alpha_n.dds" 1.112 + set $light_map "black_lightmap.dds" 1.113 + 1.114 + set $shadow_caster_material "hero/hair/caster" 1.115 + } 1.116 + */ 1.117 + 1.118 + //Read the file into memory and put it in a stringstream 1.119 + stringstream ss; 1.120 + {// after this block, the temporarly loaded data will be released 1.121 + 1.122 + /* 1.123 + We have 3 guesses for the Material filename: 1.124 + - the Material Name 1.125 + - the Name of the mesh file 1.126 + - the DefaultMaterialLib (which you can set before importing) 1.127 + */ 1.128 + 1.129 + IOStream* MatFilePtr=m_CurrentIOHandler->Open(MaterialName+".material"); 1.130 + if(NULL==MatFilePtr) 1.131 + { 1.132 + //the filename typically ends with .mesh or .mesh.xml 1.133 + const string MaterialFileName=m_CurrentFilename.substr(0, m_CurrentFilename.rfind(".mesh"))+".material"; 1.134 + 1.135 + MatFilePtr=m_CurrentIOHandler->Open(MaterialFileName); 1.136 + if(NULL==MatFilePtr) 1.137 + { 1.138 + //try the default mat Library 1.139 + if(NULL==MatFilePtr) 1.140 + { 1.141 + 1.142 + MatFilePtr=m_CurrentIOHandler->Open(m_MaterialLibFilename); 1.143 + if(NULL==MatFilePtr) 1.144 + { 1.145 + DefaultLogger::get()->error(m_MaterialLibFilename+" and "+MaterialFileName + " could not be opened, Material will not be loaded!"); 1.146 + return new aiMaterial(); 1.147 + } 1.148 + } 1.149 + } 1.150 + } 1.151 + //Fill the stream 1.152 + boost::scoped_ptr<IOStream> MaterialFile(MatFilePtr); 1.153 + if(MaterialFile->FileSize()>0) 1.154 + { 1.155 + vector<char> FileData(MaterialFile->FileSize()); 1.156 + MaterialFile->Read(&FileData[0], MaterialFile->FileSize(), 1); 1.157 + BaseImporter::ConvertToUTF8(FileData); 1.158 + 1.159 + FileData.push_back('\0');//terminate the string with zero, so that the ss can parse it correctly 1.160 + ss << &FileData[0]; 1.161 + } 1.162 + else 1.163 + { 1.164 + DefaultLogger::get()->warn("Material " + MaterialName + " seams to be empty"); 1.165 + return NULL; 1.166 + } 1.167 + } 1.168 + 1.169 + //create the material 1.170 + aiMaterial *NewMaterial=new aiMaterial(); 1.171 + 1.172 + aiString ts(MaterialName.c_str()); 1.173 + NewMaterial->AddProperty(&ts, AI_MATKEY_NAME); 1.174 + 1.175 + string Line; 1.176 + ss >> Line; 1.177 +// unsigned int Level=0;//Hierarchielevels in the material file, like { } blocks into another 1.178 + while(!ss.eof()) 1.179 + { 1.180 + if(Line=="material") 1.181 + { 1.182 + ss >> Line; 1.183 + if(Line==MaterialName)//Load the next material 1.184 + { 1.185 + string RestOfLine; 1.186 + getline(ss, RestOfLine);//ignore the rest of the line 1.187 + ss >> Line; 1.188 + 1.189 + if(Line!="{") 1.190 + { 1.191 + DefaultLogger::get()->warn("empyt material!"); 1.192 + return NULL; 1.193 + } 1.194 + 1.195 + while(Line!="}")//read until the end of the material 1.196 + { 1.197 + //Proceed to the first technique 1.198 + ss >> Line; 1.199 + if(Line=="technique") 1.200 + { 1.201 + ReadTechnique(ss, NewMaterial); 1.202 + } 1.203 + 1.204 + DefaultLogger::get()->info(Line); 1.205 + //read informations from a custom material: 1.206 + if(Line=="set") 1.207 + { 1.208 + ss >> Line; 1.209 + if(Line=="$specular")//todo load this values: 1.210 + { 1.211 + } 1.212 + if(Line=="$diffuse") 1.213 + { 1.214 + } 1.215 + if(Line=="$ambient") 1.216 + { 1.217 + } 1.218 + if(Line=="$colormap") 1.219 + { 1.220 + ss >> Line; 1.221 + aiString ts(Line.c_str()); 1.222 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); 1.223 + } 1.224 + if(Line=="$normalmap") 1.225 + { 1.226 + ss >> Line; 1.227 + aiString ts(Line.c_str()); 1.228 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0)); 1.229 + } 1.230 + 1.231 + if(Line=="$shininess_strength") 1.232 + { 1.233 + ss >> Line; 1.234 + float Shininess=fast_atof(Line.c_str()); 1.235 + NewMaterial->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS_STRENGTH); 1.236 + } 1.237 + 1.238 + if(Line=="$shininess_exponent") 1.239 + { 1.240 + ss >> Line; 1.241 + float Shininess=fast_atof(Line.c_str()); 1.242 + NewMaterial->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS); 1.243 + } 1.244 + 1.245 + //Properties from Venetica: 1.246 + if(Line=="$diffuse_map") 1.247 + { 1.248 + ss >> Line; 1.249 + if(Line[0]=='"')// "file" -> file 1.250 + Line=Line.substr(1, Line.size()-2); 1.251 + aiString ts(Line.c_str()); 1.252 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0)); 1.253 + } 1.254 + if(Line=="$specular_map") 1.255 + { 1.256 + ss >> Line; 1.257 + if(Line[0]=='"')// "file" -> file 1.258 + Line=Line.substr(1, Line.size()-2); 1.259 + aiString ts(Line.c_str()); 1.260 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_SHININESS, 0)); 1.261 + } 1.262 + if(Line=="$normal_map") 1.263 + { 1.264 + ss >> Line; 1.265 + if(Line[0]=='"')// "file" -> file 1.266 + Line=Line.substr(1, Line.size()-2); 1.267 + aiString ts(Line.c_str()); 1.268 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0)); 1.269 + } 1.270 + if(Line=="$light_map") 1.271 + { 1.272 + ss >> Line; 1.273 + if(Line[0]=='"')// "file" -> file 1.274 + Line=Line.substr(1, Line.size()-2); 1.275 + aiString ts(Line.c_str()); 1.276 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, 0)); 1.277 + } 1.278 + } 1.279 + }//end of material 1.280 + } 1.281 + else {} //this is the wrong material, proceed the file until we reach the next material 1.282 + } 1.283 + ss >> Line; 1.284 + } 1.285 + 1.286 + return NewMaterial; 1.287 +} 1.288 + 1.289 +void OgreImporter::ReadTechnique(stringstream &ss, aiMaterial* NewMaterial) const 1.290 +{ 1.291 + unsigned int CurrentDiffuseTextureId=0; 1.292 + unsigned int CurrentSpecularTextureId=0; 1.293 + unsigned int CurrentNormalTextureId=0; 1.294 + unsigned int CurrentLightTextureId=0; 1.295 + 1.296 + 1.297 + string RestOfLine; 1.298 + getline(ss, RestOfLine);//ignore the rest of the line 1.299 + 1.300 + string Line; 1.301 + ss >> Line; 1.302 + if(Line!="{") 1.303 + { 1.304 + DefaultLogger::get()->warn("empty technique!"); 1.305 + return; 1.306 + } 1.307 + while(Line!="}")//read until the end of the technique 1.308 + { 1.309 + ss >> Line; 1.310 + if(Line=="pass") 1.311 + { 1.312 + getline(ss, RestOfLine);//ignore the rest of the line 1.313 + 1.314 + ss >> Line; 1.315 + if(Line!="{") 1.316 + { 1.317 + DefaultLogger::get()->warn("empty pass!"); 1.318 + return; 1.319 + } 1.320 + while(Line!="}")//read until the end of the pass 1.321 + { 1.322 + ss >> Line; 1.323 + if(Line=="ambient") 1.324 + { 1.325 + float r,g,b; 1.326 + ss >> r >> g >> b; 1.327 + const aiColor3D Color(r,g,b); 1.328 + NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_AMBIENT); 1.329 + } 1.330 + else if(Line=="diffuse") 1.331 + { 1.332 + float r,g,b; 1.333 + ss >> r >> g >> b; 1.334 + const aiColor3D Color(r,g,b); 1.335 + NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_DIFFUSE); 1.336 + } 1.337 + else if(Line=="specular") 1.338 + { 1.339 + float r,g,b; 1.340 + ss >> r >> g >> b; 1.341 + const aiColor3D Color(r,g,b); 1.342 + NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_SPECULAR); 1.343 + } 1.344 + else if(Line=="emmisive") 1.345 + { 1.346 + float r,g,b; 1.347 + ss >> r >> g >> b; 1.348 + const aiColor3D Color(r,g,b); 1.349 + NewMaterial->AddProperty(&Color, 1, AI_MATKEY_COLOR_EMISSIVE); 1.350 + } 1.351 + else if(Line=="texture_unit") 1.352 + { 1.353 + getline(ss, RestOfLine);//ignore the rest of the line 1.354 + 1.355 + std::string TextureName; 1.356 + int TextureType=-1; 1.357 + int UvSet=0; 1.358 + 1.359 + ss >> Line; 1.360 + if(Line!="{") 1.361 + throw DeadlyImportError("empty texture unit!"); 1.362 + while(Line!="}")//read until the end of the texture_unit 1.363 + { 1.364 + ss >> Line; 1.365 + if(Line=="texture") 1.366 + { 1.367 + ss >> Line; 1.368 + TextureName=Line; 1.369 + 1.370 + if(m_TextureTypeFromFilename) 1.371 + { 1.372 + if(Line.find("_n.")!=string::npos)// Normalmap 1.373 + { 1.374 + TextureType=aiTextureType_NORMALS; 1.375 + } 1.376 + else if(Line.find("_s.")!=string::npos)// Specularmap 1.377 + { 1.378 + TextureType=aiTextureType_SPECULAR; 1.379 + } 1.380 + else if(Line.find("_l.")!=string::npos)// Lightmap 1.381 + { 1.382 + TextureType=aiTextureType_LIGHTMAP; 1.383 + } 1.384 + else// colormap 1.385 + { 1.386 + TextureType=aiTextureType_DIFFUSE; 1.387 + } 1.388 + } 1.389 + else 1.390 + { 1.391 + TextureType=aiTextureType_DIFFUSE; 1.392 + } 1.393 + } 1.394 + else if(Line=="tex_coord_set") 1.395 + { 1.396 + ss >> UvSet; 1.397 + } 1.398 + else if(Line=="colour_op")//TODO implement this 1.399 + { 1.400 + /* 1.401 + ss >> Line; 1.402 + if("replace"==Line)//I don't think, assimp has something for this... 1.403 + { 1.404 + } 1.405 + else if("modulate"==Line) 1.406 + { 1.407 + //TODO: set value 1.408 + //NewMaterial->AddProperty(aiTextureOp_Multiply) 1.409 + } 1.410 + */ 1.411 + } 1.412 + 1.413 + }//end of texture unit 1.414 + Line="";//clear the } that would end the outer loop 1.415 + 1.416 + //give the texture to assimp: 1.417 + 1.418 + aiString ts(TextureName.c_str()); 1.419 + switch(TextureType) 1.420 + { 1.421 + case aiTextureType_DIFFUSE: 1.422 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, CurrentDiffuseTextureId)); 1.423 + NewMaterial->AddProperty(&UvSet, 1, AI_MATKEY_UVWSRC(0, CurrentDiffuseTextureId)); 1.424 + CurrentDiffuseTextureId++; 1.425 + break; 1.426 + case aiTextureType_NORMALS: 1.427 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, CurrentNormalTextureId)); 1.428 + NewMaterial->AddProperty(&UvSet, 1, AI_MATKEY_UVWSRC(0, CurrentNormalTextureId)); 1.429 + CurrentNormalTextureId++; 1.430 + break; 1.431 + case aiTextureType_SPECULAR: 1.432 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_SPECULAR, CurrentSpecularTextureId)); 1.433 + NewMaterial->AddProperty(&UvSet, 1, AI_MATKEY_UVWSRC(0, CurrentSpecularTextureId)); 1.434 + CurrentSpecularTextureId++; 1.435 + break; 1.436 + case aiTextureType_LIGHTMAP: 1.437 + NewMaterial->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, CurrentLightTextureId)); 1.438 + NewMaterial->AddProperty(&UvSet, 1, AI_MATKEY_UVWSRC(0, CurrentLightTextureId)); 1.439 + CurrentLightTextureId++; 1.440 + break; 1.441 + default: 1.442 + DefaultLogger::get()->warn("Invalid Texture Type!"); 1.443 + break; 1.444 + } 1.445 + } 1.446 + } 1.447 + Line="";//clear the } that would end the outer loop 1.448 + } 1.449 + }//end of technique 1.450 +} 1.451 + 1.452 + 1.453 +}//namespace Ogre 1.454 +}//namespace Assimp 1.455 + 1.456 +#endif // !! ASSIMP_BUILD_NO_OGRE_IMPORTER