mirror of
https://github.com/TorqueGameEngines/Torque3D.git
synced 2026-02-12 19:31:41 +00:00
WIP of assimp integration.
This commit is contained in:
parent
32c7f2c7a7
commit
bf170ffbca
2135 changed files with 1260856 additions and 7 deletions
138
Engine/source/ts/assimp/assimpAppMaterial.cpp
Normal file
138
Engine/source/ts/assimp/assimpAppMaterial.cpp
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "ts/loader/appSequence.h"
|
||||
#include "ts/assimp/assimpAppMaterial.h"
|
||||
#include "ts/assimp/assimpAppMesh.h"
|
||||
#include "materials/materialManager.h"
|
||||
#include "ts/tsMaterialList.h"
|
||||
|
||||
// assimp include files.
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include <assimp/types.h>
|
||||
|
||||
String AppMaterial::cleanString(const String& str)
|
||||
{
|
||||
String cleanStr(str);
|
||||
|
||||
// Replace invalid characters with underscores
|
||||
const String badChars(" -,.+=*/");
|
||||
for (String::SizeType i = 0; i < badChars.length(); i++)
|
||||
cleanStr.replace(badChars[i], '_');
|
||||
|
||||
// Prefix with an underscore if string starts with a number
|
||||
if ((cleanStr[0] >= '0') && (cleanStr[0] <= '9'))
|
||||
cleanStr.insert(0, '_');
|
||||
|
||||
return cleanStr;
|
||||
}
|
||||
|
||||
AssimpAppMaterial::AssimpAppMaterial(const char* matName)
|
||||
{
|
||||
name = matName;
|
||||
diffuseColor = LinearColorF::ONE;
|
||||
specularColor = LinearColorF::ONE;
|
||||
specularPower = 0.8f;
|
||||
doubleSided = false;
|
||||
|
||||
// Set some defaults
|
||||
flags |= TSMaterialList::S_Wrap;
|
||||
flags |= TSMaterialList::T_Wrap;
|
||||
}
|
||||
|
||||
AssimpAppMaterial::AssimpAppMaterial(const struct aiMaterial* mtl)
|
||||
{
|
||||
aiString matName;
|
||||
mtl->Get(AI_MATKEY_NAME, matName);
|
||||
name = matName.C_Str();
|
||||
if ( name.isEmpty() )
|
||||
name = "defaultMaterial";
|
||||
Con::printf("[ASSIMP] Loaded Material: %s", matName.C_Str());
|
||||
|
||||
// Opacity
|
||||
F32 opacity = 0.0f;
|
||||
mtl->Get(AI_MATKEY_OPACITY, opacity);
|
||||
|
||||
// Diffuse color
|
||||
aiColor3D diff_color (0.f, 0.f, 0.f);
|
||||
mtl->Get(AI_MATKEY_COLOR_DIFFUSE, diff_color);
|
||||
diffuseColor = LinearColorF(diff_color.r, diff_color.g, diff_color.b, opacity);
|
||||
|
||||
// Spec Color color
|
||||
aiColor3D spec_color (0.f, 0.f, 0.f);
|
||||
mtl->Get(AI_MATKEY_COLOR_DIFFUSE, spec_color );
|
||||
specularColor = LinearColorF(spec_color.r, spec_color.g, spec_color.b, 1.0f);
|
||||
|
||||
// Specular Power
|
||||
mtl->Get(AI_MATKEY_SHININESS_STRENGTH, specularPower);
|
||||
|
||||
// Double-Sided
|
||||
S32 dbl_sided = 0;
|
||||
mtl->Get(AI_MATKEY_TWOSIDED, dbl_sided);
|
||||
doubleSided = (dbl_sided != 0);
|
||||
|
||||
// Set some defaults
|
||||
flags |= TSMaterialList::S_Wrap;
|
||||
flags |= TSMaterialList::T_Wrap;
|
||||
}
|
||||
|
||||
Material* AssimpAppMaterial::createMaterial(const Torque::Path& path) const
|
||||
{
|
||||
// The filename and material name are used as TorqueScript identifiers, so
|
||||
// clean them up first
|
||||
String cleanFile = cleanString(TSShapeLoader::getShapePath().getFileName());
|
||||
String cleanName = cleanString(getName());
|
||||
|
||||
// Prefix the material name with the filename (if not done already by TSShapeConstructor prefix)
|
||||
//if (!cleanName.startsWith(cleanFile))
|
||||
// cleanName = cleanFile + "_" + cleanName;
|
||||
|
||||
// Determine the blend operation for this material
|
||||
Material::BlendOp blendOp = (flags & TSMaterialList::Translucent) ? Material::LerpAlpha : Material::None;
|
||||
if (flags & TSMaterialList::Additive)
|
||||
blendOp = Material::Add;
|
||||
else if (flags & TSMaterialList::Subtractive)
|
||||
blendOp = Material::Sub;
|
||||
|
||||
// Create the Material definition
|
||||
const String oldScriptFile = Con::getVariable("$Con::File");
|
||||
Con::setVariable("$Con::File", path.getFullPath()); // modify current script path so texture lookups are correct
|
||||
Material *newMat = MATMGR->allocateAndRegister( cleanName, getName() );
|
||||
Con::setVariable("$Con::File", oldScriptFile); // restore script path
|
||||
|
||||
newMat->mDiffuseMapFilename[0] = "";
|
||||
newMat->mNormalMapFilename[0] = "";
|
||||
newMat->mSpecularMapFilename[0] = "";
|
||||
|
||||
newMat->mDiffuse[0] = diffuseColor;
|
||||
//newMat->mSpecular[0] = specularColor;
|
||||
//newMat->mSpecularPower[0] = specularPower;
|
||||
|
||||
newMat->mDoubleSided = doubleSided;
|
||||
newMat->mTranslucent = (bool)(flags & TSMaterialList::Translucent);
|
||||
newMat->mTranslucentBlendOp = blendOp;
|
||||
|
||||
return newMat;
|
||||
}
|
||||
51
Engine/source/ts/assimp/assimpAppMaterial.h
Normal file
51
Engine/source/ts/assimp/assimpAppMaterial.h
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _ASSIMP_APPMATERIAL_H_
|
||||
#define _ASSIMP_APPMATERIAL_H_
|
||||
|
||||
#ifndef _APPMATERIAL_H_
|
||||
#include "ts/loader/appMaterial.h"
|
||||
#endif
|
||||
|
||||
class Material;
|
||||
|
||||
class AssimpAppMaterial : public AppMaterial
|
||||
{
|
||||
typedef AppMaterial Parent;
|
||||
|
||||
String name;
|
||||
LinearColorF diffuseColor;
|
||||
LinearColorF specularColor;
|
||||
F32 specularPower;
|
||||
bool doubleSided;
|
||||
public:
|
||||
|
||||
AssimpAppMaterial(const char* matName);
|
||||
AssimpAppMaterial(const struct aiMaterial* mtl);
|
||||
~AssimpAppMaterial() { }
|
||||
|
||||
String getName() const { return name; }
|
||||
Material* createMaterial(const Torque::Path& path) const;
|
||||
};
|
||||
|
||||
#endif // _ASSIMP_APPMATERIAL_H_
|
||||
256
Engine/source/ts/assimp/assimpAppMesh.cpp
Normal file
256
Engine/source/ts/assimp/assimpAppMesh.cpp
Normal file
|
|
@ -0,0 +1,256 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "ts/collada/colladaExtensions.h"
|
||||
#include "ts/assimp/assimpAppMesh.h"
|
||||
#include "ts/assimp/assimpAppNode.h"
|
||||
|
||||
// assimp include files.
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include <assimp/types.h>
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
AssimpAppMesh::AssimpAppMesh(const struct aiMesh* mesh, AssimpAppNode* node)
|
||||
: mMeshData(mesh), appNode(node)
|
||||
{
|
||||
Con::printf("[ASSIMP] Mesh Created: %s", getName());
|
||||
}
|
||||
|
||||
const char* AssimpAppMesh::getName(bool allowFixed)
|
||||
{
|
||||
// Some exporters add a 'PIVOT' or unnamed node between the mesh and the
|
||||
// actual object node. Detect this and return the object node name instead
|
||||
// of the pivot node.
|
||||
const char* nodeName = appNode->getName();
|
||||
if ( dStrEqual(nodeName, "null") || dStrEndsWith(nodeName, "PIVOT") )
|
||||
nodeName = appNode->getParentName();
|
||||
|
||||
// If all geometry is being fixed to the same size, append the size
|
||||
// to the name
|
||||
//return allowFixed && fixedSizeEnabled ? avar("%s %d", nodeName, fixedSize) : nodeName;
|
||||
return nodeName;
|
||||
}
|
||||
|
||||
MatrixF AssimpAppMesh::getMeshTransform(F32 time)
|
||||
{
|
||||
return appNode->getNodeTransform(time);
|
||||
}
|
||||
|
||||
void AssimpAppMesh::lockMesh(F32 t, const MatrixF& objectOffset)
|
||||
{
|
||||
// After this function, the following are expected to be populated:
|
||||
// points, normals, uvs, primitives, indices
|
||||
// There is also colors and uv2s but those don't seem to be required.
|
||||
|
||||
points.reserve(mMeshData->mNumVertices);
|
||||
uvs.reserve(mMeshData->mNumVertices);
|
||||
normals.reserve(mMeshData->mNumVertices);
|
||||
|
||||
bool noUVFound = false;
|
||||
for (U32 i = 0; i<mMeshData->mNumVertices; i++)
|
||||
{
|
||||
// Points and Normals
|
||||
aiVector3D pt = mMeshData->mVertices[i];
|
||||
aiVector3D nrm = mMeshData->mNormals[i];
|
||||
|
||||
Point3F tmpVert;
|
||||
Point3F tmpNormal;
|
||||
|
||||
if (Con::getBoolVariable("$Assimp::SwapYZ", false))
|
||||
{
|
||||
tmpVert = Point3F(pt.x, pt.z, pt.y);
|
||||
tmpNormal = Point3F(nrm.x, nrm.z, nrm.y);
|
||||
}
|
||||
else
|
||||
{
|
||||
tmpVert = Point3F(pt.x, pt.y, pt.z);
|
||||
tmpNormal = Point3F(nrm.x, nrm.y, nrm.z);
|
||||
}
|
||||
|
||||
//objectOffset.mulP(tmpVert);
|
||||
|
||||
points.push_back(tmpVert);
|
||||
|
||||
if (mMeshData->HasTextureCoords(0))
|
||||
{
|
||||
uvs.push_back(Point2F(mMeshData->mTextureCoords[0][i].x, mMeshData->mTextureCoords[0][i].y));
|
||||
}
|
||||
else
|
||||
{
|
||||
// I don't know if there's any solution to this issue.
|
||||
// If it's not mapped, it's not mapped.
|
||||
noUVFound = true;
|
||||
uvs.push_back(Point2F(1, 1));
|
||||
}
|
||||
|
||||
// UV2s
|
||||
if (mMeshData->HasTextureCoords(1))
|
||||
{
|
||||
uv2s.push_back(Point2F(mMeshData->mTextureCoords[1][i].x, mMeshData->mTextureCoords[1][i].y));
|
||||
}
|
||||
|
||||
// Vertex Colors
|
||||
if (mMeshData->HasVertexColors(0))
|
||||
{
|
||||
LinearColorF vColor(mMeshData->mColors[0][i].r,
|
||||
mMeshData->mColors[0][i].g,
|
||||
mMeshData->mColors[0][i].b,
|
||||
mMeshData->mColors[0][i].a);
|
||||
colors.push_back(vColor.toColorI());
|
||||
}
|
||||
|
||||
//uvs.push_back(mModel->mVerts[i].texcoord);
|
||||
normals.push_back(tmpNormal);
|
||||
//edgeVerts.push_back(mModel->mVerts[i].edge);
|
||||
}
|
||||
|
||||
U32 numFaces = mMeshData->mNumFaces;
|
||||
U32 primCount = 0;
|
||||
primitives.reserve(numFaces);
|
||||
|
||||
//Fetch the number of indices
|
||||
U32 indicesCount = 0;
|
||||
for (U32 i = 0; i < numFaces; i++)
|
||||
{
|
||||
indicesCount += mMeshData->mFaces[i].mNumIndices;
|
||||
}
|
||||
|
||||
indices.reserve(indicesCount);
|
||||
|
||||
/*U32 idxCount = 0;
|
||||
|
||||
for (U32 j = 0; j<mModel->mMaterials.size(); j++)
|
||||
{
|
||||
MikuModel::Material &mat = mModel->mMaterials[j];
|
||||
U32 nextIdxCount = idxCount + mat.numIndices;
|
||||
|
||||
primitives.increment();
|
||||
|
||||
TSDrawPrimitive& primitive = primitives.last();
|
||||
primitive.start = indices.size();
|
||||
primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | j;
|
||||
primitive.numElements = mat.numIndices;
|
||||
|
||||
for (U32 i = idxCount; i<nextIdxCount; i++)
|
||||
{
|
||||
indices.push_back(mModel->mIndices[i]);
|
||||
}
|
||||
|
||||
idxCount = nextIdxCount;
|
||||
}*/
|
||||
|
||||
for ( U32 n = 0; n < mMeshData->mNumFaces; ++n)
|
||||
{
|
||||
const struct aiFace* face = &mMeshData->mFaces[n];
|
||||
if ( face->mNumIndices == 3 )
|
||||
{
|
||||
// Create TSMesh primitive
|
||||
primitives.increment();
|
||||
TSDrawPrimitive& primitive = primitives.last();
|
||||
primitive.start = indices.size();
|
||||
primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | (S32)mMeshData->mMaterialIndex;
|
||||
//primitive.numElements = face->mNumIndices;//3;
|
||||
primitive.numElements = 3;
|
||||
|
||||
if (Con::getBoolVariable("$Assimp::FlipNormals", true))
|
||||
{
|
||||
U32 indexCount = face->mNumIndices;
|
||||
for (S32 ind = indexCount - 1; ind >= 0; ind--)
|
||||
{
|
||||
U32 index = face->mIndices[ind];
|
||||
indices.push_back(index);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
U32 indexCount = face->mNumIndices;
|
||||
for (U32 ind = 0; ind < indexCount; ind++)
|
||||
{
|
||||
U32 index = face->mIndices[ind];
|
||||
indices.push_back(index);
|
||||
}
|
||||
}
|
||||
|
||||
// Load the indices in.
|
||||
//indices.push_back(face->mIndices[0]);
|
||||
//indices.push_back(face->mIndices[1]);
|
||||
//indices.push_back(face->mIndices[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
Con::printf("[ASSIMP] Non-Triangle Face Found. Indices: %d", face->mNumIndices);
|
||||
}
|
||||
}
|
||||
|
||||
U32 boneCount = mMeshData->mNumBones;
|
||||
|
||||
bones.setSize(boneCount);
|
||||
|
||||
for (U32 b = 0; b < boneCount; b++)
|
||||
{
|
||||
String name = mMeshData->mBones[b]->mName.C_Str();
|
||||
|
||||
MatrixF boneTransform;
|
||||
|
||||
for (U32 m = 0; m < 16; ++m)
|
||||
{
|
||||
boneTransform[m] = *mMeshData->mBones[b]->mOffsetMatrix[m];
|
||||
}
|
||||
|
||||
//initialTransforms.push_back(boneTransform);
|
||||
initialTransforms.push_back(MatrixF::Identity);
|
||||
|
||||
//Weights
|
||||
U32 numWeights = mMeshData->mBones[b]->mNumWeights;
|
||||
|
||||
weight.setSize(numWeights);
|
||||
vertexIndex.setSize(numWeights);
|
||||
|
||||
for (U32 w = 0; w < numWeights; ++w)
|
||||
{
|
||||
aiVertexWeight* aiWeight = mMeshData->mBones[b]->mWeights;
|
||||
|
||||
weight[w] = aiWeight->mWeight;
|
||||
vertexIndex[w] = aiWeight->mVertexId;
|
||||
boneIndex[w] = b;
|
||||
//vertWeight. = aiWeight->
|
||||
}
|
||||
//= mNumWeights
|
||||
}
|
||||
|
||||
if ( noUVFound )
|
||||
Con::warnf("[ASSIMP] No UV Data for mesh.");
|
||||
}
|
||||
|
||||
void AssimpAppMesh::lookupSkinData()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
F32 AssimpAppMesh::getVisValue(F32 t)
|
||||
{
|
||||
return 1.0f;
|
||||
}
|
||||
121
Engine/source/ts/assimp/assimpAppMesh.h
Normal file
121
Engine/source/ts/assimp/assimpAppMesh.h
Normal file
|
|
@ -0,0 +1,121 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _ASSIMP_APPMESH_H_
|
||||
#define _ASSIMP_APPMESH_H_
|
||||
|
||||
#ifndef _APPMESH_H_
|
||||
#include "ts/loader/appMesh.h"
|
||||
#endif
|
||||
#ifndef _TSSHAPELOADER_H_
|
||||
#include "ts/loader/tsShapeLoader.h"
|
||||
#endif
|
||||
#ifndef _ASSIMP_APPNODE_H_
|
||||
#include "ts/assimp/assimpAppNode.h"
|
||||
#endif
|
||||
|
||||
class AssimpAppMesh : public AppMesh
|
||||
{
|
||||
typedef AppMesh Parent;
|
||||
|
||||
protected:
|
||||
class AssimpAppNode* appNode; ///< Pointer to the node that owns this mesh
|
||||
const struct aiMesh* mMeshData;
|
||||
|
||||
public:
|
||||
|
||||
AssimpAppMesh(const struct aiMesh* mesh, AssimpAppNode* node);
|
||||
~AssimpAppMesh()
|
||||
{
|
||||
//delete geomExt;
|
||||
}
|
||||
|
||||
void lookupSkinData();
|
||||
|
||||
static void fixDetailSize(bool fixed, S32 size=2)
|
||||
{
|
||||
//fixedSizeEnabled = fixed;
|
||||
//fixedSize = size;
|
||||
}
|
||||
|
||||
/// Get the name of this mesh
|
||||
///
|
||||
/// @return A string containing the name of this mesh
|
||||
const char *getName(bool allowFixed=true);
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
|
||||
/// Get a floating point property value
|
||||
///
|
||||
/// @param propName Name of the property to get
|
||||
/// @param defaultVal Reference to variable to hold return value
|
||||
///
|
||||
/// @return True if a value was set, false if not
|
||||
bool getFloat(const char *propName, F32 &defaultVal)
|
||||
{
|
||||
return appNode->getFloat(propName,defaultVal);
|
||||
}
|
||||
|
||||
/// Get an integer property value
|
||||
///
|
||||
/// @param propName Name of the property to get
|
||||
/// @param defaultVal Reference to variable to hold return value
|
||||
///
|
||||
/// @return True if a value was set, false if not
|
||||
bool getInt(const char *propName, S32 &defaultVal)
|
||||
{
|
||||
return appNode->getInt(propName,defaultVal);
|
||||
}
|
||||
|
||||
/// Get a boolean property value
|
||||
///
|
||||
/// @param propName Name of the property to get
|
||||
/// @param defaultVal Reference to variable to hold return value
|
||||
///
|
||||
/// @return True if a value was set, false if not
|
||||
bool getBool(const char *propName, bool &defaultVal)
|
||||
{
|
||||
return appNode->getBool(propName,defaultVal);
|
||||
}
|
||||
|
||||
/// Return true if this mesh is a skin
|
||||
bool isSkin()
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Generate the vertex, normal and triangle data for the mesh.
|
||||
///
|
||||
/// @param time Time at which to generate the mesh data
|
||||
/// @param objectOffset Transform to apply to the generated data (bounds transform)
|
||||
void lockMesh(F32 time, const MatrixF& objectOffset);
|
||||
|
||||
/// Get the transform of this mesh at a certain time
|
||||
///
|
||||
/// @param time Time at which to get the transform
|
||||
///
|
||||
/// @return The mesh transform at the specified time
|
||||
MatrixF getMeshTransform(F32 time);
|
||||
F32 getVisValue(F32 t);
|
||||
};
|
||||
|
||||
#endif // _COLLADA_APPMESH_H_
|
||||
147
Engine/source/ts/assimp/assimpAppNode.cpp
Normal file
147
Engine/source/ts/assimp/assimpAppNode.cpp
Normal file
|
|
@ -0,0 +1,147 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "platform/platform.h"
|
||||
#include "ts/loader/appSequence.h"
|
||||
#include "ts/assimp/assimpAppNode.h"
|
||||
#include "ts/assimp/assimpAppMesh.h"
|
||||
|
||||
// assimp include files.
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include <assimp/types.h>
|
||||
|
||||
AssimpAppNode::AssimpAppNode(const struct aiScene* scene, const struct aiNode* node, AssimpAppNode* parent)
|
||||
{
|
||||
mScene = scene;
|
||||
mNode = node;
|
||||
appParent = parent;
|
||||
|
||||
mName = dStrdup(mNode->mName.C_Str());
|
||||
if ( dStrlen(mName) == 0 )
|
||||
{
|
||||
const char* defaultName = "null";
|
||||
mName = dStrdup(defaultName);
|
||||
}
|
||||
|
||||
mParentName = dStrdup(parent ? parent->getName() : "ROOT");
|
||||
Con::printf("[ASSIMP] Node Created: %s", mName);
|
||||
}
|
||||
|
||||
// Get all child nodes
|
||||
void AssimpAppNode::buildChildList()
|
||||
{
|
||||
if (!mNode)
|
||||
{
|
||||
mNode = mScene->mRootNode;
|
||||
}
|
||||
|
||||
for (U32 n = 0; n < mNode->mNumChildren; ++n) {
|
||||
mChildNodes.push_back(new AssimpAppNode(mScene, mNode->mChildren[n], this));
|
||||
}
|
||||
}
|
||||
|
||||
// Get all geometry attached to this node
|
||||
void AssimpAppNode::buildMeshList()
|
||||
{
|
||||
for (U32 n = 0; n < mNode->mNumMeshes; ++n)
|
||||
{
|
||||
const struct aiMesh* mesh = mScene->mMeshes[mNode->mMeshes[n]];
|
||||
mMeshes.push_back(new AssimpAppMesh(mesh, this));
|
||||
}
|
||||
}
|
||||
|
||||
MatrixF AssimpAppNode::getTransform(F32 time)
|
||||
{
|
||||
// Translate from assimp matrix to torque matrix.
|
||||
// They're both row major, I wish I could just cast
|
||||
// but that doesn't seem to be an option.
|
||||
|
||||
// Note: this should be cached, it doesn't change
|
||||
// at this level. This is base transform.
|
||||
|
||||
// Y and Z and optionally swapped.
|
||||
|
||||
MatrixF mat(false);
|
||||
mat.setRow(0, Point4F((F32)mNode->mTransformation.a1,
|
||||
(F32)mNode->mTransformation.a3,
|
||||
(F32)mNode->mTransformation.a2,
|
||||
(F32)mNode->mTransformation.a4)
|
||||
);
|
||||
|
||||
// Check for Y Z Swap
|
||||
if ( Con::getBoolVariable("$Assimp::SwapYZ", false) )
|
||||
{
|
||||
mat.setRow(1, Point4F((F32)mNode->mTransformation.c1,
|
||||
(F32)mNode->mTransformation.c3,
|
||||
(F32)mNode->mTransformation.c2,
|
||||
(F32)mNode->mTransformation.c4)
|
||||
);
|
||||
mat.setRow(2, Point4F((F32)mNode->mTransformation.b1,
|
||||
(F32)mNode->mTransformation.b3,
|
||||
(F32)mNode->mTransformation.b2,
|
||||
(F32)mNode->mTransformation.b4)
|
||||
);
|
||||
}
|
||||
else
|
||||
{
|
||||
mat.setRow(1, Point4F((F32)mNode->mTransformation.b1,
|
||||
(F32)mNode->mTransformation.b3,
|
||||
(F32)mNode->mTransformation.b2,
|
||||
(F32)mNode->mTransformation.b4)
|
||||
);
|
||||
mat.setRow(2, Point4F((F32)mNode->mTransformation.c1,
|
||||
(F32)mNode->mTransformation.c3,
|
||||
(F32)mNode->mTransformation.c2,
|
||||
(F32)mNode->mTransformation.c4)
|
||||
);
|
||||
}
|
||||
|
||||
mat.setRow(3, Point4F((F32)mNode->mTransformation.d1,
|
||||
(F32)mNode->mTransformation.d3,
|
||||
(F32)mNode->mTransformation.d2,
|
||||
(F32)mNode->mTransformation.d4)
|
||||
);
|
||||
|
||||
// Node transformations are carried down the hiearchy
|
||||
// so we need all of our parents transforms to make
|
||||
// this work.
|
||||
/*if ( appParent != 0 )
|
||||
{
|
||||
MatrixF parentMat = appParent->getNodeTransform(time);
|
||||
mat.mul(parentMat);
|
||||
}*/
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
bool AssimpAppNode::animatesTransform(const AppSequence* appSeq)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Get the world transform of the node at the specified time
|
||||
MatrixF AssimpAppNode::getNodeTransform(F32 time)
|
||||
{
|
||||
return getTransform(time);
|
||||
}
|
||||
98
Engine/source/ts/assimp/assimpAppNode.h
Normal file
98
Engine/source/ts/assimp/assimpAppNode.h
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _ASSIMP_APPNODE_H_
|
||||
#define _ASSIMP_APPNODE_H_
|
||||
|
||||
#ifndef _TDICTIONARY_H_
|
||||
#include "core/tDictionary.h"
|
||||
#endif
|
||||
#ifndef _APPNODE_H_
|
||||
#include "ts/loader/appNode.h"
|
||||
#endif
|
||||
#ifndef _COLLADA_EXTENSIONS_H_
|
||||
#include "ts/collada/colladaExtensions.h"
|
||||
#endif
|
||||
|
||||
class AssimpAppNode : public AppNode
|
||||
{
|
||||
typedef AppNode Parent;
|
||||
friend class AssimpAppMesh;
|
||||
|
||||
MatrixF getTransform(F32 time);
|
||||
void buildMeshList();
|
||||
void buildChildList();
|
||||
|
||||
protected:
|
||||
|
||||
const struct aiScene* mScene;
|
||||
const struct aiNode* mNode; ///< Pointer to the node in the Collada DOM
|
||||
AssimpAppNode* appParent; ///< Parent node in Collada-space
|
||||
|
||||
public:
|
||||
|
||||
AssimpAppNode(const struct aiScene* scene, const struct aiNode* node, AssimpAppNode* parent = 0);
|
||||
virtual ~AssimpAppNode()
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
const char *getName() { return mName; }
|
||||
const char *getParentName() { return mParentName; }
|
||||
|
||||
bool isEqual(AppNode* node)
|
||||
{
|
||||
const AssimpAppNode* appNode = dynamic_cast<const AssimpAppNode*>(node);
|
||||
return (appNode && (appNode->mNode == mNode));
|
||||
}
|
||||
|
||||
// Property look-ups: only float properties are stored, the rest are
|
||||
// converted from floats as needed
|
||||
bool getFloat(const char* propName, F32& defaultVal)
|
||||
{
|
||||
//Map<StringTableEntry,F32>::Iterator itr = mProps.find(propName);
|
||||
//if (itr != mProps.end())
|
||||
// defaultVal = itr->value;
|
||||
return false;
|
||||
}
|
||||
bool getInt(const char* propName, S32& defaultVal)
|
||||
{
|
||||
F32 value = defaultVal;
|
||||
bool ret = getFloat(propName, value);
|
||||
defaultVal = (S32)value;
|
||||
return ret;
|
||||
}
|
||||
bool getBool(const char* propName, bool& defaultVal)
|
||||
{
|
||||
F32 value = defaultVal;
|
||||
bool ret = getFloat(propName, value);
|
||||
defaultVal = (value != 0);
|
||||
return ret;
|
||||
}
|
||||
|
||||
MatrixF getNodeTransform(F32 time);
|
||||
bool animatesTransform(const AppSequence* appSeq);
|
||||
bool isParentRoot() { return (appParent == NULL); }
|
||||
};
|
||||
|
||||
#endif // _ASSIMP_APPNODE_H_
|
||||
45
Engine/source/ts/assimp/assimpAppSequence.cpp
Normal file
45
Engine/source/ts/assimp/assimpAppSequence.cpp
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include "ts/assimp/assimpShapeLoader.h"
|
||||
|
||||
#include "console/console.h"
|
||||
#include "core/stream/fileStream.h"
|
||||
#include "core/stringTable.h"
|
||||
#include "math/mathIO.h"
|
||||
#include "ts/tsShape.h"
|
||||
#include "ts/tsShapeInstance.h"
|
||||
#include "materials/materialManager.h"
|
||||
#include "console/persistenceManager.h"
|
||||
#include "ts/assimp/assimpAppMaterial.h"
|
||||
#include "ts/assimp/assimpAppSequence.h"
|
||||
|
||||
AssimpAppSequence::AssimpAppSequence(aiAnimation *a) :
|
||||
mAnim(a)
|
||||
{
|
||||
fps = mAnim->mTicksPerSecond;
|
||||
}
|
||||
|
||||
AssimpAppSequence::~AssimpAppSequence()
|
||||
{
|
||||
}
|
||||
|
||||
F32 AssimpAppSequence::getStart() const
|
||||
{
|
||||
return 0.0f;
|
||||
}
|
||||
F32 AssimpAppSequence::getEnd() const
|
||||
{
|
||||
return (F32)mAnim->mDuration / fps;
|
||||
}
|
||||
|
||||
U32 AssimpAppSequence::getFlags() const
|
||||
{
|
||||
return TSShape::Blend;
|
||||
}
|
||||
F32 AssimpAppSequence::getPriority() const
|
||||
{
|
||||
return 5;
|
||||
}
|
||||
F32 AssimpAppSequence::getBlendRefTime() const
|
||||
{
|
||||
return -1.0f;
|
||||
}
|
||||
|
||||
46
Engine/source/ts/assimp/assimpAppSequence.h
Normal file
46
Engine/source/ts/assimp/assimpAppSequence.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#pragma once
|
||||
#ifndef _MMATH_H_
|
||||
#include "math/mMath.h"
|
||||
#endif
|
||||
#ifndef _TVECTOR_H_
|
||||
#include "core/util/tVector.h"
|
||||
#endif
|
||||
#ifndef _TSSHAPE_H_
|
||||
#include "ts/tsShape.h"
|
||||
#endif
|
||||
#ifndef _APPNODE_H_
|
||||
#include "ts/loader/appNode.h"
|
||||
#endif
|
||||
#ifndef _APPMESH_H_
|
||||
#include "ts/loader/appMesh.h"
|
||||
#endif
|
||||
#ifndef _APPSEQUENCE_H_
|
||||
#include "ts/loader/appSequence.h"
|
||||
#endif
|
||||
|
||||
#include <assimp/scene.h>
|
||||
|
||||
class AssimpAppSequence : public AppSequence
|
||||
{
|
||||
public:
|
||||
|
||||
AssimpAppSequence(aiAnimation *a);
|
||||
~AssimpAppSequence();
|
||||
|
||||
aiAnimation *mAnim;
|
||||
|
||||
virtual void setActive(bool active) { }
|
||||
|
||||
virtual S32 getNumTriggers() const { return 0; }
|
||||
virtual void getTrigger(S32 index, TSShape::Trigger& trigger) const { trigger.state = 0; }
|
||||
|
||||
virtual const char* getName() const { return mAnim->mName.C_Str(); }
|
||||
|
||||
virtual F32 getStart() const;
|
||||
virtual F32 getEnd() const;
|
||||
|
||||
virtual U32 getFlags() const;
|
||||
virtual F32 getPriority() const;
|
||||
virtual F32 getBlendRefTime() const;
|
||||
|
||||
};
|
||||
393
Engine/source/ts/assimp/assimpShapeLoader.cpp
Normal file
393
Engine/source/ts/assimp/assimpShapeLoader.cpp
Normal file
|
|
@ -0,0 +1,393 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
/*
|
||||
Resource stream -> Buffer
|
||||
Buffer -> Collada DOM
|
||||
Collada DOM -> TSShapeLoader
|
||||
TSShapeLoader installed into TSShape
|
||||
*/
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#include "platform/platform.h"
|
||||
|
||||
#include "ts/assimp/assimpShapeLoader.h"
|
||||
#include "ts/assimp/assimpAppNode.h"
|
||||
#include "ts/assimp/assimpAppMaterial.h"
|
||||
#include "ts/assimp/assimpAppSequence.h"
|
||||
|
||||
#include "core/util/tVector.h"
|
||||
#include "core/strings/findMatch.h"
|
||||
#include "core/stream/fileStream.h"
|
||||
#include "core/fileObject.h"
|
||||
#include "ts/tsShape.h"
|
||||
#include "ts/tsShapeInstance.h"
|
||||
#include "materials/materialManager.h"
|
||||
#include "console/persistenceManager.h"
|
||||
#include "ts/tsShapeConstruct.h"
|
||||
#include "core/util/zip/zipVolume.h"
|
||||
#include "gfx/bitmap/gBitmap.h"
|
||||
#include "gui/controls/guiTreeViewCtrl.h"
|
||||
|
||||
// assimp include files.
|
||||
#include <assimp/cimport.h>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
#include <assimp/types.h>
|
||||
#include <assimp/config.h>
|
||||
#include <exception>
|
||||
|
||||
#include <assimp/Importer.hpp>
|
||||
|
||||
MODULE_BEGIN( AssimpShapeLoader )
|
||||
MODULE_INIT_AFTER( ShapeLoader )
|
||||
MODULE_INIT
|
||||
{
|
||||
TSShapeLoader::addFormat("DirectX X", "x");
|
||||
TSShapeLoader::addFormat("Autodesk FBX", "fbx");
|
||||
TSShapeLoader::addFormat("Blender 3D", "blend" );
|
||||
TSShapeLoader::addFormat("3ds Max 3DS", "3ds");
|
||||
TSShapeLoader::addFormat("3ds Max ASE", "ase");
|
||||
TSShapeLoader::addFormat("Wavefront Object", "obj");
|
||||
TSShapeLoader::addFormat("Industry Foundation Classes (IFC/Step)", "ifc");
|
||||
TSShapeLoader::addFormat("Stanford Polygon Library", "ply");
|
||||
TSShapeLoader::addFormat("AutoCAD DXF", "dxf");
|
||||
TSShapeLoader::addFormat("LightWave", "lwo");
|
||||
TSShapeLoader::addFormat("LightWave Scene", "lws");
|
||||
TSShapeLoader::addFormat("Modo", "lxo");
|
||||
TSShapeLoader::addFormat("Stereolithography", "stl");
|
||||
TSShapeLoader::addFormat("AC3D", "ac");
|
||||
TSShapeLoader::addFormat("Milkshape 3D", "ms3d");
|
||||
TSShapeLoader::addFormat("TrueSpace COB", "cob");
|
||||
TSShapeLoader::addFormat("TrueSpace SCN", "scn");
|
||||
TSShapeLoader::addFormat("Ogre XML", "xml");
|
||||
TSShapeLoader::addFormat("Irrlicht Mesh", "irrmesh");
|
||||
TSShapeLoader::addFormat("Irrlicht Scene", "irr");
|
||||
TSShapeLoader::addFormat("Quake I", "mdl" );
|
||||
TSShapeLoader::addFormat("Quake II", "md2" );
|
||||
TSShapeLoader::addFormat("Quake III Mesh", "md3");
|
||||
TSShapeLoader::addFormat("Quake III Map/BSP", "pk3");
|
||||
TSShapeLoader::addFormat("Return to Castle Wolfenstein", "mdc");
|
||||
TSShapeLoader::addFormat("Doom 3", "md5" );
|
||||
TSShapeLoader::addFormat("Valve SMD", "smd");
|
||||
TSShapeLoader::addFormat("Valve VTA", "vta");
|
||||
TSShapeLoader::addFormat("Starcraft II M3", "m3");
|
||||
TSShapeLoader::addFormat("Unreal", "3d");
|
||||
TSShapeLoader::addFormat("BlitzBasic 3D", "b3d" );
|
||||
TSShapeLoader::addFormat("Quick3D Q3D", "q3d");
|
||||
TSShapeLoader::addFormat("Quick3D Q3S", "q3s");
|
||||
TSShapeLoader::addFormat("Neutral File Format", "nff");
|
||||
TSShapeLoader::addFormat("Object File Format", "off");
|
||||
TSShapeLoader::addFormat("PovRAY Raw", "raw");
|
||||
TSShapeLoader::addFormat("Terragen Terrain", "ter");
|
||||
TSShapeLoader::addFormat("3D GameStudio (3DGS)", "mdl");
|
||||
TSShapeLoader::addFormat("3D GameStudio (3DGS) Terrain", "hmp");
|
||||
TSShapeLoader::addFormat("Izware Nendo", "ndo");
|
||||
}
|
||||
MODULE_END;
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
AssimpShapeLoader::AssimpShapeLoader()
|
||||
{
|
||||
mScene = NULL;
|
||||
}
|
||||
|
||||
AssimpShapeLoader::~AssimpShapeLoader()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void AssimpShapeLoader::releaseImport()
|
||||
{
|
||||
aiReleaseImport(mScene);
|
||||
}
|
||||
|
||||
void AssimpShapeLoader::enumerateScene()
|
||||
{
|
||||
TSShapeLoader::updateProgress(TSShapeLoader::Load_ReadFile, "Reading File");
|
||||
Con::printf("[ASSIMP] Attempting to load file: %s", shapePath.getFullPath().c_str());
|
||||
|
||||
// Post-Processing
|
||||
unsigned int ppsteps =
|
||||
Con::getBoolVariable("$Assimp::ConvertToLeftHanded", false) ? aiProcess_ConvertToLeftHanded : 0 |
|
||||
Con::getBoolVariable("$Assimp::CalcTangentSpace", false) ? aiProcess_CalcTangentSpace : 0 |
|
||||
Con::getBoolVariable("$Assimp::JoinIdenticalVertices", false) ? aiProcess_JoinIdenticalVertices : 0 |
|
||||
Con::getBoolVariable("$Assimp::ValidateDataStructure", false) ? aiProcess_ValidateDataStructure : 0 |
|
||||
Con::getBoolVariable("$Assimp::ImproveCacheLocality", false) ? aiProcess_ImproveCacheLocality : 0 |
|
||||
Con::getBoolVariable("$Assimp::RemoveRedundantMaterials", false) ? aiProcess_RemoveRedundantMaterials : 0 |
|
||||
Con::getBoolVariable("$Assimp::FindDegenerates", false) ? aiProcess_FindDegenerates : 0 |
|
||||
Con::getBoolVariable("$Assimp::FindInvalidData", false) ? aiProcess_FindInvalidData : 0 |
|
||||
Con::getBoolVariable("$Assimp::GenUVCoords", false) ? aiProcess_GenUVCoords : 0 |
|
||||
Con::getBoolVariable("$Assimp::TransformUVCoords", false) ? aiProcess_TransformUVCoords : 0 |
|
||||
Con::getBoolVariable("$Assimp::FindInstances", false) ? aiProcess_FindInstances : 0 |
|
||||
Con::getBoolVariable("$Assimp::LimitBoneWeights", false) ? aiProcess_LimitBoneWeights : 0 |
|
||||
Con::getBoolVariable("$Assimp::OptimizeMeshes", false) ? aiProcess_OptimizeMeshes | aiProcess_OptimizeGraph : 0 |
|
||||
0;
|
||||
|
||||
if(Con::getBoolVariable("$Assimp::Triangulate", false))
|
||||
ppsteps |= aiProcess_Triangulate;
|
||||
|
||||
if (Con::getBoolVariable("$Assimp::OptimizeMeshes", false))
|
||||
ppsteps |= aiProcess_OptimizeMeshes | aiProcess_OptimizeGraph;
|
||||
|
||||
if (Con::getBoolVariable("$Assimp::SplitLargeMeshes", false))
|
||||
ppsteps |= aiProcess_SplitLargeMeshes;
|
||||
|
||||
//aiProcess_SortByPType | // make 'clean' meshes which consist of a single typ of primitives
|
||||
|
||||
aiPropertyStore* props = aiCreatePropertyStore();
|
||||
|
||||
aiSetImportPropertyInteger(props, AI_CONFIG_IMPORT_TER_MAKE_UVS, 1);
|
||||
aiSetImportPropertyInteger(props, AI_CONFIG_PP_SBP_REMOVE, (aiProcessPreset_TargetRealtime_Quality
|
||||
| aiProcess_FlipWindingOrder | aiProcess_FlipUVs
|
||||
| aiProcess_CalcTangentSpace
|
||||
| aiProcess_FixInfacingNormals)
|
||||
& ~aiProcess_RemoveRedundantMaterials);
|
||||
aiSetImportPropertyInteger(props, AI_CONFIG_GLOB_MEASURE_TIME, 1);
|
||||
aiSetImportPropertyFloat(props, AI_CONFIG_PP_GSN_MAX_SMOOTHING_ANGLE, 80.f);
|
||||
//aiSetImportPropertyInteger(props,AI_CONFIG_PP_PTV_KEEP_HIERARCHY,1);
|
||||
|
||||
//Assimp::Importer importer;
|
||||
|
||||
// Attempt to import with Assimp.
|
||||
//mScene = importer.ReadFile(shapePath.getFullPath().c_str(), (aiProcessPreset_TargetRealtime_Quality | aiProcess_FlipWindingOrder | aiProcess_FlipUVs | aiProcess_CalcTangentSpace)
|
||||
// & ~aiProcess_RemoveRedundantMaterials);
|
||||
|
||||
mScene = (aiScene*)aiImportFileExWithProperties(shapePath.getFullPath().c_str(), ppsteps, NULL, props);
|
||||
|
||||
aiReleasePropertyStore(props);
|
||||
|
||||
if ( mScene )
|
||||
{
|
||||
Con::printf("[ASSIMP] Mesh Count: %d", mScene->mNumMeshes);
|
||||
Con::printf("[ASSIMP] Material Count: %d", mScene->mNumMaterials);
|
||||
|
||||
// Load all the materials.
|
||||
for ( U32 i = 0; i < mScene->mNumMaterials; i++ )
|
||||
AppMesh::appMaterials.push_back(new AssimpAppMaterial(mScene->mMaterials[i]));
|
||||
|
||||
// Define the root node, and process down the chain.
|
||||
AssimpAppNode* node = new AssimpAppNode(mScene, mScene->mRootNode, 0);
|
||||
|
||||
if (!processNode(node))
|
||||
delete node;
|
||||
|
||||
// Check for animations and process those.
|
||||
processAnimations();
|
||||
}
|
||||
else
|
||||
{
|
||||
TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Import failed");
|
||||
Con::printf("[ASSIMP] Import Error: %s", aiGetErrorString());
|
||||
}
|
||||
}
|
||||
|
||||
void AssimpShapeLoader::processAnimations()
|
||||
{
|
||||
for(U32 n = 0; n < mScene->mNumAnimations; ++n)
|
||||
{
|
||||
Con::printf("[ASSIMP] Animation Found: %s", mScene->mAnimations[n]->mName.C_Str());
|
||||
|
||||
AssimpAppSequence* newAssimpSeq = new AssimpAppSequence(mScene->mAnimations[n]);
|
||||
appSequences.push_back(newAssimpSeq);
|
||||
}
|
||||
}
|
||||
|
||||
void AssimpShapeLoader::updateMaterialsScript(const Torque::Path &path)
|
||||
{
|
||||
Torque::Path scriptPath(path);
|
||||
scriptPath.setFileName("materials");
|
||||
scriptPath.setExtension("cs");
|
||||
|
||||
// First see what materials we need to update
|
||||
PersistenceManager persistMgr;
|
||||
for ( U32 iMat = 0; iMat < AppMesh::appMaterials.size(); iMat++ )
|
||||
{
|
||||
AssimpAppMaterial *mat = dynamic_cast<AssimpAppMaterial*>( AppMesh::appMaterials[iMat] );
|
||||
if ( mat )
|
||||
{
|
||||
Material *mappedMat;
|
||||
if ( Sim::findObject( MATMGR->getMapEntry( mat->getName() ), mappedMat ) )
|
||||
{
|
||||
// Only update existing materials if forced to
|
||||
if ( ColladaUtils::getOptions().forceUpdateMaterials )
|
||||
persistMgr.setDirty( mappedMat );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Create a new material definition
|
||||
persistMgr.setDirty( mat->createMaterial( scriptPath ), scriptPath.getFullPath() );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( persistMgr.getDirtyList().empty() )
|
||||
return;
|
||||
|
||||
persistMgr.saveDirty();
|
||||
}
|
||||
|
||||
/// Check if an up-to-date cached DTS is available for this DAE file
|
||||
bool AssimpShapeLoader::canLoadCachedDTS(const Torque::Path& path)
|
||||
{
|
||||
return false;
|
||||
|
||||
// Generate the cached filename
|
||||
Torque::Path cachedPath(path);
|
||||
cachedPath.setExtension("cached.dts");
|
||||
|
||||
// Check if a cached DTS newer than this file is available
|
||||
FileTime cachedModifyTime;
|
||||
if (Platform::getFileTimes(cachedPath.getFullPath(), NULL, &cachedModifyTime))
|
||||
{
|
||||
bool forceLoadDAE = Con::getBoolVariable("$assimp::forceLoad", false);
|
||||
|
||||
FileTime daeModifyTime;
|
||||
if (!Platform::getFileTimes(path.getFullPath(), NULL, &daeModifyTime) ||
|
||||
(!forceLoadDAE && (Platform::compareFileTimes(cachedModifyTime, daeModifyTime) >= 0) ))
|
||||
{
|
||||
// DAE not found, or cached DTS is newer
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
/// This function is invoked by the resource manager based on file extension.
|
||||
TSShape* assimpLoadShape(const Torque::Path &path)
|
||||
{
|
||||
// TODO: add .cached.dts generation.
|
||||
// Generate the cached filename
|
||||
Torque::Path cachedPath(path);
|
||||
cachedPath.setExtension("cached.dts");
|
||||
|
||||
// Check if an up-to-date cached DTS version of this file exists, and
|
||||
// if so, use that instead.
|
||||
if (AssimpShapeLoader::canLoadCachedDTS(path))
|
||||
{
|
||||
FileStream cachedStream;
|
||||
cachedStream.open(cachedPath.getFullPath(), Torque::FS::File::Read);
|
||||
if (cachedStream.getStatus() == Stream::Ok)
|
||||
{
|
||||
TSShape *shape = new TSShape;
|
||||
bool readSuccess = shape->read(&cachedStream);
|
||||
cachedStream.close();
|
||||
|
||||
if (readSuccess)
|
||||
{
|
||||
#ifdef TORQUE_DEBUG
|
||||
Con::printf("Loaded cached shape from %s", cachedPath.getFullPath().c_str());
|
||||
#endif
|
||||
return shape;
|
||||
}
|
||||
else
|
||||
delete shape;
|
||||
}
|
||||
|
||||
Con::warnf("Failed to load cached shape from %s", cachedPath.getFullPath().c_str());
|
||||
}
|
||||
|
||||
if (!Torque::FS::IsFile(path))
|
||||
{
|
||||
// File does not exist, bail.
|
||||
return NULL;
|
||||
}
|
||||
|
||||
AssimpShapeLoader loader;
|
||||
TSShape* tss = loader.generateShape(path);
|
||||
if (tss)
|
||||
{
|
||||
TSShapeLoader::updateProgress(TSShapeLoader::Load_Complete, "Import complete");
|
||||
Con::printf("[ASSIMP] Shape created successfully.");
|
||||
|
||||
// Cache the model to a DTS file for faster loading next time.
|
||||
FileStream dtsStream;
|
||||
if (dtsStream.open(cachedPath.getFullPath(), Torque::FS::File::Write))
|
||||
{
|
||||
Con::printf("Writing cached shape to %s", cachedPath.getFullPath().c_str());
|
||||
tss->write(&dtsStream);
|
||||
}
|
||||
|
||||
loader.updateMaterialsScript(path);
|
||||
}
|
||||
loader.releaseImport();
|
||||
return tss;
|
||||
}
|
||||
|
||||
DefineConsoleFunction(GetShapeInfo, GuiTreeViewCtrl*, (String filePath), ,
|
||||
"Returns a list of supported shape formats in filter form.\n"
|
||||
"Example output: DSQ Files|*.dsq|COLLADA Files|*.dae|")
|
||||
{
|
||||
Assimp::Importer importer;
|
||||
|
||||
GuiTreeViewCtrl* treeObj = new GuiTreeViewCtrl();
|
||||
treeObj->registerObject();
|
||||
|
||||
Torque::Path path = Torque::Path(filePath);
|
||||
|
||||
// Attempt to import with Assimp.
|
||||
const aiScene* shapeScene = importer.ReadFile(path.getFullPath().c_str(), (aiProcessPreset_TargetRealtime_Quality | aiProcess_CalcTangentSpace)
|
||||
& ~aiProcess_RemoveRedundantMaterials & ~aiProcess_GenSmoothNormals);
|
||||
|
||||
//Populate info
|
||||
S32 meshItem = treeObj->insertItem(0, "Shape", String::ToString("%i", shapeScene->mNumMeshes));
|
||||
S32 matItem = treeObj->insertItem(0, "Materials", String::ToString("%i", shapeScene->mNumMaterials));
|
||||
S32 animItem = treeObj->insertItem(0, "Animations", String::ToString("%i", shapeScene->mNumAnimations));
|
||||
S32 lightsItem = treeObj->insertItem(0, "Lights", String::ToString("%i", shapeScene->mNumLights));
|
||||
S32 texturesItem = treeObj->insertItem(0, "Textures", String::ToString("%i", shapeScene->mNumTextures));
|
||||
//S32 meshItem = ->insertItem(0, "Cameras", String::ToString("%s", shapeScene->mNumCameras));
|
||||
|
||||
//Details!
|
||||
for (U32 i = 0; i < shapeScene->mNumMeshes; i++)
|
||||
{
|
||||
treeObj->insertItem(meshItem, String::ToString("%s", shapeScene->mMeshes[i]->mName));
|
||||
}
|
||||
|
||||
for (U32 i = 0; i < shapeScene->mNumMaterials; i++)
|
||||
{
|
||||
aiMaterial* aiMat = shapeScene->mMaterials[i];
|
||||
|
||||
aiString matName;
|
||||
aiMat->Get(AI_MATKEY_NAME, matName);
|
||||
|
||||
aiString texPath;
|
||||
aiMat->GetTexture(aiTextureType::aiTextureType_DIFFUSE, 0, &texPath);
|
||||
|
||||
treeObj->insertItem(matItem, String::ToString("%s", matName.C_Str()), String::ToString("%s", texPath.C_Str()));
|
||||
}
|
||||
|
||||
for (U32 i = 0; i < shapeScene->mNumAnimations; i++)
|
||||
{
|
||||
treeObj->insertItem(animItem, String::ToString("%s", shapeScene->mAnimations[i]->mName.C_Str()));
|
||||
}
|
||||
|
||||
/*for (U32 i = 0; i < shapeScene->mNumLights; i++)
|
||||
{
|
||||
treeObj->insertItem(lightsItem, String::ToString("%s", shapeScene->mLights[i]->mType));
|
||||
}*/
|
||||
|
||||
return treeObj;
|
||||
}
|
||||
50
Engine/source/ts/assimp/assimpShapeLoader.h
Normal file
50
Engine/source/ts/assimp/assimpShapeLoader.h
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
//-----------------------------------------------------------------------------
|
||||
// Copyright (c) 2012 GarageGames, LLC
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to
|
||||
// deal in the Software without restriction, including without limitation the
|
||||
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
// sell copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
// IN THE SOFTWARE.
|
||||
//-----------------------------------------------------------------------------
|
||||
|
||||
#ifndef _ASSIMP_SHAPELOADER_H_
|
||||
#define _ASSIMP_SHAPELOADER_H_
|
||||
|
||||
#ifndef _TSSHAPELOADER_H_
|
||||
#include "ts/loader/tsShapeLoader.h"
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
class AssimpShapeLoader : public TSShapeLoader
|
||||
{
|
||||
friend TSShape* assimpLoadShape(const Torque::Path &path);
|
||||
|
||||
protected:
|
||||
const struct aiScene* mScene;
|
||||
|
||||
public:
|
||||
AssimpShapeLoader();
|
||||
~AssimpShapeLoader();
|
||||
|
||||
void releaseImport();
|
||||
void enumerateScene();
|
||||
void updateMaterialsScript(const Torque::Path &path);
|
||||
void processAnimations();
|
||||
|
||||
static bool canLoadCachedDTS(const Torque::Path& path);
|
||||
};
|
||||
|
||||
#endif // _ASSIMP_SHAPELOADER_H_
|
||||
Loading…
Add table
Add a link
Reference in a new issue