Skip to content

Commit

Permalink
#5584: Add getSurfaceBounds() method to IModelSurface interface
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jan 14, 2022
1 parent d20e29c commit 8fb0365
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/imodelsurface.h
Expand Up @@ -46,6 +46,9 @@ class IModelSurface
* respecting the applied skin.
*/
virtual const std::string& getActiveMaterial() const = 0;

// Returns the local bounds of this surface
virtual const AABB& getSurfaceBounds() = 0;
};

/**
Expand Down
5 changes: 5 additions & 0 deletions radiantcore/model/StaticModelSurface.cpp
Expand Up @@ -256,6 +256,11 @@ void StaticModelSurface::setActiveMaterial(const std::string& activeMaterial)
_activeMaterial = activeMaterial;
}

const AABB& StaticModelSurface::getSurfaceBounds()
{
return getAABB();
}

bool StaticModelSurface::getIntersection(const Ray& ray, Vector3& intersection, const Matrix4& localToWorld)
{
Vector3 bestIntersection = ray.origin;
Expand Down
2 changes: 2 additions & 0 deletions radiantcore/model/StaticModelSurface.h
Expand Up @@ -109,6 +109,8 @@ class StaticModelSurface :
const std::string& getActiveMaterial() const override;
void setActiveMaterial(const std::string& activeMaterial);

const AABB& getSurfaceBounds() override;

// Returns true if the given ray intersects this surface geometry and fills in
// the exact point in the given Vector3, returns false if no intersection was found.
bool getIntersection(const Ray& ray, Vector3& intersection, const Matrix4& localToWorld);
Expand Down
13 changes: 13 additions & 0 deletions radiantcore/model/export/PatchSurface.cpp
Expand Up @@ -27,6 +27,14 @@ PatchSurface::PatchSurface(const std::string& materialName, PatchMesh& mesh) :
std::transform(mesh.vertices.begin(), mesh.vertices.end(),
std::back_inserter(_vertices), convertPatchVertex);

_bounds = AABB();

// Accumulate the bounds
for (const auto& vertex : _vertices)
{
_bounds.includePoint(vertex);
}

// Generate the indices to define the triangles in clockwise order
for (std::size_t h = 0; h < mesh.height - 1; ++h)
{
Expand Down Expand Up @@ -94,4 +102,9 @@ const std::vector<unsigned int>& PatchSurface::getIndexArray() const
return _indices;
}

const AABB& PatchSurface::getSurfaceBounds()
{
return _bounds;
}

}
3 changes: 3 additions & 0 deletions radiantcore/model/export/PatchSurface.h
Expand Up @@ -19,6 +19,7 @@ class PatchSurface :
std::vector<ArbitraryMeshVertex> _vertices;
std::vector<unsigned int> _indices;
std::string _materialName;
AABB _bounds;

public:
PatchSurface(const std::string& materialName, PatchMesh& mesh);
Expand All @@ -34,6 +35,8 @@ class PatchSurface :

const std::vector<ArbitraryMeshVertex>& getVertexArray() const override;
const std::vector<unsigned int>& getIndexArray() const override;

const AABB& getSurfaceBounds() override;
};

}
5 changes: 5 additions & 0 deletions radiantcore/model/md5/MD5Surface.cpp
Expand Up @@ -277,6 +277,11 @@ void MD5Surface::setActiveMaterial(const std::string& activeMaterial)
_activeMaterial = activeMaterial;
}

const AABB& MD5Surface::getSurfaceBounds()
{
return _aabb_local;
}

void MD5Surface::updateToDefaultPose(const MD5Joints& joints)
{
if (_vertices.size() != _mesh->vertices.size())
Expand Down
2 changes: 2 additions & 0 deletions radiantcore/model/md5/MD5Surface.h
Expand Up @@ -120,6 +120,8 @@ class MD5Surface :
const std::string& getActiveMaterial() const override;
void setActiveMaterial(const std::string& activeMaterial);

const AABB& getSurfaceBounds() override;

void parseFromTokens(parser::DefTokeniser& tok);

// Rebuild the render index array - usually needs to be called only once
Expand Down
13 changes: 13 additions & 0 deletions test/ModelExport.cpp
Expand Up @@ -254,6 +254,19 @@ class TestModelSurface :
{
return indices;
}

const AABB& getSurfaceBounds() override
{
static AABB aabb;
aabb = AABB();

for (const auto& vertex : vertices)
{
aabb.includePoint(vertex);
}

return aabb;
}
};

TEST_F(ModelExportTest, LwoVertexColoursAddedBySurface)
Expand Down

0 comments on commit 8fb0365

Please sign in to comment.