1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
#include "cellwater.hpp"
#include <memory>
#include <string_view>
#include <osg/Geometry>
#include <osg/Group>
#include <osg/PositionAttitudeTransform>
#include <osg/StateAttribute>
#include <osg/StateSet>
#include <osg/Texture2D>
#include <osg/Texture>
#include <osg/Vec3d>
#include <osg/Vec3f>
#include <apps/opencs/model/world/collection.hpp>
#include <apps/opencs/model/world/idcollection.hpp>
#include <apps/opencs/model/world/record.hpp>
#include <apps/opencs/model/world/universalid.hpp>
#include <components/esm3/loadland.hpp>
#include <components/fallback/fallback.hpp>
#include <components/misc/strings/lower.hpp>
#include <components/resource/imagemanager.hpp>
#include <components/resource/resourcesystem.hpp>
#include <components/sceneutil/waterutil.hpp>
#include "../../model/world/cell.hpp"
#include "../../model/world/cellcoordinates.hpp"
#include "../../model/world/data.hpp"
#include "mask.hpp"
namespace CSVRender
{
const int CellWater::CellSize = ESM::Land::REAL_SIZE;
CellWater::CellWater(
CSMWorld::Data& data, osg::Group* cellNode, const std::string& id, const CSMWorld::CellCoordinates& cellCoords)
: mData(data)
, mId(id)
, mParentNode(cellNode)
, mWaterTransform(nullptr)
, mWaterGroup(nullptr)
, mWaterGeometry(nullptr)
, mDeleted(false)
, mExterior(false)
, mHasWater(false)
{
mWaterTransform = new osg::PositionAttitudeTransform();
mWaterTransform->setPosition(osg::Vec3f(
cellCoords.getX() * CellSize + CellSize / 2.f, cellCoords.getY() * CellSize + CellSize / 2.f, 0));
mWaterTransform->setNodeMask(Mask_Water);
mParentNode->addChild(mWaterTransform);
mWaterGroup = new osg::Group();
mWaterTransform->addChild(mWaterGroup);
const int cellIndex = mData.getCells().searchId(ESM::RefId::stringRefId(mId));
if (cellIndex > -1)
{
updateCellData(mData.getCells().getRecord(cellIndex));
}
// Keep water existence/height up to date
QAbstractItemModel* cells = mData.getTableModel(CSMWorld::UniversalId::Type_Cells);
connect(cells, &QAbstractItemModel::dataChanged, this, &CellWater::cellDataChanged);
}
CellWater::~CellWater()
{
mParentNode->removeChild(mWaterTransform);
}
void CellWater::updateCellData(const CSMWorld::Record<CSMWorld::Cell>& cellRecord)
{
mDeleted = cellRecord.isDeleted();
if (!mDeleted)
{
const CSMWorld::Cell& cell = cellRecord.get();
if (mExterior != cell.isExterior() || mHasWater != cell.hasWater())
{
mExterior = cellRecord.get().isExterior();
mHasWater = cellRecord.get().hasWater();
recreate();
}
float waterHeight = -1;
if (!mExterior)
{
waterHeight = cellRecord.get().mWater;
}
osg::Vec3d pos = mWaterTransform->getPosition();
pos.z() = waterHeight;
mWaterTransform->setPosition(pos);
}
else
{
recreate();
}
}
void CellWater::reloadAssets()
{
recreate();
}
void CellWater::cellDataChanged(const QModelIndex& topLeft, const QModelIndex& bottomRight)
{
const CSMWorld::Collection<CSMWorld::Cell>& cells = mData.getCells();
int rowStart = -1;
int rowEnd = -1;
if (topLeft.parent().isValid())
{
rowStart = topLeft.parent().row();
rowEnd = bottomRight.parent().row();
}
else
{
rowStart = topLeft.row();
rowEnd = bottomRight.row();
}
for (int row = rowStart; row <= rowEnd; ++row)
{
const CSMWorld::Record<CSMWorld::Cell>& cellRecord = cells.getRecord(row);
if (cellRecord.get().mId == ESM::RefId::stringRefId(mId))
updateCellData(cellRecord);
}
}
void CellWater::recreate()
{
const int interiorScalar = 20;
const int segmentsPerCell = 1;
const int textureRepeatsPerCell = 6;
const float alpha = 0.5f;
const int renderBin = osg::StateSet::TRANSPARENT_BIN - 1;
if (mWaterGeometry)
{
mWaterGroup->removeChild(mWaterGeometry);
mWaterGeometry = nullptr;
}
if (mDeleted || !mHasWater)
return;
float size;
int segments;
float textureRepeats;
if (mExterior)
{
size = CellSize;
segments = segmentsPerCell;
textureRepeats = textureRepeatsPerCell;
}
else
{
size = CellSize * interiorScalar;
segments = segmentsPerCell * interiorScalar;
textureRepeats = textureRepeatsPerCell * interiorScalar;
}
mWaterGeometry = SceneUtil::createWaterGeometry(size, segments, textureRepeats);
mWaterGeometry->setStateSet(SceneUtil::createSimpleWaterStateSet(alpha, renderBin));
// Add water texture
constexpr VFS::Path::NormalizedView prefix("textures/water");
VFS::Path::Normalized texturePath(prefix);
texturePath /= std::string(Fallback::Map::getString("Water_SurfaceTexture")) + "00.dds";
Resource::ImageManager* imageManager = mData.getResourceSystem()->getImageManager();
osg::ref_ptr<osg::Texture2D> waterTexture = new osg::Texture2D();
waterTexture->setImage(imageManager->getImage(texturePath));
waterTexture->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
waterTexture->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);
mWaterGeometry->getStateSet()->setTextureAttributeAndModes(0, waterTexture, osg::StateAttribute::ON);
mWaterGroup->addChild(mWaterGeometry);
}
}
|