From e3e149f4513a5f95ea5c15031257b06bf945b6b1 Mon Sep 17 00:00:00 2001 From: falsycat Date: Sat, 12 Oct 2019 00:00:00 +0000 Subject: [PATCH] [add] Added TitleTextProgram. --- src/sj/ProgramSet.d | 4 +- src/sj/TitleTextProgram.d | 97 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 src/sj/TitleTextProgram.d diff --git a/src/sj/ProgramSet.d b/src/sj/ProgramSet.d index cb391ad..6d09718 100644 --- a/src/sj/ProgramSet.d +++ b/src/sj/ProgramSet.d @@ -6,13 +6,15 @@ import std.meta, static import sjplayer = sjplayer.ProgramSet; -import sj.CubeProgram; +import sj.CubeProgram, + sj.TitleTextProgram; /// class ProgramSet { public: alias Programs = Tuple!( CubeProgram, + TitleTextProgram, ); /// diff --git a/src/sj/TitleTextProgram.d b/src/sj/TitleTextProgram.d new file mode 100644 index 0000000..880885e --- /dev/null +++ b/src/sj/TitleTextProgram.d @@ -0,0 +1,97 @@ +/// License: MIT +module sj.TitleTextProgram; + +import gl4d; + +/// +class TitleTextProgram { + public: + /// + static struct Vertex { + public: + /// + align(1) vec3 pos; + /// + align(1) vec2 uv; + } + + /// + enum ShaderHeader = "#version 330 core +#extension GL_ARB_explicit_uniform_location : enable"; + + /// + enum VertexShaderSrc = ShaderHeader ~ q{ + layout(location = 0) uniform mat4 P; + layout(location = 1) uniform mat4 V; + layout(location = 2) uniform mat4 M; + + layout(location = 0) in vec3 vert; + layout(location = 1) in vec2 uv; + + out vec2 uv_; + + void main() { + uv_ = uv; + gl_Position = P * V * M * vec4(vert, 1); + } + }; + /// + enum FragmentShaderSrc = ShaderHeader ~ q{ + layout(location = 3) uniform sampler2D tex; + + in vec2 uv_; + + out vec4 pixel_; + + void main() { + pixel_ = texture(tex, uv_); + } + }; + + /// + this() { + ProgramLinker linker; + linker.vertex = VertexShader.Compile(VertexShaderSrc); + linker.fragment = FragmentShader.Compile(FragmentShaderSrc); + program_ = linker.Link(); + program_.Validate(); + } + + /// + void SetupVertexArray(ref VertexArrayRef vao, ref ArrayBufferRef vertices) { + vao.Bind(); + VertexArrayAttacher attacher; + with (attacher) { + index = 0; + type = GL_FLOAT; + offset = 0; + stride = Vertex.sizeof; + dimension = 3; + Attach(vao, vertices); + + index = 1; + type = GL_FLOAT; + offset = vec3.sizeof; + stride = Vertex.sizeof; + dimension = 2; + Attach(vao, vertices); + } + } + + /// + void Use( + mat4 proj, mat4 view, mat4 model, + ref Texture2DRef tex, ref SamplerRef sampler) { + tex.BindToUnit(GL_TEXTURE0); + sampler.Bind(0); + + program_.Use(); + program_.uniform!0 = proj; + program_.uniform!1 = view; + program_.uniform!2 = model; + program_.uniform!3 = 0; + } + + private: + ProgramRef program_; +}