Simulant  21.12-574
A portable game engine for Windows, OSX, Linux, Dreamcast, and PSP
core_material.h
1 #pragma once
2 
3 #include "../constants.h"
4 #include "../property_value.h"
5 
6 namespace smlt {
7 
8 typedef uint32_t MaterialPropertyNameHash;
9 
10 unsigned constexpr material_property_hash(char const *input) {
11  return *input ?
12  static_cast<unsigned int>(*input) + 33 * material_property_hash(input + 1) :
13  5381;
14 }
15 
16 constexpr const MaterialPropertyNameHash DIFFUSE_PROPERTY_HASH = material_property_hash(DIFFUSE_PROPERTY_NAME);
17 constexpr const MaterialPropertyNameHash AMBIENT_PROPERTY_HASH = material_property_hash(AMBIENT_PROPERTY_NAME);
18 constexpr const MaterialPropertyNameHash SPECULAR_PROPERTY_HASH = material_property_hash(SPECULAR_PROPERTY_NAME);
19 constexpr const MaterialPropertyNameHash EMISSION_PROPERTY_HASH = material_property_hash(EMISSION_PROPERTY_NAME);
20 constexpr const MaterialPropertyNameHash SHININESS_PROPERTY_HASH = material_property_hash(SHININESS_PROPERTY_NAME);
21 constexpr const MaterialPropertyNameHash POINT_SIZE_PROPERTY_HASH = material_property_hash(POINT_SIZE_PROPERTY_NAME);
22 
23 constexpr const MaterialPropertyNameHash DEPTH_WRITE_ENABLED_PROPERTY_HASH = material_property_hash(DEPTH_WRITE_ENABLED_PROPERTY_NAME);
24 constexpr const MaterialPropertyNameHash DEPTH_TEST_ENABLED_PROPERTY_HASH = material_property_hash(DEPTH_TEST_ENABLED_PROPERTY_NAME);
25 constexpr const MaterialPropertyNameHash LIGHTING_ENABLED_PROPERTY_HASH = material_property_hash(LIGHTING_ENABLED_PROPERTY_NAME);
26 constexpr const MaterialPropertyNameHash TEXTURES_ENABLED_PROPERTY_HASH = material_property_hash(TEXTURES_ENABLED_PROPERTY_NAME);
27 
28 constexpr const MaterialPropertyNameHash DIFFUSE_MAP_PROPERTY_HASH = material_property_hash(DIFFUSE_MAP_PROPERTY_NAME);
29 constexpr const MaterialPropertyNameHash LIGHT_MAP_PROPERTY_HASH = material_property_hash(LIGHT_MAP_PROPERTY_NAME);
30 constexpr const MaterialPropertyNameHash NORMAL_MAP_PROPERTY_HASH = material_property_hash(NORMAL_MAP_PROPERTY_NAME);
31 constexpr const MaterialPropertyNameHash SPECULAR_MAP_PROPERTY_HASH = material_property_hash(SPECULAR_MAP_PROPERTY_NAME);
32 
33 constexpr const MaterialPropertyNameHash DIFFUSE_MAP_MATRIX_PROPERTY_HASH = material_property_hash(DIFFUSE_MAP_MATRIX_PROPERTY_NAME);
34 constexpr const MaterialPropertyNameHash LIGHT_MAP_MATRIX_PROPERTY_HASH = material_property_hash(LIGHT_MAP_MATRIX_PROPERTY_NAME);
35 constexpr const MaterialPropertyNameHash NORMAL_MAP_MATRIX_PROPERTY_HASH = material_property_hash(NORMAL_MAP_MATRIX_PROPERTY_NAME);
36 constexpr const MaterialPropertyNameHash SPECULAR_MAP_MATRIX_PROPERTY_HASH = material_property_hash(SPECULAR_MAP_MATRIX_PROPERTY_NAME);
37 
38 constexpr const MaterialPropertyNameHash BLEND_FUNC_PROPERTY_HASH = material_property_hash(BLEND_FUNC_PROPERTY_NAME);
39 constexpr const MaterialPropertyNameHash POLYGON_MODE_PROPERTY_HASH = material_property_hash(POLYGON_MODE_PROPERTY_NAME);
40 constexpr const MaterialPropertyNameHash SHADE_MODEL_PROPERTY_HASH = material_property_hash(SHADE_MODEL_PROPERTY_NAME);
41 constexpr const MaterialPropertyNameHash COLOUR_MATERIAL_PROPERTY_HASH = material_property_hash(COLOUR_MATERIAL_PROPERTY_NAME);
42 constexpr const MaterialPropertyNameHash CULL_MODE_PROPERTY_HASH = material_property_hash(CULL_MODE_PROPERTY_NAME);
43 constexpr const MaterialPropertyNameHash DEPTH_FUNC_PROPERTY_HASH = material_property_hash(DEPTH_FUNC_PROPERTY_NAME);
44 
45 constexpr const MaterialPropertyNameHash FOG_MODE_PROPERTY_HASH = material_property_hash(FOG_MODE_PROPERTY_NAME);
46 constexpr const MaterialPropertyNameHash FOG_DENSITY_PROPERTY_HASH = material_property_hash(FOG_DENSITY_PROPERTY_NAME);
47 constexpr const MaterialPropertyNameHash FOG_START_PROPERTY_HASH = material_property_hash(FOG_START_PROPERTY_NAME);
48 constexpr const MaterialPropertyNameHash FOG_END_PROPERTY_HASH = material_property_hash(FOG_END_PROPERTY_NAME);
49 constexpr const MaterialPropertyNameHash FOG_COLOUR_PROPERTY_HASH = material_property_hash(FOG_COLOUR_PROPERTY_NAME);
50 
51 inline constexpr bool is_core_property(const MaterialPropertyNameHash hsh) {
52  switch(hsh) {
53  case DIFFUSE_PROPERTY_HASH:
54  case AMBIENT_PROPERTY_HASH:
55  case EMISSION_PROPERTY_HASH:
56  case SPECULAR_PROPERTY_HASH:
57  case SHININESS_PROPERTY_HASH:
58  case POINT_SIZE_PROPERTY_HASH:
59  case DEPTH_WRITE_ENABLED_PROPERTY_HASH:
60  case DEPTH_TEST_ENABLED_PROPERTY_HASH:
61  case LIGHTING_ENABLED_PROPERTY_HASH:
62  case TEXTURES_ENABLED_PROPERTY_HASH:
63  case DIFFUSE_MAP_PROPERTY_HASH:
64  case SPECULAR_MAP_PROPERTY_HASH:
65  case LIGHT_MAP_PROPERTY_HASH:
66  case NORMAL_MAP_PROPERTY_HASH:
67  case DIFFUSE_MAP_MATRIX_PROPERTY_HASH:
68  case SPECULAR_MAP_MATRIX_PROPERTY_HASH:
69  case LIGHT_MAP_MATRIX_PROPERTY_HASH:
70  case NORMAL_MAP_MATRIX_PROPERTY_HASH:
71  case BLEND_FUNC_PROPERTY_HASH:
72  case POLYGON_MODE_PROPERTY_HASH:
73  case SHADE_MODEL_PROPERTY_HASH:
74  case COLOUR_MATERIAL_PROPERTY_HASH:
75  case CULL_MODE_PROPERTY_HASH:
76  case FOG_COLOUR_PROPERTY_HASH:
77  case FOG_DENSITY_PROPERTY_HASH:
78  case FOG_START_PROPERTY_HASH:
79  case FOG_END_PROPERTY_HASH:
80  case FOG_MODE_PROPERTY_HASH:
81  return true;
82  default:
83  return false;
84  }
85 }
86 
87 struct CoreMaterial {
88  const Colour diffuse = Colour(1, 1, 1, 1);
89  const Colour ambient = Colour(1, 1, 1, 1);
90  const Colour emission = Colour(0, 0, 0, 1);
91  const Colour specular = Colour(0, 0, 0, 1);
92 
93  const float shininess = 0.0f;
94  const float point_size = 1.0f;
95 
96  const bool depth_writes_enabled = true;
97  const bool depth_test_enabled = true;
98 
99  const bool lighting_enabled = true;
100  const int32_t textures_enabled = DIFFUSE_MAP_ENABLED | LIGHT_MAP_ENABLED | SPECULAR_MAP_ENABLED | NORMAL_MAP_ENABLED; /* Enable first 4 texture units */
101 
102  TexturePtr diffuse_map;
103  TexturePtr light_map;
104  TexturePtr normal_map;
105  TexturePtr specular_map;
106 
107  const Mat4 diffuse_map_matrix;
108  const Mat4 specular_map_matrix;
109  const Mat4 light_map_matrix;
110  const Mat4 normal_map_matrix;
111 
112  const EnumType blend_func = (EnumType) BLEND_NONE;
113  const EnumType polygon_mode = (EnumType) POLYGON_MODE_FILL;
114  const EnumType shade_model = (EnumType) SHADE_MODEL_SMOOTH;
115  const EnumType colour_material = (EnumType) COLOUR_MATERIAL_NONE;
116  const EnumType cull_mode = (EnumType) CULL_MODE_NONE;
117  const EnumType depth_func = (EnumType) DEPTH_FUNC_LEQUAL;
118 
119  const EnumType fog_mode = (EnumType) FOG_MODE_NONE;
120  const float fog_density = 1.0f;
121  const float fog_start = 100.0f;
122  const float fog_end = 1000.0f;
123  const Colour fog_colour = smlt::Colour::WHITE;
124 };
125 
126 void init_core_material(const CoreMaterial& base);
127 const CoreMaterial& core_material();
128 
129 typedef std::vector<std::pair<std::string, MaterialPropertyType>> PropertyList;
130 
131 const PropertyList &core_properties();
132 
133 bool is_core_property(const char* name);
134 
135 bool core_property_type(const char* name, MaterialPropertyType* type);
136 bool core_property_type(MaterialPropertyNameHash hsh, MaterialPropertyType* type);
137 
138 bool core_material_property_value(const char* name, const Colour*& out);
139 bool core_material_property_value(const MaterialPropertyNameHash hsh, const Colour*& out);
140 
141 bool core_material_property_value(const char* name, const float *&out);
142 bool core_material_property_value(const MaterialPropertyNameHash hsh, const float*& out);
143 
144 bool core_material_property_value(const char* name, const int32_t*&);
145 bool core_material_property_value(const MaterialPropertyNameHash hsh, const int32_t*& out);
146 
147 bool core_material_property_value(const char* name, const bool*& out);
148 bool core_material_property_value(const MaterialPropertyNameHash hsh, const bool*& out);
149 
150 bool core_material_property_value(const char* name, const Vec2*& out);
151 bool core_material_property_value(const MaterialPropertyNameHash hsh, const Vec2*& out);
152 
153 bool core_material_property_value(const char* name, const Vec3*& out);
154 bool core_material_property_value(const MaterialPropertyNameHash hsh, const Vec3*& out);
155 
156 bool core_material_property_value(const char* name, const Vec4*& out);
157 bool core_material_property_value(const MaterialPropertyNameHash hsh, const Vec4*& out);
158 
159 bool core_material_property_value(const char* name, const Mat3*& out);
160 bool core_material_property_value(const MaterialPropertyNameHash hsh, const Mat3*& out);
161 
162 bool core_material_property_value(const char* name, const Mat4*& out);
163 bool core_material_property_value(const MaterialPropertyNameHash hsh, const Mat4*& out);
164 
165 bool core_material_property_value(const char* name, const TexturePtr*& out);
166 bool core_material_property_value(const MaterialPropertyNameHash hsh, const TexturePtr*& out);
167 
168 }
smlt::CoreMaterial
Definition: core_material.h:87
smlt::Mat4
Definition: mat4.h:29
smlt::Vec3
Definition: vec3.h:23
smlt::Mat3
Definition: mat3.h:13
smlt
Definition: animation.cpp:25
smlt::Colour
Definition: colour.h:29
smlt::Vec4
Definition: vec4.h:12
smlt::Vec2
Definition: vec2.h:14