Skip to content

Commit

Permalink
#5785: Add IPatch::getTextureAspectRatio() and corresponding unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Oct 23, 2021
1 parent 5a8213b commit 7089876
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions include/ipatch.h
Expand Up @@ -143,6 +143,9 @@ class IPatch
// greebo: returns true if the patch's shader is visible, false otherwise
virtual bool hasVisibleMaterial() const = 0;

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

/**
* greebo: Sets/gets whether this patch is a patchDef3 (fixed tesselation)
*/
Expand Down
5 changes: 5 additions & 0 deletions libs/SurfaceShader.h
Expand Up @@ -128,6 +128,11 @@ class SurfaceShader :
return 1;
}

float getTextureAspectRatio() const
{
return static_cast<float>(getWidth()) / getHeight();
}

void setRenderSystem(const RenderSystemPtr& renderSystem)
{
_renderSystem = renderSystem;
Expand Down
5 changes: 1 addition & 4 deletions radiantcore/brush/Face.cpp
Expand Up @@ -495,10 +495,7 @@ Vector2 Face::getTexelScale() const

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

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

// Returns the index pair forming an edge, keeping the winding direction intact
Expand Down
5 changes: 5 additions & 0 deletions radiantcore/patch/Patch.cpp
Expand Up @@ -387,6 +387,11 @@ bool Patch::hasVisibleMaterial() const
return material && material->isVisible();
}

float Patch::getTextureAspectRatio() const
{
return _shader.getTextureAspectRatio();
}

int Patch::getShaderFlags() const
{
if (_shader.getGLShader() != 0)
Expand Down
2 changes: 2 additions & 0 deletions radiantcore/patch/Patch.h
Expand Up @@ -171,6 +171,8 @@ class Patch :
// greebo: returns true if the patch's shader is visible, false otherwise
bool hasVisibleMaterial() const override;

float getTextureAspectRatio() const override;

// As the name states: get the shader flags of the m_state shader
int getShaderFlags() const;

Expand Down
20 changes: 20 additions & 0 deletions test/TextureManipulation.cpp
Expand Up @@ -665,6 +665,26 @@ TEST_F(TextureManipulationTest, FaceGetTextureAspectRatio)
<< "Number texture aspect ratio should be 1.0";
}

TEST_F(TextureManipulationTest, PatchGetTextureAspectRatio)
{
auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
auto patchNode = algorithm::createPatchFromBounds(worldspawn, AABB(Vector3(4, 50, 60), Vector3(64, 128, 256)), "textures/a_1024x512");
auto patch = Node_getIPatch(patchNode);

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

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

patchNode = algorithm::createPatchFromBounds(worldspawn, AABB(Vector3(4, 50, 60), Vector3(64, 128, 256)), "textures/numbers/0");
patch = Node_getIPatch(patchNode);

EXPECT_NEAR(patch->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 7089876

Please sign in to comment.