Skip to content

Commit

Permalink
#5584: Start by breaking MD5 model rendering. Refactor MD5Model class…
Browse files Browse the repository at this point in the history
…. RenderableModelSurfaces are created for MD5 surfaces.
  • Loading branch information
codereader committed Jan 16, 2022
1 parent 7118ec6 commit fdae71a
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 33 deletions.
20 changes: 8 additions & 12 deletions radiantcore/model/md5/MD5Model.cpp
Expand Up @@ -41,16 +41,12 @@ MD5Model::MD5Model(const MD5Model& other) :
updateMaterialList();
}

MD5Model::const_iterator MD5Model::begin() const {
return _surfaces.begin();
}

MD5Model::const_iterator MD5Model::end() const {
return _surfaces.end();
}

std::size_t MD5Model::size() const {
return _surfaces.size();
void MD5Model::foreachSurface(const std::function<void(const MD5Surface&)>& functor) const
{
for (const auto& surface : _surfaces)
{
functor(*surface.surface);
}
}

MD5Surface& MD5Model::createNewSurface()
Expand Down Expand Up @@ -164,7 +160,7 @@ void MD5Model::applySkin(const ModelSkin& skin)

int MD5Model::getSurfaceCount() const
{
return static_cast<int>(size());
return static_cast<int>(_surfaces.size());
}

int MD5Model::getVertexCount() const
Expand Down Expand Up @@ -212,7 +208,7 @@ void MD5Model::captureShaders()
}
}

const model::IModelSurface& MD5Model::getSurface(unsigned surfaceNum) const
const model::IIndexedModelSurface& MD5Model::getSurface(unsigned surfaceNum) const
{
assert(surfaceNum >= 0 && surfaceNum < _surfaces.size());
return *(_surfaces[surfaceNum].surface);
Expand Down
35 changes: 14 additions & 21 deletions radiantcore/model/md5/MD5Model.h
Expand Up @@ -29,10 +29,7 @@ class MD5Model :
{
// The MD5 mesh
MD5SurfacePtr surface;
#if 0
// The name of the material with skin applied
std::string activeMaterial;
#endif

// Mapped shader
ShaderPtr shader;

Expand Down Expand Up @@ -81,12 +78,8 @@ class MD5Model :
// Surfaces are copied and assigned their default material
MD5Model(const MD5Model& other);

typedef SurfaceList::const_iterator const_iterator;

// Public iterator-related methods
const_iterator begin() const;
const_iterator end() const;
std::size_t size() const;
// Const-iterate over all surfaces
void foreachSurface(const std::function<void(const MD5Surface&)>& functor) const;

/** greebo: Reads the model data from the given tokeniser.
*/
Expand All @@ -109,44 +102,44 @@ class MD5Model :
void setFilename(const std::string& name);

// IModel implementation
virtual std::string getFilename() const;
virtual std::string getFilename() const override;

virtual std::string getModelPath() const;
virtual std::string getModelPath() const override;
void setModelPath(const std::string& modelPath);

virtual void applySkin(const ModelSkin& skin);
virtual void applySkin(const ModelSkin& skin) override;

/** Return the number of material surfaces on this model. Each material
* surface consists of a set of polygons sharing the same material.
*/
virtual int getSurfaceCount() const;
virtual int getSurfaceCount() const override;

/** Return the number of vertices in this model, equal to the sum of the
* vertex count from each surface.
*/
virtual int getVertexCount() const;
virtual int getVertexCount() const override;

/** Return the number of triangles in this model, equal to the sum of the
* triangle count from each surface.
*/
virtual int getPolyCount() const;
virtual int getPolyCount() const override;

/** Return a vector of strings listing the active materials used in this
* model, after any skin remaps. The list is owned by the model instance.
*/
virtual const std::vector<std::string>& getActiveMaterials() const;
virtual const std::vector<std::string>& getActiveMaterials() const override;

const model::IModelSurface& getSurface(unsigned surfaceNum) const;
const model::IIndexedModelSurface& getSurface(unsigned surfaceNum) const override;

// OpenGLRenderable implementation
virtual void render(const RenderInfo& info) const;

void setRenderSystem(const RenderSystemPtr& renderSystem);

// IMD5Model implementation
virtual void setAnim(const IMD5AnimPtr& anim);
virtual const IMD5AnimPtr& getAnim() const;
virtual void updateAnim(std::size_t time);
virtual void setAnim(const IMD5AnimPtr& anim) override;
virtual const IMD5AnimPtr& getAnim() const override;
virtual void updateAnim(std::size_t time) override;

/**
* Helper: Parse an MD5 vector, which consists of three separated numbers
Expand Down
22 changes: 22 additions & 0 deletions radiantcore/model/md5/MD5ModelNode.cpp
Expand Up @@ -58,6 +58,26 @@ scene::INode::Type MD5ModelNode::getNodeType() const
return Type::Model;
}

void MD5ModelNode::onInsertIntoScene(scene::IMapRootNode& root)
{
// Renderables will acquire their shaders in onPreRender
_model->foreachSurface([&](const MD5Surface& surface)
{
_renderableSurfaces.emplace_back(
std::make_shared<model::RenderableModelSurface>(surface, localToWorld())
);
});

Node::onInsertIntoScene(root);
}

void MD5ModelNode::onRemoveFromScene(scene::IMapRootNode& root)
{
Node::onRemoveFromScene(root);

_renderableSurfaces.clear();
}

void MD5ModelNode::testSelect(Selector& selector, SelectionTest& test) {
_model->testSelect(selector, test, localToWorld());
}
Expand Down Expand Up @@ -96,6 +116,7 @@ void MD5ModelNode::setRenderSystem(const RenderSystemPtr& renderSystem)
void MD5ModelNode::render(IRenderableCollector& collector, const VolumeTest& volume,
const Matrix4& localToWorld, const IRenderEntity& entity) const
{
#if 0
// Do some rough culling (per model, not per surface)
if (volume.TestAABB(localAABB(), localToWorld) == VOLUME_OUTSIDE)
{
Expand All @@ -122,6 +143,7 @@ void MD5ModelNode::render(IRenderableCollector& collector, const VolumeTest& vol
// Uncomment to render the skeleton
//collector.SetState(entity.getWireShader(), RenderableCollector::eFullMaterials);
//collector.addRenderable(_model->getRenderableSkeleton(), localToWorld, entity);
#endif
}

// Returns the name of the currently active skin
Expand Down
7 changes: 7 additions & 0 deletions radiantcore/model/md5/MD5ModelNode.h
Expand Up @@ -5,6 +5,7 @@
#include "itraceable.h"
#include "scene/Node.h"
#include "render/VectorLightList.h"
#include "../RenderableModelSurface.h"

namespace md5 {

Expand All @@ -20,10 +21,16 @@ class MD5ModelNode :
// The name of this model's skin
std::string _skin;

// The renderable surfaces attached to the shaders
std::vector<model::RenderableModelSurface::Ptr> _renderableSurfaces;

public:
MD5ModelNode(const MD5ModelPtr& model);
virtual ~MD5ModelNode();

void onInsertIntoScene(scene::IMapRootNode& root) override;
void onRemoveFromScene(scene::IMapRootNode& root) override;

// ModelNode implementation
const model::IModel& getIModel() const override;
model::IModel& getIModel() override;
Expand Down

0 comments on commit fdae71a

Please sign in to comment.