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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
#include "brushdraw.hpp"
#include <limits>
#include <set>
#include <utility>
#include <osg/Array>
#include <osg/GL>
#include <osg/Geometry>
#include <osg/Group>
#include <osg/Math>
#include <osg/PrimitiveSet>
#include <osg/StateAttribute>
#include <osg/StateSet>
#include <osg/Vec4f>
#include <osgUtil/IntersectionVisitor>
#include <osgUtil/LineSegmentIntersector>
#include <components/esm3/loadland.hpp>
#include "../../model/world/cellcoordinates.hpp"
#include "../widget/brushshapes.hpp"
#include "mask.hpp"
CSVRender::BrushDraw::BrushDraw(osg::ref_ptr<osg::Group> parentNode, bool textureMode)
: mParentNode(std::move(parentNode))
, mTextureMode(textureMode)
{
mBrushDrawNode = new osg::Group();
mGeometry = new osg::Geometry();
mBrushDrawNode->addChild(mGeometry);
mBrushDrawNode->getOrCreateStateSet()->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
mBrushDrawNode->getOrCreateStateSet()->setRenderBinDetails(11, "RenderBin");
mParentNode->addChild(mBrushDrawNode);
if (mTextureMode)
mLandSizeFactor = static_cast<float>(ESM::Land::REAL_SIZE) / static_cast<float>(ESM::Land::LAND_TEXTURE_SIZE);
else
mLandSizeFactor = static_cast<float>(ESM::Land::REAL_SIZE) / static_cast<float>(ESM::Land::LAND_SIZE - 1);
}
CSVRender::BrushDraw::~BrushDraw()
{
mBrushDrawNode->removeChild(mGeometry);
mParentNode->removeChild(mBrushDrawNode);
}
float CSVRender::BrushDraw::getIntersectionHeight(const osg::Vec3d& point)
{
osg::Vec3d start = point;
osg::Vec3d end = point;
start.z() = std::numeric_limits<int>::max();
end.z() = std::numeric_limits<int>::lowest();
osg::Vec3d direction = end - start;
// Get intersection
osg::ref_ptr<osgUtil::LineSegmentIntersector> intersector(
new osgUtil::LineSegmentIntersector(osgUtil::Intersector::MODEL, start, end));
intersector->setIntersectionLimit(osgUtil::LineSegmentIntersector::NO_LIMIT);
osgUtil::IntersectionVisitor visitor(intersector);
visitor.setTraversalMask(Mask_Terrain);
mParentNode->accept(visitor);
for (osgUtil::LineSegmentIntersector::Intersections::iterator it = intersector->getIntersections().begin();
it != intersector->getIntersections().end(); ++it)
{
osgUtil::LineSegmentIntersector::Intersection intersection = *it;
// reject back-facing polygons
if (direction * intersection.getWorldIntersectNormal() > 0)
{
continue;
}
return intersection.getWorldIntersectPoint().z();
}
return 0.0f;
}
void CSVRender::BrushDraw::buildPointGeometry(const osg::Vec3d& point)
{
osg::ref_ptr<osg::Geometry> geom(new osg::Geometry());
osg::ref_ptr<osg::Vec3Array> vertices(new osg::Vec3Array());
osg::ref_ptr<osg::Vec4Array> colors(new osg::Vec4Array());
const float brushOutlineHeight(1.0f);
const float crossHeadSize(8.0f);
osg::Vec4f lineColor(1.0f, 1.0f, 1.0f, 0.6f);
vertices->push_back(osg::Vec3d(point.x() - crossHeadSize, point.y() - crossHeadSize,
getIntersectionHeight(osg::Vec3d(point.x() - crossHeadSize, point.y() - crossHeadSize, point.z()))
+ brushOutlineHeight));
colors->push_back(lineColor);
vertices->push_back(osg::Vec3d(point.x() + crossHeadSize, point.y() + crossHeadSize,
getIntersectionHeight(osg::Vec3d(point.x() + crossHeadSize, point.y() + crossHeadSize, point.z()))
+ brushOutlineHeight));
colors->push_back(lineColor);
vertices->push_back(osg::Vec3d(point.x() + crossHeadSize, point.y() - crossHeadSize,
getIntersectionHeight(osg::Vec3d(point.x() + crossHeadSize, point.y() - crossHeadSize, point.z()))
+ brushOutlineHeight));
colors->push_back(lineColor);
vertices->push_back(osg::Vec3d(point.x() - crossHeadSize, point.y() + crossHeadSize,
getIntersectionHeight(osg::Vec3d(point.x() - crossHeadSize, point.y() + crossHeadSize, point.z()))
+ brushOutlineHeight));
colors->push_back(lineColor);
geom->setVertexArray(vertices);
geom->setColorArray(colors, osg::Array::BIND_PER_VERTEX);
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, 4));
mGeometry = geom;
}
void CSVRender::BrushDraw::buildSquareGeometry(const float& radius, const osg::Vec3d& point)
{
osg::ref_ptr<osg::Geometry> geom(new osg::Geometry());
osg::ref_ptr<osg::Vec3Array> vertices(new osg::Vec3Array());
osg::ref_ptr<osg::Vec4Array> colors(new osg::Vec4Array());
const float brushOutlineHeight(1.0f);
float diameter = radius * 2;
int resolution = static_cast<int>(2.f * diameter / mLandSizeFactor); // half a vertex resolution
float resAdjustedLandSizeFactor = mLandSizeFactor / 2; // 128
if (resolution > 128) // limit accuracy for performance
{
resolution = 128;
resAdjustedLandSizeFactor = diameter / resolution;
}
osg::Vec4f lineColor(1.0f, 1.0f, 1.0f, 0.6f);
for (int i = 0; i < resolution; i++)
{
int step = i * resAdjustedLandSizeFactor;
int step2 = (i + 1) * resAdjustedLandSizeFactor;
osg::Vec3d upHorizontalLinePoint1(point.x() - radius + step, point.y() - radius,
getIntersectionHeight(osg::Vec3d(point.x() - radius + step, point.y() - radius, point.z()))
+ brushOutlineHeight);
osg::Vec3d upHorizontalLinePoint2(point.x() - radius + step2, point.y() - radius,
getIntersectionHeight(osg::Vec3d(point.x() - radius + step2, point.y() - radius, point.z()))
+ brushOutlineHeight);
osg::Vec3d upVerticalLinePoint1(point.x() - radius, point.y() - radius + step,
getIntersectionHeight(osg::Vec3d(point.x() - radius, point.y() - radius + step, point.z()))
+ brushOutlineHeight);
osg::Vec3d upVerticalLinePoint2(point.x() - radius, point.y() - radius + step2,
getIntersectionHeight(osg::Vec3d(point.x() - radius, point.y() - radius + step2, point.z()))
+ brushOutlineHeight);
osg::Vec3d downHorizontalLinePoint1(point.x() + radius - step, point.y() + radius,
getIntersectionHeight(osg::Vec3d(point.x() + radius - step, point.y() + radius, point.z()))
+ brushOutlineHeight);
osg::Vec3d downHorizontalLinePoint2(point.x() + radius - step2, point.y() + radius,
getIntersectionHeight(osg::Vec3d(point.x() + radius - step2, point.y() + radius, point.z()))
+ brushOutlineHeight);
osg::Vec3d downVerticalLinePoint1(point.x() + radius, point.y() + radius - step,
getIntersectionHeight(osg::Vec3d(point.x() + radius, point.y() + radius - step, point.z()))
+ brushOutlineHeight);
osg::Vec3d downVerticalLinePoint2(point.x() + radius, point.y() + radius - step2,
getIntersectionHeight(osg::Vec3d(point.x() + radius, point.y() + radius - step2, point.z()))
+ brushOutlineHeight);
vertices->push_back(upHorizontalLinePoint1);
colors->push_back(lineColor);
vertices->push_back(upHorizontalLinePoint2);
colors->push_back(lineColor);
vertices->push_back(upVerticalLinePoint1);
colors->push_back(lineColor);
vertices->push_back(upVerticalLinePoint2);
colors->push_back(lineColor);
vertices->push_back(downHorizontalLinePoint1);
colors->push_back(lineColor);
vertices->push_back(downHorizontalLinePoint2);
colors->push_back(lineColor);
vertices->push_back(downVerticalLinePoint1);
colors->push_back(lineColor);
vertices->push_back(downVerticalLinePoint2);
colors->push_back(lineColor);
}
geom->setVertexArray(vertices);
geom->setColorArray(colors, osg::Array::BIND_PER_VERTEX);
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINES, 0, resolution * 8));
mGeometry = geom;
}
void CSVRender::BrushDraw::buildCircleGeometry(const float& radius, const osg::Vec3d& point)
{
osg::ref_ptr<osg::Geometry> geom(new osg::Geometry());
osg::ref_ptr<osg::Vec3Array> vertices(new osg::Vec3Array());
osg::ref_ptr<osg::Vec4Array> colors(new osg::Vec4Array());
const int amountOfPoints = 128;
const float step((osg::PI * 2.0f) / static_cast<float>(amountOfPoints));
const float brushOutlineHeight(1.0f);
osg::Vec4f lineColor(1.0f, 1.0f, 1.0f, 0.6f);
for (int i = 0; i < amountOfPoints + 2; i++)
{
float angle(i * step);
vertices->push_back(osg::Vec3d(point.x() + radius * cosf(angle), point.y() + radius * sinf(angle),
getIntersectionHeight(
osg::Vec3d(point.x() + radius * cosf(angle), point.y() + radius * sinf(angle), point.z()))
+ brushOutlineHeight));
colors->push_back(lineColor);
angle = static_cast<float>(i + 1) * step;
vertices->push_back(osg::Vec3d(point.x() + radius * cosf(angle), point.y() + radius * sinf(angle),
getIntersectionHeight(
osg::Vec3d(point.x() + radius * cosf(angle), point.y() + radius * sinf(angle), point.z()))
+ brushOutlineHeight));
colors->push_back(lineColor);
}
geom->setVertexArray(vertices);
geom->setColorArray(colors, osg::Array::BIND_PER_VERTEX);
geom->addPrimitiveSet(new osg::DrawArrays(osg::PrimitiveSet::LINE_STRIP, 0, amountOfPoints * 2));
mGeometry = geom;
}
void CSVRender::BrushDraw::buildCustomGeometry(const float& radius, const osg::Vec3d& point)
{
// Not implemented
}
void CSVRender::BrushDraw::update(osg::Vec3d point, int brushSize, CSVWidget::BrushShape toolShape)
{
if (mBrushDrawNode->containsNode(mGeometry))
mBrushDrawNode->removeChild(mGeometry);
float radius = (mLandSizeFactor * brushSize) / 2;
osg::Vec3d snapToGridPoint = point;
if (mTextureMode)
{
std::pair<int, int> snapToGridXY = CSMWorld::CellCoordinates::toTextureCoords(point);
float offsetToMiddle = mLandSizeFactor * 0.5f;
snapToGridPoint = osg::Vec3d(
CSMWorld::CellCoordinates::textureGlobalXToWorldCoords(snapToGridXY.first) + offsetToMiddle,
CSMWorld::CellCoordinates::textureGlobalYToWorldCoords(snapToGridXY.second) + offsetToMiddle, point.z());
}
else
{
std::pair<int, int> snapToGridXY = CSMWorld::CellCoordinates::toVertexCoords(point);
snapToGridPoint = osg::Vec3d(CSMWorld::CellCoordinates::vertexGlobalToWorldCoords(snapToGridXY.first),
CSMWorld::CellCoordinates::vertexGlobalToWorldCoords(snapToGridXY.second), point.z());
}
switch (toolShape)
{
case (CSVWidget::BrushShape_Point):
buildPointGeometry(snapToGridPoint);
break;
case (CSVWidget::BrushShape_Square):
buildSquareGeometry(radius, snapToGridPoint);
break;
case (CSVWidget::BrushShape_Circle):
buildCircleGeometry(radius, snapToGridPoint);
break;
case (CSVWidget::BrushShape_Custom):
buildSquareGeometry(1, snapToGridPoint);
// buildCustomGeometry
break;
}
mGeometry->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
mBrushDrawNode->addChild(mGeometry);
}
void CSVRender::BrushDraw::hide()
{
if (mBrushDrawNode->containsNode(mGeometry))
mBrushDrawNode->removeChild(mGeometry);
}
|