From 11a58b5adf012ccc608da717ccba54d3f49d7430 Mon Sep 17 00:00:00 2001 From: brunoherbelin Date: Thu, 12 Nov 2020 22:07:00 +0100 Subject: [PATCH] Adding UV texture coordinates to Surface and ImageShader. --- ImageShader.cpp | 13 +++++++++---- ImageShader.h | 2 +- Primitives.cpp | 11 +++++++++++ Primitives.h | 3 +++ rsc/shaders/image.fs | 6 +++++- 5 files changed, 29 insertions(+), 6 deletions(-) diff --git a/ImageShader.cpp b/ImageShader.cpp index bfa7c0e..45b32c4 100644 --- a/ImageShader.cpp +++ b/ImageShader.cpp @@ -10,7 +10,7 @@ static ShadingProgram imageShadingProgram("shaders/image.vs", "shaders/image.fs" const char* ImageShader::mask_names[11] = { "None", "Glow", "Halo", "Circle", "Round", "Vignette", "Top", "Botton", "Left", "Right", "Custom" }; std::vector< uint > ImageShader::mask_presets; -ImageShader::ImageShader(): Shader(), custom_textureindex(0) +ImageShader::ImageShader(): Shader(), mask(0), custom_textureindex(0), stipple(0.0), uv(0.0, 0.0, 1.0, 1.0) { // first initialization if ( mask_presets.empty() ) { @@ -36,19 +36,20 @@ void ImageShader::use() Shader::use(); program_->setUniform("stipple", stipple); + program_->setUniform("uv", uv); glActiveTexture(GL_TEXTURE1); if ( mask < 10 ) { glBindTexture(GL_TEXTURE_2D, mask_presets[mask]); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } else glBindTexture(GL_TEXTURE_2D, custom_textureindex); + glActiveTexture(GL_TEXTURE0); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); } @@ -61,6 +62,9 @@ void ImageShader::reset() custom_textureindex = mask_presets[0]; // no stippling stipple = 0.f; + // normal uv + uv = glm::vec4(0.0, 0.0, 1.0, 1.0); +// uv = glm::vec4(0.0, 0.0, 2.0, 2.0); } void ImageShader::operator = (const ImageShader &S ) @@ -70,6 +74,7 @@ void ImageShader::operator = (const ImageShader &S ) mask = S.mask; custom_textureindex = S.custom_textureindex; stipple = S.stipple; + uv = S.uv; } diff --git a/ImageShader.h b/ImageShader.h index 8affcf0..9c4ba14 100644 --- a/ImageShader.h +++ b/ImageShader.h @@ -16,7 +16,6 @@ class ImageShader : public Shader public: ImageShader(); -// virtual ~ImageShader() {} void use() override; void reset() override; @@ -27,6 +26,7 @@ public: uint mask; uint custom_textureindex; float stipple; + glm::vec4 uv; static const char* mask_names[11]; static std::vector< uint > mask_presets; diff --git a/Primitives.cpp b/Primitives.cpp index 25d12e1..4510d15 100644 --- a/Primitives.cpp +++ b/Primitives.cpp @@ -95,6 +95,17 @@ void Surface::draw(glm::mat4 modelview, glm::mat4 projection) glBindTexture(GL_TEXTURE_2D, 0); } +void Surface::setTextureUV(glm::vec4 uv) +{ + dynamic_cast(shader_)->uv = uv; +} + +glm::vec4 Surface::textureUV() const +{ + return dynamic_cast(shader_)->uv; +} + + ImageSurface::ImageSurface(const std::string& path, Shader *s) : Surface(s), resource_(path) { diff --git a/Primitives.h b/Primitives.h index e7f19e2..958f70e 100644 --- a/Primitives.h +++ b/Primitives.h @@ -27,6 +27,9 @@ public: void draw (glm::mat4 modelview, glm::mat4 projection) override; void accept (Visitor& v) override; + void setTextureUV(glm::vec4 uv); + glm::vec4 textureUV() const; + inline void setTextureIndex(uint t) { textureindex_ = t; } inline uint textureIndex() const { return textureindex_; } diff --git a/rsc/shaders/image.fs b/rsc/shaders/image.fs index 3e2e2ff..4d123bd 100644 --- a/rsc/shaders/image.fs +++ b/rsc/shaders/image.fs @@ -11,11 +11,15 @@ uniform vec3 iResolution; // viewport resolution (in pixels) uniform vec4 color; uniform float stipple; +uniform vec4 uv; void main() { + // adjust UV + vec2 texcoord = vec2(uv.x, uv.y) + vertexUV * vec2(uv.z - uv.x, uv.w - uv.y); + // color is a mix of texture (manipulated with brightness & contrast), vertex and uniform colors - vec4 textureColor = texture(iChannel0, vertexUV); + vec4 textureColor = texture(iChannel0, texcoord); vec3 RGB = textureColor.rgb * vertexColor.rgb * color.rgb; // alpha is a mix of texture alpha, vertex alpha, and uniform alpha affected by stippling