From 673c2dfe9430d616b4068be5edf3e1b8a8fa0309 Mon Sep 17 00:00:00 2001 From: falsycat Date: Wed, 16 Oct 2019 00:00:00 +0000 Subject: [PATCH] [update] Updated PostEffect that can be shared with else scenes of PlayScene. --- sjplayer/src/sjplayer/Context.d | 16 ++------------- sjplayer/src/sjplayer/PostEffect.d | 12 +++++++++++- sjplayer/src/sjplayer/package.d | 9 +++++---- src/main.d | 2 -- src/sj/AbstractGame.d | 1 - src/sj/Game.d | 31 ++++++++++++++++++++++++++++-- src/sj/LoadingScene.d | 30 ++++++++++++++--------------- src/sj/LobbyWorld.d | 4 +--- src/sj/Music.d | 4 ++-- src/sj/PlayScene.d | 4 ---- 10 files changed, 65 insertions(+), 48 deletions(-) diff --git a/sjplayer/src/sjplayer/Context.d b/sjplayer/src/sjplayer/Context.d index 016cd2c..94b3e88 100644 --- a/sjplayer/src/sjplayer/Context.d +++ b/sjplayer/src/sjplayer/Context.d @@ -28,10 +28,9 @@ import sjplayer.Actor, class Context { public: /// - this(ParametersBlock[] params, vec2i window_size, ProgramSet programs) { + this(ParametersBlock[] params, PostEffect posteffect, ProgramSet programs) { actor_ = new Actor(programs.Get!ActorProgram); background_ = new Background(programs.Get!BackgroundProgram); - posteffect_ = new PostEffect(programs.Get!PostEffectProgram, window_size); auto builder = new Builder; auto varstore = new VarStore(actor_); @@ -47,7 +46,7 @@ class Context { ), tuple( "posteffect", - PostEffectControllerFactory(varstore, posteffect_), + PostEffectControllerFactory(varstore, posteffect), ), tuple( @@ -94,7 +93,6 @@ class Context { drawers_.each!destroy; elements_.each!destroy; - posteffect_.destroy(); background_.destroy(); actor_.destroy(); } @@ -116,10 +114,6 @@ class Context { controllers_.each!(x => x.Operate(time)); } - /// - void StartDrawing() { - posteffect_.BindFramebuffer(); - } /// void DrawBackground() { background_.Draw(); @@ -132,11 +126,6 @@ class Context { void DrawActor() { actor_.Draw(); } - /// - void EndDrawing() { - posteffect_.UnbindFramebuffer(); - posteffect_.DrawFramebuffer(); - } /// @property inout(ActorControllerInterface) actor() inout { @@ -170,7 +159,6 @@ class Context { Actor actor_; Background background_; - PostEffect posteffect_; ElementInterface[] elements_; ElementDrawerInterface[] drawers_; diff --git a/sjplayer/src/sjplayer/PostEffect.d b/sjplayer/src/sjplayer/PostEffect.d index 39038c2..bd2e59f 100644 --- a/sjplayer/src/sjplayer/PostEffect.d +++ b/sjplayer/src/sjplayer/PostEffect.d @@ -28,6 +28,7 @@ class PostEffect { program_ = program; fb_ = Framebuffer.Create(); tex_ = TextureRect.Create(); + depth_ = Renderbuffer.Create(); sampler_ = Sampler.Create(); with (TextureRectAllocator()) { @@ -38,7 +39,11 @@ class PostEffect { data = null; Allocate(tex_); } - + with (RenderbufferAllocator()) { + format = GL_DEPTH_COMPONENT; + size = sz; + Allocate(depth_); + } with (SamplerConfigurer()) { filterMin = GL_NEAREST; filterMag = GL_NEAREST; @@ -48,6 +53,9 @@ class PostEffect { fb_.Bind(); fb_.attachment!(GL_COLOR_ATTACHMENT0, 0, GL_TEXTURE_RECTANGLE) = tex_; fb_.attachmentOrder = [GL_COLOR_ATTACHMENT0]; + + fb_.attachment!GL_DEPTH_ATTACHMENT = depth_; + fb_.Validate(); fb_.Unbind(); } @@ -78,6 +86,8 @@ class PostEffect { TextureRectRef tex_; + RenderbufferRef depth_; + SamplerRef sampler_; } diff --git a/sjplayer/src/sjplayer/package.d b/sjplayer/src/sjplayer/package.d index ae563c8..5f5682c 100644 --- a/sjplayer/src/sjplayer/package.d +++ b/sjplayer/src/sjplayer/package.d @@ -9,16 +9,17 @@ public { import sjscript : ScriptException; import sjplayer.Context, + sjplayer.PostEffect, sjplayer.ProgramSet, sjplayer.ScriptRuntimeException; } /// -Context CreateContextFromText(string src, vec2i window_size, ProgramSet programs) { - return src.CreateScriptAst().CreateContextFromScriptAst(window_size, programs); +Context CreateContextFromText(string src, PostEffect posteffect, ProgramSet programs) { + return src.CreateScriptAst().CreateContextFromScriptAst(posteffect, programs); } /// Context CreateContextFromScriptAst( - ParametersBlock[] params, vec2i window_size, ProgramSet programs) { - return new Context(params, window_size, programs); + ParametersBlock[] params, PostEffect posteffect, ProgramSet programs) { + return new Context(params, posteffect, programs); } diff --git a/src/main.d b/src/main.d index b33624b..ccd7ca9 100644 --- a/src/main.d +++ b/src/main.d @@ -94,8 +94,6 @@ private auto CreateWindow(ref in Args args) { sfWindow_setActive(win, true).enforce; gl.ApplyContext(); - gl.Enable(GL_BLEND); - gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); static struct Window { public: diff --git a/src/sj/AbstractGame.d b/src/sj/AbstractGame.d index 4ae41a3..ec86e92 100644 --- a/src/sj/AbstractGame.d +++ b/src/sj/AbstractGame.d @@ -21,7 +21,6 @@ class AbstractGame { } /// void Draw() { - gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); scene_.Draw(); } diff --git a/src/sj/Game.d b/src/sj/Game.d index ac4fa60..6836b6a 100644 --- a/src/sj/Game.d +++ b/src/sj/Game.d @@ -7,6 +7,10 @@ import std.algorithm, std.json, std.path; +import gl4d; + +static import sjplayer; + import sj.AbstractGame, sj.Args, sj.FontSet, @@ -35,11 +39,15 @@ class Game : AbstractGame { fonts_ = new FontSet; programs_ = new ProgramSet; + posteffect_ = new sjplayer.PostEffect( + programs_.Get!(sjplayer.PostEffectProgram), + vec2i(args.window_size, args.window_size)); + lobby_ = new LobbyWorld(programs_); title_ = new TitleScene(lobby_, programs_); select_ = new SelectScene(lobby_, programs_, fonts_, music_list_); - load_ = new LoadingScene(args, lobby_, programs_, fonts_); + load_ = new LoadingScene(lobby_, posteffect_, programs_, fonts_); play_ = new PlayScene; result_ = new ResultScene(lobby_, programs_, fonts_); @@ -59,6 +67,11 @@ class Game : AbstractGame { title_.Initialize(); super(title_); } + + // setup OpenGL + gl.Enable(GL_BLEND); + gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + gl.Disable(GL_DEPTH_TEST); } ~this() { @@ -69,6 +82,7 @@ class Game : AbstractGame { result_.destroy(); lobby_.destroy(); + posteffect_.destroy(); fonts_.destroy(); programs_.destroy(); @@ -76,13 +90,26 @@ class Game : AbstractGame { music_list_.each!destroy(); } + override void Draw() { + gl.Clear(GL_COLOR_BUFFER_BIT); + + posteffect_.BindFramebuffer(); + gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + super.Draw(); + posteffect_.UnbindFramebuffer(); + + posteffect_.DrawFramebuffer(); + } + private: Music[] music_list_; FontSet fonts_; ProgramSet programs_; - LobbyWorld lobby_; + sjplayer.PostEffect posteffect_; + LobbyWorld lobby_; TitleScene title_; SelectScene select_; diff --git a/src/sj/LoadingScene.d b/src/sj/LoadingScene.d index bfc5789..1130983 100644 --- a/src/sj/LoadingScene.d +++ b/src/sj/LoadingScene.d @@ -3,8 +3,9 @@ module sj.LoadingScene; import gl4d; -import sj.Args, - sj.FontSet, +static import sjplayer; + +import sj.FontSet, sj.KeyInput, sj.LobbyWorld, sj.Music, @@ -17,14 +18,14 @@ class LoadingScene : SceneInterface { public: /// this( - in ref Args args, - LobbyWorld lobby, - ProgramSet programs, - FontSet fonts) { - args_ = args; - lobby_ = lobby; - programs_ = programs; - fonts_ = fonts; + LobbyWorld lobby, + sjplayer.PostEffect posteffect, + ProgramSet programs, + FontSet fonts) { + lobby_ = lobby; + posteffect_ = posteffect; + programs_ = programs; + fonts_ = fonts; } ~this() { } @@ -44,8 +45,7 @@ class LoadingScene : SceneInterface { override SceneInterface Update(KeyInput input) { if (first_drawn_) { // TODO: parallelize context creation - auto context = music_.CreatePlayerContext( - vec2i(args_.window_size, args_.window_size), programs_.player); + auto context = music_.CreatePlayerContext(posteffect_, programs_.player); play_scene_.Initialize(music_, context, offset_beat_); return play_scene_; } @@ -57,9 +57,7 @@ class LoadingScene : SceneInterface { } private: - const Args args_; - - PlayScene play_scene_; + sjplayer.PostEffect posteffect_; ProgramSet programs_; @@ -67,6 +65,8 @@ class LoadingScene : SceneInterface { LobbyWorld lobby_; + PlayScene play_scene_; + Music music_; float offset_beat_; diff --git a/src/sj/LobbyWorld.d b/src/sj/LobbyWorld.d index 9658e8d..a91507a 100644 --- a/src/sj/LobbyWorld.d +++ b/src/sj/LobbyWorld.d @@ -37,15 +37,13 @@ class LobbyWorld { /// void Draw() { - gl.Disable(GL_DEPTH_TEST); - gl.DepthMask(false); background_.Draw(); gl.Enable(GL_DEPTH_TEST); - gl.DepthMask(true); cube_program_.Draw( CreateCubes(cube_matrix.Create(), cube_interval)[], Projection, view.Create(), light_pos, cube_material); + gl.Disable(GL_DEPTH_TEST); } /// diff --git a/src/sj/Music.d b/src/sj/Music.d index 902a855..721cb68 100644 --- a/src/sj/Music.d +++ b/src/sj/Music.d @@ -83,9 +83,9 @@ class Music { /// sjplayer.Context CreatePlayerContext( - vec2i winsz, sjplayer.ProgramSet programs) const { + sjplayer.PostEffect posteffect, sjplayer.ProgramSet programs) const { return sjplayer.CreateContextFromText( - script_path_.readText, winsz, programs); + script_path_.readText, posteffect, programs); } /// diff --git a/src/sj/PlayScene.d b/src/sj/PlayScene.d index 3049aa6..c33ecb3 100644 --- a/src/sj/PlayScene.d +++ b/src/sj/PlayScene.d @@ -74,14 +74,10 @@ class PlayScene : SceneInterface { return this; } override void Draw() { - context_.StartDrawing(); - context_.DrawBackground(); context_.DrawElements(); context_.DrawActor(); - context_.EndDrawing(); - if (beat_ >= context_.length) { context_.destroy(); }