Adding UV texture coordinates to Surface and ImageShader.

This commit is contained in:
brunoherbelin
2020-11-12 22:07:00 +01:00
parent 7c5374552d
commit 11a58b5adf
5 changed files with 29 additions and 6 deletions

View File

@@ -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" }; const char* ImageShader::mask_names[11] = { "None", "Glow", "Halo", "Circle", "Round", "Vignette", "Top", "Botton", "Left", "Right", "Custom" };
std::vector< uint > ImageShader::mask_presets; 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 // first initialization
if ( mask_presets.empty() ) { if ( mask_presets.empty() ) {
@@ -36,19 +36,20 @@ void ImageShader::use()
Shader::use(); Shader::use();
program_->setUniform("stipple", stipple); program_->setUniform("stipple", stipple);
program_->setUniform("uv", uv);
glActiveTexture(GL_TEXTURE1); glActiveTexture(GL_TEXTURE1);
if ( mask < 10 ) { if ( mask < 10 ) {
glBindTexture(GL_TEXTURE_2D, mask_presets[mask]); 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_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
} }
else else
glBindTexture(GL_TEXTURE_2D, custom_textureindex); glBindTexture(GL_TEXTURE_2D, custom_textureindex);
glActiveTexture(GL_TEXTURE0); 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]; custom_textureindex = mask_presets[0];
// no stippling // no stippling
stipple = 0.f; 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 ) void ImageShader::operator = (const ImageShader &S )
@@ -70,6 +74,7 @@ void ImageShader::operator = (const ImageShader &S )
mask = S.mask; mask = S.mask;
custom_textureindex = S.custom_textureindex; custom_textureindex = S.custom_textureindex;
stipple = S.stipple; stipple = S.stipple;
uv = S.uv;
} }

View File

@@ -16,7 +16,6 @@ class ImageShader : public Shader
public: public:
ImageShader(); ImageShader();
// virtual ~ImageShader() {}
void use() override; void use() override;
void reset() override; void reset() override;
@@ -27,6 +26,7 @@ public:
uint mask; uint mask;
uint custom_textureindex; uint custom_textureindex;
float stipple; float stipple;
glm::vec4 uv;
static const char* mask_names[11]; static const char* mask_names[11];
static std::vector< uint > mask_presets; static std::vector< uint > mask_presets;

View File

@@ -95,6 +95,17 @@ void Surface::draw(glm::mat4 modelview, glm::mat4 projection)
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
} }
void Surface::setTextureUV(glm::vec4 uv)
{
dynamic_cast<ImageShader*>(shader_)->uv = uv;
}
glm::vec4 Surface::textureUV() const
{
return dynamic_cast<ImageShader*>(shader_)->uv;
}
ImageSurface::ImageSurface(const std::string& path, Shader *s) : Surface(s), resource_(path) ImageSurface::ImageSurface(const std::string& path, Shader *s) : Surface(s), resource_(path)
{ {

View File

@@ -27,6 +27,9 @@ public:
void draw (glm::mat4 modelview, glm::mat4 projection) override; void draw (glm::mat4 modelview, glm::mat4 projection) override;
void accept (Visitor& v) override; void accept (Visitor& v) override;
void setTextureUV(glm::vec4 uv);
glm::vec4 textureUV() const;
inline void setTextureIndex(uint t) { textureindex_ = t; } inline void setTextureIndex(uint t) { textureindex_ = t; }
inline uint textureIndex() const { return textureindex_; } inline uint textureIndex() const { return textureindex_; }

View File

@@ -11,11 +11,15 @@ uniform vec3 iResolution; // viewport resolution (in pixels)
uniform vec4 color; uniform vec4 color;
uniform float stipple; uniform float stipple;
uniform vec4 uv;
void main() 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 // 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; vec3 RGB = textureColor.rgb * vertexColor.rgb * color.rgb;
// alpha is a mix of texture alpha, vertex alpha, and uniform alpha affected by stippling // alpha is a mix of texture alpha, vertex alpha, and uniform alpha affected by stippling