| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of LEXIExporter |
|---|
| 4 | |
|---|
| 5 | Copyright 2006 NDS Limited |
|---|
| 6 | |
|---|
| 7 | Author(s): |
|---|
| 8 | Mark Folkenberg, |
|---|
| 9 | Bo Krohn |
|---|
| 10 | |
|---|
| 11 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 12 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 13 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 14 | version. |
|---|
| 15 | |
|---|
| 16 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 17 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 18 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 19 | |
|---|
| 20 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 21 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 22 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 23 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 24 | ----------------------------------------------------------------------------- |
|---|
| 25 | */ |
|---|
| 26 | |
|---|
| 27 | #include "LexiStdAfx.h" |
|---|
| 28 | #include "LexiOgreMaterialCompiler.h" |
|---|
| 29 | #include "OgreMaterialManager.h" |
|---|
| 30 | #include "OgreMaterial.h" |
|---|
| 31 | #include <fstream> |
|---|
| 32 | #include <iostream> |
|---|
| 33 | #include <direct.h> |
|---|
| 34 | |
|---|
| 35 | |
|---|
| 36 | COgreMaterialCompiler::COgreMaterialCompiler( CIntermediateMaterial* pIntermediateMaterial ) |
|---|
| 37 | { |
|---|
| 38 | m_pOgreMaterial.setNull(); |
|---|
| 39 | m_pIMaterial = pIntermediateMaterial; |
|---|
| 40 | |
|---|
| 41 | InitializeOgreComponents(); |
|---|
| 42 | CreateOgreMaterial(); |
|---|
| 43 | |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | COgreMaterialCompiler::~COgreMaterialCompiler( void ) |
|---|
| 47 | { |
|---|
| 48 | |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | void COgreMaterialCompiler::InitializeOgreComponents( void ) |
|---|
| 52 | { |
|---|
| 53 | Ogre::MaterialManager::getSingletonPtr()->unloadAll(); |
|---|
| 54 | Ogre::MaterialManager::getSingletonPtr()->removeAll(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | |
|---|
| 58 | void COgreMaterialCompiler::CreateOgreMaterial( void ) |
|---|
| 59 | { |
|---|
| 60 | assert(m_pIMaterial); |
|---|
| 61 | |
|---|
| 62 | Ogre::MaterialManager* pMatMgr = Ogre::MaterialManager::getSingletonPtr(); |
|---|
| 63 | m_pOgreMaterial = pMatMgr->getByName( m_pIMaterial->GetName().c_str() ); |
|---|
| 64 | |
|---|
| 65 | if(m_pOgreMaterial.isNull()) |
|---|
| 66 | m_pOgreMaterial = (Ogre::MaterialPtr)pMatMgr->create( m_pIMaterial->GetName().c_str(), Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME ); |
|---|
| 67 | else |
|---|
| 68 | return; |
|---|
| 69 | |
|---|
| 70 | assert(!m_pOgreMaterial.isNull()); |
|---|
| 71 | |
|---|
| 72 | Ogre::Technique* technique = m_pOgreMaterial->getTechnique(0); |
|---|
| 73 | if(technique == NULL) |
|---|
| 74 | technique = m_pOgreMaterial->createTechnique(); |
|---|
| 75 | |
|---|
| 76 | technique->setName("Default"); |
|---|
| 77 | |
|---|
| 78 | Ogre::Pass* pass = technique->getPass(0); |
|---|
| 79 | if(pass == NULL) |
|---|
| 80 | pass = technique->createPass(); |
|---|
| 81 | |
|---|
| 82 | pass->setName("Main"); |
|---|
| 83 | pass->setAmbient( m_pIMaterial->GetAmbientColor() ); |
|---|
| 84 | pass->setSpecular( m_pIMaterial->GetSpecularColor() ); |
|---|
| 85 | pass->setShininess( m_pIMaterial->GetGlossiness() ); |
|---|
| 86 | //pass->setSelfIllumination( ); |
|---|
| 87 | pass->setCullingMode( m_pIMaterial->Get2Sided() ? Ogre::CULL_NONE : Ogre::CULL_CLOCKWISE); |
|---|
| 88 | pass->setShadingMode( m_pIMaterial->GetFaceted() ? Ogre::SO_FLAT : Ogre::SO_GOURAUD); |
|---|
| 89 | pass->setPolygonMode( m_pIMaterial->GetWired() ? Ogre::PM_WIREFRAME : Ogre::PM_SOLID); |
|---|
| 90 | |
|---|
| 91 | // create texture units |
|---|
| 92 | ParseMaterialMaps(pass); |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | void COgreMaterialCompiler::ParseMaterialMaps( Ogre::Pass* pass ) |
|---|
| 96 | { |
|---|
| 97 | // Here we should pass the intermediate material on to the plugin which is registered with |
|---|
| 98 | // a matching mask.. when we have the plugin system up and running :) |
|---|
| 99 | |
|---|
| 100 | // mask to see if material is of type diffuse or other |
|---|
| 101 | |
|---|
| 102 | short mask = m_pIMaterial->GetMask(); |
|---|
| 103 | |
|---|
| 104 | LOGINFO "Parsing Material(%s) with Map Mask: %i", m_pIMaterial->GetName().c_str(), mask); |
|---|
| 105 | |
|---|
| 106 | if(mask == 0) // no texture maps |
|---|
| 107 | CreatePureBlinn(pass); |
|---|
| 108 | else if(mask == 2) // only diffuse |
|---|
| 109 | CreateDiffuse(pass); |
|---|
| 110 | else if(mask == 4) // only specular color |
|---|
| 111 | CreateSpecularColor(pass); |
|---|
| 112 | else if(mask == 8) // only specular level |
|---|
| 113 | CreateSpecularLevel(pass); |
|---|
| 114 | else if(mask == 32) // only self illumination map |
|---|
| 115 | CreateSelfIllumination(pass); |
|---|
| 116 | else if(mask == 66) // diffuse map + opacity |
|---|
| 117 | CreateDiffuseAndOpacity(pass); |
|---|
| 118 | else if(mask & 2) // fallback on ordinary diffuse |
|---|
| 119 | CreateDiffuse(pass); |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | // const std::map< Ogre::String, STextureMapInfo >& lMats = m_pIMaterial->GetTextureMaps(); |
|---|
| 123 | // std::map< Ogre::String, STextureMapInfo >::const_iterator it = lMats.begin(); |
|---|
| 124 | // while (it != lMats.end()) |
|---|
| 125 | // { |
|---|
| 126 | // Ogre::String ident = it->first; |
|---|
| 127 | // STextureMapInfo texInfo = it->second; |
|---|
| 128 | // |
|---|
| 129 | // Ogre::TextureUnitState* pTexUnit; |
|---|
| 130 | // |
|---|
| 131 | // Ogre::String baseFile; |
|---|
| 132 | // Ogre::String basePath; |
|---|
| 133 | // Ogre::StringUtil::splitFilename( texInfo.m_sFilename, baseFile, basePath); |
|---|
| 134 | // |
|---|
| 135 | // pTexUnit = pass->createTextureUnitState( baseFile, texInfo.m_iCoordSet-1 ); |
|---|
| 136 | // pTexUnit->setTextureNameAlias( texInfo.m_sMapType ); |
|---|
| 137 | // |
|---|
| 138 | // if(texInfo.m_fOffset[0] != 0 || texInfo.m_fOffset[1] != 0) |
|---|
| 139 | // pTexUnit->setTextureScroll(texInfo.m_fOffset[0], texInfo.m_fOffset[1]); |
|---|
| 140 | // if(texInfo.m_fAngle) |
|---|
| 141 | // pTexUnit->setTextureRotate(Ogre::Radian(-texInfo.m_fAngle)); |
|---|
| 142 | // if(texInfo.m_fScale[0] != 1 || texInfo.m_fScale[1] != 1) |
|---|
| 143 | // pTexUnit->setTextureScale(1/texInfo.m_fScale[0], 1/texInfo.m_fScale[1]); |
|---|
| 144 | // |
|---|
| 145 | // pTexUnit->setTextureAddressingMode(texInfo.m_AdressingMode); |
|---|
| 146 | // |
|---|
| 147 | //// if(texInfo.m_sMapType.compare( "self_illumination" ) == 0) |
|---|
| 148 | //// pTexUnit->setColourOperation(Ogre::LBO_ADD); |
|---|
| 149 | //// else if(texInfo.m_sMapType.compare( "reflection" ) == 0) |
|---|
| 150 | //// pTexUnit->setEnvironmentMap( true, Ogre::Env) |
|---|
| 151 | // //else |
|---|
| 152 | // // pTexUnit->setColourOperationEx(Ogre::LBX_MODULATE_X2); |
|---|
| 153 | // |
|---|
| 154 | // |
|---|
| 155 | // pass->setAlphaRejectFunction( Ogre::CMPF_ALWAYS_PASS ); //Default |
|---|
| 156 | // |
|---|
| 157 | // if(texInfo.m_bAlpha) |
|---|
| 158 | // { |
|---|
| 159 | // pass->setAlphaRejectFunction(Ogre::CMPF_GREATER); |
|---|
| 160 | // pass->setAlphaRejectValue(128); |
|---|
| 161 | // } |
|---|
| 162 | // |
|---|
| 163 | // it++; |
|---|
| 164 | // } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void COgreMaterialCompiler::CreateTextureUnits( Ogre::Pass* pass, STextureMapInfo texInfo ) |
|---|
| 168 | { |
|---|
| 169 | Ogre::TextureUnitState* pTexUnit; |
|---|
| 170 | |
|---|
| 171 | Ogre::String baseFile; |
|---|
| 172 | Ogre::String basePath; |
|---|
| 173 | Ogre::StringUtil::splitFilename( texInfo.m_sFilename, baseFile, basePath); |
|---|
| 174 | |
|---|
| 175 | pTexUnit = pass->createTextureUnitState( baseFile, texInfo.m_iCoordSet-1 ); |
|---|
| 176 | pTexUnit->setTextureNameAlias( texInfo.m_sMapType ); |
|---|
| 177 | |
|---|
| 178 | if(texInfo.m_fOffset[0] != 0 || texInfo.m_fOffset[1] != 0) |
|---|
| 179 | pTexUnit->setTextureScroll(texInfo.m_fOffset[0], texInfo.m_fOffset[1]); |
|---|
| 180 | if(texInfo.m_fAngle) |
|---|
| 181 | pTexUnit->setTextureRotate(Ogre::Radian(-texInfo.m_fAngle)); |
|---|
| 182 | if(texInfo.m_fScale[0] != 1 || texInfo.m_fScale[1] != 1) |
|---|
| 183 | pTexUnit->setTextureScale(1/texInfo.m_fScale[0], 1/texInfo.m_fScale[1]); |
|---|
| 184 | |
|---|
| 185 | pTexUnit->setTextureAddressingMode(texInfo.m_AdressingMode); |
|---|
| 186 | |
|---|
| 187 | // const std::map< Ogre::String, STextureMapInfo >& lMats = m_pIMaterial->GetTextureMaps(); |
|---|
| 188 | // std::map< Ogre::String, STextureMapInfo >::const_iterator it = lMats.begin(); |
|---|
| 189 | // while (it != lMats.end()) |
|---|
| 190 | // { |
|---|
| 191 | // Ogre::String ident = it->first; |
|---|
| 192 | // STextureMapInfo texInfo = it->second; |
|---|
| 193 | // |
|---|
| 194 | // Ogre::TextureUnitState* pTexUnit; |
|---|
| 195 | // |
|---|
| 196 | // Ogre::String baseFile; |
|---|
| 197 | // Ogre::String basePath; |
|---|
| 198 | // Ogre::StringUtil::splitFilename( texInfo.m_sFilename, baseFile, basePath); |
|---|
| 199 | // |
|---|
| 200 | // pTexUnit = pass->createTextureUnitState( baseFile, texInfo.m_iCoordSet-1 ); |
|---|
| 201 | // pTexUnit->setTextureNameAlias( texInfo.m_sMapType ); |
|---|
| 202 | // |
|---|
| 203 | // if(texInfo.m_fOffset[0] != 0 || texInfo.m_fOffset[1] != 0) |
|---|
| 204 | // pTexUnit->setTextureScroll(texInfo.m_fOffset[0], texInfo.m_fOffset[1]); |
|---|
| 205 | // if(texInfo.m_fAngle) |
|---|
| 206 | // pTexUnit->setTextureRotate(Ogre::Radian(-texInfo.m_fAngle)); |
|---|
| 207 | // if(texInfo.m_fScale[0] != 1 || texInfo.m_fScale[1] != 1) |
|---|
| 208 | // pTexUnit->setTextureScale(1/texInfo.m_fScale[0], 1/texInfo.m_fScale[1]); |
|---|
| 209 | // |
|---|
| 210 | // pTexUnit->setTextureAddressingMode(texInfo.m_AdressingMode); |
|---|
| 211 | // |
|---|
| 212 | //// if(texInfo.m_sMapType.compare( "self_illumination" ) == 0) |
|---|
| 213 | //// pTexUnit->setColourOperation(Ogre::LBO_ADD); |
|---|
| 214 | //// else if(texInfo.m_sMapType.compare( "reflection" ) == 0) |
|---|
| 215 | //// pTexUnit->setEnvironmentMap( true, Ogre::Env) |
|---|
| 216 | // //else |
|---|
| 217 | // // pTexUnit->setColourOperationEx(Ogre::LBX_MODULATE_X2); |
|---|
| 218 | // |
|---|
| 219 | // |
|---|
| 220 | // pass->setAlphaRejectFunction( Ogre::CMPF_ALWAYS_PASS ); //Default |
|---|
| 221 | // |
|---|
| 222 | // if(texInfo.m_bAlpha) |
|---|
| 223 | // { |
|---|
| 224 | // pass->setAlphaRejectFunction(Ogre::CMPF_GREATER); |
|---|
| 225 | // pass->setAlphaRejectValue(128); |
|---|
| 226 | // } |
|---|
| 227 | // |
|---|
| 228 | // it++; |
|---|
| 229 | // } |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | void COgreMaterialCompiler::CreatePureBlinn( Ogre::Pass* pass ) |
|---|
| 233 | { |
|---|
| 234 | pass->setVertexProgram("BlinnVP"); |
|---|
| 235 | pass->setFragmentProgram( "Blinn_Pure_FP" ); |
|---|
| 236 | |
|---|
| 237 | Ogre::GpuProgramParametersSharedPtr params = pass->getFragmentProgramParameters(); |
|---|
| 238 | |
|---|
| 239 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 240 | float specHack = m_pIMaterial->GetSpecularLevel() > 9.98 ? 9.98 : m_pIMaterial->GetSpecularLevel() ; |
|---|
| 241 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 242 | float glossHack = m_pIMaterial->GetGlossiness() == 1 ? 0.9999 : m_pIMaterial->GetGlossiness() ; |
|---|
| 243 | glossHack = glossHack > 0.01 ? glossHack * 100 : 0.0 ; |
|---|
| 244 | |
|---|
| 245 | params->setNamedConstant("ambientColor", m_pIMaterial->GetAmbientColor()); |
|---|
| 246 | params->setNamedConstant("diffuseColor", m_pIMaterial->GetDiffuseColor()); |
|---|
| 247 | params->setNamedConstant("specularColor", m_pIMaterial->GetSpecularColor()); |
|---|
| 248 | params->setNamedConstant("specularLevel", specHack); |
|---|
| 249 | params->setNamedConstant("glossLevel", glossHack); |
|---|
| 250 | params->setNamedConstant("opacity", m_pIMaterial->GetOpacity()); |
|---|
| 251 | |
|---|
| 252 | pass->setFragmentProgramParameters(params); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | void COgreMaterialCompiler::CreateDiffuse( Ogre::Pass* pass ) |
|---|
| 256 | { |
|---|
| 257 | pass->setVertexProgram("BlinnVP"); |
|---|
| 258 | pass->setFragmentProgram( "Blinn_DiffuseMap_FP" ); |
|---|
| 259 | |
|---|
| 260 | Ogre::GpuProgramParametersSharedPtr params = pass->getFragmentProgramParameters(); |
|---|
| 261 | |
|---|
| 262 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 263 | float specHack = m_pIMaterial->GetSpecularLevel() > 9.98 ? 9.98 : m_pIMaterial->GetSpecularLevel() ; |
|---|
| 264 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 265 | float glossHack = m_pIMaterial->GetGlossiness() == 1 ? 0.9999 : m_pIMaterial->GetGlossiness() ; |
|---|
| 266 | glossHack = glossHack > 0.01 ? glossHack * 100 : 0.0 ; |
|---|
| 267 | |
|---|
| 268 | params->setNamedConstant("ambientColor", m_pIMaterial->GetAmbientColor()); |
|---|
| 269 | params->setNamedConstant("diffuseColor", m_pIMaterial->GetDiffuseColor()); |
|---|
| 270 | params->setNamedConstant("specularColor", m_pIMaterial->GetSpecularColor()); |
|---|
| 271 | params->setNamedConstant("specularLevel", specHack); |
|---|
| 272 | params->setNamedConstant("glossLevel", glossHack); |
|---|
| 273 | params->setNamedConstant("opacity", m_pIMaterial->GetOpacity()); |
|---|
| 274 | |
|---|
| 275 | STextureMapInfo texInfo = m_pIMaterial->GetTextureMapInfoFromName("diffuse"); |
|---|
| 276 | if ( !texInfo.isNull()) |
|---|
| 277 | params->setNamedConstant("amount", texInfo.m_fAmount); |
|---|
| 278 | |
|---|
| 279 | CreateTextureUnits(pass, texInfo); |
|---|
| 280 | |
|---|
| 281 | pass->setFragmentProgramParameters(params); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | void COgreMaterialCompiler::CreateSpecularColor( Ogre::Pass* pass ) |
|---|
| 285 | { |
|---|
| 286 | pass->setVertexProgram("BlinnVP"); |
|---|
| 287 | pass->setFragmentProgram( "Blinn_SpecularColor_FP" ); |
|---|
| 288 | |
|---|
| 289 | Ogre::GpuProgramParametersSharedPtr params = pass->getFragmentProgramParameters(); |
|---|
| 290 | |
|---|
| 291 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 292 | float specHack = m_pIMaterial->GetSpecularLevel() > 9.98 ? 9.98 : m_pIMaterial->GetSpecularLevel() ; |
|---|
| 293 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 294 | float glossHack = m_pIMaterial->GetGlossiness() == 1 ? 0.9999 : m_pIMaterial->GetGlossiness() ; |
|---|
| 295 | glossHack = glossHack > 0.01 ? glossHack * 100 : 0.0 ; |
|---|
| 296 | |
|---|
| 297 | params->setNamedConstant("ambientColor", m_pIMaterial->GetAmbientColor()); |
|---|
| 298 | params->setNamedConstant("diffuseColor", m_pIMaterial->GetDiffuseColor()); |
|---|
| 299 | params->setNamedConstant("specularColor", m_pIMaterial->GetSpecularColor()); |
|---|
| 300 | params->setNamedConstant("specularLevel", specHack); |
|---|
| 301 | params->setNamedConstant("glossLevel", glossHack); |
|---|
| 302 | params->setNamedConstant("opacity", m_pIMaterial->GetOpacity()); |
|---|
| 303 | |
|---|
| 304 | STextureMapInfo texInfo = m_pIMaterial->GetTextureMapInfoFromName("specular_color"); |
|---|
| 305 | if ( !texInfo.isNull()) |
|---|
| 306 | params->setNamedConstant("amount", texInfo.m_fAmount); |
|---|
| 307 | |
|---|
| 308 | CreateTextureUnits(pass, texInfo); |
|---|
| 309 | |
|---|
| 310 | pass->setFragmentProgramParameters(params); |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | void COgreMaterialCompiler::CreateSpecularLevel( Ogre::Pass* pass ) |
|---|
| 314 | { |
|---|
| 315 | pass->setVertexProgram("BlinnVP"); |
|---|
| 316 | pass->setFragmentProgram( "Blinn_SpecularLevel_FP" ); |
|---|
| 317 | |
|---|
| 318 | Ogre::GpuProgramParametersSharedPtr params = pass->getFragmentProgramParameters(); |
|---|
| 319 | |
|---|
| 320 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 321 | float specHack = m_pIMaterial->GetSpecularLevel() > 9.98 ? 9.98 : m_pIMaterial->GetSpecularLevel() ; |
|---|
| 322 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 323 | float glossHack = m_pIMaterial->GetGlossiness() == 1 ? 0.9999 : m_pIMaterial->GetGlossiness() ; |
|---|
| 324 | glossHack = glossHack > 0.01 ? glossHack * 100 : 0.0 ; |
|---|
| 325 | |
|---|
| 326 | params->setNamedConstant("ambientColor", m_pIMaterial->GetAmbientColor()); |
|---|
| 327 | params->setNamedConstant("diffuseColor", m_pIMaterial->GetDiffuseColor()); |
|---|
| 328 | params->setNamedConstant("specularColor", m_pIMaterial->GetSpecularColor()); |
|---|
| 329 | params->setNamedConstant("specularLevel", specHack); |
|---|
| 330 | params->setNamedConstant("glossLevel", glossHack); |
|---|
| 331 | params->setNamedConstant("opacity", m_pIMaterial->GetOpacity()); |
|---|
| 332 | |
|---|
| 333 | STextureMapInfo texInfo = m_pIMaterial->GetTextureMapInfoFromName("specular_level"); |
|---|
| 334 | if ( !texInfo.isNull()) |
|---|
| 335 | params->setNamedConstant("amount", texInfo.m_fAmount); |
|---|
| 336 | |
|---|
| 337 | CreateTextureUnits(pass, texInfo); |
|---|
| 338 | |
|---|
| 339 | pass->setFragmentProgramParameters(params); |
|---|
| 340 | } |
|---|
| 341 | |
|---|
| 342 | void COgreMaterialCompiler::CreateSelfIllumination( Ogre::Pass* pass ) |
|---|
| 343 | { |
|---|
| 344 | pass->setVertexProgram("Blinn_4UV_VP"); |
|---|
| 345 | pass->setFragmentProgram( "Blinn_SelfIllumination_FP" ); |
|---|
| 346 | |
|---|
| 347 | Ogre::GpuProgramParametersSharedPtr params = pass->getFragmentProgramParameters(); |
|---|
| 348 | |
|---|
| 349 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 350 | float specHack = m_pIMaterial->GetSpecularLevel() > 9.98 ? 9.98 : m_pIMaterial->GetSpecularLevel() ; |
|---|
| 351 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 352 | float glossHack = m_pIMaterial->GetGlossiness() == 1 ? 0.9999 : m_pIMaterial->GetGlossiness() ; |
|---|
| 353 | glossHack = glossHack > 0.01 ? glossHack * 100 : 0.0 ; |
|---|
| 354 | |
|---|
| 355 | params->setNamedConstant("ambientColor", m_pIMaterial->GetAmbientColor()); |
|---|
| 356 | params->setNamedConstant("diffuseColor", m_pIMaterial->GetDiffuseColor()); |
|---|
| 357 | params->setNamedConstant("specularColor", m_pIMaterial->GetSpecularColor()); |
|---|
| 358 | params->setNamedConstant("specularLevel", specHack); |
|---|
| 359 | params->setNamedConstant("glossLevel", glossHack); |
|---|
| 360 | params->setNamedConstant("opacity", m_pIMaterial->GetOpacity()); |
|---|
| 361 | |
|---|
| 362 | STextureMapInfo texInfo = m_pIMaterial->GetTextureMapInfoFromName("self_illumination"); |
|---|
| 363 | if ( !texInfo.isNull()) |
|---|
| 364 | { |
|---|
| 365 | params->setNamedConstant("amount", texInfo.m_fAmount); |
|---|
| 366 | params->setNamedConstant("uvIndex", (float)texInfo.m_iCoordSet-1); |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | CreateTextureUnits(pass, texInfo); |
|---|
| 370 | |
|---|
| 371 | pass->setFragmentProgramParameters(params); |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| 374 | void COgreMaterialCompiler::CreateDiffuseAndOpacity( Ogre::Pass* pass ) |
|---|
| 375 | { |
|---|
| 376 | pass->setVertexProgram("BlinnVP"); |
|---|
| 377 | pass->setFragmentProgram( "Blinn_DiffuseAndOpacityMap_FP" ); |
|---|
| 378 | |
|---|
| 379 | pass->setSceneBlending( Ogre::SBT_TRANSPARENT_ALPHA ); |
|---|
| 380 | pass->setDepthWriteEnabled(false); |
|---|
| 381 | |
|---|
| 382 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 383 | float specHack = m_pIMaterial->GetSpecularLevel() > 9.98 ? 9.98 : m_pIMaterial->GetSpecularLevel() ; |
|---|
| 384 | // HACK: For some reason we cannot set the SpecularLevel if it is its max at 9.99, so we clamp it to 9.98 |
|---|
| 385 | float glossHack = m_pIMaterial->GetGlossiness() == 1 ? 0.9999 : m_pIMaterial->GetGlossiness() ; |
|---|
| 386 | glossHack = glossHack > 0.01 ? glossHack * 100 : 0.0 ; |
|---|
| 387 | |
|---|
| 388 | Ogre::GpuProgramParametersSharedPtr params = pass->getFragmentProgramParameters(); |
|---|
| 389 | params->setNamedConstant("ambientColor", m_pIMaterial->GetAmbientColor()); |
|---|
| 390 | params->setNamedConstant("diffuseColor", m_pIMaterial->GetDiffuseColor()); |
|---|
| 391 | params->setNamedConstant("specularColor", m_pIMaterial->GetSpecularColor()); |
|---|
| 392 | params->setNamedConstant("specularLevel", specHack); |
|---|
| 393 | params->setNamedConstant("glossLevel", glossHack); |
|---|
| 394 | |
|---|
| 395 | STextureMapInfo texInfo = m_pIMaterial->GetTextureMapInfoFromName("diffuse"); |
|---|
| 396 | if ( !texInfo.isNull()) |
|---|
| 397 | params->setNamedConstant("amount", texInfo.m_fAmount); |
|---|
| 398 | |
|---|
| 399 | CreateTextureUnits(pass, texInfo); |
|---|
| 400 | |
|---|
| 401 | texInfo = m_pIMaterial->GetTextureMapInfoFromName("opacity"); |
|---|
| 402 | if ( !texInfo.isNull()) |
|---|
| 403 | params->setNamedConstant("opacity", texInfo.m_fAmount); |
|---|
| 404 | |
|---|
| 405 | CreateTextureUnits(pass, texInfo); |
|---|
| 406 | |
|---|
| 407 | pass->setFragmentProgramParameters(params); |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | void COgreMaterialCompiler::CopyTextureMaps( Ogre::String outPath ) |
|---|
| 411 | { |
|---|
| 412 | // copy texture files to target path |
|---|
| 413 | // optionally convert to .dds |
|---|
| 414 | |
|---|
| 415 | const std::map< Ogre::String, STextureMapInfo >& lMats = m_pIMaterial->GetTextureMaps(); |
|---|
| 416 | std::map< Ogre::String, STextureMapInfo >::const_iterator it = lMats.begin(); |
|---|
| 417 | while (it != lMats.end()) |
|---|
| 418 | { |
|---|
| 419 | Ogre::String ident = it->first; |
|---|
| 420 | STextureMapInfo texInfo = it->second; |
|---|
| 421 | |
|---|
| 422 | Ogre::String baseFile; |
|---|
| 423 | Ogre::String basePath; |
|---|
| 424 | Ogre::StringUtil::splitFilename( texInfo.m_sFilename, baseFile, basePath); |
|---|
| 425 | |
|---|
| 426 | //Ogre::String srcFile = texInfo.m_sFilename; |
|---|
| 427 | |
|---|
| 428 | if( doFileCopy(texInfo.m_sFilename, outPath+baseFile) != 0 ) |
|---|
| 429 | LOGWARNING "Couldn´t copy texture map: %s", texInfo.m_sFilename.c_str()); |
|---|
| 430 | it++; |
|---|
| 431 | } |
|---|
| 432 | } |
|---|
| 433 | |
|---|
| 434 | void COgreMaterialCompiler::CopyShaderSources( Ogre::String outPath ) |
|---|
| 435 | { |
|---|
| 436 | // copy texture files to target path |
|---|
| 437 | // optionally convert to .dds |
|---|
| 438 | |
|---|
| 439 | // ensure we are in the 3dMax dir |
|---|
| 440 | char szAppPath[MAX_PATH] = ""; |
|---|
| 441 | ::GetModuleFileName(NULL,szAppPath,sizeof(szAppPath) - 1); |
|---|
| 442 | |
|---|
| 443 | Ogre::String cwd(szAppPath); |
|---|
| 444 | Ogre::String fileName, filePath; |
|---|
| 445 | Ogre::StringUtil::splitFilename(cwd, fileName, filePath); |
|---|
| 446 | |
|---|
| 447 | _chdir(filePath.c_str()); |
|---|
| 448 | |
|---|
| 449 | doFileCopy("LexiExporter\\shaders\\Blinn.cg", outPath+Ogre::String("Blinn.cg") ); |
|---|
| 450 | doFileCopy("LexiExporter\\shaders\\Shaders.program", outPath+Ogre::String("Shaders.program") ); |
|---|
| 451 | } |
|---|
| 452 | |
|---|
| 453 | bool COgreMaterialCompiler::WriteOgreMaterial( const Ogre::String& sFilename ) |
|---|
| 454 | { |
|---|
| 455 | Ogre::MaterialSerializer* pMatWriter = new Ogre::MaterialSerializer(); |
|---|
| 456 | try |
|---|
| 457 | { |
|---|
| 458 | pMatWriter->exportMaterial( m_pOgreMaterial, sFilename ); |
|---|
| 459 | } |
|---|
| 460 | catch (Ogre::Exception& e) |
|---|
| 461 | { |
|---|
| 462 | MessageBox( NULL, e.getFullDescription().c_str(), "ERROR", MB_ICONERROR); |
|---|
| 463 | return false; |
|---|
| 464 | } |
|---|
| 465 | delete pMatWriter; |
|---|
| 466 | return true; |
|---|
| 467 | } |
|---|
| 468 | |
|---|
| 469 | Ogre::MaterialPtr COgreMaterialCompiler::GetOgreMaterial( void ) |
|---|
| 470 | { |
|---|
| 471 | return m_pOgreMaterial; |
|---|
| 472 | } |
|---|
| 473 | |
|---|
| 474 | |
|---|
| 475 | #define BUFF_SIZE 2048 |
|---|
| 476 | int COgreMaterialCompiler::doFileCopy(Ogre::String inFile, Ogre::String outFile) |
|---|
| 477 | { |
|---|
| 478 | char buff[BUFF_SIZE]; |
|---|
| 479 | int readBytes = 1; |
|---|
| 480 | |
|---|
| 481 | ifstream inFileStream(inFile.c_str(), ios::in|ios::binary); |
|---|
| 482 | if(!inFileStream) |
|---|
| 483 | { |
|---|
| 484 | return -1; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | ifstream tmpStream(outFile.c_str()); |
|---|
| 488 | if(tmpStream) |
|---|
| 489 | { |
|---|
| 490 | return -1; |
|---|
| 491 | } |
|---|
| 492 | tmpStream.close(); |
|---|
| 493 | |
|---|
| 494 | ofstream outFileStream(outFile.c_str(), ios::out|ios::binary); |
|---|
| 495 | if(!outFileStream) |
|---|
| 496 | { |
|---|
| 497 | return -1; |
|---|
| 498 | } |
|---|
| 499 | |
|---|
| 500 | while(readBytes != 0) |
|---|
| 501 | { |
|---|
| 502 | inFileStream.read((char*)buff, BUFF_SIZE); |
|---|
| 503 | readBytes = inFileStream.gcount(); |
|---|
| 504 | outFileStream.write((char*)buff, readBytes); |
|---|
| 505 | } |
|---|
| 506 | return 0; |
|---|
| 507 | } |
|---|