19 #ifndef TYPES_H_INCLUDED
20 #define TYPES_H_INCLUDED
31 #include "generic/optional.h"
33 #include "math/vec2.h"
34 #include "math/vec3.h"
35 #include "math/vec4.h"
36 #include "math/quaternion.h"
37 #include "math/degrees.h"
38 #include "math/radians.h"
39 #include "math/mat3.h"
40 #include "math/mat4.h"
41 #include "math/aabb.h"
42 #include "math/plane.h"
44 #include "math/operators.h"
46 #include "generic/object_manager.h"
47 #include "generic/unique_id.h"
48 #include "generic/default_init_ptr.h"
50 #include "utils/unicode.h"
51 #include "material_constants.h"
64 return Seconds(value_ + rhs.value_);
72 bool operator>(
float rhs)
const {
76 float to_float()
const {
84 enum VertexAttribute {
85 VERTEX_ATTRIBUTE_NONE,
90 VERTEX_ATTRIBUTE_PACKED_VEC4_1I
93 class VertexSpecification;
104 operator VertexAttribute()
const {
105 return spec_->*attr_;
118 typedef int16_t AttributeOffset;
119 const static AttributeOffset INVALID_ATTRIBUTE_OFFSET = -1;
125 VertexAttribute position_attribute_ = VERTEX_ATTRIBUTE_NONE;
126 VertexAttribute normal_attribute_ = VERTEX_ATTRIBUTE_NONE;
127 VertexAttribute texcoord0_attribute_ = VERTEX_ATTRIBUTE_NONE;
128 VertexAttribute texcoord1_attribute_ = VERTEX_ATTRIBUTE_NONE;
129 VertexAttribute texcoord2_attribute_ = VERTEX_ATTRIBUTE_NONE;
130 VertexAttribute texcoord3_attribute_ = VERTEX_ATTRIBUTE_NONE;
131 VertexAttribute texcoord4_attribute_ = VERTEX_ATTRIBUTE_NONE;
132 VertexAttribute texcoord5_attribute_ = VERTEX_ATTRIBUTE_NONE;
133 VertexAttribute texcoord6_attribute_ = VERTEX_ATTRIBUTE_NONE;
134 VertexAttribute texcoord7_attribute_ = VERTEX_ATTRIBUTE_NONE;
135 VertexAttribute diffuse_attribute_ = VERTEX_ATTRIBUTE_NONE;
136 VertexAttribute specular_attribute_ = VERTEX_ATTRIBUTE_NONE;
138 AttributeOffset position_offset_ = 0;
139 AttributeOffset normal_offset_ = 0;
140 AttributeOffset texcoord0_offset_ = 0;
141 AttributeOffset texcoord1_offset_ = 0;
142 AttributeOffset texcoord2_offset_ = 0;
143 AttributeOffset texcoord3_offset_ = 0;
144 AttributeOffset texcoord4_offset_ = 0;
145 AttributeOffset texcoord5_offset_ = 0;
146 AttributeOffset texcoord6_offset_ = 0;
147 AttributeOffset texcoord7_offset_ = 0;
148 AttributeOffset diffuse_offset_ = 0;
149 AttributeOffset specular_offset_ = 0;
171 position_attribute_(rhs.position_attribute_),
172 normal_attribute_(rhs.normal_attribute_),
173 texcoord0_attribute_(rhs.texcoord0_attribute_),
174 texcoord1_attribute_(rhs.texcoord1_attribute_),
175 texcoord2_attribute_(rhs.texcoord2_attribute_),
176 texcoord3_attribute_(rhs.texcoord3_attribute_),
177 texcoord4_attribute_(rhs.texcoord4_attribute_),
178 texcoord5_attribute_(rhs.texcoord5_attribute_),
179 texcoord6_attribute_(rhs.texcoord6_attribute_),
180 texcoord7_attribute_(rhs.texcoord7_attribute_),
181 diffuse_attribute_(rhs.diffuse_attribute_),
182 specular_attribute_(rhs.specular_attribute_),
183 position_attribute(
this, &VertexSpecification::position_attribute_),
184 normal_attribute(
this, &VertexSpecification::normal_attribute_),
185 texcoord0_attribute(
this, &VertexSpecification::texcoord0_attribute_),
186 texcoord1_attribute(
this, &VertexSpecification::texcoord1_attribute_),
187 texcoord2_attribute(
this, &VertexSpecification::texcoord2_attribute_),
188 texcoord3_attribute(
this, &VertexSpecification::texcoord3_attribute_),
189 texcoord4_attribute(
this, &VertexSpecification::texcoord4_attribute_),
190 texcoord5_attribute(
this, &VertexSpecification::texcoord5_attribute_),
191 texcoord6_attribute(
this, &VertexSpecification::texcoord6_attribute_),
192 texcoord7_attribute(
this, &VertexSpecification::texcoord7_attribute_),
193 diffuse_attribute(
this, &VertexSpecification::diffuse_attribute_),
194 specular_attribute(
this, &VertexSpecification::specular_attribute_) {
196 recalc_stride_and_offsets();
200 position_attribute_(rhs.position_attribute_),
201 normal_attribute_(rhs.normal_attribute_),
202 texcoord0_attribute_(rhs.texcoord0_attribute_),
203 texcoord1_attribute_(rhs.texcoord1_attribute_),
204 texcoord2_attribute_(rhs.texcoord2_attribute_),
205 texcoord3_attribute_(rhs.texcoord3_attribute_),
206 texcoord4_attribute_(rhs.texcoord4_attribute_),
207 texcoord5_attribute_(rhs.texcoord5_attribute_),
208 texcoord6_attribute_(rhs.texcoord6_attribute_),
209 texcoord7_attribute_(rhs.texcoord7_attribute_),
210 diffuse_attribute_(rhs.diffuse_attribute_),
211 specular_attribute_(rhs.specular_attribute_),
212 position_attribute(
this, &VertexSpecification::position_attribute_),
213 normal_attribute(
this, &VertexSpecification::normal_attribute_),
214 texcoord0_attribute(
this, &VertexSpecification::texcoord0_attribute_),
215 texcoord1_attribute(
this, &VertexSpecification::texcoord1_attribute_),
216 texcoord2_attribute(
this, &VertexSpecification::texcoord2_attribute_),
217 texcoord3_attribute(
this, &VertexSpecification::texcoord3_attribute_),
218 texcoord4_attribute(
this, &VertexSpecification::texcoord4_attribute_),
219 texcoord5_attribute(
this, &VertexSpecification::texcoord5_attribute_),
220 texcoord6_attribute(
this, &VertexSpecification::texcoord6_attribute_),
221 texcoord7_attribute(
this, &VertexSpecification::texcoord7_attribute_),
222 diffuse_attribute(
this, &VertexSpecification::diffuse_attribute_),
223 specular_attribute(
this, &VertexSpecification::specular_attribute_) {
225 recalc_stride_and_offsets();
229 position_attribute_ = rhs.position_attribute_;
230 normal_attribute_ = rhs.normal_attribute_;
231 texcoord0_attribute_ = rhs.texcoord0_attribute_;
232 texcoord1_attribute_ = rhs.texcoord1_attribute_;
233 texcoord2_attribute_ = rhs.texcoord2_attribute_;
234 texcoord3_attribute_ = rhs.texcoord3_attribute_;
235 texcoord4_attribute_ = rhs.texcoord4_attribute_;
236 texcoord5_attribute_ = rhs.texcoord5_attribute_;
237 texcoord6_attribute_ = rhs.texcoord6_attribute_;
238 texcoord7_attribute_ = rhs.texcoord7_attribute_;
239 diffuse_attribute_ = rhs.diffuse_attribute_;
240 specular_attribute_ = rhs.specular_attribute_;
242 recalc_stride_and_offsets();
248 VertexAttribute position,
249 VertexAttribute normal = VERTEX_ATTRIBUTE_NONE,
250 VertexAttribute texcoord0 = VERTEX_ATTRIBUTE_NONE,
251 VertexAttribute texcoord1 = VERTEX_ATTRIBUTE_NONE,
252 VertexAttribute texcoord2 = VERTEX_ATTRIBUTE_NONE,
253 VertexAttribute texcoord3 = VERTEX_ATTRIBUTE_NONE,
254 VertexAttribute texcoord4 = VERTEX_ATTRIBUTE_NONE,
255 VertexAttribute texcoord5 = VERTEX_ATTRIBUTE_NONE,
256 VertexAttribute texcoord6 = VERTEX_ATTRIBUTE_NONE,
257 VertexAttribute texcoord7 = VERTEX_ATTRIBUTE_NONE,
258 VertexAttribute diffuse = VERTEX_ATTRIBUTE_NONE,
259 VertexAttribute specular = VERTEX_ATTRIBUTE_NONE
263 return position_attribute == rhs.position_attribute &&
264 normal_attribute == rhs.normal_attribute &&
265 texcoord0_attribute == rhs.texcoord0_attribute &&
266 texcoord1_attribute == rhs.texcoord1_attribute &&
267 texcoord2_attribute == rhs.texcoord2_attribute &&
268 texcoord3_attribute == rhs.texcoord3_attribute &&
269 texcoord4_attribute == rhs.texcoord4_attribute &&
270 texcoord5_attribute == rhs.texcoord5_attribute &&
271 texcoord6_attribute == rhs.texcoord6_attribute &&
272 texcoord7_attribute == rhs.texcoord7_attribute &&
273 diffuse_attribute == rhs.diffuse_attribute &&
274 specular_attribute == rhs.specular_attribute;
278 return !(*
this == rhs);
281 inline uint32_t stride()
const {
return stride_; }
283 bool has_positions()
const {
return bool(position_attribute_); }
284 bool has_normals()
const {
return bool(normal_attribute_); }
286 bool has_texcoordX(uint8_t which)
const;
288 VertexAttribute texcoordX_attribute(uint8_t which)
const;
290 bool has_texcoord0()
const {
return bool(texcoord0_attribute_); }
291 bool has_texcoord1()
const {
return bool(texcoord1_attribute_); }
292 bool has_texcoord2()
const {
return bool(texcoord2_attribute_); }
293 bool has_texcoord3()
const {
return bool(texcoord3_attribute_); }
294 bool has_texcoord4()
const {
return bool(texcoord4_attribute_); }
295 bool has_texcoord5()
const {
return bool(texcoord5_attribute_); }
296 bool has_texcoord6()
const {
return bool(texcoord6_attribute_); }
297 bool has_texcoord7()
const {
return bool(texcoord7_attribute_); }
299 bool has_diffuse()
const {
return bool(diffuse_attribute_); }
300 bool has_specular()
const {
return bool(specular_attribute_); }
302 AttributeOffset position_offset(
bool check=
true)
const;
303 AttributeOffset normal_offset(
bool check=
true)
const;
304 AttributeOffset texcoord0_offset(
bool check=
true)
const;
305 AttributeOffset texcoord1_offset(
bool check=
true)
const;
306 AttributeOffset texcoord2_offset(
bool check=
true)
const;
307 AttributeOffset texcoord3_offset(
bool check=
true)
const;
308 AttributeOffset texcoord4_offset(
bool check=
true)
const;
309 AttributeOffset texcoord5_offset(
bool check=
true)
const;
310 AttributeOffset texcoord6_offset(
bool check=
true)
const;
311 AttributeOffset texcoord7_offset(
bool check=
true)
const;
313 AttributeOffset texcoordX_offset(uint8_t which,
bool check=
true)
const;
315 AttributeOffset diffuse_offset(
bool check=
true)
const;
316 AttributeOffset specular_offset(
bool check=
true)
const;
321 uint16_t stride_ = 0;
323 void recalc_stride_and_offsets();
332 enum MeshArrangement {
333 MESH_ARRANGEMENT_TRIANGLES,
334 MESH_ARRANGEMENT_TRIANGLE_FAN,
335 MESH_ARRANGEMENT_TRIANGLE_STRIP,
336 MESH_ARRANGEMENT_QUADS,
337 MESH_ARRANGEMENT_LINES,
338 MESH_ARRANGEMENT_LINE_STRIP
341 enum AvailablePartitioner {
349 LIGHT_TYPE_DIRECTIONAL,
350 LIGHT_TYPE_SPOT_LIGHT
356 ASPECT_RATIO_16_BY_9,
357 ASPECT_RATIO_16_BY_10
362 enum ProjectionType {
363 PROJECTION_TYPE_PERSPECTIVE,
364 PROJECTION_TYPE_ORTHOGRAPHIC
367 enum BufferClearFlag {
368 BUFFER_CLEAR_COLOUR_BUFFER = 0x1,
369 BUFFER_CLEAR_DEPTH_BUFFER = 0x2,
370 BUFFER_CLEAR_STENCIL_BUFFER = 0x4,
371 BUFFER_CLEAR_ALL = BUFFER_CLEAR_COLOUR_BUFFER | BUFFER_CLEAR_DEPTH_BUFFER | BUFFER_CLEAR_STENCIL_BUFFER
374 typedef int32_t RenderPriority;
375 const RenderPriority RENDER_PRIORITY_MIN = -250;
376 const RenderPriority RENDER_PRIORITY_ABSOLUTE_BACKGROUND = -250;
377 const RenderPriority RENDER_PRIORITY_BACKGROUND = -100;
378 const RenderPriority RENDER_PRIORITY_DISTANT = -50;
379 const RenderPriority RENDER_PRIORITY_MAIN = 0;
380 const RenderPriority RENDER_PRIORITY_NEAR = 50;
381 const RenderPriority RENDER_PRIORITY_FOREGROUND = 100;
382 const RenderPriority RENDER_PRIORITY_ABSOLUTE_FOREGROUND = 250;
383 const RenderPriority RENDER_PRIORITY_MAX = RENDER_PRIORITY_ABSOLUTE_FOREGROUND + 1;
385 extern const std::vector<RenderPriority> RENDER_PRIORITIES;
389 SHADER_TYPE_FRAGMENT,
393 enum VirtualGamepadConfig {
394 VIRTUAL_GAMEPAD_CONFIG_TWO_BUTTONS,
395 VIRTUAL_GAMEPAD_CONFIG_HAT_AND_BUTTON
398 #define DEFAULT_MATERIAL_SCHEME "default"
401 typedef std::weak_ptr<Mesh> MeshRef;
402 typedef std::shared_ptr<Mesh> MeshPtr;
405 typedef SubMesh* SubMeshPtr;
408 typedef std::weak_ptr<Material> MaterialRef;
409 typedef std::shared_ptr<Material> MaterialPtr;
412 typedef std::weak_ptr<Texture> TextureRef;
413 typedef std::shared_ptr<Texture> TexturePtr;
415 class ParticleScript;
416 typedef std::shared_ptr<ParticleScript> ParticleScriptPtr;
419 typedef std::shared_ptr<Binary> BinaryPtr;
422 typedef std::weak_ptr<Sound> SoundRef;
423 typedef std::shared_ptr<Sound> SoundPtr;
426 typedef std::weak_ptr<Font> FontRef;
427 typedef std::shared_ptr<Font> FontPtr;
430 typedef default_init_ptr<Actor> ActorPtr;
433 typedef default_init_ptr<Geom> GeomPtr;
436 typedef default_init_ptr<MeshInstancer> MeshInstancerPtr;
438 class ParticleSystem;
439 typedef default_init_ptr<ParticleSystem> ParticleSystemPtr;
442 typedef default_init_ptr<Sprite> SpritePtr;
445 typedef default_init_ptr<Light> LightPtr;
450 typedef default_init_ptr<Camera> CameraPtr;
455 typedef default_init_ptr<Background> BackgroundPtr;
460 typedef default_init_ptr<Stage> StagePtr;
470 typedef default_init_ptr<Widget> WidgetPtr;
475 typedef std::shared_ptr<AssetManager> ResourceManagerPtr;
478 typedef std::shared_ptr<Compositor> CompositorPtr;
481 typedef default_init_ptr<Pipeline> PipelinePtr;
488 typedef std::shared_ptr<GPUProgram> GPUProgramPtr;
491 typedef default_init_ptr<Skybox> SkyboxPtr;
493 typedef UniqueID<MeshPtr> MeshID;
494 typedef UniqueID<TexturePtr> TextureID;
495 typedef UniqueID<FontPtr> FontID;
496 typedef UniqueID<CameraPtr> CameraID;
497 typedef UniqueID<MaterialPtr> MaterialID;
498 typedef UniqueID<ParticleScriptPtr> ParticleScriptID;
499 typedef UniqueID<BinaryPtr> BinaryID;
501 typedef UniqueID<LightPtr> LightID;
502 typedef UniqueID<StagePtr> StageID;
503 typedef UniqueID<ActorPtr> ActorID;
504 typedef UniqueID<GeomPtr> GeomID;
505 typedef UniqueID<MeshInstancerPtr> MeshInstancerID;
506 typedef UniqueID<SoundPtr> SoundID;
507 typedef UniqueID<SpritePtr> SpriteID;
508 typedef UniqueID<BackgroundPtr> BackgroundID;
509 typedef UniqueID<ParticleSystemPtr> ParticleSystemID;
510 typedef UniqueID<SkyboxPtr> SkyID;
511 typedef UniqueID<GPUProgramPtr> GPUProgramID;
512 typedef UniqueID<ui::WidgetPtr> WidgetID;
515 typedef unsigned long DWORD;
516 typedef DWORD ProcessID;
518 typedef uint32_t ProcessID;
524 static const uint16_t BUFFER_ATTRIBUTE_ALIGNMENT = 4;
527 static const uint16_t BUFFER_STRIDE_ALIGNMENT = 16;
530 constexpr uint16_t round_to_bytes(uint16_t stride, uint16_t bytes) {
531 return ((stride % bytes) == 0) ? stride : stride + bytes - (stride % bytes);
534 constexpr uint16_t vertex_attribute_size(VertexAttribute attr) {
535 return round_to_bytes(
536 (attr == VERTEX_ATTRIBUTE_2F) ?
sizeof(
float) * 2 :
537 (attr == VERTEX_ATTRIBUTE_3F) ?
sizeof(
float) * 3 :
538 (attr == VERTEX_ATTRIBUTE_4F) ?
sizeof(
float) * 4 :
539 (attr == VERTEX_ATTRIBUTE_4UB) ?
sizeof(uint8_t) * 4 :
540 (attr == VERTEX_ATTRIBUTE_PACKED_VEC4_1I) ?
sizeof(uint32_t) : 0,
541 BUFFER_ATTRIBUTE_ALIGNMENT
561 inline void hash_combine(std::size_t& seed, T
const& v)
563 seed ^= std::hash<T>()(v) + 0x9e3779b9 + (seed<<6) + (seed>>2);
567 template <class Tuple, size_t Index = std::tuple_size<Tuple>::value - 1>
570 static void apply(
size_t& seed, Tuple
const& tuple)
572 HashValueImpl<Tuple, Index-1>::apply(seed, tuple);
573 hash_combine(seed, std::get<Index>(tuple));
577 template <
class Tuple>
578 struct HashValueImpl<Tuple,0>
580 static void apply(
size_t& seed, Tuple
const& tuple)
582 hash_combine(seed, std::get<0>(tuple));
587 template <
typename ... TT>
588 struct hash<
std::tuple<TT...>>
591 operator()(std::tuple<TT...>
const& tt)
const
594 HashValueImpl<std::tuple<TT...> >::apply(seed, tt);
601 #define DEFINE_ENUM_HASH(enum_name) \
602 template <> struct hash<enum_name> { \
603 size_t operator() (const enum_name& t) const { return std::hash<size_t>()((size_t) t); } \
607 DEFINE_ENUM_HASH(smlt::MeshArrangement);
608 DEFINE_ENUM_HASH(smlt::IndexType);
610 template<>
struct hash<
smlt::VertexSpecification> {
614 hash_combine(seed, (
unsigned int) spec.position_attribute_);
615 hash_combine(seed, (
unsigned int) spec.normal_attribute_);
616 hash_combine(seed, (
unsigned int) spec.texcoord0_attribute_);
617 hash_combine(seed, (
unsigned int) spec.texcoord1_attribute_);
618 hash_combine(seed, (
unsigned int) spec.texcoord2_attribute_);
619 hash_combine(seed, (
unsigned int) spec.texcoord3_attribute_);
620 hash_combine(seed, (
unsigned int) spec.texcoord4_attribute_);
621 hash_combine(seed, (
unsigned int) spec.texcoord5_attribute_);
622 hash_combine(seed, (
unsigned int) spec.texcoord6_attribute_);
623 hash_combine(seed, (
unsigned int) spec.texcoord7_attribute_);
624 hash_combine(seed, (
unsigned int) spec.diffuse_attribute_);
625 hash_combine(seed, (
unsigned int) spec.specular_attribute_);
632 #endif // TYPES_H_INCLUDED