2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2021-02-28 11:17:54 +00:00
|
|
|
Copyright (c) 2006-2021, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-05-19 03:48:29 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
|
|
|
|
|
|
|
|
#include "OgreImporter.h"
|
2021-03-09 20:08:28 +00:00
|
|
|
#include <assimp/StringUtils.h>
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/TinyFormatter.h>
|
2021-03-09 20:08:28 +00:00
|
|
|
#include <assimp/fast_atof.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/material.h>
|
|
|
|
#include <assimp/scene.h>
|
|
|
|
#include <assimp/DefaultLogger.hpp>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
#include <memory>
|
2021-03-09 20:08:28 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <vector>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
namespace Assimp {
|
|
|
|
namespace Ogre {
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
static const string partComment = "//";
|
2015-05-19 03:48:29 +00:00
|
|
|
static const string partBlockStart = "{";
|
2021-03-09 20:08:28 +00:00
|
|
|
static const string partBlockEnd = "}";
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
void OgreImporter::ReadMaterials(const std::string &pFile, Assimp::IOSystem *pIOHandler, aiScene *pScene, Mesh *mesh) {
|
|
|
|
std::vector<aiMaterial *> materials;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Create materials that can be found and parsed via the IOSystem.
|
2021-03-09 20:08:28 +00:00
|
|
|
for (size_t i = 0, len = mesh->NumSubMeshes(); i < len; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
SubMesh *submesh = mesh->GetSubMesh(i);
|
2021-03-09 20:08:28 +00:00
|
|
|
if (submesh && !submesh->materialRef.empty()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMaterial *material = ReadMaterial(pFile, pIOHandler, submesh->materialRef);
|
2021-03-09 20:08:28 +00:00
|
|
|
if (material) {
|
2016-11-20 01:49:33 +00:00
|
|
|
submesh->materialIndex = static_cast<int>(materials.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
materials.push_back(material);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AssignMaterials(pScene, materials);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
void OgreImporter::ReadMaterials(const std::string &pFile, Assimp::IOSystem *pIOHandler, aiScene *pScene, MeshXml *mesh) {
|
|
|
|
std::vector<aiMaterial *> materials;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// Create materials that can be found and parsed via the IOSystem.
|
2021-03-09 20:08:28 +00:00
|
|
|
for (size_t i = 0, len = mesh->NumSubMeshes(); i < len; ++i) {
|
|
|
|
SubMeshXml *submesh = mesh->GetSubMesh(static_cast<uint16_t>(i));
|
|
|
|
if (submesh && !submesh->materialRef.empty()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
aiMaterial *material = ReadMaterial(pFile, pIOHandler, submesh->materialRef);
|
2021-03-09 20:08:28 +00:00
|
|
|
if (material) {
|
2016-11-20 01:49:33 +00:00
|
|
|
submesh->materialIndex = static_cast<int>(materials.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
materials.push_back(material);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AssignMaterials(pScene, materials);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
void OgreImporter::AssignMaterials(aiScene *pScene, std::vector<aiMaterial *> &materials) {
|
2016-11-20 01:49:33 +00:00
|
|
|
pScene->mNumMaterials = static_cast<unsigned int>(materials.size());
|
2021-03-09 20:08:28 +00:00
|
|
|
if (pScene->mNumMaterials > 0) {
|
|
|
|
pScene->mMaterials = new aiMaterial *[pScene->mNumMaterials];
|
|
|
|
for (size_t i = 0; i < pScene->mNumMaterials; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mMaterials[i] = materials[i];
|
|
|
|
}
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
aiMaterial *OgreImporter::ReadMaterial(const std::string &pFile, Assimp::IOSystem *pIOHandler, const std::string &materialName) {
|
2015-05-19 03:57:13 +00:00
|
|
|
if (materialName.empty()) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full reference and examples of Ogre Material Script
|
|
|
|
// can be found from http://www.ogre3d.org/docs/manual/manual_14.html
|
|
|
|
|
|
|
|
/*and here is another one:
|
|
|
|
|
|
|
|
import * from abstract_base_passes_depth.material
|
|
|
|
import * from abstract_base.material
|
|
|
|
import * from mat_shadow_caster.material
|
|
|
|
import * from mat_character_singlepass.material
|
|
|
|
|
|
|
|
material hero/hair/caster : mat_shadow_caster_skin_areject
|
|
|
|
{
|
|
|
|
set $diffuse_map "hero_hair_alpha_c.dds"
|
|
|
|
}
|
|
|
|
|
|
|
|
material hero/hair_alpha : mat_char_cns_singlepass_areject_4weights
|
|
|
|
{
|
|
|
|
set $diffuse_map "hero_hair_alpha_c.dds"
|
|
|
|
set $specular_map "hero_hair_alpha_s.dds"
|
|
|
|
set $normal_map "hero_hair_alpha_n.dds"
|
|
|
|
set $light_map "black_lightmap.dds"
|
|
|
|
|
|
|
|
set $shadow_caster_material "hero/hair/caster"
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
stringstream ss;
|
|
|
|
|
|
|
|
// Scope for scopre_ptr auto release
|
|
|
|
{
|
|
|
|
/* There are three .material options in priority order:
|
|
|
|
1) File with the material name (materialName)
|
|
|
|
2) File with the mesh files base name (pFile)
|
|
|
|
3) Optional user defined material library file (m_userDefinedMaterialLibFile) */
|
|
|
|
std::vector<string> potentialFiles;
|
|
|
|
potentialFiles.push_back(materialName + ".material");
|
|
|
|
potentialFiles.push_back(pFile.substr(0, pFile.rfind(".mesh")) + ".material");
|
|
|
|
if (!m_userDefinedMaterialLibFile.empty())
|
|
|
|
potentialFiles.push_back(m_userDefinedMaterialLibFile);
|
|
|
|
|
|
|
|
IOStream *materialFile = 0;
|
2021-03-09 20:08:28 +00:00
|
|
|
for (size_t i = 0; i < potentialFiles.size(); ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
materialFile = pIOHandler->Open(potentialFiles[i]);
|
|
|
|
if (materialFile) {
|
|
|
|
break;
|
|
|
|
}
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Source file for material '", materialName, "' ", potentialFiles[i], " does not exist");
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2021-03-09 20:08:28 +00:00
|
|
|
if (!materialFile) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_ERROR("Failed to find source file for material '", materialName, "'");
|
2015-05-19 03:57:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-05 21:23:53 +00:00
|
|
|
std::unique_ptr<IOStream> stream(materialFile);
|
2021-03-09 20:08:28 +00:00
|
|
|
if (stream->FileSize() == 0) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_WARN("Source file for material '", materialName, "' is empty (size is 0 bytes)");
|
2015-05-19 03:57:13 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read bytes
|
|
|
|
vector<char> data(stream->FileSize());
|
|
|
|
stream->Read(&data[0], stream->FileSize(), 1);
|
|
|
|
|
|
|
|
// Convert to UTF-8 and terminate the string for ss
|
|
|
|
BaseImporter::ConvertToUTF8(data);
|
|
|
|
data.push_back('\0');
|
|
|
|
|
|
|
|
ss << &data[0];
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Reading material '", materialName, "'");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
aiMaterial *material = new aiMaterial();
|
|
|
|
m_textures.clear();
|
|
|
|
|
2020-03-01 12:15:45 +00:00
|
|
|
aiString matName(materialName);
|
|
|
|
material->AddProperty(&matName, AI_MATKEY_NAME);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// The stringstream will push words from a line until newline.
|
|
|
|
// It will also trim whitespace from line start and between words.
|
|
|
|
string linePart;
|
|
|
|
ss >> linePart;
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
const string partMaterial = "material";
|
|
|
|
const string partTechnique = "technique";
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
while (!ss.eof()) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Skip commented lines
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == partComment) {
|
2015-05-19 03:57:13 +00:00
|
|
|
NextAfterNewLine(ss, linePart);
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart != partMaterial) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ss >> linePart;
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart != materialName) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NextAfterNewLine(ss, linePart);
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart != partBlockStart) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_ERROR("Invalid material: block start missing near index ", ss.tellg());
|
2015-05-19 03:57:13 +00:00
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("material '", materialName, "'");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
while (linePart != partBlockEnd) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Proceed to the first technique
|
|
|
|
ss >> linePart;
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == partTechnique) {
|
|
|
|
std::string techniqueName = SkipLine(ss);
|
|
|
|
ReadTechnique(ai_trim(techniqueName), ss, material);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 22:19:26 +00:00
|
|
|
// Read information from a custom material
|
2015-05-19 03:57:13 +00:00
|
|
|
/** @todo This "set $x y" does not seem to be a official Ogre material system feature.
|
|
|
|
Materials can inherit other materials and override texture units by using the (unique)
|
|
|
|
parent texture unit name in your cloned material.
|
|
|
|
This is not yet supported and below code is probably some hack from the original
|
|
|
|
author of this Ogre importer. Should be removed? */
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == "set") {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == "$specular") //todo load this values:
|
2015-05-19 03:57:13 +00:00
|
|
|
{
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == "$diffuse") {
|
|
|
|
} else if (linePart == "$ambient") {
|
|
|
|
} else if (linePart == "$colormap") {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
2020-03-01 12:15:45 +00:00
|
|
|
aiString cm(linePart);
|
|
|
|
material->AddProperty(&cm, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == "$normalmap") {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
2020-03-01 12:15:45 +00:00
|
|
|
aiString nm(linePart);
|
|
|
|
material->AddProperty(&nm, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == "$shininess_strength") {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
|
|
|
float Shininess = fast_atof(linePart.c_str());
|
|
|
|
material->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS_STRENGTH);
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == "$shininess_exponent") {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
|
|
|
float Shininess = fast_atof(linePart.c_str());
|
|
|
|
material->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS);
|
|
|
|
}
|
|
|
|
//Properties from Venetica:
|
2021-03-09 20:08:28 +00:00
|
|
|
else if (linePart == "$diffuse_map") {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart[0] == '"') // "file" -> file
|
|
|
|
linePart = linePart.substr(1, linePart.size() - 2);
|
2015-05-19 03:57:13 +00:00
|
|
|
aiString ts(linePart);
|
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == "$specular_map") {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart[0] == '"') // "file" -> file
|
|
|
|
linePart = linePart.substr(1, linePart.size() - 2);
|
2015-05-19 03:57:13 +00:00
|
|
|
aiString ts(linePart);
|
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_SHININESS, 0));
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == "$normal_map") {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart[0] == '"') // "file" -> file
|
|
|
|
linePart = linePart.substr(1, linePart.size() - 2);
|
2015-05-19 03:57:13 +00:00
|
|
|
aiString ts(linePart);
|
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == "$light_map") {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart[0] == '"') {
|
2015-05-19 03:57:13 +00:00
|
|
|
linePart = linePart.substr(1, linePart.size() - 2);
|
|
|
|
}
|
|
|
|
aiString ts(linePart);
|
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ss >> linePart;
|
|
|
|
}
|
|
|
|
|
|
|
|
return material;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
bool OgreImporter::ReadTechnique(const std::string &techniqueName, stringstream &ss, aiMaterial *material) {
|
2015-05-19 03:57:13 +00:00
|
|
|
string linePart;
|
|
|
|
ss >> linePart;
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart != partBlockStart) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_ERROR("Invalid material: Technique block start missing near index ", ss.tellg());
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" technique '", techniqueName, "'");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
const string partPass = "pass";
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
while (linePart != partBlockEnd) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
// Skip commented lines
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == partComment) {
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipLine(ss);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @todo Techniques have other attributes than just passes.
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == partPass) {
|
2015-05-19 03:57:13 +00:00
|
|
|
string passName = SkipLine(ss);
|
2021-03-09 20:08:28 +00:00
|
|
|
ReadPass(ai_trim(passName), ss, material);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
bool OgreImporter::ReadPass(const std::string &passName, stringstream &ss, aiMaterial *material) {
|
2015-05-19 03:57:13 +00:00
|
|
|
string linePart;
|
|
|
|
ss >> linePart;
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart != partBlockStart) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_ERROR("Invalid material: Pass block start missing near index ", ss.tellg());
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" pass '", passName, "'");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
const string partAmbient = "ambient";
|
|
|
|
const string partDiffuse = "diffuse";
|
|
|
|
const string partSpecular = "specular";
|
|
|
|
const string partEmissive = "emissive";
|
2015-05-19 03:57:13 +00:00
|
|
|
const string partTextureUnit = "texture_unit";
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
while (linePart != partBlockEnd) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
// Skip commented lines
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == partComment) {
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipLine(ss);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Colors
|
|
|
|
/// @todo Support alpha via aiColor4D.
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == partAmbient || linePart == partDiffuse || linePart == partSpecular || linePart == partEmissive) {
|
2015-05-19 03:57:13 +00:00
|
|
|
float r, g, b;
|
|
|
|
ss >> r >> g >> b;
|
|
|
|
const aiColor3D color(r, g, b);
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" ", linePart, " ", r, " ", g, " ", b);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == partAmbient) {
|
2015-05-19 03:57:13 +00:00
|
|
|
material->AddProperty(&color, 1, AI_MATKEY_COLOR_AMBIENT);
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == partDiffuse) {
|
2015-05-19 03:57:13 +00:00
|
|
|
material->AddProperty(&color, 1, AI_MATKEY_COLOR_DIFFUSE);
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == partSpecular) {
|
2015-05-19 03:57:13 +00:00
|
|
|
material->AddProperty(&color, 1, AI_MATKEY_COLOR_SPECULAR);
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == partEmissive) {
|
2015-05-19 03:57:13 +00:00
|
|
|
material->AddProperty(&color, 1, AI_MATKEY_COLOR_EMISSIVE);
|
|
|
|
}
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == partTextureUnit) {
|
2015-05-19 03:57:13 +00:00
|
|
|
string textureUnitName = SkipLine(ss);
|
2021-03-09 20:08:28 +00:00
|
|
|
ReadTextureUnit(ai_trim(textureUnitName), ss, material);
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
bool OgreImporter::ReadTextureUnit(const std::string &textureUnitName, stringstream &ss, aiMaterial *material) {
|
2015-05-19 03:57:13 +00:00
|
|
|
string linePart;
|
|
|
|
ss >> linePart;
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart != partBlockStart) {
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_ERROR("Invalid material: Texture unit block start missing near index ", ss.tellg());
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" texture_unit '", textureUnitName, "'");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
const string partTexture = "texture";
|
2015-05-19 03:57:13 +00:00
|
|
|
const string partTextCoordSet = "tex_coord_set";
|
2021-03-09 20:08:28 +00:00
|
|
|
const string partColorOp = "colour_op";
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
aiTextureType textureType = aiTextureType_NONE;
|
|
|
|
std::string textureRef;
|
|
|
|
int uvCoord = 0;
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
while (linePart != partBlockEnd) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
// Skip commented lines
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == partComment) {
|
2015-05-19 03:57:13 +00:00
|
|
|
SkipLine(ss);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
if (linePart == partTexture) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> linePart;
|
|
|
|
textureRef = linePart;
|
|
|
|
|
|
|
|
// User defined Assimp config property to detect texture type from filename.
|
2021-03-09 20:08:28 +00:00
|
|
|
if (m_detectTextureTypeFromFilename) {
|
2021-06-22 16:27:15 +00:00
|
|
|
size_t posSuffix = textureRef.find_last_of('.');
|
|
|
|
size_t posUnderscore = textureRef.find_last_of('_');
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
if (posSuffix != string::npos && posUnderscore != string::npos && posSuffix > posUnderscore) {
|
2021-04-03 08:25:03 +00:00
|
|
|
string identifier = ai_tolower(textureRef.substr(posUnderscore, posSuffix - posUnderscore));
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG("Detecting texture type from filename postfix '", identifier, "'");
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
if (identifier == "_n" || identifier == "_nrm" || identifier == "_nrml" || identifier == "_normal" || identifier == "_normals" || identifier == "_normalmap") {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_NORMALS;
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (identifier == "_s" || identifier == "_spec" || identifier == "_specular" || identifier == "_specularmap") {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_SPECULAR;
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (identifier == "_l" || identifier == "_light" || identifier == "_lightmap" || identifier == "_occ" || identifier == "_occlusion") {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_LIGHTMAP;
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (identifier == "_disp" || identifier == "_displacement") {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_DISPLACEMENT;
|
2021-03-09 20:08:28 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_DIFFUSE;
|
|
|
|
}
|
2021-03-09 20:08:28 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_DIFFUSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Detect from texture unit name. This cannot be too broad as
|
|
|
|
// authors might give names like "LightSaber" or "NormalNinja".
|
2021-03-09 20:08:28 +00:00
|
|
|
else {
|
2021-04-03 08:25:03 +00:00
|
|
|
string unitNameLower = ai_tolower(textureUnitName);
|
2021-03-09 20:08:28 +00:00
|
|
|
if (unitNameLower.find("normalmap") != string::npos) {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_NORMALS;
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (unitNameLower.find("specularmap") != string::npos) {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_SPECULAR;
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (unitNameLower.find("lightmap") != string::npos) {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_LIGHTMAP;
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (unitNameLower.find("displacementmap") != string::npos) {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_DISPLACEMENT;
|
2021-03-09 20:08:28 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
textureType = aiTextureType_DIFFUSE;
|
|
|
|
}
|
|
|
|
}
|
2021-03-09 20:08:28 +00:00
|
|
|
} else if (linePart == partTextCoordSet) {
|
2015-05-19 03:57:13 +00:00
|
|
|
ss >> uvCoord;
|
|
|
|
}
|
|
|
|
/// @todo Implement
|
2021-03-09 20:08:28 +00:00
|
|
|
else if (linePart == partColorOp) {
|
2015-05-19 03:57:13 +00:00
|
|
|
/*
|
|
|
|
ss >> linePart;
|
|
|
|
if("replace"==linePart)//I don't think, assimp has something for this...
|
|
|
|
{
|
|
|
|
}
|
|
|
|
else if("modulate"==linePart)
|
|
|
|
{
|
|
|
|
//TODO: set value
|
|
|
|
//material->AddProperty(aiTextureOp_Multiply)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
if (textureRef.empty()) {
|
2018-04-19 15:21:21 +00:00
|
|
|
ASSIMP_LOG_WARN("Texture reference is empty, ignoring texture_unit.");
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2021-03-09 20:08:28 +00:00
|
|
|
if (textureType == aiTextureType_NONE) {
|
2021-05-13 11:05:31 +00:00
|
|
|
ASSIMP_LOG_WARN("Failed to detect texture type for '", textureRef, "', ignoring texture_unit.");
|
2015-05-19 03:57:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int textureTypeIndex = m_textures[textureType];
|
|
|
|
m_textures[textureType]++;
|
|
|
|
|
2021-05-13 09:25:27 +00:00
|
|
|
ASSIMP_LOG_VERBOSE_DEBUG(" texture '", textureRef, "' type ", textureType,
|
2021-03-09 20:08:28 +00:00
|
|
|
" index ", textureTypeIndex, " UV ", uvCoord);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
aiString assimpTextureRef(textureRef);
|
|
|
|
material->AddProperty(&assimpTextureRef, AI_MATKEY_TEXTURE(textureType, textureTypeIndex));
|
|
|
|
material->AddProperty(&uvCoord, 1, AI_MATKEY_UVWSRC(textureType, textureTypeIndex));
|
|
|
|
|
|
|
|
return true;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
2021-03-09 20:08:28 +00:00
|
|
|
} // namespace Ogre
|
|
|
|
} // namespace Assimp
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
|