2010-03-19 20:45:40 +00:00
|
|
|
/*
|
2012-02-03 03:38:30 +00:00
|
|
|
Open Asset Import Library (assimp)
|
2010-04-30 07:15:08 +00:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
Copyright (c) 2006-2012, assimp team
|
2010-04-30 07:15:08 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
|
|
|
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.
|
|
|
|
|
2012-02-03 03:38:30 +00:00
|
|
|
* Neither the name of the assimp team, nor the names of its
|
2010-04-30 07:15:08 +00:00
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
2012-02-03 03:38:30 +00:00
|
|
|
written permission of the assimp team.
|
2010-04-30 07:15:08 +00:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
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
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2015-04-15 23:00:17 +00:00
|
|
|
|
2010-03-19 20:45:40 +00:00
|
|
|
|
|
|
|
#ifndef ASSIMP_BUILD_NO_OGRE_IMPORTER
|
|
|
|
|
2014-05-01 13:49:26 +00:00
|
|
|
#include "OgreImporter.h"
|
2015-04-15 23:00:17 +00:00
|
|
|
#include "TinyFormatter.h"
|
|
|
|
#include "../include/assimp/material.h"
|
|
|
|
#include "../include/assimp/scene.h"
|
|
|
|
#include "../include/assimp/DefaultLogger.hpp"
|
2014-05-20 01:52:53 +00:00
|
|
|
#include "fast_atof.h"
|
|
|
|
|
|
|
|
#include <vector>
|
2015-04-15 23:00:17 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <boost/scoped_ptr.hpp>
|
2014-05-20 01:52:53 +00:00
|
|
|
|
2014-05-01 13:49:26 +00:00
|
|
|
using namespace std;
|
|
|
|
|
2010-03-19 20:45:40 +00:00
|
|
|
namespace Assimp
|
|
|
|
{
|
|
|
|
namespace Ogre
|
|
|
|
{
|
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
static const string partComment = "//";
|
|
|
|
static const string partBlockStart = "{";
|
|
|
|
static const string partBlockEnd = "}";
|
2010-03-19 20:45:40 +00:00
|
|
|
|
2014-05-18 08:57:44 +00:00
|
|
|
void OgreImporter::ReadMaterials(const std::string &pFile, Assimp::IOSystem *pIOHandler, aiScene *pScene, Mesh *mesh)
|
|
|
|
{
|
|
|
|
std::vector<aiMaterial*> materials;
|
|
|
|
|
|
|
|
// Create materials that can be found and parsed via the IOSystem.
|
|
|
|
for (size_t i=0, len=mesh->NumSubMeshes(); i<len; ++i)
|
|
|
|
{
|
2014-05-20 01:52:53 +00:00
|
|
|
SubMesh *submesh = mesh->GetSubMesh(i);
|
2014-05-18 08:57:44 +00:00
|
|
|
if (submesh && !submesh->materialRef.empty())
|
|
|
|
{
|
|
|
|
aiMaterial *material = ReadMaterial(pFile, pIOHandler, submesh->materialRef);
|
|
|
|
if (material)
|
|
|
|
{
|
|
|
|
submesh->materialIndex = materials.size();
|
|
|
|
materials.push_back(material);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-20 01:52:53 +00:00
|
|
|
AssignMaterials(pScene, materials);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OgreImporter::ReadMaterials(const std::string &pFile, Assimp::IOSystem *pIOHandler, aiScene *pScene, MeshXml *mesh)
|
|
|
|
{
|
|
|
|
std::vector<aiMaterial*> materials;
|
|
|
|
|
|
|
|
// Create materials that can be found and parsed via the IOSystem.
|
|
|
|
for (size_t i=0, len=mesh->NumSubMeshes(); i<len; ++i)
|
|
|
|
{
|
|
|
|
SubMeshXml *submesh = mesh->GetSubMesh(i);
|
|
|
|
if (submesh && !submesh->materialRef.empty())
|
|
|
|
{
|
|
|
|
aiMaterial *material = ReadMaterial(pFile, pIOHandler, submesh->materialRef);
|
|
|
|
if (material)
|
|
|
|
{
|
|
|
|
submesh->materialIndex = materials.size();
|
|
|
|
materials.push_back(material);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AssignMaterials(pScene, materials);
|
|
|
|
}
|
|
|
|
|
|
|
|
void OgreImporter::AssignMaterials(aiScene *pScene, std::vector<aiMaterial*> &materials)
|
|
|
|
{
|
2014-05-18 08:57:44 +00:00
|
|
|
pScene->mNumMaterials = materials.size();
|
|
|
|
if (pScene->mNumMaterials > 0)
|
|
|
|
{
|
|
|
|
pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
|
|
|
|
for(size_t i=0;i<pScene->mNumMaterials; ++i) {
|
|
|
|
pScene->mMaterials[i] = materials[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
aiMaterial* OgreImporter::ReadMaterial(const std::string &pFile, Assimp::IOSystem *pIOHandler, const std::string materialName)
|
|
|
|
{
|
2014-05-02 21:20:17 +00:00
|
|
|
if (materialName.empty()) {
|
2014-05-18 08:57:44 +00:00
|
|
|
return 0;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2010-03-19 20:45:40 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
// Full reference and examples of Ogre Material Script
|
|
|
|
// can be found from http://www.ogre3d.org/docs/manual/manual_14.html
|
2010-03-19 20:45:40 +00:00
|
|
|
|
2012-02-20 10:27:27 +00:00
|
|
|
/*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"
|
|
|
|
}
|
|
|
|
*/
|
2010-03-19 20:45:40 +00:00
|
|
|
|
|
|
|
stringstream ss;
|
2014-05-01 13:33:15 +00:00
|
|
|
|
|
|
|
// 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);
|
2012-03-09 11:42:47 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
IOStream *materialFile = 0;
|
|
|
|
for(size_t i=0; i<potentialFiles.size(); ++i)
|
2010-03-19 20:45:40 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
materialFile = pIOHandler->Open(potentialFiles[i]);
|
2014-05-02 21:20:17 +00:00
|
|
|
if (materialFile) {
|
2014-05-01 13:33:15 +00:00
|
|
|
break;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
DefaultLogger::get()->debug(Formatter::format() << "Source file for material '" << materialName << "' " << potentialFiles[i] << " does not exist");
|
2010-03-19 20:45:40 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
if (!materialFile)
|
2012-08-12 17:00:53 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
DefaultLogger::get()->error(Formatter::format() << "Failed to find source file for material '" << materialName << "'");
|
2014-05-18 08:57:44 +00:00
|
|
|
return 0;
|
2012-08-12 17:00:53 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
|
|
|
|
boost::scoped_ptr<IOStream> stream(materialFile);
|
|
|
|
if (stream->FileSize() == 0)
|
2012-08-12 17:00:53 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
DefaultLogger::get()->warn(Formatter::format() << "Source file for material '" << materialName << "' is empty (size is 0 bytes)");
|
2014-05-18 08:57:44 +00:00
|
|
|
return 0;
|
2012-08-12 17:00:53 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
|
|
|
|
// 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];
|
2010-03-19 20:45:40 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
|
|
|
|
DefaultLogger::get()->debug("Reading material '" + materialName + "'");
|
2010-03-19 20:45:40 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
aiMaterial *material = new aiMaterial();
|
|
|
|
m_textures.clear();
|
|
|
|
|
|
|
|
aiString ts(materialName);
|
|
|
|
material->AddProperty(&ts, AI_MATKEY_NAME);
|
2012-03-09 11:42:47 +00:00
|
|
|
|
2014-05-01 13:33:15 +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;
|
|
|
|
|
|
|
|
const string partMaterial = "material";
|
|
|
|
const string partTechnique = "technique";
|
2012-03-09 11:42:47 +00:00
|
|
|
|
2010-03-19 20:45:40 +00:00
|
|
|
while(!ss.eof())
|
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
// Skip commented lines
|
|
|
|
if (linePart == partComment)
|
|
|
|
{
|
2014-05-21 01:37:45 +00:00
|
|
|
NextAfterNewLine(ss, linePart);
|
2014-05-01 13:33:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (linePart != partMaterial)
|
|
|
|
{
|
|
|
|
ss >> linePart;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ss >> linePart;
|
|
|
|
if (linePart != materialName)
|
|
|
|
{
|
|
|
|
//DefaultLogger::get()->debug(Formatter::format() << "Found material '" << linePart << "' that does not match at index " << ss.tellg());
|
|
|
|
ss >> linePart;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
NextAfterNewLine(ss, linePart);
|
|
|
|
if (linePart != partBlockStart)
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->error(Formatter::format() << "Invalid material: block start missing near index " << ss.tellg());
|
|
|
|
return material;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultLogger::get()->debug("material '" + materialName + "'");
|
|
|
|
|
|
|
|
while(linePart != partBlockEnd)
|
2010-03-19 20:45:40 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
// Proceed to the first technique
|
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
if (linePart == partTechnique)
|
2010-03-19 20:45:40 +00:00
|
|
|
{
|
2014-05-01 22:38:13 +00:00
|
|
|
string techniqueName = SkipLine(ss);
|
|
|
|
ReadTechnique(Trim(techniqueName), ss, material);
|
2014-05-01 13:33:15 +00:00
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
// Read informations from a custom material
|
|
|
|
/** @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? */
|
2014-05-02 21:20:17 +00:00
|
|
|
if (linePart=="set")
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
ss >> linePart;
|
2014-05-02 21:20:17 +00:00
|
|
|
if (linePart=="$specular")//todo load this values:
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
}
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$diffuse")
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
}
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$ambient")
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
}
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$colormap")
|
2012-03-07 22:42:40 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
ss >> linePart;
|
2014-05-02 21:20:17 +00:00
|
|
|
aiString ts(linePart);
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
|
|
|
|
}
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$normalmap")
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
ss >> linePart;
|
2014-05-02 21:20:17 +00:00
|
|
|
aiString ts(linePart);
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
|
|
|
|
}
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$shininess_strength")
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
ss >> linePart;
|
2014-05-02 21:20:17 +00:00
|
|
|
float Shininess = fast_atof(linePart.c_str());
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS_STRENGTH);
|
2012-03-07 22:42:40 +00:00
|
|
|
}
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$shininess_exponent")
|
2010-03-19 20:45:40 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
ss >> linePart;
|
2014-05-02 21:20:17 +00:00
|
|
|
float Shininess = fast_atof(linePart.c_str());
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&Shininess, 1, AI_MATKEY_SHININESS);
|
|
|
|
}
|
|
|
|
//Properties from Venetica:
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$diffuse_map")
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
ss >> linePart;
|
2014-05-02 21:20:17 +00:00
|
|
|
if (linePart[0] == '"')// "file" -> file
|
|
|
|
linePart = linePart.substr(1, linePart.size()-2);
|
|
|
|
aiString ts(linePart);
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_DIFFUSE, 0));
|
|
|
|
}
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$specular_map")
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
ss >> linePart;
|
2014-05-02 21:20:17 +00:00
|
|
|
if (linePart[0] == '"')// "file" -> file
|
|
|
|
linePart = linePart.substr(1, linePart.size()-2);
|
|
|
|
aiString ts(linePart);
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_SHININESS, 0));
|
|
|
|
}
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$normal_map")
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
ss >> linePart;
|
2014-05-02 21:20:17 +00:00
|
|
|
if (linePart[0]=='"')// "file" -> file
|
|
|
|
linePart = linePart.substr(1, linePart.size()-2);
|
|
|
|
aiString ts(linePart);
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_NORMALS, 0));
|
|
|
|
}
|
2014-05-02 21:20:17 +00:00
|
|
|
else if (linePart=="$light_map")
|
2014-05-01 13:33:15 +00:00
|
|
|
{
|
|
|
|
ss >> linePart;
|
2014-05-02 21:20:17 +00:00
|
|
|
if (linePart[0]=='"') {
|
|
|
|
linePart = linePart.substr(1, linePart.size() - 2);
|
|
|
|
}
|
|
|
|
aiString ts(linePart);
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&ts, AI_MATKEY_TEXTURE(aiTextureType_LIGHTMAP, 0));
|
|
|
|
}
|
|
|
|
}
|
2010-03-19 20:45:40 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
ss >> linePart;
|
2010-03-19 20:45:40 +00:00
|
|
|
}
|
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
return material;
|
2010-03-19 20:45:40 +00:00
|
|
|
}
|
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
bool OgreImporter::ReadTechnique(const std::string &techniqueName, stringstream &ss, aiMaterial *material)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
string linePart;
|
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
if (linePart != partBlockStart)
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->error(Formatter::format() << "Invalid material: Technique block start missing near index " << ss.tellg());
|
|
|
|
return false;
|
|
|
|
}
|
2012-05-23 09:23:34 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
DefaultLogger::get()->debug(" technique '" + techniqueName + "'");
|
2012-05-23 09:23:34 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
const string partPass = "pass";
|
2012-03-09 11:43:31 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
while(linePart != partBlockEnd)
|
2012-03-07 22:42:40 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
// Skip commented lines
|
|
|
|
if (linePart == partComment)
|
|
|
|
{
|
2014-05-21 01:37:45 +00:00
|
|
|
SkipLine(ss);
|
2014-05-01 13:33:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @todo Techniques have other attributes than just passes.
|
|
|
|
if (linePart == partPass)
|
|
|
|
{
|
2014-05-01 22:38:13 +00:00
|
|
|
string passName = SkipLine(ss);
|
|
|
|
ReadPass(Trim(passName), ss, material);
|
2014-05-01 13:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OgreImporter::ReadPass(const std::string &passName, stringstream &ss, aiMaterial *material)
|
|
|
|
{
|
|
|
|
string linePart;
|
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
if (linePart != partBlockStart)
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->error(Formatter::format() << "Invalid material: Pass block start missing near index " << ss.tellg());
|
|
|
|
return false;
|
2012-03-07 22:42:40 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
|
|
|
|
DefaultLogger::get()->debug(" pass '" + passName + "'");
|
|
|
|
|
|
|
|
const string partAmbient = "ambient";
|
|
|
|
const string partDiffuse = "diffuse";
|
|
|
|
const string partSpecular = "specular";
|
|
|
|
const string partEmissive = "emissive";
|
|
|
|
const string partTextureUnit = "texture_unit";
|
|
|
|
|
|
|
|
while(linePart != partBlockEnd)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
// Skip commented lines
|
|
|
|
if (linePart == partComment)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-05-21 01:37:45 +00:00
|
|
|
SkipLine(ss);
|
2014-05-01 13:33:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-03-09 11:43:31 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
// Colors
|
|
|
|
/// @todo Support alpha via aiColor4D.
|
|
|
|
if (linePart == partAmbient || linePart == partDiffuse || linePart == partSpecular || linePart == partEmissive)
|
|
|
|
{
|
|
|
|
float r, g, b;
|
|
|
|
ss >> r >> g >> b;
|
|
|
|
const aiColor3D color(r, g, b);
|
|
|
|
|
|
|
|
DefaultLogger::get()->debug(Formatter::format() << " " << linePart << " " << r << " " << g << " " << b);
|
|
|
|
|
|
|
|
if (linePart == partAmbient)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&color, 1, AI_MATKEY_COLOR_AMBIENT);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (linePart == partDiffuse)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&color, 1, AI_MATKEY_COLOR_DIFFUSE);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (linePart == partSpecular)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&color, 1, AI_MATKEY_COLOR_SPECULAR);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (linePart == partEmissive)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
material->AddProperty(&color, 1, AI_MATKEY_COLOR_EMISSIVE);
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
}
|
|
|
|
else if (linePart == partTextureUnit)
|
|
|
|
{
|
2014-05-01 22:38:13 +00:00
|
|
|
string textureUnitName = SkipLine(ss);
|
|
|
|
ReadTextureUnit(Trim(textureUnitName), ss, material);
|
2014-05-01 13:33:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OgreImporter::ReadTextureUnit(const std::string &textureUnitName, stringstream &ss, aiMaterial *material)
|
|
|
|
{
|
|
|
|
string linePart;
|
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
if (linePart != partBlockStart)
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->error(Formatter::format() << "Invalid material: Texture unit block start missing near index " << ss.tellg());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
DefaultLogger::get()->debug(" texture_unit '" + textureUnitName + "'");
|
|
|
|
|
|
|
|
const string partTexture = "texture";
|
|
|
|
const string partTextCoordSet = "tex_coord_set";
|
|
|
|
const string partColorOp = "colour_op";
|
|
|
|
|
|
|
|
aiTextureType textureType = aiTextureType_NONE;
|
|
|
|
std::string textureRef;
|
|
|
|
int uvCoord = 0;
|
|
|
|
|
|
|
|
while(linePart != partBlockEnd)
|
|
|
|
{
|
|
|
|
ss >> linePart;
|
|
|
|
|
|
|
|
// Skip commented lines
|
|
|
|
if (linePart == partComment)
|
|
|
|
{
|
2014-05-21 01:37:45 +00:00
|
|
|
SkipLine(ss);
|
2014-05-01 13:33:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (linePart == partTexture)
|
|
|
|
{
|
|
|
|
ss >> linePart;
|
|
|
|
textureRef = linePart;
|
|
|
|
|
|
|
|
// User defined Assimp config property to detect texture type from filename.
|
|
|
|
if (m_detectTextureTypeFromFilename)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
size_t posSuffix = textureRef.find_last_of(".");
|
|
|
|
size_t posUnderscore = textureRef.find_last_of("_");
|
|
|
|
|
|
|
|
if (posSuffix != string::npos && posUnderscore != string::npos && posSuffix > posUnderscore)
|
2012-02-20 10:27:27 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
string identifier = Ogre::ToLower(textureRef.substr(posUnderscore, posSuffix - posUnderscore));
|
|
|
|
DefaultLogger::get()->debug(Formatter::format() << "Detecting texture type from filename postfix '" << identifier << "'");
|
|
|
|
|
|
|
|
if (identifier == "_n" || identifier == "_nrm" || identifier == "_nrml" || identifier == "_normal" || identifier == "_normals" || identifier == "_normalmap")
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_NORMALS;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (identifier == "_s" || identifier == "_spec" || identifier == "_specular" || identifier == "_specularmap")
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_SPECULAR;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (identifier == "_l" || identifier == "_light" || identifier == "_lightmap" || identifier == "_occ" || identifier == "_occlusion")
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_LIGHTMAP;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (identifier == "_disp" || identifier == "_displacement")
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_DISPLACEMENT;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_DIFFUSE;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_DIFFUSE;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
}
|
|
|
|
// Detect from texture unit name. This cannot be too broad as
|
|
|
|
// authors might give names like "LightSaber" or "NormalNinja".
|
|
|
|
else
|
|
|
|
{
|
|
|
|
string unitNameLower = Ogre::ToLower(textureUnitName);
|
|
|
|
if (unitNameLower.find("normalmap") != string::npos)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_NORMALS;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (unitNameLower.find("specularmap") != string::npos)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_SPECULAR;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (unitNameLower.find("lightmap") != string::npos)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_LIGHTMAP;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (unitNameLower.find("displacementmap") != string::npos)
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_DISPLACEMENT;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else
|
2014-05-02 21:20:17 +00:00
|
|
|
{
|
2014-05-01 13:33:15 +00:00
|
|
|
textureType = aiTextureType_DIFFUSE;
|
2014-05-02 21:20:17 +00:00
|
|
|
}
|
2012-02-20 10:27:27 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-01 13:33:15 +00:00
|
|
|
else if (linePart == partTextCoordSet)
|
|
|
|
{
|
|
|
|
ss >> uvCoord;
|
|
|
|
}
|
|
|
|
/// @todo Implement
|
|
|
|
else if(linePart == partColorOp)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (textureRef.empty())
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->warn("Texture reference is empty, ignoring texture_unit.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (textureType == aiTextureType_NONE)
|
|
|
|
{
|
|
|
|
DefaultLogger::get()->warn("Failed to detect texture type for '" + textureRef + "', ignoring texture_unit.");
|
|
|
|
return false;
|
|
|
|
}
|
2010-03-19 20:45:40 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
unsigned int textureTypeIndex = m_textures[textureType];
|
|
|
|
m_textures[textureType]++;
|
|
|
|
|
|
|
|
DefaultLogger::get()->debug(Formatter::format() << " texture '" << textureRef << "' type " << textureType
|
|
|
|
<< " index " << textureTypeIndex << " UV " << uvCoord);
|
|
|
|
|
|
|
|
aiString assimpTextureRef(textureRef);
|
|
|
|
material->AddProperty(&assimpTextureRef, AI_MATKEY_TEXTURE(textureType, textureTypeIndex));
|
|
|
|
material->AddProperty(&uvCoord, 1, AI_MATKEY_UVWSRC(textureType, textureTypeIndex));
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2010-03-19 20:45:40 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
} // Ogre
|
|
|
|
} // Assimp
|
2010-03-19 20:45:40 +00:00
|
|
|
|
2014-05-01 13:33:15 +00:00
|
|
|
#endif // ASSIMP_BUILD_NO_OGRE_IMPORTER
|