1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-19 22:23:51 +00:00
openmw-tes3mp/apps/openmw/mwrender/npcanimation.cpp

258 lines
8.1 KiB
C++
Raw Normal View History

#include "npcanimation.hpp"
#include "../mwworld/world.hpp"
2012-04-03 13:13:47 +00:00
#include "renderconst.hpp"
2011-12-14 04:49:03 +00:00
2012-04-08 02:02:20 +00:00
using namespace Ogre;
using namespace NifOgre;
namespace MWRender{
2011-12-12 04:42:39 +00:00
NpcAnimation::~NpcAnimation(){
}
2012-04-12 00:16:22 +00:00
NpcAnimation::NpcAnimation(const MWWorld::Ptr& ptr, MWWorld::Environment& _env,OEngine::Render::OgreRenderer& _rend, MWWorld::InventoryStore& _inv): Animation(_env,_rend), mStateID(-1), inv(_inv), timeToChange(0), robe(inv.getSlot(MWWorld::InventoryStore::Slot_Robe)){
ESMS::LiveCellRef<ESM::NPC, MWWorld::RefData> *ref =
ptr.get<ESM::NPC>();
2012-04-08 02:02:20 +00:00
Ogre::Entity* blank = 0;
std::vector<Nif::NiTriShapeCopy>* blankshape = 0;
chest = std::make_pair(blank, blankshape);
2012-04-08 21:27:56 +00:00
tail = std::make_pair(blank, blankshape);
lBeastFoot = std::make_pair(blank, blankshape);
rBeastFoot = std::make_pair(blank, blankshape);
rhand = std::make_pair(blank, blankshape);
lhand = std::make_pair(blank, blankshape);
skirt = std::make_pair(blank, blankshape);
//Part selection on last character of the file string
// " Tri Chest
// * Tri Tail
// : Tri Left Foot
// < Tri Right Foot
// > Tri Left Hand
// ? Tri Right Hand
// | Normal
//Mirroring Parts on second to last character
//suffix == '*'
// vector = Ogre::Vector3(-1,1,1);
// suffix == '?'
// vector = Ogre::Vector3(1,-1,1);
// suffix == '<'
// vector = Ogre::Vector3(1,1,-1);
2012-04-08 21:27:56 +00:00
hairID = ref->base->hair;
headID = ref->base->head;
npcName = ref->base->name;
2012-01-17 14:10:53 +00:00
//ESMStore::Races r =
2012-01-01 22:28:46 +00:00
const ESM::Race* race = mEnvironment.mWorld->getStore().races.find(ref->base->race);
2012-01-17 14:10:53 +00:00
2012-04-08 21:27:56 +00:00
bodyRaceID = headID.substr(0, headID.find_last_of("head_") - 4);
char secondtolast = bodyRaceID.at(bodyRaceID.length() - 2);
bool female = tolower(secondtolast) == 'f';
2012-03-03 23:26:11 +00:00
std::transform(bodyRaceID.begin(), bodyRaceID.end(), bodyRaceID.begin(), ::tolower);
2012-04-08 21:27:56 +00:00
isBeast = bodyRaceID == "b_n_khajiit_m_" || bodyRaceID == "b_n_khajiit_f_" || bodyRaceID == "b_n_argonian_m_" || bodyRaceID == "b_n_argonian_f_";
2012-02-19 20:39:12 +00:00
2012-01-06 02:45:17 +00:00
/*std::cout << "Race: " << ref->base->race ;
2012-01-01 22:28:46 +00:00
if(female){
std::cout << " Sex: Female" << " Height: " << race->data.height.female << "\n";
}
else{
std::cout << " Sex: Male" << " Height: " << race->data.height.male << "\n";
2012-01-06 02:45:17 +00:00
}*/
2012-01-17 14:10:53 +00:00
std::string smodel = "meshes\\base_anim.nif";
2012-04-08 21:27:56 +00:00
if(isBeast)
smodel = "meshes\\base_animkna.nif";
2011-12-27 05:20:14 +00:00
insert = ptr.getRefData().getBaseNode();
assert(insert);
NifOgre::NIFLoader::load(smodel);
2012-01-17 14:10:53 +00:00
base = mRend.getScene()->createEntity(smodel);
2012-04-03 02:08:46 +00:00
2012-04-03 13:13:47 +00:00
base->setVisibilityFlags(RV_Actors);
2012-04-04 16:53:40 +00:00
bool transparent = false;
for (unsigned int i=0; i<base->getNumSubEntities(); ++i)
{
Ogre::MaterialPtr mat = base->getSubEntity(i)->getMaterial();
Ogre::Material::TechniqueIterator techIt = mat->getTechniqueIterator();
while (techIt.hasMoreElements())
{
Ogre::Technique* tech = techIt.getNext();
Ogre::Technique::PassIterator passIt = tech->getPassIterator();
while (passIt.hasMoreElements())
{
Ogre::Pass* pass = passIt.getNext();
if (pass->getDepthWriteEnabled() == false)
transparent = true;
}
}
}
base->setRenderQueueGroup(transparent ? RQG_Alpha : RQG_Main);
2012-04-09 00:41:49 +00:00
2012-04-04 16:53:40 +00:00
2012-01-17 14:10:53 +00:00
base->setSkipAnimationStateUpdate(true); //Magical line of code, this makes the bones
//stay in the same place when we skipanim, or open a gui window
2011-12-14 04:49:03 +00:00
2012-01-17 14:10:53 +00:00
2012-02-19 20:39:12 +00:00
2012-01-17 14:10:53 +00:00
if((transformations = (NIFLoader::getSingletonPtr())->getAnim(smodel))){
2011-12-15 05:33:10 +00:00
2012-01-06 02:45:17 +00:00
for(unsigned int init = 0; init < transformations->size(); init++){
2011-12-14 04:49:03 +00:00
rindexI.push_back(0);
tindexI.push_back(0);
2011-12-15 05:33:10 +00:00
}
2012-01-17 14:10:53 +00:00
2011-12-15 05:33:10 +00:00
stopTime = transformations->begin()->getStopTime();
2012-01-06 02:45:17 +00:00
startTime = transformations->begin()->getStartTime();
2011-12-15 05:33:10 +00:00
}
2012-01-06 05:55:02 +00:00
textmappings = NIFLoader::getSingletonPtr()->getTextIndices(smodel);
insert->attachObject(base);
2012-04-03 02:08:46 +00:00
2012-01-17 14:10:53 +00:00
2012-01-01 22:28:46 +00:00
if(female)
insert->scale(race->data.height.female, race->data.height.female, race->data.height.female);
else
insert->scale(race->data.height.male, race->data.height.male, race->data.height.male);
2012-04-12 00:16:22 +00:00
std::cout << "Inv" << inv.getStateId() << "\n";
2012-04-08 21:27:56 +00:00
updateParts();
}
void NpcAnimation::updateParts(){
std::string headModel = "meshes\\" +
mEnvironment.mWorld->getStore().bodyParts.find(headID)->model;
std::string hairModel = "meshes\\" +
mEnvironment.mWorld->getStore().bodyParts.find(hairID)->model;
2012-04-08 21:27:56 +00:00
//inv.getSlot(MWWorld::InventoryStore::Slot_Robe);
2012-04-12 00:16:22 +00:00
if(robe != inv.getSlot(MWWorld::InventoryStore::Slot_Robe)){
//A robe was added or removed
if(chest.first)
{
insert->detachObject(chest.first); chest.first = 0;
}
robe = inv.getSlot(MWWorld::InventoryStore::Slot_Robe);
if(robe != inv.end())
{
MWWorld::Ptr ptr = *robe;
const ESM::Clothing *clothes = (ptr.get<ESM::Clothing>())->base;
std::vector<ESM::PartReference> parts = clothes->parts.parts;
for(int i = 0; i < parts.size(); i++)
{
ESM::PartReference part = parts[i];
if(part.part == ESM::PRT_Cuirass)
{
const ESM::BodyPart *bodypart = mEnvironment.mWorld->getStore().bodyParts.search (part.male);
chest = insertFreePart("meshes\\" + bodypart->model, ":\"");
}
}
}
2012-04-08 21:27:56 +00:00
2012-04-12 00:16:22 +00:00
}
if(robe == inv.end() ){
//if(inv.getSlot(MWWorld::InventoryStore::Cuirass) != inv.end())
if(chest.first == 0){
const ESM::BodyPart *chestPart = mEnvironment.mWorld->getStore().bodyParts.search (bodyRaceID + "chest");
chest = insertFreePart("meshes\\" + chestPart->model, ":\"");
}
}
2012-04-09 04:51:10 +00:00
2012-04-08 21:27:56 +00:00
2012-04-03 02:08:46 +00:00
}
Ogre::Entity* NpcAnimation::insertBoundedPart(const std::string &mesh, std::string bonename){
2012-01-29 05:42:55 +00:00
NIFLoader::load(mesh);
2012-04-08 02:02:20 +00:00
Ogre::Entity* part = mRend.getScene()->createEntity(mesh);
2012-01-17 14:10:53 +00:00
2012-04-08 02:02:20 +00:00
base->attachObjectToBone(bonename, part);
return part;
2011-12-14 04:49:03 +00:00
}
2012-04-08 02:02:20 +00:00
std::pair<Ogre::Entity*, std::vector<Nif::NiTriShapeCopy>*> NpcAnimation::insertFreePart(const std::string &mesh, const std::string suffix){
2012-01-17 14:10:53 +00:00
std::string meshNumbered = mesh + getUniqueID(mesh + suffix) + suffix;
NIFLoader::load(meshNumbered);
2012-01-17 14:10:53 +00:00
2012-04-08 02:02:20 +00:00
Ogre::Entity* part = mRend.getScene()->createEntity(meshNumbered);
2012-01-08 04:21:19 +00:00
2012-02-19 20:39:12 +00:00
2012-01-08 04:21:19 +00:00
2012-04-08 02:02:20 +00:00
insert->attachObject(part);
std::vector<Nif::NiTriShapeCopy>* shape = ((NIFLoader::getSingletonPtr())->getShapes(mesh + "0000" + suffix));
if(shape){
handleShapes(shape, part, base->getSkeleton());
2011-12-15 05:33:10 +00:00
}
2012-04-08 02:02:20 +00:00
std::pair<Ogre::Entity*, std::vector<Nif::NiTriShapeCopy>*> pair = std::make_pair(part, shape);
return pair;
}
2011-12-27 00:23:46 +00:00
void NpcAnimation::runAnimation(float timepassed){
2012-04-09 00:41:49 +00:00
2012-04-12 00:16:22 +00:00
if(timeToChange > .2){
2012-04-09 00:44:44 +00:00
2012-04-12 00:16:22 +00:00
timeToChange = 0;
updateParts();
2012-04-08 21:27:56 +00:00
}
2012-04-08 02:02:20 +00:00
2012-04-12 00:16:22 +00:00
timeToChange += timepassed;
2012-04-08 02:02:20 +00:00
2012-01-06 02:45:17 +00:00
//1. Add the amount of time passed to time
2011-12-27 00:23:46 +00:00
2012-01-06 02:45:17 +00:00
//2. Handle the animation transforms dependent on time
2011-12-27 00:23:46 +00:00
2012-01-06 02:45:17 +00:00
//3. Handle the shapes dependent on animation transforms
if(animate > 0){
2011-12-27 00:23:46 +00:00
time += timepassed;
2012-01-17 14:10:53 +00:00
if(time > stopTime){
animate--;
2012-01-17 14:10:53 +00:00
if(animate == 0)
time = stopTime;
else
time = startTime + (time - stopTime);
}
2012-04-03 02:08:46 +00:00
handleAnimationTransforms();
2012-01-05 04:48:25 +00:00
2012-04-08 02:02:20 +00:00
vecRotPos.clear();
2012-04-08 02:02:20 +00:00
2012-04-08 21:27:56 +00:00
if(lBeastFoot.first)
handleShapes(lBeastFoot.second, lBeastFoot.first, base->getSkeleton());
if(rBeastFoot.first)
handleShapes(rBeastFoot.second, rBeastFoot.first, base->getSkeleton());
if(chest.first)
handleShapes(chest.second, chest.first, base->getSkeleton());
if(tail.first)
handleShapes(tail.second, tail.first, base->getSkeleton());
if(skirt.first)
handleShapes(skirt.second, skirt.first, base->getSkeleton());
if(lhand.first)
handleShapes(lhand.second, lhand.first, base->getSkeleton());
if(rhand.first)
handleShapes(rhand.second, rhand.first, base->getSkeleton());
}
2012-01-17 14:10:53 +00:00
}
}