14#include "OGRE/OgreImage.h"
22 mUseVertexColor = useVertexColor;
25 mHaveAlphaDetails =
false;
32 void ALMaterial::manageTexturePath(
ALTextureData* texData, boost::filesystem::path expPath)
35 boost::filesystem::path texPath(texData->
path);
39 boost::filesystem::path filepath(texData->
path);
40 filename = filepath.generic_string();
42 if (filename.find_last_of(
'/') != std::string::npos)
43 filename = filename.substr(filename.find_last_of(
'/'));
46 expPath /=
"textures";
48 if (!filename.empty() && filename.front() ==
'/')
55 bool optimize =
false;
56 unsigned int optSize = 1024;
75 if (
const aiTexture* texture = mScene->
getAiScene()->GetEmbeddedTexture(texData->
embname.c_str()))
77 if (!texture->mHeight)
81 if (FILE* file = fopen(expPath.generic_string().c_str(),
"wb"))
83 fwrite(texture->pcData, texture->mWidth, 1, file);
91 std::ifstream ifs(expPath.generic_string().c_str(), std::ios::binary | std::ios::in);
92 std::string ext = boost::filesystem::extension(expPath).substr(1);
95 Ogre::DataStreamPtr data_stream(
new Ogre::FileStreamDataStream(expPath.generic_string(), &ifs,
false));
96 image.load(data_stream, ext);
100 if (image.getWidth() > optSize || image.getHeight() > optSize)
103 image.resize(optSize, optSize);
107 catch (Ogre::Exception &e)
109 std::cerr <<
"Texture optimization failed! : " << e.what() << std::endl;
114 catch (std::exception &e)
116 std::cerr <<
"File format error : " << e.what() << std::endl;
123 std::string format(texture->achFormatHint);
124 Ogre::PixelFormat oformat = Ogre::PF_UNKNOWN;
125 if (format ==
"argb8888")
126 oformat = Ogre::PF_A8R8G8B8;
127 else if (format ==
"rgba8888")
128 oformat = Ogre::PF_R8G8B8A8;
129 else if (format ==
"rgb888")
130 oformat = Ogre::PF_R8G8B8;
131 else if (format ==
"bgr888")
132 oformat = Ogre::PF_B8G8R8;
133 else if (format ==
"rgba5650")
134 oformat = Ogre::PF_R5G6B5;
136 if (oformat != Ogre::PF_UNKNOWN)
139 image.loadDynamicImage((uchar*)texture->pcData, texture->mWidth, texture->mHeight, oformat);
141 if (optimize && (texture->mWidth > optSize || texture->mHeight > optSize))
142 image.resize(optSize, optSize);
144 std::string ext = boost::filesystem::extension(expPath).substr(1);
149 catch (Ogre::Exception &e)
151 std::cerr <<
"File format error : " << e.what() << std::endl;
165 std::ifstream ifs(texPath.generic_string().c_str(), std::ios::binary | std::ios::in);
166 std::string ext = boost::filesystem::extension(texPath).substr(1);
169 Ogre::DataStreamPtr data_stream(
new Ogre::FileStreamDataStream(texPath.generic_string(), &ifs,
false));
170 image.load(data_stream, ext);
174 if (image.getWidth() > optSize || image.getHeight() > optSize)
177 image.resize(optSize, optSize);
184 boost::filesystem::copy_file(texPath, expPath, boost::filesystem::copy_option::overwrite_if_exists);
186 catch (boost::filesystem::filesystem_error &e)
188 std::cerr <<
"File already exist : " << e.what() << std::endl;
192 catch (Ogre::Exception &e)
194 std::cerr <<
"Texture optimization failed! : " << e.what() << std::endl;
201 boost::filesystem::copy_file(texPath, expPath, boost::filesystem::copy_option::overwrite_if_exists);
203 catch (boost::filesystem::filesystem_error &e)
205 std::cerr <<
"File already exist : " << e.what() << std::endl;
211 texData->
path = expPath.generic_string();
214 void ALMaterial::buildData(
int count, aiTextureType type, std::vector<ALTextureData>& datas)
216 for (
int n = 0; n < count; ++n)
218 ALTextureData texData;
220 texData.embedded =
false;
221 texData.useAlpha =
false;
222 texData.mapmode[0] = aiTextureMapMode_Wrap;
223 texData.mapmode[1] = aiTextureMapMode_Wrap;
224 texData.mapmode[2] = aiTextureMapMode_Wrap;
228 mMat->GetTexture(type, n, &aipath, &texData.mapping, &texData.uvindex, &texData.blend, &texData.op, texData.mapmode);
231 if (AI_SUCCESS == mMat->Get(AI_MATKEY_TEXFLAGS(type, n), tFlags))
233 if ((tFlags &aiTextureFlags_UseAlpha) || !(tFlags &aiTextureFlags_IgnoreAlpha))
234 texData.useAlpha =
true;
238 texData.useAlpha =
true;
241 boost::filesystem::path tpath(aipath.C_Str());
244 if (
const aiTexture* texture = mScene->
getAiScene()->GetEmbeddedTexture(aipath.C_Str()))
246 tpath = boost::filesystem::path(texture->mFilename.C_Str());
251 std::string format(texture->achFormatHint);
253 if (texture->mHeight)
256 std::string genname = std::string(aipath.C_Str());
257 genname = genname.substr(1);
258 genname =
"texgen_" + genname +
"." + format;
259 tpath = boost::filesystem::path(genname);
261 texData.embname = aipath.C_Str();
262 texData.embedded =
true;
265 else if (!tpath.is_absolute())
270 tpath = boost::filesystem::canonical(tpath, base);
272 catch (boost::filesystem::filesystem_error &e)
274 std::cerr <<
"File not found : " << e.what() << std::endl;
282 exist = boost::filesystem::exists(tpath);
284 catch (boost::filesystem::filesystem_error &e)
286 std::cerr <<
"File not found : " << e.what() << std::endl;
292 std::string filename;
293 boost::filesystem::path filepath(tpath);
294 filename = filepath.generic_string();
296 if (filename.find_last_of(
'/') != std::string::npos)
297 filename = filename.substr(filename.find_last_of(
'/'));
305 exist = boost::filesystem::exists(tpath);
307 catch (boost::filesystem::filesystem_error &)
320 exist = boost::filesystem::exists(tpath);
322 catch (boost::filesystem::filesystem_error &)
336 exist = boost::filesystem::exists(tpath);
338 catch (boost::filesystem::filesystem_error &)
352 exist = boost::filesystem::exists(tpath);
354 catch (boost::filesystem::filesystem_error &)
367 texData.path = tpath.generic_string();
370 if (texCount.
metalCount && ((type == aiTextureType_LIGHTMAP) || (type == aiTextureType_AMBIENT_OCCLUSION)))
373 for (
size_t j = 0; j < datas.size(); ++j)
375 ALTextureData cd = datas[j];
376 if (((cd.type == aiTextureType_METALNESS) || (cd.type == aiTextureType_DIFFUSE_ROUGHNESS)) && (cd.path == texData.path))
384 datas.push_back(texData);
387 datas.push_back(texData);
391 void ALMaterial::getTexturesCount()
394 texCount.
metalCount = mMat->GetTextureCount(aiTextureType_METALNESS);
395 texCount.
roughCount = mMat->GetTextureCount(aiTextureType_DIFFUSE_ROUGHNESS);
398 texCount.
difCount = mMat->GetTextureCount(aiTextureType_DIFFUSE);
399 texCount.
specCount = mMat->GetTextureCount(aiTextureType_SPECULAR);
400 texCount.
ambCount = mMat->GetTextureCount(aiTextureType_AMBIENT);
401 texCount.
ambOccCount = mMat->GetTextureCount(aiTextureType_AMBIENT_OCCLUSION);
402 texCount.
emiCount = mMat->GetTextureCount(aiTextureType_EMISSIVE);
405 texCount.
hmapCount = mMat->GetTextureCount(aiTextureType_HEIGHT);
406 texCount.
normCount = mMat->GetTextureCount(aiTextureType_NORMALS);
407 texCount.
dmapCount = mMat->GetTextureCount(aiTextureType_DISPLACEMENT);
410 texCount.
shineCount = mMat->GetTextureCount(aiTextureType_SHININESS);
411 texCount.
opacityCount = mMat->GetTextureCount(aiTextureType_OPACITY);
412 texCount.
lightmapCount = mMat->GetTextureCount(aiTextureType_LIGHTMAP);
413 texCount.
reflectionCount = mMat->GetTextureCount(aiTextureType_REFLECTION);
416 void ALMaterial::getTexturesInfo()
421 mColors.
diffuse = aiColor4D(1.f, 1.f, 1.f, 1.f);
422 mColors.
ambient = aiColor4D(1.f, 1.f, 1.f, 1.f);
426 mColors.
emissive = aiColor4D(0.f, 0.f, 0.f, 1.f);
428 buildData(texCount.
ambCount, aiTextureType_AMBIENT, texInfo);
429 buildData(texCount.
difCount, aiTextureType_DIFFUSE, texInfo);
430 buildData(texCount.
specCount, aiTextureType_SPECULAR, texInfo);
431 buildData(texCount.
hmapCount, aiTextureType_HEIGHT, texInfo);
432 buildData(texCount.
normCount, aiTextureType_NORMALS, texInfo);
433 buildData(texCount.
dmapCount, aiTextureType_DISPLACEMENT, texInfo);
434 buildData(texCount.
shineCount, aiTextureType_SHININESS, texInfo);
435 buildData(texCount.
reflectionCount, aiTextureType_REFLECTION, texInfo);
436 buildData(texCount.
emiCount, aiTextureType_EMISSIVE, texInfo);
439 buildData(texCount.
roughCount, aiTextureType_DIFFUSE_ROUGHNESS, texInfo);
441 buildData(texCount.
metalCount, aiTextureType_METALNESS, texInfo);
443 buildData(texCount.
ambOccCount, aiTextureType_AMBIENT_OCCLUSION, texInfo);
444 buildData(texCount.
lightmapCount, aiTextureType_LIGHTMAP, texInfo);
446 buildData(texCount.
opacityCount, aiTextureType_OPACITY, texInfo);
449 void ALMaterial::getColorInfo()
456 mColors.
diffuse = aiColor4D(1.f, 1.f, 1.f, 1.f);
457 if (AI_SUCCESS != mMat->Get(AI_MATKEY_COLOR_DIFFUSE, mColors.
diffuse))
459 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(
"Warning: No diffuse in " + matInfo.
name +
" Material.");
464 mColors.
diffuse = aiColor4D(1.f, 1.f, 1.f, 1.f);
466 mColors.
specular = aiColor4D(0.0f, 0.0f, 0.0f, 1.0f);
467 if (AI_SUCCESS != mMat->Get(AI_MATKEY_COLOR_SPECULAR, mColors.
specular))
469 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(
"Warning: No specular in " + matInfo.
name +
" Material.");
472 mColors.
ambient = aiColor4D(1.f, 1.f, 1.f, 1.f);
473 if (AI_SUCCESS != mMat->Get(AI_MATKEY_COLOR_AMBIENT, mColors.
ambient))
475 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(
"Warning: No ambient in " + matInfo.
name +
" Material.");
480 mColors.
ambient = aiColor4D(1.f, 1.f, 1.f, 1.f);
482 mColors.
emissive = aiColor4D(0.f, 0.f, 0.f, 1.f);
483 if (AI_SUCCESS != mMat->Get(AI_MATKEY_COLOR_EMISSIVE, mColors.
emissive))
485 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(
"Warning: No emissive in " + matInfo.
name +
" Material.");
489 if (AI_SUCCESS != mMat->Get(AI_MATKEY_COLOR_TRANSPARENT, mColors.
transparence))
491 Ogre::LogManager::getSingleton().getDefaultLog()->logMessage(
"Warning: No transparent in " + matInfo.
name +
" Material.");
495 if (AI_SUCCESS == mMat->Get(
"$mat.gltf.alphaMode", 0, 0, amode))
497 if (std::string(amode.C_Str()) ==
"BLEND")
501 else if (std::string(amode.C_Str()) ==
"MASK")
506 mHaveAlphaDetails =
true;
510 void ALMaterial::getMatInfo()
512 aiString aiMatName(
"");
513 if ((AI_SUCCESS != mMat->Get(AI_MATKEY_NAME, aiMatName)) || (aiMatName.length == 0))
524 if (AI_SUCCESS != mMat->Get(AI_MATKEY_TWOSIDED, twosided))
529 matInfo.
isTwoSided = (twosided == 0) ?
false : true;
532 if (AI_SUCCESS != mMat->Get(AI_MATKEY_ENABLE_WIREFRAME, wireframe))
537 matInfo.
isWire = (wireframe == 0) ?
false : true;
539 if (AI_SUCCESS != mMat->Get(AI_MATKEY_SHININESS, matInfo.
shininess))
544 if (AI_SUCCESS != mMat->Get(AI_MATKEY_OPACITY, matInfo.
opacity))
551 if (AI_SUCCESS != mMat->Get(AI_MATKEY_REFLECTIVITY, matInfo.
reflectivity))
557 void ALMaterial::writeOgreScript(std::ostream& script, boost::filesystem::path expPath)
559 script <<
"material \"" << matInfo.
name <<
"\"\n";
562 bool needBasicScheme =
false;
574 writeTechnique(script, expPath,
true);
579 void ALMaterial::writeTechnique(std::ostream& script, boost::filesystem::path expPath,
bool extended,
bool basicscheme)
582 script <<
"\ttechnique ";
583 script << matInfo.
name;
588 script <<
"_technique" <<
"\n";
592 script <<
"\t\tscheme basic\n";
594 writePass(script, expPath, extended);
596 script <<
"\t}" <<
"\n";
599 void ALMaterial::writePass(std::ostream& script, boost::filesystem::path expPath,
bool extended)
601 bool hasAlphaBitmap =
false;
602 if (mHaveAlphaDetails)
604 hasAlphaBitmap = mUseAlpha;
608 for (
size_t n = 0; n < texInfo.size(); ++n)
611 boost::filesystem::path fpath(texInfo[n].path);
612 bool isBmp = (fpath.extension() ==
".bmp") || (fpath.extension() ==
".BMP");
616 if (texInfo[n].type == aiTextureType_DIFFUSE)
621 std::string texPath = texInfo[n].path;
622 if (fpath.is_absolute())
626 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(fpath.parent_path().generic_string(),
"FileSystem", mScene->
getRessourceGroup());
627 texPath = fpath.filename().generic_string();
629 catch (Ogre::Exception&)
635 hasAlphaBitmap = img.getHasAlpha() && texInfo[n].useAlpha;
637 catch (Ogre::Exception &)
642 else if (texInfo[n].type == aiTextureType_OPACITY)
644 hasAlphaBitmap =
true;
651 script <<
"\t\tpass ";
652 script << matInfo.
name <<
"_";
654 script <<
"standard\n";
660 script <<
"\t\t\tcull_hardware none\n";
666 script <<
"\t\t\tpolygon_mode points\n";
667 script <<
"\t\t\tpolygon_mode_overrideable false\n";
668 script <<
"\t\t\tpoint_size 1.5\n";
677 script <<
"\t\t\tambient vertexcolour\n";
679 script <<
"\t\t\tambient " << 1.0f <<
" " << 1.0f <<
" " << 1.0f <<
" " << matInfo.
opacity <<
"\n";
685 script <<
"\t\t\tdiffuse vertexcolour\n";
687 script <<
"\t\t\tdiffuse " << 1.0f <<
" " << 1.0f <<
" " << 1.0f <<
" " << matInfo.
opacity <<
"\n";
698 if (matInfo.
opacity < 1.0f || (hasAlphaBitmap && !mCutout))
700 script <<
"\n\t\t\tscene_blend alpha_blend\n";
701 script <<
"\t\t\tdepth_write off\n";
705 script <<
"\n\t\t\talpha_rejection greater 128\n";
708 writeTextureUnit(script, expPath, extended);
710 script <<
"\t\t}" <<
"\n";
713 std::string ALMaterial::getTextureName(ALTextureData texData,
bool full,
bool& createTex)
715 std::string path(texData.path);
725 switch (texData.type)
727 case aiTextureType_AMBIENT:
728 tname = full ?
"Ambient_" + tname :
"Ambient";
730 case aiTextureType_AMBIENT_OCCLUSION:
731 tname = full ?
"Ambient_" + tname :
"Ambient";
733 case aiTextureType_LIGHTMAP:
734 tname = full ?
"Lightmap_" + tname :
"Lightmap";
736 case aiTextureType_DIFFUSE:
737 tname = full ?
"Diffuse_" + tname :
"Diffuse";
739 case aiTextureType_EMISSIVE:
740 tname = full ?
"Emissive_" + tname :
"Emissive";
742 case aiTextureType_NORMALS:
743 tname = full ?
"Normal_" + tname :
"Normal";
745 case aiTextureType_DISPLACEMENT:
746 tname = full ?
"Displacement_" + tname :
"Displacement";
748 case aiTextureType_OPACITY:
749 tname = full ?
"Opacity_" + tname :
"Opacity";
751 case aiTextureType_REFLECTION:
752 tname = full ?
"Reflection_" + tname :
"Reflection";
754 case aiTextureType_SPECULAR:
755 tname = full ?
"Specular_" + tname :
"Specular";
757 case aiTextureType_SHININESS:
758 tname = full ?
"Shininess_" + tname :
"Shininess";
760 case aiTextureType_METALNESS:
761 tname = full ?
"Metallic_" + tname :
"Metallic";
763 case aiTextureType_DIFFUSE_ROUGHNESS:
764 tname = full ?
"Roughness_" + tname :
"Roughness";
771 void ALMaterial::writeTextureUnit(std::ostream& script, boost::filesystem::path expPath,
bool extended)
773 packdir mypack = (packdir)SCgetExtra(
"FirstPack");
774 boost::filesystem::path partitionPath(mypack->path);
775 partitionPath = partitionPath.generic_string();
777 for (
size_t n = 0; n < texInfo.size(); ++n)
779 std::string filepath(texInfo[n].path);
780 filepath = filepath.substr(partitionPath.generic_string().length());
782 bool createTex =
false;
783 std::string tname = getTextureName(texInfo[n],
false, createTex);
785 switch (texInfo[n].type)
787 case aiTextureType_NORMALS:
788 case aiTextureType_DISPLACEMENT:
789 case aiTextureType_SPECULAR:
793 script <<
"\n\t\t\ttexture_unit ";
794 script << tname <<
"\n";
795 script <<
"\t\t\t{\n";
796 script <<
"\t\t\t\ttexture ";
803 script <<
"\t\t\t\ttex_coord_set " << texInfo[n].uvindex <<
"\n";
806 for (
unsigned int iv = 0; iv < 3; ++iv)
808 switch (texInfo[n].mapmode[0])
810 case aiTextureMapMode_Wrap:
814 case aiTextureMapMode_Clamp:
818 case aiTextureMapMode_Mirror:
822 case aiTextureMapMode_Decal:
831 script <<
"\t\t\t\ttex_address_mode " << oguv[0] <<
" " << oguv[1] <<
" " << oguv[2] <<
"\n";
833 script <<
"\t\t\t\tcolour_op ";
834 if (texInfo[n].op == aiTextureOp_Multiply)
835 script <<
"modulate";
836 else if (texInfo[n].op == aiTextureOp_Add)
839 script <<
"modulate";
844 script <<
"\t\t\t}" <<
"\n";
849 case aiTextureType_DIFFUSE:
850 case aiTextureType_AMBIENT:
851 case aiTextureType_AMBIENT_OCCLUSION:
852 case aiTextureType_LIGHTMAP:
853 case aiTextureType_EMISSIVE:
855 script <<
"\n\t\t\ttexture_unit ";
856 script << tname <<
"\n";
857 script <<
"\t\t\t{\n";
858 script <<
"\t\t\t\ttexture ";
865 script <<
"\t\t\t\ttex_coord_set " << texInfo[n].uvindex <<
"\n";
868 for (
unsigned int iv = 0; iv < 3; ++iv)
870 switch (texInfo[n].mapmode[0])
872 case aiTextureMapMode_Wrap:
876 case aiTextureMapMode_Clamp:
880 case aiTextureMapMode_Mirror:
884 case aiTextureMapMode_Decal:
893 script <<
"\t\t\t\ttex_address_mode " << oguv[0] <<
" " << oguv[1] <<
" " << oguv[2] <<
"\n";
895 script <<
"\t\t\t\tcolour_op ";
896 if (texInfo[n].op == aiTextureOp_Add || texInfo[n].type == aiTextureType_EMISSIVE)
898 else if (texInfo[n].op == aiTextureOp_Multiply)
899 script <<
"modulate";
901 script <<
"modulate";
906 script <<
"\t\t\t}" <<
"\n";
910 case aiTextureType_REFLECTION:
912 bool hasCubicReflection =
false;
917 if (img.getNumFaces() == 6)
918 hasCubicReflection =
true;
920 catch (Ogre::Exception &)
925 script <<
"\n\t\t\ttexture_unit ";
926 script << tname << (hasCubicReflection ?
" cubic\n" :
"\n");
927 script <<
"\t\t\t{\n";
928 script <<
"\t\t\t\ttexture ";
932 if (hasCubicReflection)
938 script <<
"\t\t\t\ttex_coord_set " << texInfo[n].uvindex <<
"\n";
941 for (
unsigned int iv = 0; iv < 3; ++iv)
943 switch (texInfo[n].mapmode[0])
945 case aiTextureMapMode_Wrap:
949 case aiTextureMapMode_Clamp:
953 case aiTextureMapMode_Mirror:
957 case aiTextureMapMode_Decal:
966 script <<
"\t\t\t\ttex_address_mode " << oguv[0] <<
" " << oguv[1] <<
" " << oguv[2] <<
"\n";
968 script <<
"\t\t\t\tcolour_op ";
969 if (texInfo[n].op == aiTextureOp_Multiply)
970 script <<
"modulate";
971 else if (texInfo[n].op == aiTextureOp_Add)
974 script <<
"modulate";
978 if (hasCubicReflection)
979 script <<
"\t\t\t\tenv_map cubic_reflection\n";
982 switch (texInfo[n].mapping)
984 case aiTextureMapping_PLANE:
985 script <<
"\t\t\t\tenv_map planar\n";
988 case aiTextureMapping_SPHERE:
989 script <<
"\t\t\t\tenv_map spherical\n";
992 case aiTextureMapping_CYLINDER:
993 script <<
"\t\t\t\tenv_map spherical\n";
997 script <<
"\t\t\t\tenv_map planar\n";
1002 script <<
"\t\t\t}" <<
"\n";
1006 case aiTextureType_DIFFUSE_ROUGHNESS:
1007 case aiTextureType_METALNESS:
1011 script <<
"\n\t\t\ttexture_unit ";
1012 script << tname <<
"\n";
1013 script <<
"\t\t\t{\n";
1014 script <<
"\t\t\t\ttexture ";
1021 script <<
"\t\t\t\ttex_coord_set " << texInfo[n].uvindex <<
"\n";
1023 std::string oguv[3];
1024 for (
unsigned int iv = 0; iv < 3; ++iv)
1026 switch (texInfo[n].mapmode[0])
1028 case aiTextureMapMode_Wrap:
1032 case aiTextureMapMode_Clamp:
1036 case aiTextureMapMode_Mirror:
1037 oguv[iv] =
"mirror";
1040 case aiTextureMapMode_Decal:
1041 oguv[iv] =
"border";
1049 script <<
"\t\t\t\ttex_address_mode " << oguv[0] <<
" " << oguv[1] <<
" " << oguv[2] <<
"\n";
1051 script <<
"\t\t\t\tcolour_op ";
1052 if (texInfo[n].op == aiTextureOp_Multiply)
1053 script <<
"modulate";
1054 else if (texInfo[n].op == aiTextureOp_Add)
1062 script <<
"\t\t\t}" <<
"\n";
1067 case aiTextureType_OPACITY:
1069 script <<
"\n\t\t\ttexture_unit ";
1070 script << tname <<
"\n";
1071 script <<
"\t\t\t{\n";
1072 script <<
"\t\t\t\ttexture ";
1079 script <<
"\t\t\t\ttex_coord_set " << texInfo[n].uvindex <<
"\n";
1081 std::string oguv[3];
1082 for (
unsigned int iv = 0; iv < 3; ++iv)
1084 switch (texInfo[n].mapmode[0])
1086 case aiTextureMapMode_Wrap:
1090 case aiTextureMapMode_Clamp:
1094 case aiTextureMapMode_Mirror:
1095 oguv[iv] =
"mirror";
1098 case aiTextureMapMode_Decal:
1099 oguv[iv] =
"border";
1107 script <<
"\t\t\t\ttex_address_mode " << oguv[0] <<
" " << oguv[1] <<
" " << oguv[2] <<
"\n";
1109 script <<
"\t\t\t\tcolour_op ";
1110 if (texInfo[n].op == aiTextureOp_Multiply)
1111 script <<
"modulate";
1112 else if (texInfo[n].op == aiTextureOp_Add)
1115 script <<
"alpha_blend";
1120 script <<
"\t\t\t}" <<
"\n";
1140 for (
size_t n = 0; n < texInfo.size(); ++n)
1141 manageTexturePath(&texInfo[n], expPath);
1144 std::string outPath;
1145 outPath = expPath.generic_string();
1146 if (outPath.back() !=
'/')
1147 outPath.push_back(
'/');
1148 outPath +=
"materials/";
1152 std::ofstream script;
1154 script.open(outPath, std::ofstream::out | std::ofstream::trunc);
1156 script.open(outPath, std::ofstream::out | std::ofstream::app | std::ofstream::ate);
1158 script.precision(8);
1159 script.setf(std::ios::fixed, std::ios::floatfield);
1160 writeOgreScript(script, expPath);
1163 return matInfo.
name;
1166 STexture* ALMaterial::createOrRetrieveTexture(std::string name, std::string path,
bool create)
1185 if (!texture && !create)
1199 STexture* ALMaterial::createOrRetrieveTexture(std::string name, Ogre::Image image,
bool create)
1201 STexture *texture = 0;
1218 if (!texture && !create)
1232 STexture* ALMaterial::loadTexture(ALTextureData* texData, std::string tname,
bool createTex,
bool &isCubic)
1234 STexture* texture = 0;
1236 if (texData->embedded)
1238 if (
const aiTexture* aitexture = mScene->
getAiScene()->GetEmbeddedTexture(texData->embname.c_str()))
1240 if (!aitexture->mHeight)
1245 std::string ext = boost::filesystem::extension(texData->path).substr(1);
1246 Ogre::DataStreamPtr data_stream(
new Ogre::MemoryDataStream(texData->path, aitexture->pcData, aitexture->mWidth,
false,
true));
1247 image.load(data_stream, ext);
1248 if (image.getNumFaces() == 6)
1250 texture = createOrRetrieveTexture(tname, image, createTex);
1252 catch (Ogre::Exception &e)
1254 std::cerr <<
"Texture loading failed! : " << e.what() << std::endl;
1262 std::string format(aitexture->achFormatHint);
1263 Ogre::PixelFormat oformat = Ogre::PF_UNKNOWN;
1264 if (format ==
"argb8888")
1265 oformat = Ogre::PF_A8R8G8B8;
1266 else if (format ==
"rgba8888")
1267 oformat = Ogre::PF_R8G8B8A8;
1268 else if (format ==
"rgb888")
1269 oformat = Ogre::PF_R8G8B8;
1270 else if (format ==
"bgr888")
1271 oformat = Ogre::PF_B8G8R8;
1272 else if (format ==
"rgba5650")
1273 oformat = Ogre::PF_R5G6B5;
1275 if (oformat != Ogre::PF_UNKNOWN)
1278 image.loadDynamicImage((uchar*)aitexture->pcData, aitexture->mWidth, aitexture->mHeight, oformat);
1279 if (image.getNumFaces() == 6)
1281 texture = createOrRetrieveTexture(tname, image, createTex);
1284 catch (Ogre::Exception &e)
1286 std::cerr <<
"Texture loading failed! : " << e.what() << std::endl;
1296 if (img.getNumFaces() == 6)
1299 catch (Ogre::Exception &)
1304 texture = createOrRetrieveTexture(tname, texData->path, createTex);
1339 for (
unsigned int n = 0; n < nbTech; ++n)
1344 for (
unsigned int n = 0; n < nbPass; ++n)
1367 ambcolor = Ogre::ColourValue(1.0f, 1.0f, 1.0f, matInfo.
opacity);
1370 diffcolor = Ogre::ColourValue(1.0f, 1.0f, 1.0f, matInfo.
opacity);
1386 bool hasAlphaBitmap =
false;
1387 if (mHaveAlphaDetails)
1389 hasAlphaBitmap = mUseAlpha;
1393 for (
size_t n = 0; n < texInfo.size(); ++n)
1395 if (texInfo[n].type == aiTextureType_DIFFUSE)
1400 boost::filesystem::path fpath(texInfo[n].path);
1401 std::string texPath = texInfo[n].path;
1402 if (fpath.is_absolute())
1406 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(fpath.parent_path().generic_string(),
"FileSystem", mScene->
getRessourceGroup());
1407 texPath = fpath.filename().generic_string();
1409 catch (Ogre::Exception&)
1415 hasAlphaBitmap = img.getHasAlpha() && texInfo[n].useAlpha;
1417 catch (Ogre::Exception &)
1423 else if (texInfo[n].type == aiTextureType_OPACITY)
1425 hasAlphaBitmap =
true;
1430 if (matInfo.
opacity < 1.0f || (hasAlphaBitmap && !mCutout))
1439 unsigned int texIndex = 0;
1440 bool createTex =
false;
1441 bool ignore =
false;
1442 for (
size_t n = 0; n < texInfo.size(); ++n)
1444 std::string tname = getTextureName(texInfo[n],
true, createTex);
1445 std::string unitname = getTextureName(texInfo[n],
false, ignore);
1448 std::string path(texData->
path);
1449 boost::filesystem::path fpath(path);
1450 if (fpath.is_absolute())
1454 Ogre::ResourceGroupManager::getSingleton().addResourceLocation(fpath.parent_path().generic_string(),
"FileSystem", mScene->
getRessourceGroup());
1455 path = fpath.filename().generic_string();
1457 catch (Ogre::Exception&)
1463 switch (texData->
type)
1465 case aiTextureType_NORMALS:
1466 case aiTextureType_DISPLACEMENT:
1467 case aiTextureType_SPECULAR:
1468 case aiTextureType_DIFFUSE:
1469 case aiTextureType_DIFFUSE_ROUGHNESS:
1470 case aiTextureType_AMBIENT:
1471 case aiTextureType_AMBIENT_OCCLUSION:
1472 case aiTextureType_LIGHTMAP:
1473 case aiTextureType_EMISSIVE:
1474 case aiTextureType_METALNESS:
1476 bool isCubic =
false;
1477 STexture* texture = loadTexture(texData, tname, createTex, isCubic);
1485 pass->
SetTexture(texIndex, texture, unitname);
1488 Ogre::TextureAddressingMode oguv[3];
1489 for (
unsigned int iv = 0; iv < 3; ++iv)
1493 case aiTextureMapMode_Wrap:
1494 oguv[iv] = Ogre::TextureUnitState::TAM_WRAP;
1497 case aiTextureMapMode_Clamp:
1498 oguv[iv] = Ogre::TextureUnitState::TAM_CLAMP;
1501 case aiTextureMapMode_Mirror:
1502 oguv[iv] = Ogre::TextureUnitState::TAM_MIRROR;
1505 case aiTextureMapMode_Decal:
1506 oguv[iv] = Ogre::TextureUnitState::TAM_BORDER;
1510 oguv[iv] = Ogre::TextureUnitState::TAM_WRAP;
1514 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setTextureAddressingMode(oguv[0], oguv[1], oguv[2]);
1516 if (texData->
op == aiTextureOp_Multiply)
1517 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setColourOperation(Ogre::LBO_MODULATE);
1518 else if (texData->
op == aiTextureOp_Add || texData->
type == aiTextureType_METALNESS || texData->
type == aiTextureType_DIFFUSE_ROUGHNESS || texData->
type == aiTextureType_EMISSIVE)
1519 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setColourOperation(Ogre::LBO_ADD);
1521 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setColourOperation(Ogre::LBO_MODULATE);
1525 catch (Ogre::Exception&)
1532 case aiTextureType_REFLECTION:
1534 bool isCubic =
false;
1535 STexture* texture = loadTexture(texData, tname, createTex, isCubic);
1543 pass->
SetTexture(texIndex, texture, unitname);
1549 Ogre::TextureAddressingMode oguv[3];
1550 for (
unsigned int iv = 0; iv < 3; ++iv)
1554 case aiTextureMapMode_Wrap:
1555 oguv[iv] = Ogre::TextureUnitState::TAM_WRAP;
1558 case aiTextureMapMode_Clamp:
1559 oguv[iv] = Ogre::TextureUnitState::TAM_CLAMP;
1562 case aiTextureMapMode_Mirror:
1563 oguv[iv] = Ogre::TextureUnitState::TAM_MIRROR;
1566 case aiTextureMapMode_Decal:
1567 oguv[iv] = Ogre::TextureUnitState::TAM_BORDER;
1571 oguv[iv] = Ogre::TextureUnitState::TAM_WRAP;
1575 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setTextureAddressingMode(oguv[0], oguv[1], oguv[2]);
1577 if (texData->
op == aiTextureOp_Multiply)
1578 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setColourOperation(Ogre::LBO_MODULATE);
1579 else if (texData->
op == aiTextureOp_Add)
1580 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setColourOperation(Ogre::LBO_ADD);
1582 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setColourOperation(Ogre::LBO_MODULATE);
1584 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setEnvironmentMap(
true, isCubic ? Ogre::TextureUnitState::ENV_REFLECTION : Ogre::TextureUnitState::ENV_PLANAR);
1588 catch (Ogre::Exception&)
1595 case aiTextureType_OPACITY:
1597 bool isCubic =
false;
1598 STexture* texture = loadTexture(texData, tname, createTex, isCubic);
1606 pass->
SetTexture(texIndex, texture, unitname);
1609 Ogre::TextureAddressingMode oguv[3];
1610 for (
unsigned int iv = 0; iv < 3; ++iv)
1614 case aiTextureMapMode_Wrap:
1615 oguv[iv] = Ogre::TextureUnitState::TAM_WRAP;
1618 case aiTextureMapMode_Clamp:
1619 oguv[iv] = Ogre::TextureUnitState::TAM_CLAMP;
1622 case aiTextureMapMode_Mirror:
1623 oguv[iv] = Ogre::TextureUnitState::TAM_MIRROR;
1626 case aiTextureMapMode_Decal:
1627 oguv[iv] = Ogre::TextureUnitState::TAM_BORDER;
1631 oguv[iv] = Ogre::TextureUnitState::TAM_WRAP;
1635 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setTextureAddressingMode(oguv[0], oguv[1], oguv[2]);
1637 if (texData->
op == aiTextureOp_Multiply)
1638 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setColourOperation(Ogre::LBO_MODULATE);
1639 else if (texData->
op == aiTextureOp_Add)
1640 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setColourOperation(Ogre::LBO_ADD);
1642 pass->
GetOgrePassPointer()->getTextureUnitState(texIndex)->setColourOperation(Ogre::LBO_ALPHA_BLEND);
1646 catch (Ogre::Exception&)
static std::string cleanString(std::string str, bool toLower=true, bool clASCII=true, bool clSpaces=true, bool bUID=true)
Clean a string.
std::string convert(boost::filesystem::path expPath, bool clearFile=false)
ALMaterial(ALScene *alscene, aiMaterial *srcMaterial, bool useVertexColor=false)
std::string getRessourceGroup()
ALSceneLoader * GetSceneLoader()
const aiScene * getAiScene()
bool addTextureRef(std::string name, std::string path)
std::string getSceneName()
std::string getTextureRef(std::string path)
std::string getRessourcePath()
Base class for SO3 custom exception.
STechnique * GetTechnique(const unsigned int &technique)
void SetPolygonMode(PolygonMode mode)
void SetPointSize(Ogre::Real size)
@ SO3_COMPARE_FUNCTION_GREATER
void SetSelfIlluminationColor(const int &color)
void SetShininess(const float &value)
void BuildShader(bool force=false)
void SetAmbientColor(const int &color)
void SetSpecularColor(const int &color)
void SetAlphaRejection(CompareFunction func, unsigned char value, bool alphaToCoverageEnabled=false)
void SetSceneBlending(const SPass::SceneBlendFactor sourceFactor, const SPass::SceneBlendFactor destFactor)
void SetDepthWriteEnabled(bool enabled)
void SetUseVertexColor(bool state)
void OrderTexturesUnitsByType()
void SetTexture(const unsigned int &textureUnit, STexture *texture, std::string name="")
void SetDiffuseColor(const int &color)
Ogre::Pass * GetOgrePassPointer()
@ SO3_SCENE_BLEND_FACTOR_SOURCE_ALPHA
@ SO3_SCENE_BLEND_FACTOR_ONE_MINUS_SOURCE_ALPHA
STexture * CreateTexture(const std::string &groupname, const std::string &texname, const std::string &path, const int &w=0, const int &h=0)
SMaterial * GetMaterial(const std::string &groupName, const std::string &materialName, bool searchOtherGroups=true)
STexture * GetTexture(const std::string &groupName, const std::string &texName)
SMaterial * CreateMaterial(const std::string &groupname, const std::string &matname, const bool &loadedFromScript=false)
SPass * GetPass(const unsigned int &pass)
Ogre::TexturePtr getOgreTexturePointer()
Contain the texture's meta-data.
aiTextureMapMode mapmode[3]