From 924fa2413a18c06d9f1978dc717270fb4d3e55d1 Mon Sep 17 00:00:00 2001 From: Alberto Torres Date: Wed, 21 Aug 2024 19:22:26 +0200 Subject: [PATCH] Register loaders and add engine.loadScene that chooses the appropriate loader. --- src/loaders/blend.nim | 20 +++++++++++++++----- src/loaders/loader_base.nim | 17 +++++++++++++---- src/myou_engine.nim | 18 ++++++++++++++++++ src/objects/cubemap_probe.nim | 10 ++++++++++ src/types.nim | 24 +++++++++++------------- 5 files changed, 67 insertions(+), 22 deletions(-) diff --git a/src/loaders/blend.nim b/src/loaders/blend.nim index b849b56..c0cea54 100644 --- a/src/loaders/blend.nim +++ b/src/loaders/blend.nim @@ -33,8 +33,11 @@ {.warning[UseBase]: off.} # can't seem to satisy this... {.warning[UnusedImport]: off.} # for ./loader_base +const myouUseBlendLoader {.booldefine.} = true + import ../types import ./loader_base +export loader_base import std/tables import std/strutils import std/strformat @@ -66,8 +69,9 @@ import float16 when defined(nimdoc): type TYPES* = BlendLoader -proc newBlendLoader*(engine: MyouEngine, use_indices:bool = true, - shader_library: string = "", shader_textures = initTable[string, Texture]()): BlendLoader = +proc newBlendLoader*(engine: MyouEngine, + shader_library: string = "", + shader_textures = initTable[string, Texture]()): BlendLoader = result = new BlendLoader result.engine = engine if shader_library != "": @@ -76,8 +80,13 @@ proc newBlendLoader*(engine: MyouEngine, use_indices:bool = true, else: result.shader_library = get_builtin_shader_library() result.shader_textures = get_builtin_shader_textures() - result.file_extensions = @["blend"] - result.use_indices = use_indices + result.use_indices = true + +proc registerBlendLoader*(engine: MyouEngine) = + when myouUseBlendLoader: + engine.registerLoader(@["blend"], proc(e: MyouEngine): Loader = + e.newBlendLoader() + ) var used_resources: seq[LoadableResource] @@ -583,4 +592,5 @@ method loadScene*(self: BlendLoader, name: string="", scene: Scene=nil, callback assert false, &"Scene {name} not found" proc debug_dump*(self: BlendLoader) = - self.blend_file.debug_dump \ No newline at end of file + self.blend_file.debug_dump + diff --git a/src/loaders/loader_base.nim b/src/loaders/loader_base.nim index bd66710..e767304 100644 --- a/src/loaders/loader_base.nim +++ b/src/loaders/loader_base.nim @@ -31,13 +31,22 @@ # version of this file under either the CPAL or the [AGPL-3] License. import ../types +import std/tables when defined(nimdoc): type TYPES* = Loader -method openAssetFile*(self: Loader, path: string) {.base, locks: "unknown".} = +method openAssetFile*(self: Loader, path: string) {.base.} = discard -method loadScene*(self: Loader, name: string="", scene: Scene=nil): Scene {.base, locks: "unknown".} = +method loadScene*(self: Loader, name: string="", scene: Scene=nil, + callback: proc(scene: Scene)) {.base.} = discard -method loadImageImpl*(self: Loader) {.base, locks: "unknown".} = - discard \ No newline at end of file +method loadImageImpl*(self: Loader) {.base.} = + discard + +proc registerLoader*(engine: MyouEngine, + extensions: seq[string], + constructor: proc(e: MyouEngine): Loader) = + + for ext in extensions: + engine.loaders_by_ext.mgetOrPut(ext, @[]).add constructor diff --git a/src/myou_engine.nim b/src/myou_engine.nim index 931ccce..08c5565 100644 --- a/src/myou_engine.nim +++ b/src/myou_engine.nim @@ -74,6 +74,7 @@ import std/monotimes import ./graphics/render import ./screen import ./platform/platform +import ./loaders/blend import ./util import arr_ref @@ -129,6 +130,8 @@ proc newMyouEngine*( when not defined(nimdoc): init_graphics(result, width, height, title, opengl_version, opengl_es) discard result.newScreen(width, height, title) + + registerBlendLoader(result) proc get_builtin_shader_library*(use_cubemap_prefiltering = true): string = ## Returns a string with the code of the default shader library of the @@ -179,3 +182,18 @@ proc run*(self: MyouEngine) = last_time = getmonotime().ticks.float/1000000000 when not defined(nimdoc): start_platform_main_loop(self, myou_main_loop) + +proc loadScene*(self: MyouEngine, uri: string, callback: proc(scene: Scene), name = "", ext = "") = + let ext = if ext == "": + uri.rsplit('.', 1)[1] + else: + ext + + if ext notin self.loaders_by_ext: + raise ValueError.newException "File extension not supported: " & ext + + # TODO: use the next loader if the first one fails + let loaders = self.loaders_by_ext[ext] + let loader = loaders[0](self) + loader.openAssetFile(uri) + loader.loadScene(name, nil, callback) diff --git a/src/objects/cubemap_probe.nim b/src/objects/cubemap_probe.nim index 8844516..340faee 100644 --- a/src/objects/cubemap_probe.nim +++ b/src/objects/cubemap_probe.nim @@ -38,6 +38,16 @@ when defined(nimdoc): type TYPES* = ProbeInfluenceType | ProbeParallaxType | CubemapProbe | CubemapProbeUniform | SH9Uniform # Forward declarations and ob type methods +proc newCubemapProbe*(engine: MyouEngine, name: string="camera", scene: Scene = nil, + influence_type: ProbeInfluenceType = SphereInfluence, + influence_distance: float32 = 2.5, + falloff: float32 = 0.2, + intensity: float32 = 1, + clipping_start: float32 = 0.8, + clipping_end: float32 = 40, + parallax_type: ProbeParallaxType = NoParallax, + parallax_distance: float32 = 0.0, # 0 means auto + ): CubemapProbe func getCubemapSideMatrix*(side: int, position = vec3()): Mat4 proc render_cubemap*(self: CubemapProbe, use_roughness_prefiltering = false, mipmap_shader: Material = nil) proc render_background_cubemap*(scene: Scene, use_roughness_prefiltering = false, mipmap_shader: Material = nil, world_to_cube_matrix: Mat4 = mat4(), upload_UBO = true) diff --git a/src/types.nim b/src/types.nim index 5c92299..e3bb9bf 100644 --- a/src/types.nim +++ b/src/types.nim @@ -81,6 +81,7 @@ type renderer*: RenderManager tone_mapping_library*: string tone_mapping_function*: string + loaders_by_ext*: Table[string, seq[proc(e: MyouEngine): Loader]] # TODO: remove this and query it when screen is created width*, height*: int glsl_version*: string @@ -738,6 +739,16 @@ type influence_distance*: float32 falloff*: float32 + # loader_base.nim + + Loader* = ref object of RootObj + engine*: MyouEngine + shader_library*: string + shader_textures*: Table[string, Texture] + # on_destroy*: OnDestroy + path_handler*: proc(path: string): string + override_textures_sampler_type*: Table[string, string] + # INCOMPLETE Armature* = ref object of GameObject @@ -806,15 +817,6 @@ const HARDCODED_MAXUBOS* = 80 # LOADERS -type Loader* = ref object of RootObj - engine*: MyouEngine - file_extensions*: seq[string] - shader_library*: string - shader_textures*: Table[string, Texture] - # on_destroy*: OnDestroy - path_handler*: proc(path: string): string - override_textures_sampler_type*: Table[string, string] - type BlendLoader* = ref object of Loader blend_file_path*: string ## private blend_file*: BlendFile ## private @@ -823,10 +825,6 @@ type BlendLoader* = ref object of Loader (string, seq[Varying], OrderedTable[string, string], OrderedTable[string, TexturePixels])] ## private resource*: LoadableResource -template AllLoaders*: untyped = @[ - newBlendLoader, -] - template enqueue*(self: RenderManager, fun: proc()) = if self.initialized: fun()