1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-24 09:53:53 +00:00

Use chunk-relative position

Also apply .clang-format
This commit is contained in:
AnyOldName3 2023-04-24 22:39:18 +01:00
parent f721be90aa
commit f2fa607f41

View file

@ -34,17 +34,21 @@ namespace MWRender
+ (value_type)coord[2] * (value_type)matrix(2, 2) + matrix(3, 2)); + (value_type)coord[2] * (value_type)matrix(2, 2) + matrix(3, 2));
} }
inline osg::Matrix computeInstanceMatrix(const Groundcover::GroundcoverEntry& entry) inline osg::Matrix computeInstanceMatrix(
const Groundcover::GroundcoverEntry& entry, const osg::Vec3& chunkPosition)
{ {
return osg::Matrix::scale(entry.mScale, entry.mScale, entry.mScale) * osg::Matrix(Misc::Convert::makeOsgQuat(entry.mPos)) return osg::Matrix::scale(entry.mScale, entry.mScale, entry.mScale)
* osg::Matrix::translate(entry.mPos.asVec3()); * osg::Matrix(Misc::Convert::makeOsgQuat(entry.mPos))
* osg::Matrix::translate(entry.mPos.asVec3() - chunkPosition);
} }
class InstancedComputeNearFarCullCallback : public osg::DrawableCullCallback class InstancedComputeNearFarCullCallback : public osg::DrawableCullCallback
{ {
public: public:
InstancedComputeNearFarCullCallback(const std::vector<Groundcover::GroundcoverEntry>& instances) InstancedComputeNearFarCullCallback(
const std::vector<Groundcover::GroundcoverEntry>& instances, const osg::Vec3& chunkPosition)
: mInstances(instances) : mInstances(instances)
, mChunkPosition(chunkPosition)
{ {
} }
@ -98,7 +102,7 @@ namespace MWRender
dNear = std::numeric_limits<value_type>::max(); dNear = std::numeric_limits<value_type>::max();
for (const auto& entry : mInstances) for (const auto& entry : mInstances)
{ {
osg::Matrix instanceMatrix = computeInstanceMatrix(entry); osg::Matrix instanceMatrix = computeInstanceMatrix(entry, mChunkPosition);
value_type newNear = cullVisitor.computeNearestPointInFrustum( value_type newNear = cullVisitor.computeNearestPointInFrustum(
instanceMatrix * matrix, planes, *drawable); instanceMatrix * matrix, planes, *drawable);
dNear = std::min(dNear, newNear); dNear = std::min(dNear, newNear);
@ -112,7 +116,7 @@ namespace MWRender
dFar = -std::numeric_limits<value_type>::max(); dFar = -std::numeric_limits<value_type>::max();
for (const auto& entry : mInstances) for (const auto& entry : mInstances)
{ {
osg::Matrix instanceMatrix = computeInstanceMatrix(entry); osg::Matrix instanceMatrix = computeInstanceMatrix(entry, mChunkPosition);
value_type newFar = cullVisitor.computeFurthestPointInFrustum( value_type newFar = cullVisitor.computeFurthestPointInFrustum(
instanceMatrix * matrix, planes, *drawable); instanceMatrix * matrix, planes, *drawable);
dFar = std::max(dFar, newFar); dFar = std::max(dFar, newFar);
@ -128,6 +132,7 @@ namespace MWRender
private: private:
std::vector<Groundcover::GroundcoverEntry> mInstances; std::vector<Groundcover::GroundcoverEntry> mInstances;
osg::Vec3 mChunkPosition;
}; };
class InstancingVisitor : public osg::NodeVisitor class InstancingVisitor : public osg::NodeVisitor
@ -177,7 +182,7 @@ namespace MWRender
geom.setVertexAttribArray(6, transforms.get(), osg::Array::BIND_PER_VERTEX); geom.setVertexAttribArray(6, transforms.get(), osg::Array::BIND_PER_VERTEX);
geom.setVertexAttribArray(7, rotations.get(), osg::Array::BIND_PER_VERTEX); geom.setVertexAttribArray(7, rotations.get(), osg::Array::BIND_PER_VERTEX);
geom.addCullCallback(new InstancedComputeNearFarCullCallback(mInstances)); geom.addCullCallback(new InstancedComputeNearFarCullCallback(mInstances, mChunkPosition));
} }
private: private: