Skip to content

Commit

Permalink
#5740: Add IFace::getTextureAspectRatio() method plus unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 12, 2021
1 parent ed0b027 commit b1349f5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/ibrush.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ class IFace
// This is based on the image returned by the material, usually the editor image
virtual Vector2 getTexelScale() const = 0;

// Returns the texture aspect ratio width/height
virtual float getTextureAspectRatio() const = 0;

// Fits the texture on this face
virtual void fitTexture(float s_repeat, float t_repeat) = 0;

Expand Down
8 changes: 8 additions & 0 deletions radiantcore/brush/Face.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,14 @@ Vector2 Face::getTexelScale() const
return Vector2(textureMatrix.xx() * imageWidth, textureMatrix.yy() * imageHeight);
}

float Face::getTextureAspectRatio() const
{
auto imageWidth = _shader.getWidth();
auto imageHeight = _shader.getHeight();

return static_cast<float>(imageWidth) / imageHeight;
}

// Returns the index pair forming an edge, keeping the winding direction intact
inline std::pair<std::size_t, std::size_t> getEdgeIndexPair(std::size_t first, std::size_t second, std::size_t windingSize)
{
Expand Down
1 change: 1 addition & 0 deletions radiantcore/brush/Face.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class Face :
void rotateTexdef(float angle) override;

Vector2 getTexelScale() const override;
float getTextureAspectRatio() const override;

void fitTexture(float s_repeat, float t_repeat) override;
void flipTexture(unsigned int flipAxis) override;
Expand Down
21 changes: 21 additions & 0 deletions test/TextureManipulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,27 @@ TEST_F(TextureManipulationTest, FaceGetTexelScale)
EXPECT_NEAR(texelScale.y(), expectedTexelScaleY, 0.01) << "Texel scale Y is off";
}

TEST_F(TextureManipulationTest, FaceGetTextureAspectRatio)
{
auto brushNode = create512CubeTextured1x1("textures/a_1024x512");

// Get the texture dimensions
auto editorImage = GlobalMaterialManager().getMaterial("textures/a_1024x512")->getEditorImage();
auto textureWidth = editorImage->getWidth();
auto textureHeight = editorImage->getHeight();

// Get a face and check the texture bounds
auto& face = Node_getIBrush(brushNode)->getFace(0);

EXPECT_NEAR(face.getTextureAspectRatio(), static_cast<float>(textureWidth) / textureHeight, 0.01)
<< "Wrong texture aspect ratio reported";

brushNode = create512CubeTextured1x1("textures/numbers/0");
auto& anotherFace = Node_getIBrush(brushNode)->getFace(0);
EXPECT_NEAR(anotherFace.getTextureAspectRatio(), 1.0, 0.001)
<< "Number texture aspect ratio should be 1.0";
}

TEST_F(TextureManipulationTest, FaceTextureChangePreservesTexelScale)
{
auto largeTexture = "textures/a_1024x512";
Expand Down

0 comments on commit b1349f5

Please sign in to comment.