2011-10-25 18:06:44 +00:00
|
|
|
#include "debugging.hpp"
|
2010-06-05 18:37:01 +00:00
|
|
|
|
2012-07-03 10:30:50 +00:00
|
|
|
#include <cassert>
|
2010-06-05 18:37:01 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
#include <osg/Geometry>
|
|
|
|
#include <osg/PositionAttitudeTransform>
|
|
|
|
#include <osg/Geode>
|
|
|
|
#include <osg/Group>
|
2010-06-06 11:36:45 +00:00
|
|
|
|
2013-02-07 05:47:09 +00:00
|
|
|
#include <openengine/bullet/physic.hpp>
|
|
|
|
|
2010-08-25 07:19:15 +00:00
|
|
|
#include <components/esm/loadstat.hpp>
|
2012-03-08 06:46:34 +00:00
|
|
|
#include <components/esm/loadpgrd.hpp>
|
2010-08-25 07:19:15 +00:00
|
|
|
|
2012-07-03 10:30:50 +00:00
|
|
|
#include "../mwbase/world.hpp" // these includes can be removed once the static-hack is gone
|
|
|
|
#include "../mwbase/environment.hpp"
|
|
|
|
|
|
|
|
#include "../mwworld/ptr.hpp"
|
2014-02-23 19:11:05 +00:00
|
|
|
#include "../mwworld/cellstore.hpp"
|
|
|
|
#include "../mwworld/esmstore.hpp"
|
2015-03-08 05:11:54 +00:00
|
|
|
#include "../mwmechanics/pathfinding.hpp"
|
2012-07-03 10:30:50 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
#include "vismask.hpp"
|
2010-06-05 18:37:01 +00:00
|
|
|
|
2012-03-13 23:06:56 +00:00
|
|
|
namespace MWRender
|
|
|
|
{
|
|
|
|
|
2012-04-01 13:51:37 +00:00
|
|
|
static const int POINT_MESH_BASE = 35;
|
2012-03-13 23:06:56 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::ref_ptr<osg::Geometry> Debugging::createPathgridLines(const ESM::Pathgrid *pathgrid)
|
2012-03-14 11:03:04 +00:00
|
|
|
{
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
|
2012-03-14 11:03:04 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
|
2012-03-13 23:06:56 +00:00
|
|
|
|
2012-09-17 07:37:50 +00:00
|
|
|
for(ESM::Pathgrid::EdgeList::const_iterator it = pathgrid->mEdges.begin();
|
|
|
|
it != pathgrid->mEdges.end();
|
2012-06-06 18:29:30 +00:00
|
|
|
++it)
|
2012-04-01 13:27:18 +00:00
|
|
|
{
|
|
|
|
const ESM::Pathgrid::Edge &edge = *it;
|
2012-09-17 07:37:50 +00:00
|
|
|
const ESM::Pathgrid::Point &p1 = pathgrid->mPoints[edge.mV0], &p2 = pathgrid->mPoints[edge.mV1];
|
2015-05-02 20:45:27 +00:00
|
|
|
|
|
|
|
osg::Vec3f direction = MWMechanics::PathFinder::MakeOsgVec3(p2) - MWMechanics::PathFinder::MakeOsgVec3(p1);
|
|
|
|
osg::Vec3f lineDisplacement = (direction^osg::Vec3f(0,0,1));
|
|
|
|
lineDisplacement.normalize();
|
|
|
|
|
2012-04-01 13:51:37 +00:00
|
|
|
lineDisplacement = lineDisplacement * POINT_MESH_BASE +
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::Vec3f(0, 0, 10); // move lines up a little, so they will be less covered by meshes/landscape
|
|
|
|
|
|
|
|
vertices->push_back(MWMechanics::PathFinder::MakeOsgVec3(p1) + lineDisplacement);
|
|
|
|
vertices->push_back(MWMechanics::PathFinder::MakeOsgVec3(p2) + lineDisplacement);
|
2012-04-01 13:27:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
geom->setVertexArray(vertices);
|
|
|
|
|
|
|
|
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, vertices->size()));
|
2012-09-28 15:02:18 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(1.f, 1.f, 0.f, 1.f));
|
|
|
|
geom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
|
|
|
|
|
|
|
return geom;
|
2012-03-13 23:06:56 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::ref_ptr<osg::Geometry> Debugging::createPathgridPoints(const ESM::Pathgrid *pathgrid)
|
2012-03-13 23:06:56 +00:00
|
|
|
{
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::ref_ptr<osg::Geometry> geom = new osg::Geometry;
|
|
|
|
|
2012-04-01 13:51:37 +00:00
|
|
|
const float height = POINT_MESH_BASE * sqrtf(2);
|
2012-04-01 13:27:18 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
|
|
|
|
osg::ref_ptr<osg::UShortArray> indices = new osg::UShortArray;
|
2012-04-01 13:27:18 +00:00
|
|
|
|
|
|
|
bool first = true;
|
2015-05-02 20:45:27 +00:00
|
|
|
unsigned short startIndex = 0;
|
2012-09-17 07:37:50 +00:00
|
|
|
for(ESM::Pathgrid::PointList::const_iterator it = pathgrid->mPoints.begin();
|
|
|
|
it != pathgrid->mPoints.end();
|
2014-04-27 17:03:33 +00:00
|
|
|
++it, startIndex += 6)
|
2012-04-01 13:27:18 +00:00
|
|
|
{
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::Vec3f pointPos(MWMechanics::PathFinder::MakeOsgVec3(*it));
|
2012-04-01 13:27:18 +00:00
|
|
|
|
|
|
|
if (!first)
|
|
|
|
{
|
|
|
|
// degenerate triangle from previous octahedron
|
2015-05-02 20:45:27 +00:00
|
|
|
indices->push_back(startIndex - 4); // 2nd point of previous octahedron
|
|
|
|
indices->push_back(startIndex); // start point of current octahedron
|
2012-04-01 13:27:18 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
float pointMeshBase = static_cast<float>(POINT_MESH_BASE);
|
|
|
|
|
|
|
|
vertices->push_back(pointPos + osg::Vec3f(0, 0, height)); // 0
|
|
|
|
vertices->push_back(pointPos + osg::Vec3f(-pointMeshBase, -pointMeshBase, 0)); // 1
|
|
|
|
vertices->push_back(pointPos + osg::Vec3f(pointMeshBase, -pointMeshBase, 0)); // 2
|
|
|
|
vertices->push_back(pointPos + osg::Vec3f(pointMeshBase, pointMeshBase, 0)); // 3
|
|
|
|
vertices->push_back(pointPos + osg::Vec3f(-pointMeshBase, pointMeshBase, 0)); // 4
|
|
|
|
vertices->push_back(pointPos + osg::Vec3f(0, 0, -height)); // 5
|
|
|
|
|
|
|
|
indices->push_back(startIndex + 0);
|
|
|
|
indices->push_back(startIndex + 1);
|
|
|
|
indices->push_back(startIndex + 2);
|
|
|
|
indices->push_back(startIndex + 5);
|
|
|
|
indices->push_back(startIndex + 3);
|
|
|
|
indices->push_back(startIndex + 4);
|
2012-04-01 13:27:18 +00:00
|
|
|
// degenerates
|
2015-05-02 20:45:27 +00:00
|
|
|
indices->push_back(startIndex + 4);
|
|
|
|
indices->push_back(startIndex + 5);
|
|
|
|
indices->push_back(startIndex + 5);
|
2012-04-01 13:27:18 +00:00
|
|
|
// end degenerates
|
2015-05-02 20:45:27 +00:00
|
|
|
indices->push_back(startIndex + 1);
|
|
|
|
indices->push_back(startIndex + 4);
|
|
|
|
indices->push_back(startIndex + 0);
|
|
|
|
indices->push_back(startIndex + 3);
|
|
|
|
indices->push_back(startIndex + 2);
|
2012-04-01 13:27:18 +00:00
|
|
|
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
geom->setVertexArray(vertices);
|
2012-04-01 13:27:18 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
geom->addPrimitiveSet(new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_STRIP, indices->size(), &(*indices)[0]));
|
2012-09-28 15:02:18 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array;
|
|
|
|
colors->push_back(osg::Vec4(1.f, 0.f, 0.f, 1.f));
|
|
|
|
geom->setColorArray(colors, osg::Array::BIND_OVERALL);
|
|
|
|
|
|
|
|
return geom;
|
2012-03-13 23:06:56 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
Debugging::Debugging(osg::ref_ptr<osg::Group> root /*, OEngine::Physic::PhysicEngine *engine*/)
|
|
|
|
: mRootNode(root)
|
|
|
|
, mPathgridEnabled(false)
|
|
|
|
, mInteriorPathgridNode(NULL)
|
|
|
|
, mPathGridRoot(NULL)
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
2012-03-13 23:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Debugging::~Debugging()
|
|
|
|
{
|
2012-03-14 11:03:04 +00:00
|
|
|
if (mPathgridEnabled)
|
|
|
|
{
|
|
|
|
togglePathgrid();
|
|
|
|
}
|
2011-10-22 04:15:15 +00:00
|
|
|
}
|
|
|
|
|
2011-10-24 17:42:36 +00:00
|
|
|
|
2011-10-22 04:15:15 +00:00
|
|
|
bool Debugging::toggleRenderMode (int mode){
|
2012-03-08 06:46:34 +00:00
|
|
|
switch (mode)
|
2011-10-22 04:15:15 +00:00
|
|
|
{
|
2015-05-02 20:45:27 +00:00
|
|
|
case Render_Pathgrid:
|
2012-03-08 06:46:34 +00:00
|
|
|
togglePathgrid();
|
2012-03-14 11:03:04 +00:00
|
|
|
return mPathgridEnabled;
|
2015-05-02 20:45:27 +00:00
|
|
|
default:
|
|
|
|
return false;
|
2011-10-22 04:15:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2012-03-08 06:46:34 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
void Debugging::addCell(const MWWorld::CellStore *store)
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
|
|
|
mActiveCells.push_back(store);
|
2012-03-14 11:03:04 +00:00
|
|
|
if (mPathgridEnabled)
|
2012-03-13 23:06:56 +00:00
|
|
|
enableCellPathgrid(store);
|
2012-03-08 06:46:34 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
void Debugging::removeCell(const MWWorld::CellStore *store)
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
|
|
|
mActiveCells.erase(std::remove(mActiveCells.begin(), mActiveCells.end(), store), mActiveCells.end());
|
2012-03-14 11:03:04 +00:00
|
|
|
if (mPathgridEnabled)
|
2012-03-13 23:06:56 +00:00
|
|
|
disableCellPathgrid(store);
|
2012-03-08 06:46:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Debugging::togglePathgrid()
|
|
|
|
{
|
2012-03-14 11:03:04 +00:00
|
|
|
mPathgridEnabled = !mPathgridEnabled;
|
|
|
|
if (mPathgridEnabled)
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
|
|
|
// add path grid meshes to already loaded cells
|
2015-05-02 20:45:27 +00:00
|
|
|
mPathGridRoot = new osg::Group;
|
|
|
|
mPathGridRoot->setNodeMask(Mask_Debug);
|
|
|
|
mRootNode->addChild(mPathGridRoot);
|
|
|
|
|
2012-06-06 18:29:30 +00:00
|
|
|
for(CellList::iterator it = mActiveCells.begin(); it != mActiveCells.end(); ++it)
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
2012-03-13 23:06:56 +00:00
|
|
|
enableCellPathgrid(*it);
|
2012-03-08 06:46:34 +00:00
|
|
|
}
|
|
|
|
}
|
2012-03-14 11:03:04 +00:00
|
|
|
else
|
|
|
|
{
|
2012-03-08 06:46:34 +00:00
|
|
|
// remove path grid meshes from already loaded cells
|
2012-06-06 18:29:30 +00:00
|
|
|
for(CellList::iterator it = mActiveCells.begin(); it != mActiveCells.end(); ++it)
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
2012-03-13 23:06:56 +00:00
|
|
|
disableCellPathgrid(*it);
|
2012-03-08 06:46:34 +00:00
|
|
|
}
|
2015-05-02 20:45:27 +00:00
|
|
|
|
|
|
|
if (mPathGridRoot)
|
|
|
|
{
|
|
|
|
mRootNode->removeChild(mPathGridRoot);
|
|
|
|
mPathGridRoot = NULL;
|
|
|
|
}
|
2012-03-08 06:46:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
void Debugging::enableCellPathgrid(const MWWorld::CellStore *store)
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
2014-12-08 23:13:56 +00:00
|
|
|
MWBase::World* world = MWBase::Environment::get().getWorld();
|
2012-11-06 07:53:00 +00:00
|
|
|
const ESM::Pathgrid *pathgrid =
|
2014-12-11 14:19:48 +00:00
|
|
|
world->getStore().get<ESM::Pathgrid>().search(*store->getCell());
|
2012-03-14 11:03:04 +00:00
|
|
|
if (!pathgrid) return;
|
2012-03-08 06:46:34 +00:00
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
osg::Vec3f cellPathGridPos(0, 0, 0);
|
2014-02-21 10:35:46 +00:00
|
|
|
if (store->getCell()->isExterior())
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
2015-05-02 20:45:27 +00:00
|
|
|
cellPathGridPos.x() = static_cast<float>(store->getCell()->mData.mX * ESM::Land::REAL_SIZE);
|
|
|
|
cellPathGridPos.y() = static_cast<float>(store->getCell()->mData.mY * ESM::Land::REAL_SIZE);
|
2012-03-13 23:06:56 +00:00
|
|
|
}
|
2015-05-02 20:45:27 +00:00
|
|
|
|
|
|
|
osg::ref_ptr<osg::PositionAttitudeTransform> cellPathGrid = new osg::PositionAttitudeTransform;
|
|
|
|
cellPathGrid->setPosition(cellPathGridPos);
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Geode> lineGeode = new osg::Geode;
|
|
|
|
osg::ref_ptr<osg::Geometry> lines = createPathgridLines(pathgrid);
|
|
|
|
lineGeode->addDrawable(lines);
|
|
|
|
|
|
|
|
osg::ref_ptr<osg::Geode> pointGeode = new osg::Geode;
|
|
|
|
osg::ref_ptr<osg::Geometry> points = createPathgridPoints(pathgrid);
|
|
|
|
pointGeode->addDrawable(points);
|
|
|
|
|
|
|
|
cellPathGrid->addChild(lineGeode);
|
|
|
|
cellPathGrid->addChild(pointGeode);
|
|
|
|
|
|
|
|
mPathGridRoot->addChild(cellPathGrid);
|
2012-03-13 23:06:56 +00:00
|
|
|
|
2014-02-21 10:35:46 +00:00
|
|
|
if (store->getCell()->isExterior())
|
2012-03-13 23:06:56 +00:00
|
|
|
{
|
2014-02-21 10:35:46 +00:00
|
|
|
mExteriorPathgridNodes[std::make_pair(store->getCell()->getGridX(), store->getCell()->getGridY())] = cellPathGrid;
|
2012-03-08 06:46:34 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-13 23:06:56 +00:00
|
|
|
assert(mInteriorPathgridNode == NULL);
|
|
|
|
mInteriorPathgridNode = cellPathGrid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-02 20:45:27 +00:00
|
|
|
void Debugging::disableCellPathgrid(const MWWorld::CellStore *store)
|
2012-03-13 23:06:56 +00:00
|
|
|
{
|
2014-02-21 10:35:46 +00:00
|
|
|
if (store->getCell()->isExterior())
|
2012-03-13 23:06:56 +00:00
|
|
|
{
|
|
|
|
ExteriorPathgridNodes::iterator it =
|
2014-02-21 10:35:46 +00:00
|
|
|
mExteriorPathgridNodes.find(std::make_pair(store->getCell()->getGridX(), store->getCell()->getGridY()));
|
2012-03-13 23:06:56 +00:00
|
|
|
if (it != mExteriorPathgridNodes.end())
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
2015-05-02 20:45:27 +00:00
|
|
|
mPathGridRoot->removeChild(it->second);
|
2012-03-13 23:06:56 +00:00
|
|
|
mExteriorPathgridNodes.erase(it);
|
2012-03-08 06:46:34 +00:00
|
|
|
}
|
2012-03-13 23:06:56 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mInteriorPathgridNode)
|
2012-03-08 06:46:34 +00:00
|
|
|
{
|
2015-05-02 20:45:27 +00:00
|
|
|
mPathGridRoot->removeChild(mInteriorPathgridNode);
|
2012-03-13 23:06:56 +00:00
|
|
|
mInteriorPathgridNode = NULL;
|
2012-03-08 06:46:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-11 22:12:56 +00:00
|
|
|
|
2012-03-13 23:06:56 +00:00
|
|
|
}
|