diff options
-rw-r--r-- | shaders/lightmap.cpp | 20 | ||||
-rw-r--r-- | shaders/lightmap.vert | 1 | ||||
-rw-r--r-- | shaders/shader.cpp | 12 | ||||
-rw-r--r-- | shaders/shader.frag | 16 | ||||
-rw-r--r-- | shaders/shader.hpp | 16 | ||||
-rw-r--r-- | shaders/shader.vert | 10 |
6 files changed, 56 insertions, 19 deletions
diff --git a/shaders/lightmap.cpp b/shaders/lightmap.cpp index 56299fb3..1174241a 100644 --- a/shaders/lightmap.cpp +++ b/shaders/lightmap.cpp @@ -40,17 +40,21 @@ auto lightmap_shader::make_framebuffer(Vector2i size) -> Framebuffer { Framebuffer framebuffer; - framebuffer.fb = GL::Framebuffer{{ {}, size }}; - framebuffer.color = GL::Texture2D{}; - framebuffer.color.setStorage(1, GL::TextureFormat::RGB8, size); + framebuffer.color + .setStorage(1, GL::TextureFormat::RGB8, size) + .setWrapping(GL::SamplerWrapping::ClampToBorder) + .setBorderColor(Color4{0.f, 0.f, 0.f, 1.f}); + //framebuffer.depth = GL::Renderbuffer{}; - //framebuffer.depth.setStorage(GL::RenderbufferFormat::DepthComponent32F, fb_size); + //framebuffer.depth.setStorage(GL::RenderbufferFormat::DepthComponent32F, size); - framebuffer.fb.attachTexture(GL::Framebuffer::ColorAttachment{0}, framebuffer.color, 0); - //framebuffer.fb.attachRenderbuffer(GL::Framebuffer::BufferAttachment::Depth, framebuffer.depth); - framebuffer.fb.clearColor(0, Color4{0.f, 0.f, 0.f, 1.f}); - //framebuffer.fb.clearDepth(0); + framebuffer.fb = GL::Framebuffer{{ {}, size }}; + framebuffer.fb + //.attachRenderbuffer(GL::Framebuffer::BufferAttachment::Depth, framebuffer.depth); + .attachTexture(GL::Framebuffer::ColorAttachment{0}, framebuffer.color, 0) + //.clearDepth(0); + .clearColor(0, Color4{0.f, 0.f, 0.f, 1.f}); return framebuffer; } diff --git a/shaders/lightmap.vert b/shaders/lightmap.vert index 7b1ca696..8cda9926 100644 --- a/shaders/lightmap.vert +++ b/shaders/lightmap.vert @@ -4,6 +4,7 @@ layout (location = 0) uniform vec4 color_intensity; layout (location = 1) uniform vec2 center; layout (location = 2) uniform uint falloff; layout (location = 3) uniform vec2 size; +layout (location = 4) uniform bool enable_lightmap; layout (location = 0) in vec4 position; diff --git a/shaders/shader.cpp b/shaders/shader.cpp index 0c66bef2..4b184057 100644 --- a/shaders/shader.cpp +++ b/shaders/shader.cpp @@ -32,7 +32,10 @@ tile_shader::tile_shader() set_scale({640, 480}); set_tint({1, 1, 1, 1}); - setUniform(OffsetUniform, Vector3{}); + setUniform(OffsetUniform, Vector3(Vector2(_camera_offset), _depth_offset)); + setUniform(EnableLightmapUniform, _enable_lightmap); + setUniform(SamplerUniform, 0); + setUniform(LightmapSamplerUniform, 1); } tile_shader::~tile_shader() = default; @@ -57,6 +60,13 @@ tile_shader& tile_shader::set_tint(const Vector4& tint) return *this; } +tile_shader& tile_shader::set_lightmap_enabled(bool value) +{ + if (value != _enable_lightmap) + setUniform(EnableLightmapUniform, _enable_lightmap = value); + return *this; +} + void tile_shader::_draw() { fm_assert(std::fabs(_camera_offset[0]) < 1 << 24 && std::fabs(_camera_offset[1]) < 1 << 24); diff --git a/shaders/shader.frag b/shaders/shader.frag index 9e05d677..faea53fb 100644 --- a/shaders/shader.frag +++ b/shaders/shader.frag @@ -1,14 +1,22 @@ precision highp float; -uniform sampler2D sampler; -layout (location = 2) uniform vec4 tint = vec4(1, 1, 1, 1); +layout (location = 0) uniform vec2 scale; +layout (location = 1) uniform vec3 offset; +layout (location = 2) uniform vec4 tint; +layout (location = 3) uniform bool enable_lightmap; +layout (location = 4) uniform sampler2D sampler; +layout (location = 5) uniform sampler2D lightmap_sampler; -noperspective in vec2 frag_texcoords; +layout (location = 0) noperspective in vec2 frag_texcoords; +layout (location = 1) noperspective in vec2 frag_light_coord; out vec4 color; //layout (depth_greater) out float gl_FragDepth; void main() { - color = texture(sampler, frag_texcoords) * tint; + vec4 light = tint; + if (enable_lightmap) + light *= vec4(texture(lightmap_sampler, frag_light_coord).rgb, 1); + color = texture(sampler, frag_texcoords) * light; if (color.a == 0) discard; } diff --git a/shaders/shader.hpp b/shaders/shader.hpp index 79ceb940..68e85d35 100644 --- a/shaders/shader.hpp +++ b/shaders/shader.hpp @@ -14,7 +14,8 @@ struct tile_shader final : private GL::AbstractShaderProgram { using Position = GL::Attribute<0, Vector3>; using TextureCoordinates = GL::Attribute<1, Vector2>; - using Depth = GL::Attribute<2, float>; + using LightCoord = GL::Attribute<2, Vector2>; + using Depth = GL::Attribute<3, float>; explicit tile_shader(); ~tile_shader() override; @@ -27,12 +28,13 @@ struct tile_shader final : private GL::AbstractShaderProgram tile_shader& set_tint(const Vector4& tint); float depth_offset() const { return _depth_offset; } static float depth_value(const local_coords& xy, float offset = 0) noexcept; + bool is_lightmap_enabled() const { return _enable_lightmap; } + tile_shader& set_lightmap_enabled(bool value); template<typename T = float> static constexpr Math::Vector2<T> project(const Math::Vector3<T>& pt); template<typename T = float> static constexpr Math::Vector2<T> unproject(const Math::Vector2<T>& px); - template<typename T, typename... Xs> - decltype(auto) draw(T&& mesh, Xs&&... xs); + template<typename T, typename... Xs> decltype(auto) draw(T&& mesh, Xs&&... xs); static constexpr Vector2s max_screen_tiles = {8, 8}; static constexpr float character_depth_offset = 1 + 1./64; @@ -41,6 +43,7 @@ struct tile_shader final : private GL::AbstractShaderProgram static constexpr float wall_depth_offset = 1; static constexpr float z_depth_offset = 1 + 2./64; static constexpr float depth_tile_size = 1/(double)(TILE_MAX_DIM * 2 * max_screen_tiles.product()); + static constexpr float foreshortening_factor = 0.578125f; private: void _draw(); @@ -50,8 +53,13 @@ private: Vector2 _scale; Vector3 _real_camera_offset; float _depth_offset = 0; + bool _enable_lightmap : 1 = false; - enum { ScaleUniform = 0, OffsetUniform = 1, TintUniform = 2, }; + enum { + ScaleUniform = 0, OffsetUniform = 1, TintUniform = 2, + EnableLightmapUniform = 3, + SamplerUniform = 4, LightmapSamplerUniform = 5, + }; }; template<typename T, typename... Xs> diff --git a/shaders/shader.vert b/shaders/shader.vert index 9190fe8d..88ed1e93 100644 --- a/shaders/shader.vert +++ b/shaders/shader.vert @@ -2,14 +2,20 @@ precision highp float; layout (location = 0) uniform vec2 scale; layout (location = 1) uniform vec3 offset; +layout (location = 2) uniform vec4 tint; +layout (location = 3) uniform bool enable_lightmap; layout (location = 0) in vec4 position; layout (location = 1) in vec2 texcoords; -layout (location = 2) in float depth; -noperspective out vec2 frag_texcoords; +layout (location = 2) in vec2 light_coord; +layout (location = 3) in float depth; + +layout (location = 0) noperspective out vec2 frag_texcoords; +layout (location = 1) noperspective out vec2 frag_light_coord; void main() { float x = -position.y, y = -position.x, z = position.z; gl_Position = vec4((x-y+offset.x)*scale.x, ((x+y+z*2)*0.578125-offset.y)*scale.y, offset.z + depth, 1); frag_texcoords = texcoords; + frag_light_coord = light_coord; } |