ModelInfo.h 3.42 KB
Newer Older
1 2 3 4 5 6
#pragma once

// Management for model resources in our engine!

#include <Streaming.common.h>

7 8
#include "TexDict.h"

9 10
#include <Console.CommandHelpers.h>

11 12 13 14 15 16 17
// custom include guard
#ifndef COLLISION_H
#define COLLISION_H

#include "src/collision.h"
#endif

NTAuthority's avatar
NTAuthority committed
18 19
#define MODEL_ID_BASE 0
#define MAX_MODELS 20000
20 21 22 23 24 25 26

namespace krt
{

// General model information management container.
struct ModelManager : public streaming::StreamingTypeInterface
{
NTAuthority's avatar
NTAuthority committed
27 28 29 30 31 32
	enum class eModelType
	{
		ATOMIC,
		VEHICLE,
		PED
	};
33

NTAuthority's avatar
NTAuthority committed
34 35 36 37 38 39
	struct ModelResource
	{
		inline streaming::ident_t GetID(void) const { return this->id; }
		inline eModelType GetType(void) const { return this->modelType; }
		inline float GetLODDistance(void) const { return this->lodDistance; }
		inline int GetFlags(void) const { return this->flags; }
40

NTAuthority's avatar
NTAuthority committed
41
		inline ModelResource* GetLODModel(void) { return this->lod_model; }
42

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
		inline std::shared_ptr<CColModel> GetCollisionModel()
		{
			if (this->col_model.expired())
			{
				return nullptr;
			}

			return this->col_model.lock();
		}

		inline void SetCollisionModel(const std::shared_ptr<CColModel>& pointer)
		{
			this->col_model = pointer;
		}

NTAuthority's avatar
NTAuthority committed
58 59
		rw::Object* CloneModel(void);
		void ReleaseModel(rw::Object* rwobj);
60

61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
		inline float GetMinimumDistance(void) const
		{
			// if not a LOD, no minimum distance
			if (this->lodDistance < 300.0f)
			{
				return 0.0f;
			}

			// if we have a non-LOD model, don't draw once we draw that
			if (this->non_lod_model)
			{
				return this->non_lod_model->GetLODDistance();
			}

			// use the game-specific minimum distance
			return this->minimumDistance;
		}

79
	private:
NTAuthority's avatar
NTAuthority committed
80
		friend struct ModelManager;
81

NTAuthority's avatar
NTAuthority committed
82
		static void NativeReleaseModel(rw::Object* rwobj);
83

NTAuthority's avatar
NTAuthority committed
84 85 86 87
		inline ModelResource(vfs::DevicePtr device, std::string pathToRes) : vfsResLoc(device, std::move(pathToRes))
		{
			return;
		}
88

NTAuthority's avatar
NTAuthority committed
89 90 91 92
		inline ~ModelResource(void)
		{
			return;
		}
93

NTAuthority's avatar
NTAuthority committed
94
		ModelManager* manager;
95

NTAuthority's avatar
NTAuthority committed
96 97 98
		streaming::ident_t id;
		streaming::ident_t texDictID;
		float lodDistance;
99
		float minimumDistance;
NTAuthority's avatar
NTAuthority committed
100
		int flags;
101

102
		ModelResource* non_lod_model; // non-SA: model of a higher-quality model than this one
103
		ModelResource* lod_model;     // model of a lower quality model than this one.
104

105 106
		std::weak_ptr<CColModel> col_model;

NTAuthority's avatar
NTAuthority committed
107
		eModelType modelType;
108

NTAuthority's avatar
NTAuthority committed
109
		DeviceResourceLocation vfsResLoc;
110

NTAuthority's avatar
NTAuthority committed
111
		rw::Object* modelPtr;
112

NTAuthority's avatar
NTAuthority committed
113 114
		SRWLOCK_VIRTUAL lockModelLoading;
	};
115

NTAuthority's avatar
NTAuthority committed
116 117
	ModelManager(streaming::StreamMan& streaming, TextureManager& texManager);
	~ModelManager(void);
118

NTAuthority's avatar
NTAuthority committed
119 120 121
	streaming::ident_t RegisterAtomicModel(
	    std::string name, std::string texDictName, float lodDistance, int flags,
	    std::string absFilePath);
122

NTAuthority's avatar
NTAuthority committed
123
	ModelResource* GetModelByID(streaming::ident_t id);
124

NTAuthority's avatar
NTAuthority committed
125
	ModelResource* GetModelByName(const std::string& name);
126

NTAuthority's avatar
NTAuthority committed
127
	void LoadAllModels(void);
128

NTAuthority's avatar
NTAuthority committed
129 130
	void LoadResource(streaming::ident_t localID, const void* dataBuf, size_t memSize) override;
	void UnloadResource(streaming::ident_t localID) override;
131

NTAuthority's avatar
NTAuthority committed
132
	size_t GetObjectMemorySize(streaming::ident_t localID) const override;
133

134
private:
NTAuthority's avatar
NTAuthority committed
135 136
	streaming::StreamMan& streaming;
	TextureManager& texManager;
137

NTAuthority's avatar
NTAuthority committed
138
	std::vector<ModelResource*> models;
139

NTAuthority's avatar
NTAuthority committed
140
	std::atomic<streaming::ident_t> curModelId;
141

NTAuthority's avatar
NTAuthority committed
142 143 144 145
	std::map<std::string, ModelResource*> modelByName;

	std::map<std::string, ModelResource*> basifierLookup;
	std::map<std::string, ModelResource*> lodifierLookup;
146 147

	std::unique_ptr<ConsoleCommand> loadAllModelsCommand;
148 149
};
}