From 2db1573ea2290bf1349737b7f8506490c4ac7629 Mon Sep 17 00:00:00 2001 From: codereader Date: Sun, 14 Jun 2020 18:53:31 +0200 Subject: [PATCH] #5231: Refactor TexDef handling. TextureMatrix is still declared as friend, as it's closely coupled to its implementation. --- radiantcore/brush/TexDef.cpp | 26 +++++++++++++++++ radiantcore/brush/TexDef.h | 7 +++++ radiantcore/brush/TextureMatrix.cpp | 37 +++++++++++++++---------- radiantcore/brush/TextureProjection.cpp | 32 +++++++++++++-------- 4 files changed, 76 insertions(+), 26 deletions(-) diff --git a/radiantcore/brush/TexDef.cpp b/radiantcore/brush/TexDef.cpp index 220a46d61f..204d8cdbfb 100644 --- a/radiantcore/brush/TexDef.cpp +++ b/radiantcore/brush/TexDef.cpp @@ -45,6 +45,32 @@ TexDef::TexDef(double width, double height, const Matrix4& transform) } } +double TexDef::getRotation() const +{ + return _rotate; +} + +void TexDef::setRotation(double rotation) +{ + _rotate = rotation; +} + +Vector2 TexDef::getShift() const +{ + return Vector2(_shift); +} + +Vector2 TexDef::getScale() const +{ + return Vector2(_scale); +} + +void TexDef::setScale(const Vector2& scale) +{ + _scale[0] = scale[0]; + _scale[1] = scale[1]; +} + void TexDef::shift(double s, double t) { _shift[0] += s; diff --git a/radiantcore/brush/TexDef.h b/radiantcore/brush/TexDef.h index d5f243d41f..169e92d843 100644 --- a/radiantcore/brush/TexDef.h +++ b/radiantcore/brush/TexDef.h @@ -22,6 +22,12 @@ class TexDef // Destructor virtual ~TexDef() {} + Vector2 getShift() const; + Vector2 getScale() const; + void setScale(const Vector2& scale); + double getRotation() const; + void setRotation(double rotation); + void shift(double s, double t); void scale(double s, double t); void rotate(double angle); @@ -47,6 +53,7 @@ class TexDef static TexDef CreateFromShiftScaleRotation(const ShiftScaleRotation& scr); friend std::ostream& operator<<(std::ostream& st, const TexDef& texdef); + friend struct TextureMatrix; // for TextureMatrix::getFakeTexCoords }; inline std::ostream& operator<<(std::ostream& st, const TexDef& texdef) diff --git a/radiantcore/brush/TextureMatrix.cpp b/radiantcore/brush/TextureMatrix.cpp index c0b314c615..9541a214a0 100644 --- a/radiantcore/brush/TextureMatrix.cpp +++ b/radiantcore/brush/TextureMatrix.cpp @@ -25,18 +25,23 @@ TextureMatrix::TextureMatrix(const Matrix4& transform) { } // Construct a TextureMatrix out of "fake" shift scale rot definitions -TextureMatrix::TextureMatrix(const TexDef& texdef) { - float r = degrees_to_radians(-texdef._rotate); +TextureMatrix::TextureMatrix(const TexDef& texdef) +{ + float r = degrees_to_radians(-texdef.getRotation()); float c = cos(r); float s = sin(r); - float x = 1.0f / texdef._scale[0]; - float y = 1.0f / texdef._scale[1]; + + auto scale = texdef.getScale(); + float x = 1.0f / scale[0]; + float y = 1.0f / scale[1]; + + auto shift = texdef.getShift(); coords[0][0] = x * c; coords[1][0] = x * s; coords[0][1] = y * -s; coords[1][1] = y * c; - coords[0][2] = -texdef._shift[0]; - coords[1][2] = texdef._shift[1]; + coords[0][2] = -shift[0]; + coords[1][2] = shift[1]; } // shift a texture (texture adjustments) along it's current texture axes @@ -59,26 +64,28 @@ void TextureMatrix::scale(float s, float t, std::size_t shaderWidth, std::size_t // compute fake shift scale rot TexDef texdef = getFakeTexCoords(); - float newXScale = texdef._scale[0] + s; - float newYScale = texdef._scale[1] + t; + auto scale = texdef.getScale(); + double newXScale = scale[0] + s; + double newYScale = scale[1] + t; // Don't allow zero (or almost zero) scale values - if (float_equal_epsilon(newXScale, 0, 1e-5f) || - float_equal_epsilon(newYScale, 0, 1e-5f)) + if (float_equal_epsilon(newXScale, 0, 1e-5) || + float_equal_epsilon(newYScale, 0, 1e-5)) { return; } // Don't allow sign changes - if ((newXScale*texdef._scale[0]) < 0.0f || - (newYScale*texdef._scale[1]) < 0.0f) + if ((newXScale*scale[0]) < 0.0 || + (newYScale*scale[1]) < 0.0) { return; } // update - texdef._scale[0] = newXScale; - texdef._scale[1] = newYScale; + scale[0] = newXScale; + scale[1] = newYScale; + texdef.setScale(scale); // compute new normalized texture matrix *this = TextureMatrix(texdef); @@ -97,7 +104,7 @@ void TextureMatrix::rotate(float angle, std::size_t shaderWidth, std::size_t sha TexDef texdef = getFakeTexCoords(); // update - texdef._rotate += angle; + texdef.setRotation(texdef.getRotation() + angle); // compute new normalized texture matrix *this = TextureMatrix(texdef); diff --git a/radiantcore/brush/TextureProjection.cpp b/radiantcore/brush/TextureProjection.cpp index bbce582f48..9e68a0c688 100644 --- a/radiantcore/brush/TextureProjection.cpp +++ b/radiantcore/brush/TextureProjection.cpp @@ -20,14 +20,14 @@ TextureProjection::TextureProjection(const TextureMatrix& otherMatrix) : TextureMatrix TextureProjection::GetDefaultProjection() { // Cache the registry key because this constructor is called a lot - static registry::CachedKey scale( + static registry::CachedKey scaleKey( "user/ui/textures/defaultTextureScale" ); TexDef tempTexDef; - tempTexDef._scale[0] = scale.get(); - tempTexDef._scale[1] = scale.get(); + double scale = scaleKey.get(); + tempTexDef.setScale(Vector2(scale, scale)); return TextureMatrix(tempTexDef); } @@ -211,22 +211,32 @@ void TextureProjection::fitTexture(std::size_t width, std::size_t height, const normalise((float)width, (float)height); } -void TextureProjection::flipTexture(unsigned int flipAxis) { +void TextureProjection::flipTexture(unsigned int flipAxis) +{ // Retrieve the "fake" texture coordinates (shift, scale, rotation) TexDef texdef = matrix.getFakeTexCoords(); // Check for x flip (x-component not zero) - if (flipAxis == 0) { + if (flipAxis == 0) + { // Invert the x scale and rotate 180° - texdef._scale[0] *= -1; - texdef._rotate -= 180; + auto scale = texdef.getScale(); + scale[0] *= -1; + texdef.setScale(scale); + + texdef.setRotation(texdef.getRotation() - 180); } - else if (flipAxis == 1) { + else if (flipAxis == 1) + { // Invert the y scale and rotate 180° - texdef._scale[1] *= -1; - texdef._rotate -= 180; + auto scale = texdef.getScale(); + scale[1] *= -1; + texdef.setScale(scale); + + texdef.setRotation(texdef.getRotation() - 180); } - else { + else + { // Do nothing, leave the TextureMatrix untouched return; }