Use shared water geometry + state generation,

Fix water position and render bin,
Fix passed cell parameter being ignored
pull/34/head
Aesylwinn 9 years ago
parent 5753f52b47
commit 9454f4f2e7

@ -75,7 +75,7 @@ CSVRender::CellMarker::CellMarker(
mMarkerNode->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN); mMarkerNode->setAutoRotateMode(osg::AutoTransform::ROTATE_TO_SCREEN);
mMarkerNode->setAutoScaleToScreen(true); mMarkerNode->setAutoScaleToScreen(true);
mMarkerNode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); mMarkerNode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
mMarkerNode->getOrCreateStateSet()->setRenderBinDetails(2000, "RenderBin"); mMarkerNode->getOrCreateStateSet()->setRenderBinDetails(osg::StateSet::TRANSPARENT_BIN + 1, "RenderBin");
mMarkerNode->setUserData(new CellMarkerTag(this)); mMarkerNode->setUserData(new CellMarkerTag(this));
mMarkerNode->setNodeMask(Mask_CellMarker); mMarkerNode->setNodeMask(Mask_CellMarker);

@ -3,11 +3,11 @@
#include <osg/Geode> #include <osg/Geode>
#include <osg/Geometry> #include <osg/Geometry>
#include <osg/Group> #include <osg/Group>
#include <osg/PolygonOffset>
#include <osg/PositionAttitudeTransform> #include <osg/PositionAttitudeTransform>
#include <components/esm/loadland.hpp> #include <components/esm/loadland.hpp>
#include <components/misc/stringops.hpp> #include <components/misc/stringops.hpp>
#include <components/sceneutil/waterutil.hpp>
#include "../../model/world/cell.hpp" #include "../../model/world/cell.hpp"
#include "../../model/world/cellcoordinates.hpp" #include "../../model/world/cellcoordinates.hpp"
@ -27,12 +27,14 @@ namespace CSVRender
, mWaterTransform(0) , mWaterTransform(0)
, mWaterNode(0) , mWaterNode(0)
, mWaterGeometry(0) , mWaterGeometry(0)
, mDeleted(false)
, mExterior(false) , mExterior(false)
, mHasWater(false) , mHasWater(false)
, mWaterHeight(0)
{ {
mWaterTransform = new osg::PositionAttitudeTransform(); mWaterTransform = new osg::PositionAttitudeTransform();
mWaterTransform->setPosition(osg::Vec3f(cellCoords.getX() * CellSize, cellCoords.getY() * CellSize, 0)); mWaterTransform->setPosition(osg::Vec3f(cellCoords.getX() * CellSize + CellSize / 2.f,
cellCoords.getY() * CellSize + CellSize / 2.f, 0));
mWaterTransform->setNodeMask(Mask_Water); mWaterTransform->setNodeMask(Mask_Water);
mParentNode->addChild(mWaterTransform); mParentNode->addChild(mWaterTransform);
@ -42,7 +44,7 @@ namespace CSVRender
int cellIndex = mData.getCells().searchId(mId); int cellIndex = mData.getCells().searchId(mId);
if (cellIndex > -1) if (cellIndex > -1)
{ {
updateCellData(mData.getCells().getRecord(cellIndex).get()); updateCellData(mData.getCells().getRecord(cellIndex));
} }
// Keep water existance/height up to date // Keep water existance/height up to date
@ -56,28 +58,35 @@ namespace CSVRender
mParentNode->removeChild(mWaterTransform); mParentNode->removeChild(mWaterTransform);
} }
void CellWater::updateCellData(const CSMWorld::Cell& cell) void CellWater::updateCellData(const CSMWorld::Record<CSMWorld::Cell>& cellRecord)
{ {
int cellIndex = mData.getCells().searchId(mId); mDeleted = cellRecord.isDeleted();
if (cellIndex > -1) if (!mDeleted)
{ {
const CSMWorld::Record<CSMWorld::Cell>& cellRecord = mData.getCells().getRecord(cellIndex); const CSMWorld::Cell& cell = cellRecord.get();
mDeleted = cellRecord.isDeleted(); if (mExterior != cell.isExterior() || mHasWater != cell.hasWater())
if (!mDeleted)
{ {
mExterior = cellRecord.get().isExterior(); mExterior = cellRecord.get().isExterior();
mHasWater = cellRecord.get().hasWater(); mHasWater = cellRecord.get().hasWater();
mWaterHeight = cellRecord.get().mWater;
recreate();
}
float waterHeight = -1;
if (!mExterior)
{
waterHeight = cellRecord.get().mWater;
} }
osg::Vec3d pos = mWaterTransform->getPosition();
pos.z() = waterHeight;
mWaterTransform->setPosition(pos);
} }
else else
{ {
mDeleted = true; recreate();
} }
update();
} }
void CellWater::cellDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight) void CellWater::cellDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
@ -100,42 +109,22 @@ namespace CSVRender
for (int row = rowStart; row <= rowEnd; ++row) for (int row = rowStart; row <= rowEnd; ++row)
{ {
const CSMWorld::Cell& cell = cells.getRecord(row).get(); const CSMWorld::Record<CSMWorld::Cell>& cellRecord = cells.getRecord(row);
if (Misc::StringUtils::lowerCase(cell.mId) == mId) if (Misc::StringUtils::lowerCase(cellRecord.get().mId) == mId)
updateCellData(cell); updateCellData(cellRecord);
} }
} }
void CellWater::update() void CellWater::recreate()
{ {
const int InteriorSize = CellSize * 10; const int InteriorScalar = 20;
const int SegmentsPerCell = 1;
const int TextureRepeatsPerCell = 6;
const size_t NumPoints = 4; const float Alpha = 0.5f;
const size_t NumIndices = 6;
const osg::Vec3f ExteriorPoints[] = const int RenderBin = osg::StateSet::TRANSPARENT_BIN - 1;
{
osg::Vec3f(0, 0, mWaterHeight),
osg::Vec3f(0, CellSize, mWaterHeight),
osg::Vec3f(CellSize, CellSize, mWaterHeight),
osg::Vec3f(CellSize, 0, mWaterHeight)
};
const osg::Vec3f InteriorPoints[] =
{
osg::Vec3f(-InteriorSize, -InteriorSize, mWaterHeight),
osg::Vec3f(-InteriorSize, InteriorSize, mWaterHeight),
osg::Vec3f( InteriorSize, InteriorSize, mWaterHeight),
osg::Vec3f( InteriorSize, -InteriorSize, mWaterHeight)
};
const unsigned short TriangleStrip[] =
{
0, 1, 2, 3, 0, 1
};
const osg::Vec4f Color = osg::Vec4f(0.6f, 0.7f, 1.f, 0.5f);
if (mWaterGeometry) if (mWaterGeometry)
{ {
@ -146,45 +135,25 @@ namespace CSVRender
if (mDeleted || !mHasWater) if (mDeleted || !mHasWater)
return; return;
mWaterGeometry = new osg::Geometry(); float size;
int segments;
osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array(); float textureRepeats;
osg::ref_ptr<osg::Vec4Array> colors = new osg::Vec4Array();
osg::ref_ptr<osg::DrawElementsUShort> indices = new osg::DrawElementsUShort(osg::PrimitiveSet::TRIANGLE_STRIP,
NumIndices);
for (size_t i = 0; i < NumPoints; ++i) if (mExterior)
{ {
if (mExterior) size = CellSize;
vertices->push_back(ExteriorPoints[i]); segments = SegmentsPerCell;
else textureRepeats = TextureRepeatsPerCell;
vertices->push_back(InteriorPoints[i]);
} }
else
colors->push_back(Color);
for (size_t i = 0; i < NumIndices; ++i)
{ {
indices->setElement(i, TriangleStrip[i]); size = CellSize * InteriorScalar;
segments = SegmentsPerCell * InteriorScalar;
textureRepeats = TextureRepeatsPerCell * InteriorScalar;
} }
mWaterGeometry->setVertexArray(vertices); mWaterGeometry = SceneUtil::createWaterGeometry(size, segments, textureRepeats);
mWaterGeometry->setColorArray(colors, osg::Array::BIND_OVERALL); mWaterGeometry->setStateSet(SceneUtil::createSimpleWaterStateSet(Alpha, RenderBin));
mWaterGeometry->addPrimitiveSet(indices);
// Transparency
mWaterGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
mWaterGeometry->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON );
mWaterGeometry->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN);
mWaterGeometry->getOrCreateStateSet()->setRenderBinDetails(1000, "RenderBin");
// Reduce some z-fighting
osg::ref_ptr<osg::PolygonOffset> polygonOffset = new osg::PolygonOffset();
polygonOffset->setFactor(0.2f);
polygonOffset->setUnits(0.2f);
mWaterGeometry->getOrCreateStateSet()->setAttributeAndModes(polygonOffset,
osg::StateAttribute::OVERRIDE | osg::StateAttribute::ON);
mWaterNode->addDrawable(mWaterGeometry); mWaterNode->addDrawable(mWaterGeometry);
} }

@ -8,6 +8,8 @@
#include <QObject> #include <QObject>
#include <QModelIndex> #include <QModelIndex>
#include "../../model/world/record.hpp"
namespace osg namespace osg
{ {
class Geode; class Geode;
@ -38,7 +40,7 @@ namespace CSVRender
~CellWater(); ~CellWater();
void updateCellData(const CSMWorld::Cell& cell); void updateCellData(const CSMWorld::Record<CSMWorld::Cell>& cellRecord);
private slots: private slots:
@ -46,7 +48,7 @@ namespace CSVRender
private: private:
void update(); void recreate();
static const int CellSize; static const int CellSize;
@ -61,9 +63,7 @@ namespace CSVRender
bool mDeleted; bool mDeleted;
bool mExterior; bool mExterior;
bool mHasWater; bool mHasWater;
float mWaterHeight;
}; };
} }

Loading…
Cancel
Save