ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

stark/model/skeleton.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/stark/model/skeleton.h"
00024 
00025 #include "engines/stark/model/skeleton_anim.h"
00026 #include "engines/stark/services/archiveloader.h"
00027 
00028 namespace Stark {
00029 
00030 Skeleton::Skeleton() :
00031         _anim(nullptr),
00032         _lastTime(-1) {
00033 
00034 }
00035 
00036 Skeleton::~Skeleton() {
00037     for (Common::Array<BoneNode *>::iterator it = _bones.begin(); it != _bones.end(); ++it)
00038         delete *it;
00039 }
00040 
00041 const Common::Array<BoneNode *> Skeleton::getBones() {
00042     return _bones;
00043 }
00044 
00045 void Skeleton::readFromStream(ArchiveReadStream *stream) {
00046     uint32 numBones = stream->readUint32LE();
00047     for (uint32 i = 0; i < numBones; ++i) {
00048         BoneNode *node = new BoneNode();
00049         node->_name = stream->readString();
00050         node->_u1 = stream->readFloat();
00051 
00052         uint32 len = stream->readUint32LE();
00053         for (uint32 j = 0; j < len; ++j)
00054             node->_children.push_back(stream->readUint32LE());
00055 
00056         node->_idx = _bones.size();
00057         _bones.push_back(node);
00058     }
00059 
00060     for (uint32 i = 0; i < numBones; ++i) {
00061         BoneNode *node = _bones[i];
00062         for (uint j = 0; j < node->_children.size(); ++j) {
00063             _bones[node->_children[j]]->_parent = i;
00064         }
00065     }
00066 }
00067 
00068 void Skeleton::setAnim(SkeletonAnim *anim) {
00069     _anim = anim;
00070 }
00071 
00072 void Skeleton::setNode(uint32 time, BoneNode *bone, const BoneNode *parent) {
00073     _anim->getCoordForBone(time, bone->_idx, bone->_animPos, bone->_animRot);
00074 
00075     if (parent) {
00076         parent->_animRot.transform(bone->_animPos);
00077 
00078         bone->_animPos = parent->_animPos + bone->_animPos;
00079         bone->_animRot = parent->_animRot * bone->_animRot;
00080     }
00081 
00082     for (uint i = 0; i < bone->_children.size(); ++i) {
00083         setNode(time, _bones[bone->_children[i]], bone);
00084     }
00085 }
00086 
00087 void Skeleton::animate(uint32 time) {
00088     // Start at root bone
00089     // For each child
00090     //  - Set childs animation coordinate
00091     //  - Process that childs children
00092 
00093     if (time != _lastTime) {
00094         setNode(time, _bones[0], nullptr);
00095         _lastTime = time;
00096     }
00097 }
00098 
00099 bool BoneNode::intersectRay(const Math::Ray &ray) const {
00100     Math::Ray localRay = ray;
00101     localRay.translate(-_animPos);
00102     localRay.rotate(_animRot.inverse());
00103 
00104     return localRay.intersectAABB(_boundingBox);
00105 }
00106 } // End of namespace Stark


Generated on Sat Apr 9 2016 05:00:24 for ResidualVM by doxygen 1.7.1
curved edge   curved edge