Skip to content

Commit

Permalink
#5576: StaticModel no longer directly relies on picomodel_t structures
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Apr 4, 2021
1 parent bb5e34d commit 53f905f
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 42 deletions.
6 changes: 5 additions & 1 deletion radiantcore/model/picomodel/AseModelLoader.cpp
Expand Up @@ -5,6 +5,8 @@
#include "string/case_conv.h"
#include "StaticModel.h"

#include "PicoModelLoader.h"

extern "C"
{
extern const picoModule_t picoModuleASE;
Expand Down Expand Up @@ -55,7 +57,9 @@ IModelPtr AseModelLoader::loadModelFromPath(const std::string& path)
return IModelPtr();
}

auto modelObj = std::make_shared<StaticModel>(model, string::to_lower_copy(getExtension()));
auto surfaces = PicoModelLoader::CreateSurfaces(model, string::to_lower_copy(getExtension()));

auto modelObj = std::make_shared<StaticModel>(surfaces);

// Set the filename
modelObj->setFilename(os::getFilename(file->getName()));
Expand Down
44 changes: 43 additions & 1 deletion radiantcore/model/picomodel/PicoModelLoader.cpp
Expand Up @@ -11,6 +11,7 @@
#include "idatastream.h"
#include "string/case_conv.h"
#include "StaticModel.h"
#include "StaticModelSurface.h"

namespace model {

Expand Down Expand Up @@ -60,7 +61,10 @@ IModelPtr PicoModelLoader::loadModelFromPath(const std::string& path)
return IModelPtr();
}

auto modelObj = std::make_shared<StaticModel>(model, fExt);
// Convert the pico model surfaces to StaticModelSurfaces
auto surfaces = CreateSurfaces(model, fExt);

auto modelObj = std::make_shared<StaticModel>(surfaces);

// Set the filename
modelObj->setFilename(os::getFilename(file->getName()));
Expand All @@ -71,4 +75,42 @@ IModelPtr PicoModelLoader::loadModelFromPath(const std::string& path)
return modelObj;
}

std::vector<StaticModelSurfacePtr> PicoModelLoader::CreateSurfaces(picoModel_t* picoModel, const std::string& extension)
{
// Convert the pico model surfaces to StaticModelSurfaces
std::vector<StaticModelSurfacePtr> surfaces;

// Get the number of surfaces to create
int nSurf = PicoGetModelNumSurfaces(picoModel);

// Create a StaticModelSurface for each surface in the structure
for (int n = 0; n < nSurf; ++n)
{
// Retrieve the surface, discarding it if it is null or non-triangulated (?)
picoSurface_t* surf = PicoGetModelSurface(picoModel, n);

auto rSurf = CreateSurface(surf, extension);

if (!rSurf) continue;

surfaces.emplace_back(rSurf);
}

return surfaces;
}

StaticModelSurfacePtr PicoModelLoader::CreateSurface(picoSurface_t* picoSurface, const std::string& extension)
{
if (picoSurface == 0 || PicoGetSurfaceType(picoSurface) != PICO_TRIANGLES)
{
return StaticModelSurfacePtr();
}

// Fix the normals of the surface (?)
PicoFixSurfaceNormals(picoSurface);

// Create the StaticModelSurface object and add it to the vector
return std::make_shared<StaticModelSurface>(picoSurface, extension);
}

} // namespace model
9 changes: 9 additions & 0 deletions radiantcore/model/picomodel/PicoModelLoader.h
@@ -1,8 +1,11 @@
#pragma once

#include "ModelImporterBase.h"
#include "StaticModel.h"

typedef struct picoModule_s picoModule_t;
typedef struct picoSurface_s picoSurface_t;
typedef struct picoModel_s picoModel_t;

namespace model
{
Expand All @@ -18,6 +21,12 @@ class PicoModelLoader :

// Load the given model from the path, VFS or absolute
IModelPtr loadModelFromPath(const std::string& name) override;

public:
static std::vector<StaticModelSurfacePtr> CreateSurfaces(picoModel_t* picoModel, const std::string& extension);

private:
static StaticModelSurfacePtr CreateSurface(picoSurface_t* picoSurface, const std::string& extension);
};

} // namespace model
30 changes: 0 additions & 30 deletions radiantcore/model/picomodel/StaticModel.cpp
Expand Up @@ -15,36 +15,6 @@
namespace model
{

StaticModel::StaticModel(picoModel_t* mod, const std::string& fExt) :
StaticModel(std::vector<StaticModelSurfacePtr>{})
{
// Get the number of surfaces to create
int nSurf = PicoGetModelNumSurfaces(mod);

// Create a StaticModelSurface for each surface in the structure
for (int n = 0; n < nSurf; ++n)
{
// Retrieve the surface, discarding it if it is null or non-triangulated (?)
picoSurface_t* surf = PicoGetModelSurface(mod, n);

if (surf == 0 || PicoGetSurfaceType(surf) != PICO_TRIANGLES)
{
continue;
}

// Fix the normals of the surface (?)
PicoFixSurfaceNormals(surf);

// Create the StaticModelSurface object and add it to the vector
auto rSurf = std::make_shared<StaticModelSurface>(surf, fExt);

_surfVec.push_back(Surface(rSurf));

// Extend the model AABB to include the surface's AABB
_localAABB.includeAABB(rSurf->getAABB());
}
}

StaticModel::StaticModel(const std::vector<StaticModelSurfacePtr>& surfaces) :
_scaleTransformed(1, 1, 1),
_scale(1, 1, 1),
Expand Down
11 changes: 1 addition & 10 deletions radiantcore/model/picomodel/StaticModel.h
Expand Up @@ -3,7 +3,6 @@
#include "iundo.h"
#include "mapfile.h"
#include "imodel.h"
#include "lib/picomodel.h"
#include "math/AABB.h"
#include "imodelsurface.h"

Expand Down Expand Up @@ -39,7 +38,7 @@ class StaticModel :
public IUndoable
{
private:
// greebo: RenderablePicoSurfaces are shared objects, the actual shaders
// greebo: StaticModelSurfaces are shared objects, the actual shaders
// and the model skin handling are managed by the nodes/imodels referencing them
struct Surface
{
Expand All @@ -62,7 +61,6 @@ class StaticModel :
{}
};

// List of RenderablePicoSurfaces.
typedef std::vector<Surface> SurfaceList;

// Vector of renderable surfaces for this model
Expand Down Expand Up @@ -110,13 +108,6 @@ class StaticModel :

public:

/**
* Constructor. Accepts a picoModel_t struct containing the raw model data
* loaded from picomodel, and a string filename extension to allow the
* correct handling of material paths (which differs between ASE and LWO)
*/
StaticModel(picoModel_t* mod, const std::string& fExt);

/**
* Construct a StaticModel with the given set of surfaces.
*/
Expand Down

0 comments on commit 53f905f

Please sign in to comment.