2008-05-09 17:24:28 +00:00
|
|
|
/*
|
2008-05-22 10:20:31 +00:00
|
|
|
Open Asset Import Library (ASSIMP)
|
2008-05-09 17:24:28 +00:00
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
|
|
|
Copyright (c) 2006-2008, ASSIMP Development Team
|
|
|
|
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.
|
|
|
|
|
|
|
|
* 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 Development Team.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
/** @file MaterialSystem.cpp
|
|
|
|
* @brief Implementation of the material system of the library
|
|
|
|
*/
|
|
|
|
|
2008-10-13 16:45:48 +00:00
|
|
|
#include "AssimpPCH.h"
|
2008-08-06 23:01:38 +00:00
|
|
|
#include "Hash.h"
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
// Get a specific property from a material
|
2008-05-05 12:36:31 +00:00
|
|
|
aiReturn aiGetMaterialProperty(const aiMaterial* pMat,
|
|
|
|
const char* pKey,
|
2008-11-16 21:56:45 +00:00
|
|
|
unsigned int type,
|
|
|
|
unsigned int index,
|
2008-05-05 12:36:31 +00:00
|
|
|
const aiMaterialProperty** pPropOut)
|
|
|
|
{
|
|
|
|
ai_assert (pMat != NULL);
|
|
|
|
ai_assert (pKey != NULL);
|
|
|
|
ai_assert (pPropOut != NULL);
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
/* Just search for a property with exactly this name ..
|
|
|
|
* could be improved by hashing, but it's possibly
|
2009-08-21 11:26:54 +00:00
|
|
|
* no worth the effort (we're bound to C structures,
|
|
|
|
* thus std::map or derivates are not applicable. */
|
2009-03-11 22:56:16 +00:00
|
|
|
for (unsigned int i = 0; i < pMat->mNumProperties;++i) {
|
2008-11-16 21:56:45 +00:00
|
|
|
aiMaterialProperty* prop = pMat->mProperties[i];
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
if (prop /* just for safety ... */
|
2009-08-21 11:26:54 +00:00
|
|
|
&& 0 == strcmp( prop->mKey.data, pKey )
|
|
|
|
&& (0xffffffff == type || prop->mSemantic == type) /* 0xffffffff is a wildcard, but this is undocumented */
|
2009-03-11 22:56:16 +00:00
|
|
|
&& (0xffffffff == index || prop->mIndex == index))
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2008-11-16 21:56:45 +00:00
|
|
|
*pPropOut = pMat->mProperties[i];
|
|
|
|
return AI_SUCCESS;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
*pPropOut = NULL;
|
|
|
|
return AI_FAILURE;
|
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
// Get an array of floating-point values from the material.
|
2008-05-05 12:36:31 +00:00
|
|
|
aiReturn aiGetMaterialFloatArray(const aiMaterial* pMat,
|
|
|
|
const char* pKey,
|
2008-11-16 21:56:45 +00:00
|
|
|
unsigned int type,
|
|
|
|
unsigned int index,
|
2008-05-05 12:36:31 +00:00
|
|
|
float* pOut,
|
|
|
|
unsigned int* pMax)
|
|
|
|
{
|
|
|
|
ai_assert (pOut != NULL);
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
aiMaterialProperty* prop;
|
|
|
|
aiGetMaterialProperty(pMat,pKey,type,index, (const aiMaterialProperty**) &prop);
|
|
|
|
if (!prop)
|
|
|
|
return AI_FAILURE;
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
// data is given in floats, simply copy it
|
|
|
|
if( aiPTI_Float == prop->mType || aiPTI_Buffer == prop->mType) {
|
|
|
|
unsigned int iWrite = prop->mDataLength / sizeof(float);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
if (pMax)iWrite = *pMax < iWrite ? *pMax : iWrite;
|
|
|
|
::memcpy (pOut, prop->mData, iWrite * sizeof (float));
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
if (pMax)*pMax = iWrite;
|
|
|
|
}
|
|
|
|
// data is given in ints, convert to float
|
|
|
|
else if( aiPTI_Integer == prop->mType) {
|
|
|
|
unsigned int iWrite = prop->mDataLength / sizeof(int);
|
|
|
|
|
|
|
|
if (pMax)iWrite = *pMax < iWrite ? *pMax : iWrite;
|
|
|
|
for (unsigned int a = 0; a < iWrite;++a) {
|
|
|
|
pOut[a] = (float) ((int*)prop->mData)[a];
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-03-11 22:56:16 +00:00
|
|
|
if (pMax)*pMax = iWrite;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-03-11 22:56:16 +00:00
|
|
|
// it is a string ... no way to read something out of this
|
|
|
|
else {
|
|
|
|
DefaultLogger::get()->error("Material property" + std::string(pKey) + " was found, but is not an float array");
|
|
|
|
if (pMax)*pMax = 0;
|
|
|
|
return AI_FAILURE;
|
|
|
|
}
|
|
|
|
return AI_SUCCESS;
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
// Get an array if integers from the material
|
2008-05-05 12:36:31 +00:00
|
|
|
aiReturn aiGetMaterialIntegerArray(const aiMaterial* pMat,
|
|
|
|
const char* pKey,
|
2008-11-16 21:56:45 +00:00
|
|
|
unsigned int type,
|
|
|
|
unsigned int index,
|
2008-05-05 12:36:31 +00:00
|
|
|
int* pOut,
|
|
|
|
unsigned int* pMax)
|
|
|
|
{
|
|
|
|
ai_assert (pOut != NULL);
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
aiMaterialProperty* prop;
|
|
|
|
aiGetMaterialProperty(pMat,pKey,type,index,(const aiMaterialProperty**) &prop);
|
|
|
|
if (!prop)
|
|
|
|
return AI_FAILURE;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
// data is given in ints, simply copy it
|
|
|
|
if( aiPTI_Integer == prop->mType || aiPTI_Buffer == prop->mType) {
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
unsigned int iWrite = prop->mDataLength / sizeof(int);
|
|
|
|
|
|
|
|
if (pMax)iWrite = *pMax < iWrite ? *pMax : iWrite;
|
|
|
|
::memcpy (pOut, prop->mData, iWrite * sizeof (int));
|
|
|
|
if (pMax)*pMax = iWrite;
|
|
|
|
}
|
|
|
|
// data is given in floats convert to int (lossy!)
|
|
|
|
else if( aiPTI_Float == prop->mType) {
|
|
|
|
unsigned int iWrite = prop->mDataLength / sizeof(float);
|
|
|
|
|
|
|
|
if (pMax)iWrite = *pMax < iWrite ? *pMax : iWrite;
|
|
|
|
for (unsigned int a = 0; a < iWrite;++a) {
|
|
|
|
pOut[a] = (int) ((float*)prop->mData)[a];
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-03-11 22:56:16 +00:00
|
|
|
if (pMax)*pMax = iWrite;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-03-11 22:56:16 +00:00
|
|
|
// it is a string ... no way to read something out of this
|
|
|
|
else {
|
|
|
|
DefaultLogger::get()->error("Material property" + std::string(pKey) + " was found, but is not an integer array");
|
|
|
|
if (pMax)*pMax = 0;
|
|
|
|
return AI_FAILURE;
|
|
|
|
}
|
|
|
|
return AI_SUCCESS;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
// Get a color (3 or 4 floats) from the material
|
2008-05-05 12:36:31 +00:00
|
|
|
aiReturn aiGetMaterialColor(const aiMaterial* pMat,
|
|
|
|
const char* pKey,
|
2008-11-16 21:56:45 +00:00
|
|
|
unsigned int type,
|
2009-03-11 22:56:16 +00:00
|
|
|
unsigned int index,
|
2008-05-05 12:36:31 +00:00
|
|
|
aiColor4D* pOut)
|
|
|
|
{
|
|
|
|
unsigned int iMax = 4;
|
2008-11-16 21:56:45 +00:00
|
|
|
aiReturn eRet = aiGetMaterialFloatArray(pMat,pKey,type,index,(float*)pOut,&iMax);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
// if no alpha channel is defined: set it to 1.0
|
2009-08-21 11:26:54 +00:00
|
|
|
if (3 == iMax) {
|
2009-03-11 22:56:16 +00:00
|
|
|
pOut->a = 1.0f;
|
2009-08-21 11:26:54 +00:00
|
|
|
}
|
|
|
|
|
2008-05-05 12:36:31 +00:00
|
|
|
return eRet;
|
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
// Get a string from the material
|
2008-05-05 12:36:31 +00:00
|
|
|
aiReturn aiGetMaterialString(const aiMaterial* pMat,
|
|
|
|
const char* pKey,
|
2008-11-16 21:56:45 +00:00
|
|
|
unsigned int type,
|
2009-03-11 22:56:16 +00:00
|
|
|
unsigned int index,
|
2008-05-05 12:36:31 +00:00
|
|
|
aiString* pOut)
|
|
|
|
{
|
|
|
|
ai_assert (pOut != NULL);
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
aiMaterialProperty* prop;
|
|
|
|
aiGetMaterialProperty(pMat,pKey,type,index,(const aiMaterialProperty**)&prop);
|
2009-08-21 11:26:54 +00:00
|
|
|
if (!prop) {
|
2009-03-11 22:56:16 +00:00
|
|
|
return AI_FAILURE;
|
2009-08-21 11:26:54 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
if( aiPTI_String == prop->mType) {
|
|
|
|
|
|
|
|
// WARN: There's not the whole string stored ..
|
|
|
|
const aiString* pcSrc = (const aiString*)prop->mData;
|
2009-08-21 11:26:54 +00:00
|
|
|
memcpy (pOut->data, pcSrc->data, (pOut->length = pcSrc->length)+1);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2009-03-11 22:56:16 +00:00
|
|
|
// Wrong type
|
|
|
|
else {
|
|
|
|
DefaultLogger::get()->error("Material property" + std::string(pKey) + " was found, but is no string" );
|
|
|
|
return AI_FAILURE;
|
|
|
|
}
|
|
|
|
return AI_SUCCESS;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2009-08-21 11:26:54 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Get the number of textures on a particular texture stack
|
|
|
|
ASSIMP_API unsigned int aiGetMaterialTextureCount(const C_STRUCT aiMaterial* pMat,
|
|
|
|
C_ENUM aiTextureType type)
|
|
|
|
{
|
|
|
|
ai_assert (pMat != NULL);
|
|
|
|
|
|
|
|
/* Textures are always stored with ascending indices (ValidateDS provides a check, so we don't need to check) */
|
|
|
|
unsigned int max = 0;
|
|
|
|
for (unsigned int i = 0; i < pMat->mNumProperties;++i) {
|
|
|
|
aiMaterialProperty* prop = pMat->mProperties[i];
|
|
|
|
|
|
|
|
if (prop /* just a sanity check ... */
|
|
|
|
&& 0 == strcmp( prop->mKey.data, _AI_MATKEY_TEXTURE_BASE )
|
|
|
|
&& prop->mSemantic == type) {
|
|
|
|
|
|
|
|
max = std::max(max,prop->mIndex+1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return max;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiReturn aiGetMaterialTexture(const C_STRUCT aiMaterial* mat,
|
|
|
|
aiTextureType type,
|
|
|
|
unsigned int index,
|
|
|
|
C_STRUCT aiString* path,
|
|
|
|
aiTextureMapping* _mapping /*= NULL*/,
|
|
|
|
unsigned int* uvindex /*= NULL*/,
|
|
|
|
float* blend /*= NULL*/,
|
|
|
|
aiTextureOp* op /*= NULL*/,
|
|
|
|
aiTextureMapMode* mapmode /*= NULL*/,
|
|
|
|
unsigned int* flags /*= NULL*/
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ai_assert(NULL != mat && NULL != path);
|
|
|
|
|
|
|
|
// Get the path to the texture
|
|
|
|
if (AI_SUCCESS != aiGetMaterialString(mat,AI_MATKEY_TEXTURE(type,index),path)) {
|
|
|
|
return AI_FAILURE;
|
|
|
|
}
|
|
|
|
// Determine mapping type
|
|
|
|
aiTextureMapping mapping = aiTextureMapping_UV;
|
|
|
|
aiGetMaterialInteger(mat,AI_MATKEY_MAPPING(type,index),(int*)&mapping);
|
|
|
|
if (_mapping)
|
|
|
|
*_mapping = mapping;
|
|
|
|
|
|
|
|
// Get UV index
|
|
|
|
if (aiTextureMapping_UV == mapping && uvindex) {
|
|
|
|
aiGetMaterialInteger(mat,AI_MATKEY_UVWSRC(type,index),(int*)uvindex);
|
|
|
|
}
|
|
|
|
// Get blend factor
|
|
|
|
if (blend) {
|
|
|
|
aiGetMaterialFloat(mat,AI_MATKEY_TEXBLEND(type,index),blend);
|
|
|
|
}
|
|
|
|
// Get texture operation
|
|
|
|
if (op){
|
|
|
|
aiGetMaterialInteger(mat,AI_MATKEY_TEXOP(type,index),(int*)op);
|
|
|
|
}
|
|
|
|
// Get texture mapping modes
|
|
|
|
if (mapmode) {
|
|
|
|
aiGetMaterialInteger(mat,AI_MATKEY_MAPPINGMODE_U(type,index),(int*)&mapmode[0]);
|
|
|
|
aiGetMaterialInteger(mat,AI_MATKEY_MAPPINGMODE_V(type,index),(int*)&mapmode[1]);
|
|
|
|
}
|
|
|
|
// Get texture flags
|
|
|
|
if (flags){
|
|
|
|
aiGetMaterialInteger(mat,AI_MATKEY_TEXFLAGS(type,index),(int*)flags);
|
|
|
|
}
|
|
|
|
return AI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
// Construction. Actually the one and only way to get an aiMaterial instance
|
2008-08-06 23:01:38 +00:00
|
|
|
MaterialHelper::MaterialHelper()
|
|
|
|
{
|
2008-11-16 21:56:45 +00:00
|
|
|
// Allocate 5 entries by default
|
|
|
|
mNumProperties = 0;
|
|
|
|
mNumAllocated = 5;
|
|
|
|
mProperties = new aiMaterialProperty*[5];
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
MaterialHelper::~MaterialHelper()
|
|
|
|
{
|
2009-01-12 22:06:54 +00:00
|
|
|
_InternDestruct();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
aiMaterial::~aiMaterial()
|
|
|
|
{
|
2009-03-11 22:56:16 +00:00
|
|
|
// HACK (Aramis): This is safe: aiMaterial has a private constructor,
|
|
|
|
// so instances must be created indirectly via MaterialHelper. We can't
|
|
|
|
// use a virtual d'tor because we need to preserve binary compatibility
|
|
|
|
// with good old C ...
|
2009-01-12 22:06:54 +00:00
|
|
|
((MaterialHelper*)this)->_InternDestruct();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Manual destructor
|
|
|
|
void MaterialHelper::_InternDestruct()
|
|
|
|
{
|
|
|
|
// First clean up all properties
|
2008-10-19 11:32:33 +00:00
|
|
|
Clear();
|
2009-01-12 22:06:54 +00:00
|
|
|
|
|
|
|
// Then delete the array that stored them
|
|
|
|
delete[] mProperties;
|
|
|
|
AI_DEBUG_INVALIDATE_PTR(mProperties);
|
|
|
|
|
|
|
|
// Update members
|
|
|
|
mNumAllocated = 0;
|
2008-10-19 11:32:33 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-10-19 11:32:33 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void MaterialHelper::Clear()
|
|
|
|
{
|
|
|
|
for (unsigned int i = 0; i < mNumProperties;++i)
|
2008-08-06 23:01:38 +00:00
|
|
|
{
|
2008-10-19 11:32:33 +00:00
|
|
|
// delete this entry
|
|
|
|
delete mProperties[i];
|
2009-01-12 22:06:54 +00:00
|
|
|
AI_DEBUG_INVALIDATE_PTR(mProperties[i]);
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
mNumProperties = 0;
|
|
|
|
|
2009-01-12 22:06:54 +00:00
|
|
|
// The array remains allocated, we just invalidated its contents
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2009-01-12 22:06:54 +00:00
|
|
|
uint32_t MaterialHelper::ComputeHash(bool includeMatName /*= false*/)
|
2008-07-30 23:00:25 +00:00
|
|
|
{
|
|
|
|
uint32_t hash = 1503; // magic start value, choosen to be my birthday :-)
|
|
|
|
for (unsigned int i = 0; i < this->mNumProperties;++i)
|
|
|
|
{
|
|
|
|
aiMaterialProperty* prop;
|
|
|
|
|
2009-02-15 20:29:07 +00:00
|
|
|
// Exclude all properties whose first character is '?' from the hash
|
|
|
|
// See doc for aiMaterialProperty.
|
|
|
|
if ((prop = mProperties[i]) && (includeMatName || prop->mKey.data[0] != '?'))
|
2008-07-30 23:00:25 +00:00
|
|
|
{
|
2008-08-28 17:35:36 +00:00
|
|
|
hash = SuperFastHash(prop->mKey.data,(unsigned int)prop->mKey.length,hash);
|
2008-07-30 23:00:25 +00:00
|
|
|
hash = SuperFastHash(prop->mData,prop->mDataLength,hash);
|
2008-11-16 21:56:45 +00:00
|
|
|
|
|
|
|
// Combine the semantic and the index with the hash
|
2009-02-15 20:29:07 +00:00
|
|
|
hash = SuperFastHash((const char*)&prop->mSemantic,sizeof(unsigned int),hash);
|
|
|
|
hash = SuperFastHash((const char*)&prop->mIndex,sizeof(unsigned int),hash);
|
2008-07-30 23:00:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-07-30 23:00:25 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-11-16 21:56:45 +00:00
|
|
|
aiReturn MaterialHelper::RemoveProperty (const char* pKey,unsigned int type,
|
2009-03-11 22:56:16 +00:00
|
|
|
unsigned int index
|
|
|
|
)
|
2008-05-13 23:26:52 +00:00
|
|
|
{
|
|
|
|
ai_assert(NULL != pKey);
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
for (unsigned int i = 0; i < mNumProperties;++i) {
|
2008-11-16 21:56:45 +00:00
|
|
|
aiMaterialProperty* prop = mProperties[i];
|
|
|
|
|
|
|
|
if (prop && !::strcmp( prop->mKey.data, pKey ) &&
|
|
|
|
prop->mSemantic == type && prop->mIndex == index)
|
2008-05-13 23:26:52 +00:00
|
|
|
{
|
2008-11-16 21:56:45 +00:00
|
|
|
// Delete this entry
|
|
|
|
delete mProperties[i];
|
|
|
|
|
|
|
|
// collapse the array behind --.
|
|
|
|
--mNumProperties;
|
2009-03-11 22:56:16 +00:00
|
|
|
for (unsigned int a = i; a < mNumProperties;++a) {
|
2008-11-16 21:56:45 +00:00
|
|
|
mProperties[a] = mProperties[a+1];
|
2008-05-13 23:26:52 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
return AI_SUCCESS;
|
2008-05-13 23:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return AI_FAILURE;
|
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-05-13 23:26:52 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
aiReturn MaterialHelper::AddBinaryProperty (const void* pInput,
|
2008-11-16 21:56:45 +00:00
|
|
|
unsigned int pSizeInBytes,
|
2008-05-05 12:36:31 +00:00
|
|
|
const char* pKey,
|
2008-11-16 21:56:45 +00:00
|
|
|
unsigned int type,
|
|
|
|
unsigned int index,
|
2009-03-11 22:56:16 +00:00
|
|
|
aiPropertyTypeInfo pType
|
|
|
|
)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
|
|
|
ai_assert (pInput != NULL);
|
|
|
|
ai_assert (pKey != NULL);
|
|
|
|
ai_assert (0 != pSizeInBytes);
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
// first search the list whether there is already an entry with this key
|
|
|
|
unsigned int iOutIndex = 0xffffffff;
|
|
|
|
for (unsigned int i = 0; i < mNumProperties;++i) {
|
2008-11-16 21:56:45 +00:00
|
|
|
aiMaterialProperty* prop = mProperties[i];
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
if (prop /* just for safety */
|
|
|
|
&& !::strcmp( prop->mKey.data, pKey )
|
|
|
|
&& prop->mSemantic == type
|
|
|
|
&& prop->mIndex == index)
|
2008-05-13 23:26:52 +00:00
|
|
|
{
|
2008-11-16 21:56:45 +00:00
|
|
|
// delete this entry
|
2009-03-11 22:56:16 +00:00
|
|
|
delete mProperties[i];
|
2008-11-16 21:56:45 +00:00
|
|
|
iOutIndex = i;
|
2008-05-13 23:26:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-16 21:56:45 +00:00
|
|
|
// Allocate a new material property
|
2008-05-05 12:36:31 +00:00
|
|
|
aiMaterialProperty* pcNew = new aiMaterialProperty();
|
|
|
|
|
2008-11-16 21:56:45 +00:00
|
|
|
// Fill this
|
2008-05-05 12:36:31 +00:00
|
|
|
pcNew->mType = pType;
|
2008-11-16 21:56:45 +00:00
|
|
|
pcNew->mSemantic = type;
|
|
|
|
pcNew->mIndex = index;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
|
|
|
pcNew->mDataLength = pSizeInBytes;
|
|
|
|
pcNew->mData = new char[pSizeInBytes];
|
2008-11-16 21:56:45 +00:00
|
|
|
::memcpy (pcNew->mData,pInput,pSizeInBytes);
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
pcNew->mKey.length = ::strlen(pKey);
|
|
|
|
ai_assert ( MAXLEN > pcNew->mKey.length);
|
|
|
|
::strcpy( pcNew->mKey.data, pKey );
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
if (0xffffffff != iOutIndex) {
|
2008-11-16 21:56:45 +00:00
|
|
|
mProperties[iOutIndex] = pcNew;
|
2008-05-13 23:26:52 +00:00
|
|
|
return AI_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
// resize the array ... double the storage allocated
|
|
|
|
if (mNumProperties == mNumAllocated) {
|
|
|
|
const unsigned int iOld = mNumAllocated;
|
2009-01-12 22:06:54 +00:00
|
|
|
mNumAllocated *= 2;
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
aiMaterialProperty** ppTemp;
|
|
|
|
try {
|
|
|
|
ppTemp = new aiMaterialProperty*[mNumAllocated];
|
|
|
|
} catch (std::bad_alloc&) {
|
|
|
|
return AI_OUTOFMEMORY;
|
|
|
|
}
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
// just copy all items over; then replace the old array
|
2008-11-16 21:56:45 +00:00
|
|
|
::memcpy (ppTemp,mProperties,iOld * sizeof(void*));
|
2008-05-05 12:36:31 +00:00
|
|
|
|
2008-11-16 21:56:45 +00:00
|
|
|
delete[] mProperties;
|
|
|
|
mProperties = ppTemp;
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
|
|
|
// push back ...
|
2008-11-16 21:56:45 +00:00
|
|
|
mProperties[mNumProperties++] = pcNew;
|
2008-05-05 12:36:31 +00:00
|
|
|
return AI_SUCCESS;
|
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2008-05-05 12:36:31 +00:00
|
|
|
aiReturn MaterialHelper::AddProperty (const aiString* pInput,
|
2008-11-16 21:56:45 +00:00
|
|
|
const char* pKey,
|
|
|
|
unsigned int type,
|
|
|
|
unsigned int index)
|
2008-05-05 12:36:31 +00:00
|
|
|
{
|
2009-03-11 22:56:16 +00:00
|
|
|
// We don't want to add the whole buffer ..
|
|
|
|
return AddBinaryProperty(pInput,
|
|
|
|
(unsigned int)pInput->length+1+
|
2008-08-28 17:35:36 +00:00
|
|
|
(unsigned int)(((uint8_t*)&pInput->data - (uint8_t*)&pInput->length)),
|
2009-03-11 22:56:16 +00:00
|
|
|
pKey,
|
|
|
|
type,
|
|
|
|
index,
|
|
|
|
aiPTI_String);
|
2008-05-05 12:36:31 +00:00
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|
2008-05-09 17:24:28 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
void MaterialHelper::CopyPropertyList(MaterialHelper* pcDest,
|
2009-03-11 22:56:16 +00:00
|
|
|
const MaterialHelper* pcSrc
|
|
|
|
)
|
2008-05-09 17:24:28 +00:00
|
|
|
{
|
|
|
|
ai_assert(NULL != pcDest);
|
|
|
|
ai_assert(NULL != pcSrc);
|
|
|
|
|
|
|
|
unsigned int iOldNum = pcDest->mNumProperties;
|
|
|
|
pcDest->mNumAllocated += pcSrc->mNumAllocated;
|
|
|
|
pcDest->mNumProperties += pcSrc->mNumProperties;
|
|
|
|
|
|
|
|
aiMaterialProperty** pcOld = pcDest->mProperties;
|
|
|
|
pcDest->mProperties = new aiMaterialProperty*[pcDest->mNumAllocated];
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
if (iOldNum && pcOld) {
|
2009-08-21 11:26:54 +00:00
|
|
|
for (unsigned int i = 0; i < iOldNum;++i) {
|
2008-05-09 17:24:28 +00:00
|
|
|
pcDest->mProperties[i] = pcOld[i];
|
2009-08-21 11:26:54 +00:00
|
|
|
}
|
2008-05-09 17:24:28 +00:00
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
delete[] pcOld;
|
2008-05-09 17:24:28 +00:00
|
|
|
}
|
2009-03-11 22:56:16 +00:00
|
|
|
for (unsigned int i = iOldNum; i< pcDest->mNumProperties;++i) {
|
2008-08-06 23:01:38 +00:00
|
|
|
aiMaterialProperty* propSrc = pcSrc->mProperties[i];
|
|
|
|
|
2009-03-11 22:56:16 +00:00
|
|
|
// search whether we have already a property with this name -> if yes, overwrite it
|
2008-08-06 23:01:38 +00:00
|
|
|
aiMaterialProperty* prop;
|
2009-03-11 22:56:16 +00:00
|
|
|
for (unsigned int q = 0; q < iOldNum;++q) {
|
2008-08-06 23:01:38 +00:00
|
|
|
prop = pcDest->mProperties[q];
|
2009-03-11 22:56:16 +00:00
|
|
|
if (prop /* just for safety */
|
|
|
|
&& prop->mKey == propSrc->mKey
|
|
|
|
&& prop->mSemantic == propSrc->mSemantic
|
|
|
|
&& prop->mIndex == propSrc->mIndex) {
|
2008-08-06 23:01:38 +00:00
|
|
|
delete prop;
|
|
|
|
|
|
|
|
// collapse the whole array ...
|
2009-08-21 11:26:54 +00:00
|
|
|
memmove(&pcDest->mProperties[q],&pcDest->mProperties[q+1],i-q);
|
|
|
|
i--;
|
|
|
|
pcDest->mNumProperties--;
|
2008-08-06 23:01:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-16 21:56:45 +00:00
|
|
|
// Allocate the output property and copy the source property
|
2008-08-06 23:01:38 +00:00
|
|
|
prop = pcDest->mProperties[i] = new aiMaterialProperty();
|
|
|
|
prop->mKey = propSrc->mKey;
|
|
|
|
prop->mDataLength = propSrc->mDataLength;
|
|
|
|
prop->mType = propSrc->mType;
|
2008-11-16 21:56:45 +00:00
|
|
|
prop->mSemantic = propSrc->mSemantic;
|
|
|
|
prop->mIndex = propSrc->mIndex;
|
|
|
|
|
2008-08-06 23:01:38 +00:00
|
|
|
prop->mData = new char[propSrc->mDataLength];
|
2009-08-21 11:26:54 +00:00
|
|
|
memcpy(prop->mData,propSrc->mData,prop->mDataLength);
|
2008-05-09 17:24:28 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2008-11-16 21:56:45 +00:00
|
|
|
|