| 1 |
#pragma once |
| 2 |
|
| 3 |
#include "Bounded.h" |
| 4 |
#include "irender.h" |
| 5 |
#include "inode.h" |
| 6 |
#include "modelskin.h" |
| 7 |
#include "imodule.h" |
| 8 |
#include "imodelsurface.h" |
| 9 |
#include <vector> |
| 10 |
|
| 11 |
#include "math/Vector3.h" |
| 12 |
|
| 13 |
/* Forward decls */ |
| 14 |
class AABB; |
| 15 |
class ModelSkin; |
| 16 |
class Matrix4; |
| 17 |
|
| 18 |
namespace model |
| 19 |
{ |
| 20 |
|
| 21 |
typedef std::vector<std::string> StringList; |
| 22 |
|
| 23 |
/** |
| 24 |
* \brief |
| 25 |
* Interface for static models. |
| 26 |
* |
| 27 |
* This interface provides functions for obtaining information about a LWO or |
| 28 |
* ASE model, such as its bounding box or poly count. |
| 29 |
*/ |
| 30 |
class IModel : public Bounded |
| 31 |
{ |
| 32 |
public: |
| 33 |
/** |
| 34 |
* greebo: The filename (without path) of this model. |
| 35 |
*/ |
| 36 |
virtual std::string getFilename() const = 0; |
| 37 |
|
| 38 |
/** |
| 39 |
* greebo: Returns the VFS path which can be used to load |
| 40 |
* this model from the modelcache. |
| 41 |
*/ |
| 42 |
virtual std::string getModelPath() const = 0; |
| 43 |
|
| 44 |
/** Apply the given skin to this model. |
| 45 |
* |
| 46 |
* @param skin |
| 47 |
* The skin instance to apply to this model. |
| 48 |
* Passing an empty skin will just remove a previously applied remap. |
| 49 |
*/ |
| 50 |
virtual void applySkin(const decl::ISkin::Ptr& skin) = 0; |
| 51 |
|
| 52 |
/** Return the number of material surfaces on this model. Each material |
| 53 |
* surface consists of a set of polygons sharing the same material. |
| 54 |
*/ |
| 55 |
virtual int getSurfaceCount() const = 0; |
| 56 |
|
| 57 |
/** Return the number of vertices in this model, equal to the sum of the |
| 58 |
* vertex count from each surface. |
| 59 |
*/ |
| 60 |
virtual int getVertexCount() const = 0; |
| 61 |
|
| 62 |
/** Return the number of triangles in this model, equal to the sum of the |
| 63 |
* triangle count from each surface. |
| 64 |
*/ |
| 65 |
virtual int getPolyCount() const = 0; |
| 66 |
|
| 67 |
/** |
| 68 |
* \brief |
| 69 |
* Return a vector of strings listing the active materials used in this |
| 70 |
* model, after any skin remaps. |
| 71 |
* |
| 72 |
* The list is owned by the model instance. |
| 73 |
*/ |
| 74 |
virtual const StringList& getActiveMaterials() const = 0; |
| 75 |
|
| 76 |
/** |
| 77 |
* \brief |
| 78 |
* Return the surface with the given index |
| 79 |
* |
| 80 |
* Retrieves the interface of a specific surface, to get access to the |
| 81 |
* surface's polygons and vertices. |
| 82 |
* |
| 83 |
* \param surfaceNum |
| 84 |
* The surface index, must be in [0..getSurfaceCount()) |
| 85 |
*/ |
| 86 |
virtual const IModelSurface& getSurface(unsigned surfaceNum) const = 0; |
| 87 |
}; |
| 88 |
|
| 89 |
// Smart pointer typedefs |
| 90 |
typedef std::shared_ptr<IModel> IModelPtr; |
| 91 |
typedef std::weak_ptr<IModel> IModelWeakPtr; |
| 92 |
|
| 93 |
/** |
| 94 |
* greebo: Each node in the scene that represents "just" a model, |
| 95 |
* derives from this class. Use a cast on this class to |
| 96 |
* identify model nodes in the scene. |
| 97 |
*/ |
| 98 |
class ModelNode |
| 99 |
{ |
| 100 |
public: |
| 101 |
virtual ~ModelNode() |
| 102 |
{} |
| 103 |
|
| 104 |
// Returns the contained IModel |
| 105 |
virtual const IModel& getIModel() const = 0; |
| 106 |
|
| 107 |
// Returns the contained IModel |
| 108 |
virtual IModel& getIModel() = 0; |
| 109 |
|
| 110 |
// Returns true if this model's scale has been modified |
| 111 |
// and needs to be written to file |
| 112 |
virtual bool hasModifiedScale() = 0; |
| 113 |
|
| 114 |
// Returns the current scale of this model |
| 115 |
virtual Vector3 getModelScale() = 0; |
| 116 |
}; |
| 117 |
typedef std::shared_ptr<ModelNode> ModelNodePtr; |
| 118 |
|
| 119 |
class IModelExporter; |
| 120 |
typedef std::shared_ptr<IModelExporter> IModelExporterPtr; |
| 121 |
|
| 122 |
/** |
| 123 |
* Exporter Interface for models (meshes). |
| 124 |
*/ |
| 125 |
class IModelExporter |
| 126 |
{ |
| 127 |
public: |
| 128 |
virtual ~IModelExporter() |
| 129 |
{} |
| 130 |
|
| 131 |
// Virtual constructor idiom. Use this method to generate a new |
| 132 |
// instance of the implementing subclass. This way the model format manager |
| 133 |
// can create a fresh instance of this exporter on demand. |
| 134 |
virtual IModelExporterPtr clone() = 0; |
| 135 |
|
| 136 |
// The display name for referencing this exporter in the GUI |
| 137 |
virtual const std::string& getDisplayName() const = 0; |
| 138 |
|
| 139 |
// Returns the uppercase file extension this exporter is suitable for |
| 140 |
virtual const std::string& getExtension() const = 0; |
| 141 |
|
| 142 |
// Adds the given Surface to the exporter's queue |
| 143 |
// The given transform is applied to the surface before the vertices are added to the queue. |
| 144 |
// Note: Scaling components of the matrix are not treated separately here. |
| 145 |
virtual void addSurface(const IModelSurface& surface, const Matrix4& localToWorld) = 0; |
| 146 |
|
| 147 |
// Adds the given set of polygons to the named surface |
| 148 |
// The given transform is applied to the surface before the vertices are added to the queue. |
| 149 |
// Note: Scaling components of the matrix are not treated separately here. |
| 150 |
// Note 2: Polygons are expected to have their vertices defined counter-clockwise |
| 151 |
virtual void addPolygons(const std::string& materialName, |
| 152 |
const std::vector<ModelPolygon>& polys, const Matrix4& localToWorld) = 0; |
| 153 |
|
| 154 |
// Export to the given file in the given path (which must be absolute). |
| 155 |
// If writing to multiple files (like .obj+.mtl), the implementation |
| 156 |
// may deduct the alternative file paths from this one. |
| 157 |
// Note: the user will already have confirmed to overwrite any existing |
| 158 |
// file with that path, but hasn't explicitly confirmed overwriting |
| 159 |
// secondary or tertiary output files. |
| 160 |
// Throws std::runtime_error in case of failures. |
| 161 |
virtual void exportToPath(const std::string& outputPath, const std::string& filename) = 0; |
| 162 |
}; |
| 163 |
|
| 164 |
/** |
| 165 |
* Importer interface for models. An importer must be able |
| 166 |
* to load a model (node) from the VFS and from an absolute path. |
| 167 |
* The importer instance shouldn't maintain an internal state, |
| 168 |
* such that the same instance can be used to load several models, |
| 169 |
* from different client code. |
| 170 |
*/ |
| 171 |
class IModelImporter |
| 172 |
{ |
| 173 |
public: |
| 174 |
// Returns the uppercase file extension this exporter is suitable for |
| 175 |
virtual const std::string& getExtension() const = 0; |
| 176 |
|
| 177 |
/** |
| 178 |
* greebo: Returns a newly created model node for the given model name. |
| 179 |
* |
| 180 |
* @modelName: This is usually the value of the "model" spawnarg of entities. |
| 181 |
* |
| 182 |
* @returns: the newly created modelnode (can be NULL if the model was not found). |
| 183 |
*/ |
| 184 |
virtual scene::INodePtr loadModel(const std::string& modelName) = 0; |
| 185 |
|
| 186 |
/** |
| 187 |
* Load a model from the given (maybe be VFS or absolute), and return the IModel subclass for it. |
| 188 |
* |
| 189 |
* @returns: the IModelPtr containing the renderable model or |
| 190 |
* an empty IModelPtr if the model loader could not load the file. |
| 191 |
*/ |
| 192 |
virtual model::IModelPtr loadModelFromPath(const std::string& path) = 0; |
| 193 |
}; |
| 194 |
typedef std::shared_ptr<IModelImporter> IModelImporterPtr; |
| 195 |
|
| 196 |
class IModelFormatManager : |
| 197 |
public RegisterableModule |
| 198 |
{ |
| 199 |
public: |
| 200 |
virtual ~IModelFormatManager() |
| 201 |
{} |
| 202 |
|
| 203 |
// Register/unregister an importer class |
| 204 |
virtual void registerImporter(const IModelImporterPtr& importer) = 0; |
| 205 |
virtual void unregisterImporter(const IModelImporterPtr& importer) = 0; |
| 206 |
|
| 207 |
// Find an importer for the given extension, returns the NullModelLoader if nothing found |
| 208 |
// Passing in an empty extension will return the NullModelLoader as well |
| 209 |
virtual IModelImporterPtr getImporter(const std::string& extension) = 0; |
| 210 |
|
| 211 |
// Register/unregister an exporter class |
| 212 |
virtual void registerExporter(const IModelExporterPtr& exporter) = 0; |
| 213 |
virtual void unregisterExporter(const IModelExporterPtr& exporter) = 0; |
| 214 |
|
| 215 |
// Find an exporter for the given extension, returns empty if nothing found |
| 216 |
virtual IModelExporterPtr getExporter(const std::string& extension) = 0; |
| 217 |
|
| 218 |
// Calls the functor with each registered model importer as argument |
| 219 |
virtual void foreachImporter(const std::function<void(const IModelImporterPtr&)>& functor) = 0; |
| 220 |
|
| 221 |
// Calls the functor with each registered model exporter as argument |
| 222 |
virtual void foreachExporter(const std::function<void(const IModelExporterPtr&)>& functor) = 0; |
| 223 |
}; |
| 224 |
|
| 225 |
} // namespace model |
| 226 |
|
| 227 |
// Utility methods |
| 228 |
inline bool Node_isModel(const scene::INodePtr& node) |
| 229 |
{ |
| 230 |
return std::dynamic_pointer_cast<model::ModelNode>(node) != nullptr; |
| 231 |
} |
| 232 |
|
| 233 |
inline model::ModelNodePtr Node_getModel(const scene::INodePtr& node) |
| 234 |
{ |
| 235 |
return std::dynamic_pointer_cast<model::ModelNode>(node); |
| 236 |
} |
| 237 |
|
| 238 |
// Contains the default format used for exporting scaled models |
| 239 |
const char* const RKEY_DEFAULT_MODEL_EXPORT_FORMAT = "user/ui/map/defaultScaledModelExportFormat"; |
| 240 |
|
| 241 |
const char* const MODULE_MODELFORMATMANAGER("ModelFormatManager"); |
| 242 |
|
| 243 |
inline model::IModelFormatManager& GlobalModelFormatManager() |
| 244 |
{ |
| 245 |
static module::InstanceReference<model::IModelFormatManager> _reference(MODULE_MODELFORMATMANAGER); |
| 246 |
return _reference; |
| 247 |
} |