|
|
|
@ -26,11 +26,11 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
|
|
#include <libs/mangle/vfs/servers/ogre_vfs.hpp>
|
|
|
|
|
#include "components/nif/nif_file.hpp"
|
|
|
|
|
#include "components/nif/node.hpp"
|
|
|
|
|
#include "components/nif/data.hpp"
|
|
|
|
|
#include "components/nif/property.hpp"
|
|
|
|
|
#include "libs/platform/strings.h"
|
|
|
|
|
#include "../nif/nif_file.hpp"
|
|
|
|
|
#include "../nif/node.hpp"
|
|
|
|
|
#include "../nif/data.hpp"
|
|
|
|
|
#include "../nif/property.hpp"
|
|
|
|
|
#include <libs/platform/strings.h>
|
|
|
|
|
|
|
|
|
|
// For warning messages
|
|
|
|
|
#include <iostream>
|
|
|
|
@ -45,21 +45,43 @@ using namespace Ogre;
|
|
|
|
|
using namespace Nif;
|
|
|
|
|
using namespace Mangle::VFS;
|
|
|
|
|
|
|
|
|
|
// This is the interface to the Ogre resource system. It allows us to
|
|
|
|
|
// load NIFs from BSAs, in the file system and in any other place we
|
|
|
|
|
// tell Ogre to look (eg. in zip or rar files.) It's also used to
|
|
|
|
|
// check for the existence of texture files, so we can exchange the
|
|
|
|
|
// extension from .tga to .dds if the texture is missing.
|
|
|
|
|
static OgreVFS *vfs;
|
|
|
|
|
NIFLoader& NIFLoader::getSingleton()
|
|
|
|
|
{
|
|
|
|
|
static NIFLoader instance;
|
|
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NIFLoader* NIFLoader::getSingletonPtr()
|
|
|
|
|
{
|
|
|
|
|
return &getSingleton();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NIFLoader::warn(string msg)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "NIFLoader: Warn:" << msg << "\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Singleton instance used by load()
|
|
|
|
|
static NIFLoader g_sing;
|
|
|
|
|
void NIFLoader::fail(string msg)
|
|
|
|
|
{
|
|
|
|
|
std::cerr << "NIFLoader: Fail: "<< msg << std::endl;
|
|
|
|
|
assert(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Vector3 NIFLoader::convertVector3(const Nif::Vector& vec)
|
|
|
|
|
{
|
|
|
|
|
return Ogre::Vector3(vec.array);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Makeshift error reporting system
|
|
|
|
|
static string errName;
|
|
|
|
|
static void warn(const string &msg)
|
|
|
|
|
Quaternion NIFLoader::convertRotation(const Nif::Matrix& rot)
|
|
|
|
|
{
|
|
|
|
|
cout << "WARNING (NIF:" << errName << "): " << msg << endl;
|
|
|
|
|
Real matrix[3][3];
|
|
|
|
|
|
|
|
|
|
for (int i=0; i<3; i++)
|
|
|
|
|
for (int j=0; j<3; j++)
|
|
|
|
|
matrix[i][j] = rot.v[i].array[j];
|
|
|
|
|
|
|
|
|
|
return Quaternion(Matrix3(matrix));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper class that computes the bounding box and of a mesh
|
|
|
|
@ -126,12 +148,24 @@ public:
|
|
|
|
|
return sqrt(X.getMaxSquared() + Y.getMaxSquared() + Z.getMaxSquared());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
float minX() { return X.min; }
|
|
|
|
|
float maxX() { return X.max; }
|
|
|
|
|
float minY() { return Y.min; }
|
|
|
|
|
float maxY() { return Y.max; }
|
|
|
|
|
float minZ() { return Z.min; }
|
|
|
|
|
float maxZ() { return Z.max; }
|
|
|
|
|
float minX() {
|
|
|
|
|
return X.min;
|
|
|
|
|
}
|
|
|
|
|
float maxX() {
|
|
|
|
|
return X.max;
|
|
|
|
|
}
|
|
|
|
|
float minY() {
|
|
|
|
|
return Y.min;
|
|
|
|
|
}
|
|
|
|
|
float maxY() {
|
|
|
|
|
return Y.max;
|
|
|
|
|
}
|
|
|
|
|
float minZ() {
|
|
|
|
|
return Z.min;
|
|
|
|
|
}
|
|
|
|
|
float maxZ() {
|
|
|
|
|
return Z.max;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Conversion of blend / test mode from NIF -> OGRE.
|
|
|
|
@ -177,7 +211,7 @@ static CompareFunction getTestMode(int mode)
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static void createMaterial(const String &name,
|
|
|
|
|
void NIFLoader::createMaterial(const String &name,
|
|
|
|
|
const Vector &ambient,
|
|
|
|
|
const Vector &diffuse,
|
|
|
|
|
const Vector &specular,
|
|
|
|
@ -186,7 +220,7 @@ static void createMaterial(const String &name,
|
|
|
|
|
float alphaFlags, float alphaTest,
|
|
|
|
|
const String &texName)
|
|
|
|
|
{
|
|
|
|
|
MaterialPtr material = MaterialManager::getSingleton().create(name, "General");
|
|
|
|
|
MaterialPtr material = MaterialManager::getSingleton().create(name, resourceGroup);
|
|
|
|
|
|
|
|
|
|
// This assigns the texture to this material. If the texture name is
|
|
|
|
|
// a file name, and this file exists (in a resource directory), it
|
|
|
|
@ -196,7 +230,8 @@ static void createMaterial(const String &name,
|
|
|
|
|
if (!texName.empty())
|
|
|
|
|
{
|
|
|
|
|
Pass *pass = material->getTechnique(0)->getPass(0);
|
|
|
|
|
/*TextureUnitState *txt =*/ pass->createTextureUnitState(texName);
|
|
|
|
|
/*TextureUnitState *txt =*/
|
|
|
|
|
pass->createTextureUnitState(texName);
|
|
|
|
|
|
|
|
|
|
/* As of yet UNTESTED code from Chris:
|
|
|
|
|
pass->setTextureFiltering(Ogre::TFO_ANISOTROPIC);
|
|
|
|
@ -254,7 +289,7 @@ static void createMaterial(const String &name,
|
|
|
|
|
|
|
|
|
|
// Takes a name and adds a unique part to it. This is just used to
|
|
|
|
|
// make sure that all materials are given unique names.
|
|
|
|
|
static String getUniqueName(const String &input)
|
|
|
|
|
String NIFLoader::getUniqueName(const String &input)
|
|
|
|
|
{
|
|
|
|
|
static int addon = 0;
|
|
|
|
|
static char buf[8];
|
|
|
|
@ -270,7 +305,7 @@ static String getUniqueName(const String &input)
|
|
|
|
|
// does not, change the string IN PLACE to say .dds instead and try
|
|
|
|
|
// that. The texture may still not exist, but no information of value
|
|
|
|
|
// is lost in that case.
|
|
|
|
|
static void findRealTexture(String &texName)
|
|
|
|
|
void NIFLoader::findRealTexture(String &texName)
|
|
|
|
|
{
|
|
|
|
|
assert(vfs);
|
|
|
|
|
if (vfs->isFile(texName)) return;
|
|
|
|
@ -286,7 +321,7 @@ static void findRealTexture(String &texName)
|
|
|
|
|
|
|
|
|
|
// Convert Nif::NiTriShape to Ogre::SubMesh, attached to the given
|
|
|
|
|
// mesh.
|
|
|
|
|
static void createOgreMesh(Mesh *mesh, NiTriShape *shape, const String &material)
|
|
|
|
|
void NIFLoader::createOgreSubMesh(NiTriShape *shape, const String &material)
|
|
|
|
|
{
|
|
|
|
|
NiTriShapeData *data = shape->data.getPtr();
|
|
|
|
|
SubMesh *sub = mesh->createSubMesh(shape->name.toString());
|
|
|
|
@ -372,21 +407,33 @@ static void createOgreMesh(Mesh *mesh, NiTriShape *shape, const String &material
|
|
|
|
|
// Set material if one was given
|
|
|
|
|
if (!material.empty()) sub->setMaterialName(material);
|
|
|
|
|
|
|
|
|
|
/* Old commented D code. Might be useful when reimplementing
|
|
|
|
|
animation.
|
|
|
|
|
// Assign this submesh to the given bone
|
|
|
|
|
VertexBoneAssignment v;
|
|
|
|
|
v.boneIndex = ((Bone*)bone)->getHandle();
|
|
|
|
|
v.weight = 1.0;
|
|
|
|
|
|
|
|
|
|
std::cerr << "+ Assigning bone index " << v.boneIndex << "\n";
|
|
|
|
|
// assigning bones if skindata is not empty
|
|
|
|
|
if (!shape->skin.empty())
|
|
|
|
|
{
|
|
|
|
|
NodeList *boneList = &shape->skin->bones;
|
|
|
|
|
|
|
|
|
|
for(int i=0; i < numVerts; i++)
|
|
|
|
|
for (int i=0; i<boneList->length(); i++)
|
|
|
|
|
{
|
|
|
|
|
v.vertexIndex = i;
|
|
|
|
|
sub->addBoneAssignment(v);
|
|
|
|
|
if (boneList->has(i))
|
|
|
|
|
{
|
|
|
|
|
Nif::Node *bone = &(*boneList)[i];
|
|
|
|
|
|
|
|
|
|
SkeletonPtr skel = SkeletonManager::getSingleton().getByName(getSkeletonName());
|
|
|
|
|
|
|
|
|
|
VertexBoneAssignment vba;
|
|
|
|
|
vba.boneIndex = skel->getBone(bone->name.toString())->getHandle();
|
|
|
|
|
vba.weight = 1.0;
|
|
|
|
|
|
|
|
|
|
for (unsigned int j=0; j<sub->vertexData->vertexCount; j++) //assing every vertex
|
|
|
|
|
{
|
|
|
|
|
vba.vertexIndex = j;
|
|
|
|
|
sub->addBoneAssignment(vba);
|
|
|
|
|
}
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper math functions. Reinventing linear algebra for the win!
|
|
|
|
@ -432,7 +479,7 @@ static void vectorMul(const Matrix &A, float *C)
|
|
|
|
|
C[i] = a*A.v[i].array[0] + b*A.v[i].array[1] + c*A.v[i].array[2];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void handleNiTriShape(Mesh *mesh, NiTriShape *shape, int flags, BoundsFinder &bounds)
|
|
|
|
|
void NIFLoader::handleNiTriShape(NiTriShape *shape, int flags, BoundsFinder &bounds)
|
|
|
|
|
{
|
|
|
|
|
assert(shape != NULL);
|
|
|
|
|
|
|
|
|
@ -451,7 +498,10 @@ static void handleNiTriShape(Mesh *mesh, NiTriShape *shape, int flags, BoundsFin
|
|
|
|
|
// If the object was marked "NCO" earlier, it shouldn't collide with
|
|
|
|
|
// anything.
|
|
|
|
|
if (flags & 0x800)
|
|
|
|
|
{ collide = false; bbcollide = false; }
|
|
|
|
|
{
|
|
|
|
|
collide = false;
|
|
|
|
|
bbcollide = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!collide && !bbcollide && hidden)
|
|
|
|
|
// This mesh apparently isn't being used for anything, so don't
|
|
|
|
@ -596,12 +646,12 @@ static void handleNiTriShape(Mesh *mesh, NiTriShape *shape, int flags, BoundsFin
|
|
|
|
|
bounds.add(optr, numVerts);
|
|
|
|
|
|
|
|
|
|
// Create the submesh
|
|
|
|
|
createOgreMesh(mesh, shape, material);
|
|
|
|
|
createOgreSubMesh(shape, material);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void handleNode(Mesh* mesh, Nif::Node *node, int flags,
|
|
|
|
|
const Transformation *trafo, BoundsFinder &bounds)
|
|
|
|
|
void NIFLoader::handleNode(Nif::Node *node, int flags,
|
|
|
|
|
const Transformation *trafo, BoundsFinder &bounds, Bone *parentBone)
|
|
|
|
|
{
|
|
|
|
|
// Accumulate the flags from all the child nodes. This works for all
|
|
|
|
|
// the flags we currently use, at least.
|
|
|
|
@ -632,6 +682,36 @@ static void handleNode(Mesh* mesh, Nif::Node *node, int flags,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Bone *bone = 0;
|
|
|
|
|
|
|
|
|
|
// create skeleton or add bones
|
|
|
|
|
if (node->recType == RC_NiNode)
|
|
|
|
|
{
|
|
|
|
|
if (node->name == "Bip01") //root node, create a skeleton
|
|
|
|
|
{
|
|
|
|
|
skel = SkeletonManager::getSingleton().create(getSkeletonName(), resourceGroup, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!skel.isNull()) //if there is a skeleton
|
|
|
|
|
{
|
|
|
|
|
bone = skel->createBone(node->name.toString());
|
|
|
|
|
|
|
|
|
|
if (parentBone)
|
|
|
|
|
parentBone->addChild(bone);
|
|
|
|
|
|
|
|
|
|
bone->setInheritOrientation(true);
|
|
|
|
|
bone->setPosition(convertVector3(node->trafo->pos));
|
|
|
|
|
bone->setOrientation(convertRotation(node->trafo->rotation));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//output for debuging purpose
|
|
|
|
|
// Ogre::Vector3 vec(node->trafo->pos.array);
|
|
|
|
|
// Ogre::Quaternion q = convertRotation(node->trafo->rotation);
|
|
|
|
|
// std::cout << node->name.toString() << ": " << vec.x << " " << vec.y << " " << vec.z << "\n";
|
|
|
|
|
// std::cout << " Y: " << q.getYaw().valueDegrees() << " P: " << q.getPitch().valueDegrees() << " R: " << q.getRoll().valueDegrees() << "\n";
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Apply the parent transformation to this node. We overwrite the
|
|
|
|
|
// existing data with the final transformation.
|
|
|
|
|
if (trafo)
|
|
|
|
@ -661,27 +741,31 @@ static void handleNode(Mesh* mesh, Nif::Node *node, int flags,
|
|
|
|
|
for (int i=0; i<n; i++)
|
|
|
|
|
{
|
|
|
|
|
if (list.has(i))
|
|
|
|
|
handleNode(mesh, &list[i], flags, node->trafo, bounds);
|
|
|
|
|
handleNode(&list[i], flags, node->trafo, bounds, bone);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (node->recType == RC_NiTriShape)
|
|
|
|
|
// For shapes
|
|
|
|
|
handleNiTriShape(mesh, dynamic_cast<NiTriShape*>(node), flags, bounds);
|
|
|
|
|
handleNiTriShape(dynamic_cast<NiTriShape*>(node), flags, bounds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NIFLoader::loadResource(Resource *resource)
|
|
|
|
|
{
|
|
|
|
|
resourceName = "";
|
|
|
|
|
mesh = 0;
|
|
|
|
|
skel.setNull();
|
|
|
|
|
|
|
|
|
|
// Set up the VFS if it hasn't been done already
|
|
|
|
|
if(!vfs) vfs = new OgreVFS("General");
|
|
|
|
|
if (!vfs) vfs = new OgreVFS(resourceGroup);
|
|
|
|
|
|
|
|
|
|
// Get the mesh
|
|
|
|
|
Mesh *mesh = dynamic_cast<Mesh*>(resource);
|
|
|
|
|
mesh = dynamic_cast<Mesh*>(resource);
|
|
|
|
|
assert(mesh);
|
|
|
|
|
|
|
|
|
|
// Look it up
|
|
|
|
|
const String &name = mesh->getName();
|
|
|
|
|
errName = name; // Set name for error messages
|
|
|
|
|
if(!vfs->isFile(name))
|
|
|
|
|
resourceName = mesh->getName();
|
|
|
|
|
|
|
|
|
|
if (!vfs->isFile(resourceName))
|
|
|
|
|
{
|
|
|
|
|
warn("File not found.");
|
|
|
|
|
return;
|
|
|
|
@ -694,7 +778,7 @@ void NIFLoader::loadResource(Resource *resource)
|
|
|
|
|
// of the early stages of development. Right now we WANT to catch
|
|
|
|
|
// every error as early and intrusively as possible, as it's most
|
|
|
|
|
// likely a sign of incomplete code rather than faulty input.
|
|
|
|
|
NIFFile nif(vfs->open(name), name);
|
|
|
|
|
NIFFile nif(vfs->open(resourceName), resourceName);
|
|
|
|
|
|
|
|
|
|
if (nif.numRecords() < 1)
|
|
|
|
|
{
|
|
|
|
@ -716,7 +800,11 @@ void NIFLoader::loadResource(Resource *resource)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Handle the node
|
|
|
|
|
handleNode(mesh, node, 0, NULL, bounds);
|
|
|
|
|
handleNode(node, 0, NULL, bounds, 0);
|
|
|
|
|
|
|
|
|
|
//set skeleton
|
|
|
|
|
if (!skel.isNull())
|
|
|
|
|
mesh->setSkeletonName(getSkeletonName());
|
|
|
|
|
|
|
|
|
|
// Finally, set the bounding value.
|
|
|
|
|
if (bounds.isValid())
|
|
|
|
@ -738,7 +826,7 @@ MeshPtr NIFLoader::load(const std::string &name,
|
|
|
|
|
return MeshPtr(ptr);
|
|
|
|
|
|
|
|
|
|
// Nope, create a new one.
|
|
|
|
|
return MeshManager::getSingleton().createManual(name, group, &g_sing);
|
|
|
|
|
return MeshManager::getSingleton().createManual(name, group, NIFLoader::getSingletonPtr());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* More code currently not in use, from the old D source. This was
|
|
|
|
|