Line data Source code
1 : #ifndef MODEL_H_
2 : #define MODEL_H_
3 :
4 : enum ModelType
5 : {
6 : MDL_MD5 = 0,
7 : MDL_OBJ,
8 : MDL_GLTF,
9 : MDL_NumMDLTypes
10 : };
11 :
12 : /* model: the base class for an ingame model, with only pure virtual methods,
13 : * all methods are defined in animmodel or one of its defined classes
14 : *
15 : * extended by animmodel (animated model) which is itself extended by skelmodel
16 : * and vertmodel
17 : *
18 : * a model format loader (e.g. md5 or obj) extends model or one of its children
19 : * and assigns the data from the file format into the object (by setting its
20 : * fields and overriding its methods)
21 : *
22 : * model class hierarchy (all are objects except bottom gvars)
23 : *
24 : * /-------\
25 : * | model |
26 : * \-------/
27 : * |
28 : * v
29 : * /-----------\
30 : * | animmodel |
31 : * \-----------/
32 : * | \__________
33 : * v \
34 : * /-----------\ v
35 : * | skelmodel | /-----------\
36 : * \-----------/ | vertmodel |
37 : * | \-----------/
38 : * | /-------------\ |
39 : * | | modelloader | |
40 : * | \-------------/ |
41 : * | | |
42 : * \__________/ \_____________/ <-- multiple inheritance via template class
43 : * | |
44 : * v v
45 : * /------------\ /------------\
46 : * | SkelLoader | | VertLoader |
47 : * \------------/ \------------/
48 : * _______| | |
49 : * | v /---------------\ v
50 : * /----\ /-----\ | modelcommands | /-----\
51 : * |gltf| -->| md5 | \---------------/ | obj |<-
52 : * \----/ | \-----/ | | \-----/ |
53 : * ^ | | | v v | |
54 : * | | | | /--------------\ /--------------\ | |
55 : * | | | | | SkelCommands | | VertCommands | | |
56 : * | | | | \--------------/ \--------------/ | |
57 : * | | | | | | | | |
58 : * | | | v v | v v |
59 : * | | /-------------------\ | /-------------------\
60 : * | | | SkelCommands<md5> | | | VertCommands<obj> |
61 : * | | | md5::md5commands | | | obj::objcommands |
62 : * | | \---static field----/ | \---static field----/
63 : * | v /
64 : * /--------------------\ /
65 : * | SkelCommands<gltf> |<-/
66 : * | gltf::gltfcommands |
67 : * \----static field----/
68 : */
69 : class model
70 : {
71 : public:
72 : //for spin, orientation: x = yaw, y = pitch, z = roll
73 : vec orientation;
74 : bool shadow, alphashadow, depthoffset;
75 : std::unique_ptr<BIH> bih;
76 : vec bbextend;
77 : float eyeheight, collidexyradius, collideheight;
78 : std::string collidemodel;
79 : int collide, batch;
80 :
81 19 : virtual ~model() = default;
82 : virtual void calcbb(vec &, vec &) const = 0;
83 : virtual int intersect(int, int, int, const vec &, float, float, float, dynent *, modelattach *, float, const vec &, const vec &) const = 0;
84 : virtual void render(int, int, int, const vec&, float, float, float , dynent *, modelattach * = nullptr, float = 1, const vec4<float>& = vec4<float>(1, 1, 1, 1)) const = 0;
85 : virtual bool load() = 0;
86 : virtual int type() const = 0;
87 : virtual void setBIH() = 0;
88 :
89 : /**
90 : * @brief Returns whether the model supports skeletal rigging.
91 : *
92 : * Returns true if the derived model type supports skeletal animation; false
93 : * otherwise. This is implemented by the skelmodel object and its derivatives.
94 : *
95 : * @return true if the model has skeletal animation capability
96 : */
97 : virtual bool skeletal() const = 0;
98 : /**
99 : * @brief Returns whether the model moves with respect to time
100 : *
101 : * Returns true if the model will move itself in any way (including
102 : * translation/rotation). This will trigger separate renderer handling,
103 : * since dynamic models require different rendering handling.
104 : *
105 : * @return true if the model is animated or moves
106 : */
107 : virtual bool animated() const = 0;
108 : /**
109 : * @brief Returns whether the model contains any set pitchscale values.
110 : *
111 : * @return true if any of the model's parts have a pitchscale set
112 : */
113 : virtual bool pitched() const = 0;
114 : /**
115 : * @brief Returns whether the model contains alpha textures.
116 : *
117 : * @return true if any of the model's skin textures are alpha (see-through)
118 : */
119 : virtual bool alphatested() const = 0;
120 :
121 : virtual void setshader(Shader *) = 0;
122 : virtual void setspec(float) = 0;
123 : virtual void setgloss(int) = 0;
124 : virtual void setglow(float, float, float) = 0;
125 : virtual void setalphatest(float) = 0;
126 : virtual void setfullbright(float) = 0;
127 : virtual void setcullface(int) = 0;
128 : virtual void setcolor(const vec &) = 0;
129 : virtual void settransformation(const std::optional<vec>,
130 : const std::optional<vec>,
131 : const std::optional<vec>,
132 : const std::optional<float>) = 0;
133 : //returns the location and size of the model
134 : virtual vec4<float> locationsize() const = 0;
135 : virtual void genshadowmesh(std::vector<triangle> &, const matrix4x3 &) const = 0;
136 :
137 : virtual void preloadBIH() = 0;
138 : virtual void preloadshaders() = 0;
139 : virtual void preloadmeshes() = 0;
140 : virtual void cleanup() = 0;
141 : virtual void startrender() const = 0;
142 : virtual void endrender() const = 0;
143 : virtual void boundbox(vec ¢er, vec &radius) = 0;
144 : virtual float collisionbox(vec ¢er, vec &radius) = 0;
145 : virtual const std::string &modelname() const = 0;
146 :
147 : protected:
148 : vec translate,
149 : spin;
150 : float scale;
151 : std::string name;
152 : vec bbcenter, bbradius, collidecenter, collideradius;
153 : float rejectradius;
154 :
155 : /**
156 : * @brief Constructs a new model object.
157 : *
158 : * The string passed will be consumed by the constructor.
159 : */
160 19 : model(std::string modelname) : orientation(0, 0, 0),
161 19 : shadow(true),
162 19 : alphashadow(true),
163 19 : depthoffset(false),
164 19 : bbextend(0, 0, 0),
165 19 : eyeheight(0.9f),
166 19 : collidexyradius(0),
167 19 : collideheight(0),
168 38 : collidemodel(""),
169 19 : collide(Collide_OrientedBoundingBox),
170 19 : batch(-1),
171 19 : translate(0, 0, 0),
172 19 : spin(0, 0, 0),
173 19 : scale(1.0f),
174 19 : name(std::move(modelname)),
175 19 : bbcenter(0, 0, 0),
176 19 : bbradius(-1, -1, -1),
177 19 : collidecenter(0, 0, 0),
178 19 : collideradius(-1, -1, -1),
179 19 : rejectradius(-1)
180 19 : {}
181 : };
182 :
183 : #endif
|