Skip to content

Commit

Permalink
#5740: Patch textures rotate around their center now
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 1, 2021
1 parent 8767805 commit e12b053
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 21 deletions.
15 changes: 1 addition & 14 deletions radiantcore/patch/Patch.cpp
Expand Up @@ -901,20 +901,7 @@ void Patch::scaleTexture(float s, float t)

void Patch::rotateTexture(float angle)
{
undoSave();

const double s = sin(degrees_to_radians(angle));
const double c = cos(degrees_to_radians(angle));

for(PatchControlIter i = _ctrl.begin(); i != _ctrl.end(); ++i)
{
const double x = i->texcoord[0];
const double y = i->texcoord[1];
i->texcoord[0] = (x * c) - (y * s);
i->texcoord[1] = (y * c) + (x * s);
}

controlPointsChanged();
selection::algorithm::TextureRotator::RotatePatch(*this, degrees_to_radians(angle));
}

void Patch::fitTexture(float s, float t)
Expand Down
2 changes: 1 addition & 1 deletion radiantcore/patch/Patch.h
Expand Up @@ -268,7 +268,7 @@ class Patch :
// This is the same as above, but with undoSave() for use in command sequences
void translateTexture(float s, float t) override;
void scaleTexture(float s, float t) override;
void rotateTexture(float angle) override;
void rotateTexture(float angle) override; // angle in degrees
void fitTexture(float repeatS, float repeatT) override; // call with s=1 t=1 for FIT

/* uses longest parallel chord to calculate texture coords for each row/col
Expand Down
44 changes: 41 additions & 3 deletions radiantcore/selection/algorithm/Texturing.cpp
Expand Up @@ -9,6 +9,18 @@ namespace selection
namespace algorithm
{

namespace
{

inline void applyTransform(const textool::INode::Ptr& node, const Matrix3& transform)
{
node->beginTransformation();
node->transform(transform);
node->commitTransformation();
}

}

TextureNodeManipulator::operator std::function<bool(const textool::INode::Ptr& node)>()
{
return [this](const textool::INode::Ptr& node)
Expand Down Expand Up @@ -43,9 +55,7 @@ TextureFlipper::TextureFlipper(const Vector2& flipCenter, int axis)

bool TextureFlipper::processNode(const textool::INode::Ptr& node)
{
node->beginTransformation();
node->transform(_transform);
node->commitTransformation();
applyTransform(node, _transform);
return true;
}

Expand All @@ -67,6 +77,34 @@ void TextureFlipper::FlipFace(IFace& face, int flipAxis)
FlipNode(std::make_shared<textool::FaceNode>(face), flipAxis);
}

// Rotation

TextureRotator::TextureRotator(const Vector2& pivot, double angle)
{
_transform = Matrix3::getTranslation(-pivot);
_transform.premultiplyBy(Matrix3::getRotation(angle));
_transform.premultiplyBy(Matrix3::getTranslation(pivot));
}

bool TextureRotator::processNode(const textool::INode::Ptr& node)
{
applyTransform(node, _transform);
return true;
}

void TextureRotator::RotatePatch(IPatch& patch, double angle)
{
RotateNode(std::make_shared<textool::PatchNode>(patch), angle);
}

void TextureRotator::RotateNode(const textool::INode::Ptr& node, double angle)
{
const auto& bounds = node->localAABB();
TextureRotator rotator({ bounds.origin.x(), bounds.origin.y() }, angle);

rotator.processNode(node);
}

}

}
21 changes: 19 additions & 2 deletions radiantcore/selection/algorithm/Texturing.h
Expand Up @@ -16,8 +16,7 @@ namespace algorithm
class TextureNodeManipulator
{
protected:
TextureNodeManipulator()
{}
TextureNodeManipulator() = default;

public:
// Conversion operator, to be able to pass an instance reference directly to
Expand Down Expand Up @@ -65,6 +64,24 @@ class TextureFlipper :
static void FlipNode(const textool::INode::Ptr& node, int flipAxis);
};

class TextureRotator :
public TextureNodeManipulator
{
private:
Matrix3 _transform;

public:
TextureRotator(const Vector2& pivot, double angle);

bool processNode(const textool::INode::Ptr& node) override;

// Directly rotate the texture of the given patch around its UV center
static void RotatePatch(IPatch& patch, double angle);

private:
static void RotateNode(const textool::INode::Ptr& node, double angle);
};

}

}
2 changes: 1 addition & 1 deletion test/TextureManipulation.cpp
Expand Up @@ -111,7 +111,7 @@ void performPatchRotateTest(bool clockwise)

// Rotate each texture coordinate around the patch center with a manual transform
auto transform = Matrix3::getTranslation({ -bounds.origin.x(), -bounds.origin.y() });
transform.premultiplyBy(Matrix3::getRotation(angle));
transform.premultiplyBy(Matrix3::getRotation(degrees_to_radians(clockwise ? angle : -angle)));
transform.premultiplyBy(Matrix3::getTranslation({ bounds.origin.x(), bounds.origin.y() }));

auto old = oldTexCoords.begin();
Expand Down

0 comments on commit e12b053

Please sign in to comment.