A 3D game engine from scratch.
at main 286 lines 12 kB view raw
1// (c) 2020 Vlad-Stefan Harbuz <vlad@vladh.net> 2 3#include "engine.hpp" 4#include "renderer.hpp" 5#include "mats.hpp" 6#include "shaders.hpp" 7#include "internals.hpp" 8 9 10void 11internals::create_internal_materials() 12{ 13 memory::Pool temp_memory_pool = {}; 14 auto *builtin_textures = renderer::get_builtin_textures(); 15 16 // unknown 17 { 18 mats::Material *material = mats::init_material(mats::push_material(), "unknown"); 19 shaders::init_shader_asset(&material->shader_asset, 20 &temp_memory_pool, 21 "unknown", shaders::Type::standard, 22 "base.vert", "unknown.frag", ""); 23 } 24 25 // lighting 26 { 27 mats::Material *material = mats::init_material(mats::push_material(), "lighting"); 28 shaders::init_shader_asset(&material->shader_asset, 29 &temp_memory_pool, 30 "lighting", shaders::Type::standard, 31 "screenquad.vert", "lighting.frag", ""); 32 mats::add_texture_to_material( 33 material, *builtin_textures->g_position_texture, "g_position_texture"); 34 mats::add_texture_to_material( 35 material, *builtin_textures->g_normal_texture, "g_normal_texture"); 36 mats::add_texture_to_material( 37 material, *builtin_textures->g_albedo_texture, "g_albedo_texture"); 38 mats::add_texture_to_material( 39 material, *builtin_textures->g_pbr_texture, "g_pbr_texture"); 40 mats::add_texture_to_material( 41 material, *builtin_textures->shadowmaps_3d_texture, "shadowmaps_3d"); 42 mats::add_texture_to_material( 43 material, *builtin_textures->shadowmaps_2d_texture, "shadowmaps_2d"); 44 } 45 46 if (SETTINGS.bloom_on) { 47 // preblur 48 { 49 mats::Material *material = mats::init_material(mats::push_material(), "preblur"); 50 shaders::init_shader_asset(&material->shader_asset, 51 &temp_memory_pool, 52 "blur", shaders::Type::standard, 53 "screenquad.vert", "blur.frag", ""); 54 mats::add_texture_to_material( 55 material, *builtin_textures->l_bright_color_texture, "source_texture"); 56 } 57 58 // blur1 59 { 60 mats::Material *material = mats::init_material(mats::push_material(), "blur1"); 61 shaders::init_shader_asset(&material->shader_asset, 62 &temp_memory_pool, 63 "blur", shaders::Type::standard, 64 "screenquad.vert", "blur.frag", ""); 65 mats::add_texture_to_material( 66 material, *builtin_textures->blur2_texture, "source_texture"); 67 } 68 69 // blur2 70 { 71 mats::Material *material = mats::init_material(mats::push_material(), "blur2"); 72 shaders::init_shader_asset(&material->shader_asset, 73 &temp_memory_pool, 74 "blur", shaders::Type::standard, 75 "screenquad.vert", "blur.frag", ""); 76 mats::add_texture_to_material( 77 material, *builtin_textures->blur1_texture, "source_texture"); 78 } 79 } 80 81 // postprocessing 82 { 83 mats::Material *material = mats::init_material(mats::push_material(), "postprocessing"); 84 shaders::init_shader_asset(&material->shader_asset, 85 &temp_memory_pool, 86 "postprocessing", shaders::Type::standard, 87 "screenquad.vert", "postprocessing.frag", ""); 88 mats::add_texture_to_material( 89 material, *builtin_textures->l_color_texture, "l_color_texture"); 90 91 if (SETTINGS.bloom_on) { 92 mats::add_texture_to_material( 93 material, *builtin_textures->blur2_texture, "bloom_texture"); 94 } 95 96 if (SETTINGS.fog_on) { 97 mats::add_texture_to_material( 98 material, *builtin_textures->l_depth_texture, "l_depth_texture"); 99 } 100 } 101 102 // renderdebug 103 { 104 mats::Material *material = mats::init_material(mats::push_material(), "renderdebug"); 105 shaders::init_shader_asset(&material->shader_asset, 106 &temp_memory_pool, 107 "renderdebug", shaders::Type::standard, 108 "screenquad.vert", "renderdebug.frag", ""); 109 110 mats::add_texture_to_material( 111 material, *builtin_textures->g_position_texture, "g_position_texture"); 112 mats::add_texture_to_material( 113 material, *builtin_textures->g_normal_texture, "g_normal_texture"); 114 mats::add_texture_to_material( 115 material, *builtin_textures->g_albedo_texture, "g_albedo_texture"); 116 mats::add_texture_to_material( 117 material, *builtin_textures->g_pbr_texture, "g_pbr_texture"); 118 119 mats::add_texture_to_material( 120 material, *builtin_textures->l_color_texture, "l_color_texture"); 121 mats::add_texture_to_material( 122 material, *builtin_textures->l_bright_color_texture, "l_bright_color_texture"); 123 124 if (SETTINGS.fog_on) { 125 mats::add_texture_to_material( 126 material, *builtin_textures->l_depth_texture, "l_depth_texture"); 127 } 128 129 if (SETTINGS.bloom_on) { 130 mats::add_texture_to_material( 131 material, *builtin_textures->blur1_texture, "blur1_texture"); 132 mats::add_texture_to_material( 133 material, *builtin_textures->blur2_texture, "blur2_texture"); 134 } 135 136 mats::add_texture_to_material( 137 material, *builtin_textures->shadowmaps_3d_texture, "shadowmaps_3d"); 138 mats::add_texture_to_material( 139 material, *builtin_textures->shadowmaps_2d_texture, "shadowmaps_2d"); 140 } 141 142 // skysphere 143 { 144 mats::Material *material = mats::init_material(mats::push_material(), "skysphere"); 145 shaders::init_shader_asset(&material->shader_asset, 146 &temp_memory_pool, 147 "skysphere", shaders::Type::standard, 148 "skysphere.vert", "skysphere.frag", ""); 149 } 150 151 // We've created all internal materials, so we will mark the next position 152 // in the array of materials, so we know where non-internal materials start. 153 mats::mark_start_of_non_internal_materials(); 154 155 memory::destroy_memory_pool(&temp_memory_pool); 156} 157 158 159void 160internals::create_internal_entities() 161{ 162 memory::Pool temp_memory_pool = {}; 163 164 shaders::init_shader_asset(renderer::get_standard_depth_shader_asset(), 165 &temp_memory_pool, "standard_depth", shaders::Type::depth, 166 "standard_depth.vert", "standard_depth.frag", "standard_depth.geom"); 167 168 // Lighting screenquad 169 { 170 entities::Entity *entity = entities::add_entity_to_set("screenquad_lighting"); 171 models::ModelLoader *model_loader = engine::push_model_loader(); 172 models::EntityLoader *entity_loader = engine::get_entity_loader(entity->handle); 173 models::init_model_loader(model_loader, "builtin:screenquad_lighting"); 174 models::init_entity_loader(entity_loader, 175 "screenquad_lighting", 176 "builtin:screenquad_lighting", 177 drawable::Pass::lighting, 178 entity->handle); 179 models::add_material_to_model_loader(model_loader, "lighting"); 180 } 181 182 if (SETTINGS.bloom_on) { 183 // Preblur screenquad 184 { 185 entities::Entity *entity = entities::add_entity_to_set("screenquad_preblur"); 186 models::ModelLoader *model_loader = engine::push_model_loader(); 187 models::EntityLoader *entity_loader = engine::get_entity_loader(entity->handle); 188 models::init_model_loader(model_loader, "builtin:screenquad_preblur"); 189 models::init_entity_loader(entity_loader, 190 "screenquad_preblur", 191 "builtin:screenquad_preblur", 192 drawable::Pass::preblur, 193 entity->handle); 194 models::add_material_to_model_loader(model_loader, "preblur"); 195 } 196 197 // Blur 1 screenquad 198 { 199 entities::Entity *entity = entities::add_entity_to_set("screenquad_blur1"); 200 models::ModelLoader *model_loader = engine::push_model_loader(); 201 models::EntityLoader *entity_loader = engine::get_entity_loader(entity->handle); 202 models::init_model_loader(model_loader, "builtin:screenquad_blur1"); 203 models::init_entity_loader(entity_loader, 204 "screenquad_blur1", 205 "builtin:screenquad_blur1", 206 drawable::Pass::blur1, 207 entity->handle); 208 models::add_material_to_model_loader(model_loader, "blur1"); 209 } 210 211 // Blur 2 screenquad 212 { 213 entities::Entity *entity = entities::add_entity_to_set("screenquad_blur2"); 214 models::ModelLoader *model_loader = engine::push_model_loader(); 215 models::EntityLoader *entity_loader = engine::get_entity_loader(entity->handle); 216 models::init_model_loader(model_loader, "builtin:screenquad_blur2"); 217 models::init_entity_loader(entity_loader, 218 "screenquad_blur2", 219 "builtin:screenquad_blur2", 220 drawable::Pass::blur2, 221 entity->handle); 222 models::add_material_to_model_loader(model_loader, "blur2"); 223 } 224 } 225 226 // Postprocessing screenquad 227 { 228 entities::Entity *entity = entities::add_entity_to_set("screenquad_postprocessing"); 229 models::ModelLoader *model_loader = engine::push_model_loader(); 230 models::EntityLoader *entity_loader = engine::get_entity_loader(entity->handle); 231 models::init_model_loader(model_loader, "builtin:screenquad_postprocessing"); 232 models::init_entity_loader(entity_loader, 233 "screenquad_postprocessing", 234 "builtin:screenquad_postprocessing", 235 drawable::Pass::postprocessing, 236 entity->handle); 237 models::add_material_to_model_loader(model_loader, "postprocessing"); 238 } 239 240 // Debug screenquad 241 { 242 entities::Entity *entity = entities::add_entity_to_set("screenquad_renderdebug"); 243 models::ModelLoader *model_loader = engine::push_model_loader(); 244 models::EntityLoader *entity_loader = engine::get_entity_loader(entity->handle); 245 models::init_model_loader(model_loader, "builtin:screenquad_renderdebug"); 246 models::init_entity_loader(entity_loader, 247 "screenquad_renderdebug", 248 "builtin:screenquad_renderdebug", 249 drawable::Pass::renderdebug, 250 entity->handle); 251 models::add_material_to_model_loader(model_loader, "renderdebug"); 252 } 253 254 // Skysphere 255 { 256 entities::Entity *entity = entities::add_entity_to_set("skysphere"); 257 models::ModelLoader *model_loader = engine::push_model_loader(); 258 models::EntityLoader *entity_loader = engine::get_entity_loader(entity->handle); 259 models::init_model_loader(model_loader, "builtin:skysphere"); 260 models::init_entity_loader(entity_loader, 261 "skysphere", 262 "builtin:skysphere", 263 drawable::Pass::forward_skybox, 264 entity->handle); 265 entity_loader->spatial_component = { 266 .entity_handle = entity->handle, 267 .position = v3(0.0f), 268 .rotation = glm::angleAxis(radians(0.0f), v3(1.0f, 0.0f, 0.0f)), 269 .scale = v3(75.0f), 270 }; 271 models::add_material_to_model_loader(model_loader, "skysphere"); 272 } 273 274 // We've created all internal entities, so we will mark the next position 275 // in the entities::Set, to know that that position is where the non-internal 276 // entities start. 277 entities::mark_first_non_internal_handle(); 278 279 memory::destroy_memory_pool(&temp_memory_pool); 280} 281 282 283void internals::init() { 284 create_internal_materials(); 285 create_internal_entities(); 286}