Skip to content

Commit

Permalink
#5740: Better implementation of IFace::getTexelScale(). Add unit test…
Browse files Browse the repository at this point in the history
… checking the pivoted rotation preserving texel scale
  • Loading branch information
codereader committed Oct 12, 2021
1 parent 8f3f0f7 commit 04b4364
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion radiantcore/brush/Face.cpp
Expand Up @@ -521,7 +521,13 @@ Vector2 Face::getTexelScale() const
auto imageHeight = _shader.getHeight();

auto textureMatrix = _texdef.getMatrix();
return Vector2(textureMatrix.xx() * imageWidth, textureMatrix.yy() * imageHeight);

// Multiplying the image dimensions onto the texture matrix yields
// the base vectors in texel space. Take the length to get the covered texels per world unit
return Vector2(
Vector2(textureMatrix.xx() * imageWidth, textureMatrix.xy() * imageHeight).getLength(),
Vector2(textureMatrix.yx() * imageWidth, textureMatrix.yy() * imageHeight).getLength()
);
}

float Face::getTextureAspectRatio() const
Expand Down
20 changes: 20 additions & 0 deletions test/TextureManipulation.cpp
Expand Up @@ -698,4 +698,24 @@ TEST_F(TextureManipulationTest, FaceTextureChangePreservesTexelScale)
EXPECT_NEAR(texelScaleBefore.y(), texelScaleAfter.y(), 0.01) << "Texel scale Y changed after applying new material";
}

TEST_F(TextureManipulationTest, FaceRotationPreservesTexelScale)
{
auto material = "textures/a_1024x512";
auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
auto test1024x512Node = algorithm::createCuboidBrush(worldspawn, AABB({ 128, 32, 16 }, { 128, 32, 16 }), material);
algorithm::foreachFace(*Node_getIBrush(test1024x512Node), [](IFace& face) { face.fitTexture(1, 1); });

auto& face = *algorithm::findBrushFaceWithNormal(Node_getIBrush(test1024x512Node), { 0, 0, 1 });

auto texelScaleBefore = face.getTexelScale();

face.rotateTexdef(45); // degrees
face.rotateTexdef(45);

auto texelScaleAfter = face.getTexelScale();

EXPECT_NEAR(texelScaleBefore.x(), texelScaleAfter.x(), 0.01) << "Texel scale X changed after rotation by 90 degrees";
EXPECT_NEAR(texelScaleBefore.y(), texelScaleAfter.y(), 0.01) << "Texel scale Y changed after rotation by 90 degrees";
}

}

0 comments on commit 04b4364

Please sign in to comment.