1
0
Fork 1
mirror of https://github.com/TES3MP/openmw-tes3mp.git synced 2025-01-16 17:19:56 +00:00
openmw-tes3mp/apps/openmw/mwrender/mwscene.cpp

236 lines
8 KiB
C++
Raw Normal View History

#include "mwscene.hpp"
#include <assert.h>
#include "OgreRoot.h"
#include "OgreRenderWindow.h"
#include "OgreSceneManager.h"
#include "OgreViewport.h"
#include "OgreCamera.h"
#include "OgreTextureManager.h"
#include "../mwworld/world.hpp" // these includes can be removed once the static-hack is gone
#include "../mwworld/ptr.hpp"
#include "../mwworld/doingphysics.hpp"
#include <components/esm/loadstat.hpp>
#include "player.hpp"
using namespace MWRender;
using namespace Ogre;
MWScene::MWScene(OEngine::Render::OgreRenderer &_rend , OEngine::Physic::PhysicEngine* physEng)
: rend(_rend)
{
eng = physEng;
rend.createScene("PlayerCam", 55, 5);
// Set default mipmap level (NB some APIs ignore this)
TextureManager::getSingleton().setDefaultNumMipmaps(5);
// Load resources
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
// Turn the entire scene (represented by the 'root' node) -90
// degrees around the x axis. This makes Z go upwards, and Y go into
// the screen (when x is to the right.) This is the orientation that
// Morrowind uses, and it automagically makes everything work as it
// should.
SceneNode *rt = rend.getScene()->getRootSceneNode();
mwRoot = rt->createChildSceneNode();
mwRoot->pitch(Degree(-90));
2010-08-03 18:17:31 +00:00
//used to obtain ingame information of ogre objects (which are faced or selected)
mRaySceneQuery = rend.getScene()->createRayQuery(Ray());
Ogre::SceneNode *playerNode = mwRoot->createChildSceneNode();
playerNode->pitch(Degree(90));
Ogre::SceneNode *cameraYawNode = playerNode->createChildSceneNode();
Ogre::SceneNode *cameraPitchNode = cameraYawNode->createChildSceneNode();
cameraPitchNode->attachObject(getCamera());
2011-02-10 11:56:19 +00:00
2011-02-22 13:11:53 +00:00
mPlayer = new MWRender::Player (getCamera(), playerNode->getName());
2011-03-21 13:08:56 +00:00
mFreeFly = true;
}
MWScene::~MWScene()
{
delete mPlayer;
}
2010-08-03 18:17:31 +00:00
std::pair<std::string, float> MWScene::getFacedHandle (MWWorld::World& world)
2010-08-03 18:17:31 +00:00
{
std::string handle = "";
2010-08-03 18:17:31 +00:00
//get a ray pointing to the center of the viewport
Ray centerRay = getCamera()->getCameraToViewportRay(
getViewport()->getWidth()/2,
getViewport()->getHeight()/2);
2011-03-17 09:54:38 +00:00
//let's avoid the capsule shape of the player.
centerRay.setOrigin(centerRay.getOrigin() + 20*centerRay.getDirection());
2011-03-19 18:54:18 +00:00
btVector3 from(centerRay.getOrigin().x,-centerRay.getOrigin().z,centerRay.getOrigin().y);
btVector3 to(centerRay.getPoint(500).x,-centerRay.getPoint(500).z,centerRay.getPoint(500).y);
return eng->rayTest(from,to);
2010-08-03 18:17:31 +00:00
}
2011-01-23 14:28:42 +00:00
void MWScene::doPhysics (float duration, MWWorld::World& world,
const std::vector<std::pair<std::string, Ogre::Vector3> >& actors)
2011-01-23 14:28:42 +00:00
{
// stop changes to world from being reported back to the physics system
MWWorld::DoingPhysics scopeGuard;
2011-01-23 14:28:42 +00:00
//set the DebugRenderingMode. To disable it,set it to 0
//eng->setDebugRenderingMode(1);
2011-02-22 13:11:53 +00:00
//set the walkdirection to 0 (no movement) for every actor)
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = eng->PhysicActorMap.begin(); it != eng->PhysicActorMap.end();it++)
{
OEngine::Physic::PhysicActor* act = it->second;
act->setWalkDirection(btVector3(0,0,0));
}
for (std::vector<std::pair<std::string, Ogre::Vector3> >::const_iterator iter (actors.begin());
iter!=actors.end(); ++iter)
{
OEngine::Physic::PhysicActor* act = eng->getCharacter(iter->first);
2011-02-19 10:36:57 +00:00
//dirty stuff to get the camera orientation. Must be changed!
2011-03-21 13:08:56 +00:00
Ogre::SceneNode *sceneNode = rend.getScene()->getSceneNode (iter->first);
Ogre::Vector3 dir;
Ogre::Node* yawNode = sceneNode->getChildIterator().getNext();
Ogre::Node* pitchNode = yawNode->getChildIterator().getNext();
if(mFreeFly)
{
Ogre::Quaternion yawQuat = yawNode->getOrientation();
Ogre::Quaternion pitchQuat = pitchNode->getOrientation();
Ogre::Vector3 dir1(iter->second.x,iter->second.z,-iter->second.y);
dir = 0.07*(yawQuat*pitchQuat*dir1);
2011-03-21 13:08:56 +00:00
}
else
{
Ogre::Quaternion quat = yawNode->getOrientation();
Ogre::Vector3 dir1(iter->second.x,iter->second.z,-iter->second.y);
dir = 0.025*(quat*dir1);
2011-03-21 13:08:56 +00:00
}
//set the walk direction
act->setWalkDirection(btVector3(dir.x,-dir.z,dir.y));
2011-02-19 10:36:57 +00:00
}
eng->stepSimulation(duration);
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = eng->PhysicActorMap.begin(); it != eng->PhysicActorMap.end();it++)
2011-02-19 10:36:57 +00:00
{
OEngine::Physic::PhysicActor* act = it->second;
btVector3 newPos = act->getPosition();
MWWorld::Ptr ptr = world.getPtrViaHandle (it->first);
world.moveObject (ptr, newPos.x(), newPos.y(), newPos.z());
}
2011-01-23 14:28:42 +00:00
}
void MWScene::addObject (const std::string& handle, const std::string& mesh,
const Ogre::Quaternion& rotation, float scale, const Ogre::Vector3& position)
{
OEngine::Physic::RigidBody* body = eng->createRigidBody(mesh,handle);
eng->addRigidBody(body);
btTransform tr;
tr.setOrigin(btVector3(position.x,position.y,position.z));
tr.setRotation(btQuaternion(rotation.x,rotation.y,rotation.z,rotation.w));
body->setWorldTransform(tr);
}
void MWScene::addActor (const std::string& handle, const std::string& mesh,
const Ogre::Vector3& position)
{
//TODO:optimize this. Searching the std::map isn't very efficient i think.
eng->addCharacter(handle);
OEngine::Physic::PhysicActor* act = eng->getCharacter(handle);
act->setPosition(btVector3(position.x,position.y,position.z));
}
void MWScene::removeObject (const std::string& handle)
{
//TODO:check if actor???
eng->removeCharacter(handle);
eng->removeRigidBody(handle);
eng->deleteRigidBody(handle);
}
void MWScene::moveObject (const std::string& handle, const Ogre::Vector3& position, bool updatePhysics)
{
2011-02-22 13:11:53 +00:00
rend.getScene()->getSceneNode(handle)->setPosition(position);
2011-03-22 19:15:19 +00:00
if(updatePhysics)//TODO: is it an actor? Done?
2011-03-22 12:48:31 +00:00
{
if (OEngine::Physic::RigidBody* body = eng->getRigidBody(handle))
{
// TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow
// start positions others than 0, 0, 0
btTransform tr = body->getWorldTransform();
tr.setOrigin(btVector3(position.x,position.y,position.z));
body->setWorldTransform(tr);
}
2011-03-22 19:15:19 +00:00
if (OEngine::Physic::PhysicActor* act = eng->getCharacter(handle))
{
// TODO very dirty hack to avoid crash during setup -> needs cleaning up to allow
// start positions others than 0, 0, 0
act->setPosition(btVector3(position.x,position.y,position.z));
}
2011-03-22 12:48:31 +00:00
}
}
void MWScene::rotateObject (const std::string& handle, const Ogre::Quaternion& rotation)
{
}
void MWScene::scaleObject (const std::string& handle, float scale)
{
}
2011-01-25 17:12:13 +00:00
bool MWScene::toggleCollisionMode()
2011-01-25 17:12:13 +00:00
{
for(std::map<std::string,OEngine::Physic::PhysicActor*>::iterator it = eng->PhysicActorMap.begin(); it != eng->PhysicActorMap.end();it++)
{
OEngine::Physic::PhysicActor* act = it->second;
bool cmode = act->getCollisionMode();
if(cmode)
{
act->enableCollisions(false);
act->setGravity(0.);
act->setVerticalVelocity(0);
2011-03-21 13:08:56 +00:00
mFreeFly = true;
return false;
}
else
{
2011-03-21 13:08:56 +00:00
mFreeFly = false;
act->enableCollisions(true);
act->setGravity(4.);
act->setVerticalVelocity(0);
return true;
}
}
return false; // This should never happen, but it shall not bother us now, since
// this part of the code needs a rewrite anyway.
2011-01-25 17:12:13 +00:00
}
bool MWScene::toggleRenderMode (int mode)
{
switch (mode)
{
case MWWorld::World::Render_CollisionDebug:
// TODO use a proper function instead of accessing the member variable
// directly.
eng->setDebugRenderingMode (!eng->isDebugCreated);
return eng->isDebugCreated;
}
return false;
}