Skip to content

Commit

Permalink
#5231: Refactor TexDef handling.
Browse files Browse the repository at this point in the history
TextureMatrix is still declared as friend, as it's closely coupled to its implementation.
  • Loading branch information
codereader committed Jun 14, 2020
1 parent fb5795e commit 2db1573
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 26 deletions.
26 changes: 26 additions & 0 deletions radiantcore/brush/TexDef.cpp
Expand Up @@ -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;
Expand Down
7 changes: 7 additions & 0 deletions radiantcore/brush/TexDef.h
Expand Up @@ -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);
Expand All @@ -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)
Expand Down
37 changes: 22 additions & 15 deletions radiantcore/brush/TextureMatrix.cpp
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
32 changes: 21 additions & 11 deletions radiantcore/brush/TextureProjection.cpp
Expand Up @@ -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<float> scale(
static registry::CachedKey<float> 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);
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 2db1573

Please sign in to comment.