[update] Updated PostEffect that can be shared with else scenes of PlayScene.

This commit is contained in:
falsycat 2019-10-16 00:00:00 +00:00
parent d3026b1fb7
commit 673c2dfe94
10 changed files with 65 additions and 48 deletions

View File

@ -28,10 +28,9 @@ import sjplayer.Actor,
class Context { class Context {
public: public:
/// ///
this(ParametersBlock[] params, vec2i window_size, ProgramSet programs) { this(ParametersBlock[] params, PostEffect posteffect, ProgramSet programs) {
actor_ = new Actor(programs.Get!ActorProgram); actor_ = new Actor(programs.Get!ActorProgram);
background_ = new Background(programs.Get!BackgroundProgram); background_ = new Background(programs.Get!BackgroundProgram);
posteffect_ = new PostEffect(programs.Get!PostEffectProgram, window_size);
auto builder = new Builder; auto builder = new Builder;
auto varstore = new VarStore(actor_); auto varstore = new VarStore(actor_);
@ -47,7 +46,7 @@ class Context {
), ),
tuple( tuple(
"posteffect", "posteffect",
PostEffectControllerFactory(varstore, posteffect_), PostEffectControllerFactory(varstore, posteffect),
), ),
tuple( tuple(
@ -94,7 +93,6 @@ class Context {
drawers_.each!destroy; drawers_.each!destroy;
elements_.each!destroy; elements_.each!destroy;
posteffect_.destroy();
background_.destroy(); background_.destroy();
actor_.destroy(); actor_.destroy();
} }
@ -116,10 +114,6 @@ class Context {
controllers_.each!(x => x.Operate(time)); controllers_.each!(x => x.Operate(time));
} }
///
void StartDrawing() {
posteffect_.BindFramebuffer();
}
/// ///
void DrawBackground() { void DrawBackground() {
background_.Draw(); background_.Draw();
@ -132,11 +126,6 @@ class Context {
void DrawActor() { void DrawActor() {
actor_.Draw(); actor_.Draw();
} }
///
void EndDrawing() {
posteffect_.UnbindFramebuffer();
posteffect_.DrawFramebuffer();
}
/// ///
@property inout(ActorControllerInterface) actor() inout { @property inout(ActorControllerInterface) actor() inout {
@ -170,7 +159,6 @@ class Context {
Actor actor_; Actor actor_;
Background background_; Background background_;
PostEffect posteffect_;
ElementInterface[] elements_; ElementInterface[] elements_;
ElementDrawerInterface[] drawers_; ElementDrawerInterface[] drawers_;

View File

@ -28,6 +28,7 @@ class PostEffect {
program_ = program; program_ = program;
fb_ = Framebuffer.Create(); fb_ = Framebuffer.Create();
tex_ = TextureRect.Create(); tex_ = TextureRect.Create();
depth_ = Renderbuffer.Create();
sampler_ = Sampler.Create(); sampler_ = Sampler.Create();
with (TextureRectAllocator()) { with (TextureRectAllocator()) {
@ -38,7 +39,11 @@ class PostEffect {
data = null; data = null;
Allocate(tex_); Allocate(tex_);
} }
with (RenderbufferAllocator()) {
format = GL_DEPTH_COMPONENT;
size = sz;
Allocate(depth_);
}
with (SamplerConfigurer()) { with (SamplerConfigurer()) {
filterMin = GL_NEAREST; filterMin = GL_NEAREST;
filterMag = GL_NEAREST; filterMag = GL_NEAREST;
@ -48,6 +53,9 @@ class PostEffect {
fb_.Bind(); fb_.Bind();
fb_.attachment!(GL_COLOR_ATTACHMENT0, 0, GL_TEXTURE_RECTANGLE) = tex_; fb_.attachment!(GL_COLOR_ATTACHMENT0, 0, GL_TEXTURE_RECTANGLE) = tex_;
fb_.attachmentOrder = [GL_COLOR_ATTACHMENT0]; fb_.attachmentOrder = [GL_COLOR_ATTACHMENT0];
fb_.attachment!GL_DEPTH_ATTACHMENT = depth_;
fb_.Validate(); fb_.Validate();
fb_.Unbind(); fb_.Unbind();
} }
@ -78,6 +86,8 @@ class PostEffect {
TextureRectRef tex_; TextureRectRef tex_;
RenderbufferRef depth_;
SamplerRef sampler_; SamplerRef sampler_;
} }

View File

@ -9,16 +9,17 @@ public {
import sjscript : ScriptException; import sjscript : ScriptException;
import sjplayer.Context, import sjplayer.Context,
sjplayer.PostEffect,
sjplayer.ProgramSet, sjplayer.ProgramSet,
sjplayer.ScriptRuntimeException; sjplayer.ScriptRuntimeException;
} }
/// ///
Context CreateContextFromText(string src, vec2i window_size, ProgramSet programs) { Context CreateContextFromText(string src, PostEffect posteffect, ProgramSet programs) {
return src.CreateScriptAst().CreateContextFromScriptAst(window_size, programs); return src.CreateScriptAst().CreateContextFromScriptAst(posteffect, programs);
} }
/// ///
Context CreateContextFromScriptAst( Context CreateContextFromScriptAst(
ParametersBlock[] params, vec2i window_size, ProgramSet programs) { ParametersBlock[] params, PostEffect posteffect, ProgramSet programs) {
return new Context(params, window_size, programs); return new Context(params, posteffect, programs);
} }

View File

@ -94,8 +94,6 @@ private auto CreateWindow(ref in Args args) {
sfWindow_setActive(win, true).enforce; sfWindow_setActive(win, true).enforce;
gl.ApplyContext(); gl.ApplyContext();
gl.Enable(GL_BLEND);
gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
static struct Window { static struct Window {
public: public:

View File

@ -21,7 +21,6 @@ class AbstractGame {
} }
/// ///
void Draw() { void Draw() {
gl.Clear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
scene_.Draw(); scene_.Draw();
} }

View File

@ -7,6 +7,10 @@ import std.algorithm,
std.json, std.json,
std.path; std.path;
import gl4d;
static import sjplayer;
import sj.AbstractGame, import sj.AbstractGame,
sj.Args, sj.Args,
sj.FontSet, sj.FontSet,
@ -35,11 +39,15 @@ class Game : AbstractGame {
fonts_ = new FontSet; fonts_ = new FontSet;
programs_ = new ProgramSet; programs_ = new ProgramSet;
posteffect_ = new sjplayer.PostEffect(
programs_.Get!(sjplayer.PostEffectProgram),
vec2i(args.window_size, args.window_size));
lobby_ = new LobbyWorld(programs_); lobby_ = new LobbyWorld(programs_);
title_ = new TitleScene(lobby_, programs_); title_ = new TitleScene(lobby_, programs_);
select_ = new SelectScene(lobby_, programs_, fonts_, music_list_); 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; play_ = new PlayScene;
result_ = new ResultScene(lobby_, programs_, fonts_); result_ = new ResultScene(lobby_, programs_, fonts_);
@ -59,6 +67,11 @@ class Game : AbstractGame {
title_.Initialize(); title_.Initialize();
super(title_); super(title_);
} }
// setup OpenGL
gl.Enable(GL_BLEND);
gl.BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gl.Disable(GL_DEPTH_TEST);
} }
~this() { ~this() {
@ -69,6 +82,7 @@ class Game : AbstractGame {
result_.destroy(); result_.destroy();
lobby_.destroy(); lobby_.destroy();
posteffect_.destroy();
fonts_.destroy(); fonts_.destroy();
programs_.destroy(); programs_.destroy();
@ -76,12 +90,25 @@ class Game : AbstractGame {
music_list_.each!destroy(); 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: private:
Music[] music_list_; Music[] music_list_;
FontSet fonts_; FontSet fonts_;
ProgramSet programs_; ProgramSet programs_;
sjplayer.PostEffect posteffect_;
LobbyWorld lobby_; LobbyWorld lobby_;
TitleScene title_; TitleScene title_;

View File

@ -3,8 +3,9 @@ module sj.LoadingScene;
import gl4d; import gl4d;
import sj.Args, static import sjplayer;
sj.FontSet,
import sj.FontSet,
sj.KeyInput, sj.KeyInput,
sj.LobbyWorld, sj.LobbyWorld,
sj.Music, sj.Music,
@ -17,12 +18,12 @@ class LoadingScene : SceneInterface {
public: public:
/// ///
this( this(
in ref Args args,
LobbyWorld lobby, LobbyWorld lobby,
sjplayer.PostEffect posteffect,
ProgramSet programs, ProgramSet programs,
FontSet fonts) { FontSet fonts) {
args_ = args;
lobby_ = lobby; lobby_ = lobby;
posteffect_ = posteffect;
programs_ = programs; programs_ = programs;
fonts_ = fonts; fonts_ = fonts;
} }
@ -44,8 +45,7 @@ class LoadingScene : SceneInterface {
override SceneInterface Update(KeyInput input) { override SceneInterface Update(KeyInput input) {
if (first_drawn_) { if (first_drawn_) {
// TODO: parallelize context creation // TODO: parallelize context creation
auto context = music_.CreatePlayerContext( auto context = music_.CreatePlayerContext(posteffect_, programs_.player);
vec2i(args_.window_size, args_.window_size), programs_.player);
play_scene_.Initialize(music_, context, offset_beat_); play_scene_.Initialize(music_, context, offset_beat_);
return play_scene_; return play_scene_;
} }
@ -57,9 +57,7 @@ class LoadingScene : SceneInterface {
} }
private: private:
const Args args_; sjplayer.PostEffect posteffect_;
PlayScene play_scene_;
ProgramSet programs_; ProgramSet programs_;
@ -67,6 +65,8 @@ class LoadingScene : SceneInterface {
LobbyWorld lobby_; LobbyWorld lobby_;
PlayScene play_scene_;
Music music_; Music music_;
float offset_beat_; float offset_beat_;

View File

@ -37,15 +37,13 @@ class LobbyWorld {
/// ///
void Draw() { void Draw() {
gl.Disable(GL_DEPTH_TEST);
gl.DepthMask(false);
background_.Draw(); background_.Draw();
gl.Enable(GL_DEPTH_TEST); gl.Enable(GL_DEPTH_TEST);
gl.DepthMask(true);
cube_program_.Draw( cube_program_.Draw(
CreateCubes(cube_matrix.Create(), cube_interval)[], CreateCubes(cube_matrix.Create(), cube_interval)[],
Projection, view.Create(), light_pos, cube_material); Projection, view.Create(), light_pos, cube_material);
gl.Disable(GL_DEPTH_TEST);
} }
/// ///

View File

@ -83,9 +83,9 @@ class Music {
/// ///
sjplayer.Context CreatePlayerContext( sjplayer.Context CreatePlayerContext(
vec2i winsz, sjplayer.ProgramSet programs) const { sjplayer.PostEffect posteffect, sjplayer.ProgramSet programs) const {
return sjplayer.CreateContextFromText( return sjplayer.CreateContextFromText(
script_path_.readText, winsz, programs); script_path_.readText, posteffect, programs);
} }
/// ///

View File

@ -74,14 +74,10 @@ class PlayScene : SceneInterface {
return this; return this;
} }
override void Draw() { override void Draw() {
context_.StartDrawing();
context_.DrawBackground(); context_.DrawBackground();
context_.DrawElements(); context_.DrawElements();
context_.DrawActor(); context_.DrawActor();
context_.EndDrawing();
if (beat_ >= context_.length) { if (beat_ >= context_.length) {
context_.destroy(); context_.destroy();
} }